|
-
Jul 31st, 2001, 07:22 PM
#1
Thread Starter
Addicted Member
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 zspeed1, bool 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;
-
Jul 31st, 2001, 08:08 PM
#2
transcendental analytic
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.
-
Jul 31st, 2001, 10:55 PM
#3
Thread Starter
Addicted Member
soo would i use pointer arithmetic too access each particle like this
PHP Code:
particle_2=particle_1++
particle_2->property=6
-
Jul 31st, 2001, 11:51 PM
#4
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.
-
Aug 1st, 2001, 12:08 AM
#5
Thread Starter
Addicted Member
Thanks for making it clear
-
Aug 1st, 2001, 11:00 AM
#6
transcendental analytic
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.
-
Aug 1st, 2001, 02:06 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|