Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/include/ZE_ZSimpleParticleSystem.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002         This file is Part of the ZEngine Library for 2D game development.
00003                    Copyright (C) 2002, 2003 James Turk
00004 
00005                      Licensed under a BSD-style license.
00006 
00007     The maintainer of this library is James Turk (james@conceptofzero.net) 
00008      and the home of this Library is http://www.zengine.sourceforge.net
00009 *******************************************************************************/
00010 
00021 #ifndef __ze_zsimpleparticlesystem_h__
00022 #define __ze_zsimpleparticlesystem_h__
00023 
00024 #include "ZEngine.h"
00025 
00026 namespace ZE
00027 {
00028 
00034 class ZSimpleParticle : public ZBaseParticle
00035 {
00036     public:
00038         float xPrev;
00040         float yPrev;
00042         float xVel;
00044         float yVel;
00046         float energyDelta;
00048         float size;
00050         float sizeDelta;
00052         Uint8 r;
00054         Uint8 g;
00056         Uint8 b;
00058         Uint8 a;
00059 };
00060 
00066 enum ParticleDrawStyle 
00067 { 
00068     DS_POINT,   
00069     DS_LINE,    
00070     DS_IMAGE    
00071 };
00072 
00081 template <class particleType>
00082 class ZSimpleParticleSystem : public ZBaseParticleSystem<particleType>
00083 {
00084     protected:
00086         ParticleDrawStyle rStyle;
00088         ZImage rImage;
00090         float rMinX;
00092         float rMaxX;
00094         float rMinY;
00096         float rMaxY;
00098         float rMinXVel;
00100         float rMaxXVel;
00102         float rMinYVel; 
00104         float rMaxYVel;
00106         float rMinEnergy;
00108         float rMaxEnergy;
00110         float rMinEnergyDelta;
00112         float rMaxEnergyDelta;
00114         float rMinSize;
00116         float rMaxSize;
00118         float rMinSizeDelta;
00120         float rMaxSizeDelta;
00122         Uint8 rMinRed;
00124         Uint8 rMaxRed;
00126         Uint8 rMinGreen;
00128         Uint8 rMaxGreen;
00130         Uint8 rMinBlue;
00132         Uint8 rMaxBlue;
00134         Uint8 rMinAlpha;
00136         Uint8 rMaxAlpha;
00137 
00144         virtual particleType NewParticle();
00145 
00154         virtual void UpdateParticle(int index, float elapsedTime);
00155 
00156     public:
00163         virtual void Render();
00164 
00170         void ReloadImage();
00171 
00178         void SetDrawStyle(ParticleDrawStyle style);
00179 
00186         void SetImage(std::string filename);
00187 
00197         void SetPosRange(float minX, float minY, float maxX, float maxY);
00198 
00208         void SetVelocityRange(float minXVel, float minYVel, float maxXVel, float maxYVel);
00209 
00220         void SetEnergyRange(float minEnergy, float maxEnergy, float minEnergyDelta=0, float maxEnergyDelta=0);
00221 
00231         void SetSizeRange(float minSize, float maxSize, float minSizeDelta=0, float maxSizeDelta=0);
00232 
00246         void SetColorRange(Uint8 minRed, Uint8 maxRed, Uint8 minGreen, Uint8 maxGreen, 
00247                             Uint8 minBlue, Uint8 maxBlue, Uint8 minAlpha, Uint8 maxAlpha);
00248 };
00249 
00250 //implementation//
00251 
00252 template <class particleType>
00253 particleType ZSimpleParticleSystem<particleType>::NewParticle()
00254 {
00255     particleType p;
00256     
00257     p.xPrev = p.xPos = rEngine->Rand(rMinX,rMaxX);
00258     p.yPrev = p.yPos = rEngine->Rand(rMinY,rMaxY);
00259     p.xVel = rEngine->Rand(rMinXVel,rMaxXVel);
00260     p.yVel = rEngine->Rand(rMinYVel,rMaxYVel);
00261     p.energy = rEngine->Rand(rMinEnergy,rMaxEnergy);
00262     p.energyDelta = rEngine->Rand(rMinEnergyDelta,rMaxEnergyDelta);
00263     p.size = rEngine->Rand(rMinSize,rMaxSize);
00264     p.sizeDelta = rEngine->Rand(rMinSizeDelta,rMaxSizeDelta);
00265     p.r = rEngine->Rand(rMinRed,rMaxRed);
00266     p.g = rEngine->Rand(rMinGreen,rMaxGreen);
00267     p.b = rEngine->Rand(rMinBlue,rMaxBlue);
00268     p.a = rEngine->Rand(rMinAlpha,rMaxAlpha);
00269 
00270     return p;
00271 }
00272 
00273 template <class particleType>
00274 void ZSimpleParticleSystem<particleType>::UpdateParticle(int index, float elapsedTime)
00275 {
00276     rParticles[index].xPrev = rParticles[index].xPos;
00277     rParticles[index].yPrev = rParticles[index].yPos;
00278     rParticles[index].xPos += rParticles[index].xVel*elapsedTime;
00279     rParticles[index].yPos += rParticles[index].yVel*elapsedTime;
00280     rParticles[index].size += rParticles[index].sizeDelta*elapsedTime;
00281     rParticles[index].energy += rParticles[index].energyDelta*elapsedTime;
00282     if(rParticles[index].size <= 0)
00283         rParticles[index].energy = 0;
00284 }
00285 
00286 #if (GFX_BACKEND == ZE_OGL)
00287 
00288 template <class particleType>
00289 void ZSimpleParticleSystem<particleType>::Render()
00290 {
00291     switch(rStyle)
00292     {
00293         case DS_POINT:
00294             glBindTexture(GL_TEXTURE_2D,0);
00295             for(unsigned int i=0; i < rCurParticles; i++)
00296             {
00297                 glPointSize(rParticles[i].size);
00298                 glBegin(GL_POINTS);
00299                 glColor4ub(rParticles[i].r,rParticles[i].g,rParticles[i].b,rParticles[i].a);
00300                 glVertex2f(rParticles[i].xPos,rParticles[i].yPos);
00301                 glEnd();
00302             }            
00303             break;
00304         case DS_LINE:
00305             glBindTexture(GL_TEXTURE_2D,0);
00306             for(unsigned int i=0; i < rCurParticles; i++)
00307             {
00308                 glLineWidth(rParticles[i].size);
00309                 glBegin(GL_LINES);
00310                 glColor4ub(rParticles[i].r,rParticles[i].g,rParticles[i].b,rParticles[i].a);
00311                 glVertex2f(rParticles[i].xPos,rParticles[i].yPos);
00312                 glVertex2f(rParticles[i].xPrev,rParticles[i].yPrev);
00313                 glEnd();
00314             }
00315             break;
00316         case DS_IMAGE:
00317             float x,y,size;
00318             rImage.Bind();
00319             glBegin(GL_QUADS);
00320             for(unsigned int i=0; i < rCurParticles; i++)
00321             {
00322                 x = rParticles[i].xPos;
00323                 y = rParticles[i].yPos;
00324                 size = rParticles[i].size;
00325 
00326                 glColor4ub(rParticles[i].r,rParticles[i].g,rParticles[i].b,rParticles[i].a);
00327                 glTexCoord2f(0,1);  glVertex2f(x,y);
00328                 glTexCoord2f(1,1);  glVertex2f(x+size,y);
00329                 glTexCoord2f(0,1);  glVertex2f(x+size,y-size);
00330                 glTexCoord2f(0,0);  glVertex2f(x,y-size);
00331             }
00332             glEnd();
00333             break;
00334     }
00335 }
00336 
00337 #elif (GFX_BACKEND == ZE_SDL)
00338 
00339 template <class particleType>
00340 void ZSimpleParticleSystem<particleType>::Render()
00341 {
00342     switch(rStyle)
00343     {
00344         case DS_POINT:
00345             for(unsigned int i=0; i < rCurParticles; i++)
00346             {
00347                 //draw point
00348             }            
00349             break;
00350         case DS_LINE:
00351             for(unsigned int i=0; i < rCurParticles; i++)
00352             {
00353                 //draw line 
00354             }
00355             break;
00356         case DS_IMAGE:
00357             for(unsigned int i=0; i < rCurParticles; i++)
00358             {
00359                 //draw image
00360             }
00361             break;
00362     }
00363 }
00364 
00365 #endif //GFX_BACKEND
00366 
00367 template <class particleType>
00368 void ZSimpleParticleSystem<particleType>::ReloadImage()
00369 {
00370     if(rStyle == DS_IMAGE)
00371         rImage.Reload();
00372 }
00373 
00374 template <class particleType>
00375 void ZSimpleParticleSystem<particleType>::SetDrawStyle(ParticleDrawStyle style)
00376 {
00377     rStyle = style;
00378 }
00379 
00380 template <class particleType>
00381 void ZSimpleParticleSystem<particleType>::SetImage(std::string filename)
00382 {
00383     rImage.Open(filename);
00384 }
00385 
00386 template <class particleType>
00387 void ZSimpleParticleSystem<particleType>::SetPosRange(float minX, float minY, float maxX, float maxY)
00388 {
00389     rMinX = minX;
00390     rMaxX = maxX;
00391     rMinY = minY;
00392     rMaxY = maxY;
00393 }
00394 
00395 template <class particleType>
00396 void ZSimpleParticleSystem<particleType>::SetVelocityRange(float minXVel, float minYVel, float maxXVel, float maxYVel)
00397 {
00398     rMinXVel = minXVel;
00399     rMaxXVel = maxXVel;
00400     rMinYVel = minYVel;
00401     rMaxYVel = maxYVel;
00402 }
00403 
00404 template <class particleType>
00405 void ZSimpleParticleSystem<particleType>::SetEnergyRange(float minEnergy, float maxEnergy, float minEnergyDelta, float maxEnergyDelta)
00406 {
00407     rMinEnergy = minEnergy;
00408     rMaxEnergy = maxEnergy;
00409     rMinEnergyDelta = minEnergyDelta;
00410     rMaxEnergyDelta = maxEnergyDelta;
00411 }
00412 
00413 template <class particleType>
00414 void ZSimpleParticleSystem<particleType>::SetSizeRange(float minSize, float maxSize, float minSizeDelta, float maxSizeDelta)
00415 {
00416     rMinSize = minSize;
00417     rMaxSize = maxSize;
00418     rMinSizeDelta = minSizeDelta;
00419     rMaxSizeDelta = maxSizeDelta;
00420 }
00421 
00422 template <class particleType>
00423 void ZSimpleParticleSystem<particleType>::SetColorRange(Uint8 minRed, Uint8 maxRed, Uint8 minGreen, Uint8 maxGreen, 
00424                             Uint8 minBlue, Uint8 maxBlue, Uint8 minAlpha, Uint8 maxAlpha)
00425 {
00426     rMinRed = minRed;
00427     rMaxRed = maxRed;
00428     rMinGreen = minGreen;
00429     rMaxGreen = maxGreen;
00430     rMinBlue = minBlue;
00431     rMaxBlue = maxBlue;
00432     rMinAlpha = minAlpha;
00433     rMaxAlpha = maxAlpha;
00434 }
00435 
00436 }   //namespace ZE
00437 
00438 #endif //__ze_zsimpleparticlesystem_h__

Generated on Sun Oct 5 19:34:45 2003 for ZEngine by doxygen1.3