Archived

This topic is now archived and is closed to further replies.

okouya

help

Recommended Posts

okouya    122
look at this 3sec please i got a little question very simple for a c++ and pascal coders please take 3sec for this **************************************************************************** Particles.cpp Author : Dave Astle Date : 2/1/2001 Written for OpenGL Game Programming *****************************************************************************/ #include "Particles.h" /***************************************************************************** CParticleSystem::Constructor Store initialization values and set defaults. *****************************************************************************/ CParticleSystem::CParticleSystem(int maxParticles, CVector origin) { m_maxParticles = maxParticles; m_origin = origin; m_particleList = NULL; } // end CParticleSystem::Constructor /***************************************************************************** CParticleSystem::Emit() Creates the number of new particles specified by the parameter, using the general particle system values with some random element. Note that only initial values will be randomized. Final values will not. This may be changed in the future. *****************************************************************************/ int CParticleSystem::Emit(int numParticles) { // create numParticles new particles (if there''s room) while (numParticles && (m_numParticles < m_maxParticles)) { // initialize the current particle and increase the count InitializeParticle(m_numParticles++); --numParticles; } return numParticles; } // end CParticleSystem::Emit /***************************************************************************** CParticleSystem::InitializeSystem() Allocate memory for the maximum number of particles in the system *****************************************************************************/ void CParticleSystem::InitializeSystem() { // if this is just a reset, free the memory if (m_particleList) { delete[] m_particleList; m_particleList = NULL; } // allocate the maximum number of particles m_particleList = new particle_t[m_maxParticles]; // reset the number of particles and accumulated time m_numParticles = 0; m_accumulatedTime = 0.0f; } // end CParticleSystem::InitializeSystem /***************************************************************************** CParticleSystem::KillSystem() Tells the emitter to stop emitting. If the parameter is true, all live particles are killed as well. Otherwise, they are allowed to die off on their own. *****************************************************************************/ void CParticleSystem::KillSystem() { if (m_particleList) { delete[] m_particleList; m_particleList = NULL; } m_numParticles = 0; } // end CParticleSystem::KillSystem Now this is the bases structure the ".H" /**************************************************************************** Particles.h Particle and particle system base classes. Author : Dave Astle Date : 2/1/2001 Written for OpenGL Game Programming *****************************************************************************/ #ifndef __PARTICLES_H_INCLUDED__ #define __PARTICLES_H_INCLUDED__ /********************************* Includes *********************************/ #include <iostream.h> #include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include "vector.h" //using namespace std; /***************************** Data structures ******************************/ class CParticleSystem; struct particle_t { CVector m_pos; // current position of the particle CVector m_prevPos; // last position of the particle CVector m_velocity; // direction and speed CVector m_acceleration; // acceleration float m_energy; // determines how long the particle is alive float m_size; // size of particle float m_sizeDelta; // amount to change the size over time float m_weight; // determines how gravity affects the particle float m_weightDelta; // change over time float m_color[4]; // current color of the particle float m_colorDelta[4]; // how the color changes with time }; class CParticleSystem { public: CParticleSystem(int maxParticles, CVector origin); // abstract functions virtual void Update(float elapsedTime) = 0; virtual void Render() = 0; virtual int Emit(int numParticles); virtual void InitializeSystem(); virtual void KillSystem(); protected: virtual void InitializeParticle(int index) = 0; particle_t *m_particleList; // particles for this emitter int m_maxParticles; // maximum number of particles in total int m_numParticles; // indicies of all free particles CVector m_origin; // center of the particle system float m_accumulatedTime; // used to track how long since the last particle was emitted CVector m_force; // force (gravity, wind, etc.) acting on the particle system }; #endif // __PARTICLES_H_INCLUDED__ so can you explain me what is he doing when he wrote this: particle_t *m_particleList; // particles for this emitter // allocate the maximum number of particles m_particleList = new particle_t[m_maxParticles]; for this how can you do this in pascal please ???????? love and peace

Share this post


Link to post
Share on other sites
parklife    132
He''s declaring a pointer to a particle_t. As for the second question, he is allocating memory for said pointer. I don''t know how to do this in pascal, though. You might.

I found this site to explain this. There is a C++ version and a pascal version, compare those.

Share this post


Link to post
Share on other sites
okouya    122
yes i see but this not explain me my problem because i don''t understand what his he doing

by

// allocate the maximum number of particles
m_particleList = new particle_t[m_maxParticles];


what is exactly m_particleList !!!! i don''t think that it''s just a pointer to a particle_t element i think that it''s a array of particle but......... how is he declarating this?????????

this is so special


particle_t *m_particleList; // particles for this emitter


normaly if you want to declarer a pointer you do this


particle_t* m_particleList; cause the type particle_t exist but the type m_particulelist doesn''t exist yet so iç don''t anderstand it''s so strange may be if you don''t know the pascal it''s not a problem but can you give me an explanation a clear explanation


love and peace

Share this post


Link to post
Share on other sites
Dark Rain    157
I'm not 100% sure I understand the question but here's my answer from what I gather.

In C++, declaring variable and pointer is done the same way, except that you add the * symbol in front of the variable name.

So int essence, doing "particle_t *m_particleList;" declares a pointer NAMED "m_particleList" to of the TYPE "particle_t". I dont know Pascal, so could it be that pointer arent typed and that's why you dont understand?

