Results 1 to 7 of 7

Thread: Can't think of a Subject

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154

    Question Can't think of a Subject

    I have a particleEgine class. the problem is i can't figure out how too have the user define the number of particles.
    Here'se the relevant code
    PHP Code:

    class ParticleEngine{
          public:
           
    ParticleEngine(int NumParticles,float posx1,float posy1,float posz1 ,float xspeed1,float yspeed1,float zspeed1bool cont,bool samecolor1 ,float red1,float green1,float blue1);
           ~
    ParticleEngine();
           
    bool Keyboard();
           
    bool Render();
           
    bool PassObject(Object Obj);
           protected:

                  static 
    int const MAX_PARTICLES;
                   
    particles particle[MAX_PARTICLES]; 
    Now the problem is i use the particle array in my Render function and other functions
    but i don't know how too make soo i can define the max number of particles in the constuctor;
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    if you want the user to define the size of the array, the data is dynamic and needs to be stored on the heap. declare pointers to the first and last item of particles array or use a STL container or make your own array. If you decide to go with pointers, you can initialize it's memory like this:

    particle_first=new particles[particle_max];
    particle_last=particle_first+particle_max;

    in the destructor destroy the memory allocated:

    delete[] particle_first;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    soo would i use pointer arithmetic too access each particle like this
    PHP Code:
    particle_2=particle_1++
    particle_2->property=
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  4. #4
    Zaei
    Guest
    In your constructor, have a statement:
    Code:
    particles = new PARTICLE[NumParticles];
    if(particles == NULL) //out of memory
    {
       // do something;
    }
    In the class declaration, define "particles" as "PARTICLE* particles;", where "PARTICLE" is the class/struct you are using to define a particle. You will also have to store "NumParticles".

    Then, to access each particle, you would do something like:
    Code:
    for(INT i = 0; i < NumParticles; ++i)
    {
        particles[i].x = 10;
    }
    You can see how you access each particle just as if it were an array.

    Then, as kedaman said, in the destructor, call "delete[] particles;" to free the memory back to windows.

    Z.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    Thanks for making it clear
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    There's always Zaei right behind me to do further explanations on things

    However if you want the power of pointeraritmetics, a pointer to the last and first element instead of first and a counter is more usefull.
    particle_2=particle_1++;
    postprefix ++ operator will increase particle_1 but will return the unchanged value of particle_1 resulting in particle_2 being old particle_1. instead do
    particle_2=++particle_1;
    which increases particle_1 and returns the reference to it.
    In case you want to preserve particle_1 as it is, you should do
    particle_2=particle_1+1;
    instead.
    Simple iteration is fast and convenient
    for(particles* i = particle_first; i < particle_last; ++i) *i.x = 10;
    will assign each particles i from first to last 10
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Zaei
    Guest
    Of course, kedaman =).

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width