Using HGE Particle Systems Editor's presets in your game
The editor saves it's presets in files particle1.psi - particle9.psi,
which can be loaded and used directly in your game. Here follows detailed step by step
explanation of how to use them.
Initialization, the simple way
Use this method if you need just the particle system parameters
from the preset and have your sprite for rendering particles ready:
hgeParticleSystem *ps;
ps = new hgeParticleSystem("particle1.psi", sprite);
Voila! The particle system is ready to run!
Initialization, the not so simple way
Use this method if you want to recreate the particle system exactly as
it was in editor, using information from the preset only.
First, you need to load the particle system preset and the texture, holding the particle image:
HTEXTURE tex = hge->Texture_Load("particles.png");
hgeParticleSystemInfo *psi =
hge->Resource_Load("particle1.psi");
Now the sprite object must be created and set up. You may use any sprite
you have, but if you want to use the editor's sprite parameters, they are stored
in the sprite member of hgeParticleSystemInfo
structure in the following way: the higher WORD contains the blending mode,
and the lower WORD contains the sprite number from 0 to 15. To extract
these values use the following code:
int nSprite = ((DWORD)psi->sprite & 0xFFFF);
int blend = ((DWORD)psi->sprite >> 16);
So, let's create the sprite now and store the actual pointer to it into the preset:
#define SIZE 32
// calculate the texx and texy texture coordinates here
// based on editor's sprite number or in any other way you want
float texx = SIZE * (nSprite % 4);
float texy = SIZE * (nSprite / 4);
hgeSprite *spr = new hgeSprite(tex, texx, texy, SIZE, SIZE);
spr->SetHotSpot(SIZE/2, SIZE/2);
spr->SetBlendMode(blend);
psi->sprite = spr;
If you want to use a sprite with dimensions other than 32x32,
you have to adjust the fSizeStart and fSizeEnd parameters of the
particle system preset to keep the particle sizes the same as they were in editor:
psi->fSizeStart = 32.0f * psi->fSizeStart / SIZE;
psi->fSizeEnd = 32.0f * psi->fSizeEnd / SIZE;
And finally let's create the particle system object and delete the preset
from memory as it isn't needed longer:
hgeParticleSystem *ps = new hgeParticleSystem(psi);
hge->Resource_Free(psi);
The particle system is initialized and ready to run now!
You could save the preset in memory and use it later to spawn
instances of your particle system either manually or with hgeParticleManager.
Running
Now call the hgeParticleSystem method
FireAt to start the particle system:
ps->FireAt(100, 100);
Then use Update
and Render methods in your
frame function to update and render the particle system respectively:
ps->Update(fDeltaTime);
ps->Render();
Also you may control the particle system with Stop
and MoveTo methods or by modifying the members
of it's info structure.
Clean up
When the particle system isn't needed longer, you must delete it
and all associated resources:
delete ps;
delete spr;
hge->Texture_Free(tex);
See also
hgeSprite,
hgeParticleSystem,
hgeParticleManager,
Tutorial 03
|