Also, in C++, you can declare a pointer but not allocate any memory for it. What this does : "m_particleList = new particle_t[m_maxParticles]" is allocating memory and making m_particleList point toward that allocated memory. It creates and array of the TYPE particle_t and a POINTER named m_particleList that point toward the data in it.

Just in case :
int c; <- int is the type, c is the variable name
int *c <- Same deal, you just need to allocate memory for it.


[edited by - Dark Rain on June 10, 2002 7:16:39 AM]

Share this post


Link to post
Share on other sites
okouya    122
ok now i anderstand because in pascal you don''t do like this there is just one way you just do this


var

a :^int;

and not

a^ : int;



love and peace

Share this post


Link to post
Share on other sites
okouya    122
ok what i will have to do in pascal is that


type
particle_t = record




end;


and
var

m_particlelist : array of particule_t ; // this is call a dynamic table
so you don''t know the size at the begining

and after you do

setlength(m_particlelist,maxparticles);

and voila i think !!!!

love and peace

Share this post


Link to post
Share on other sites
okouya    122
but what about that sorry



int CParticleSystem::Emit(int numParticles)
{
// create numParticles new particles (if there''s room)
while (numParticles && (m_numParticles < m_maxParticles))
{
// initialize the current particle and increase the count
InitializeParticle(m_numParticles++);
--numParticles;
}
return numParticles;
} // end CParticleSystem::Emit


i don''t anderstands the --numParticles; and the m_numParticles++);

can you explain how you make the for or while in c++



or when you ghot that

void CExplosion::Update(float elapsedTime)
{
for (int i = 0; i < m_numParticles; )
{
// update the particle''s position based on the elapsed time and velocity
m_particleList.m_pos = m_particleList[i].m_pos + m_particleList[i].m_velocity * elapsedTime;
m_particleList[i].m_velocity = m_particleList[i].m_velocity + m_particleList[i].m_acceleration * elapsedTime;

m_particleList[i].m_energy -= elapsedTime;
m_particleList[i].m_size += m_particleList[i].m_sizeDelta * elapsedTime;

m_particleList[i].m_color[3] += m_particleList[i].m_colorDelta[3] * elapsedTime;
m_particleList[i].m_color[1] += m_particleList[i].m_colorDelta[1] * elapsedTime;

// if the particle has hit the ground plane, kill it
if (m_particleList[i].m_energy <= 0.0)
{
// move the last particle to the current positon, and decrease the count
m_particleList[i] = m_particleList[--m_numParticles];
}
else
{
++i;
}
}
} // end CExplosion::Update()


still the i++ or -- var..;;;


love and peace

Share this post


Link to post
Share on other sites
Dark Rain    157
++ and -- and increment/decrement operators.

this code :
i++;
is equal to
i = i + 1

same deal with i-- is equal to i = i - 1;

Of course theres the post increment deal but I dont think you need it


The while loop :
The while loop, will loop as long as the statement in the while() bracket is true. For exemple, while (i == 1337), means that the while statement will loop as long as i is equal to 1337.

The for loop :
The for loop has 3 parameter and each are optional and are separated by the ";" symbol. In essence, a for( ; ; ) is an infinite loop.

The first parameter, is used to inialise stuff, like the counter you use. You can also declare variables in there, more than one separated by ",".

The second parameter : It's a true/false test, like the while loop, as long as it's true, the for statement will loop.

The third parameter, is usualy used to increment the counter or do some exotic stuff, like pointer arythmetic etc.

The most common form of for loop is this :
for (int i = 0 ; i < 20 ; i++)
{
//do stuff here
}

This will loop 20 times.

In his explosion function, he simply choose to have the i++ somewhere else in the loop, it's perfectly legal.


[edited by - Dark Rain on June 10, 2002 8:49:26 AM]

Share this post


Link to post
Share on other sites
Useless Hacker    372
quote:
Original post by okouya
int CParticleSystem::Emit(int numParticles)
{
// create numParticles new particles (if there's room)
while (numParticles && (m_numParticles < m_maxParticles))
{
// initialize the current particle and increase the count
InitializeParticle(m_numParticles++);
--numParticles;
}
return numParticles;
} // end CParticleSystem::Emit


i don't anderstands the --numParticles; and the m_numParticles++);

InitializeParticle(m_numParticles++); is equivalent to this in Pascal:
InitializeParticle(m_numParticles);
m_numParticles := m_numParticles + 1;


--numParticles; is equivalent to:
numParticles := numParticles - 1;

In Delphi Pascal, you can use Inc(m_numParticles); instead of m_numParticles := m_numParticles + 1;, and Dec(numParticles); instead of numParticles := numParticles - 1, but I'm not sure if these work in standard Pascal.

[edited by - Useless Hacker on June 10, 2002 8:54:26 AM]

Share this post


Link to post
Share on other sites
Dark Rain    157
Oh you''re right, I forgot to mention that he does the ++ thingy in the function but it gets incremented AFTER the call. That would''ve lead to some interesting bugs ^_^.

Share this post


Link to post
Share on other sites
okouya    122
the man that know about pascal and what do you think about that


if (m_particleList)
{
delete[] m_particleList;
m_particleList = NULL;
}

m_numParticles = 0;

this mean in pascal at my opinion if i declare the m_particulist as a dynamic array

if (m_particlelist<>0) then

begin

m_particlelist=nil


end;

because i don''t anderstand the test ?????????



love and peace

Share this post


Link to post
Share on other sites