src/thirdparty/particles/ParticleDLL/ParticleGroup.h

Go to the documentation of this file.
00001 // ParticleGroup.h
00002 //
00003 // Copyright 1998-2005 by David K. McAllister.
00004 //
00005 // A group of particles - Info and an array of Particles
00006 //
00007 // Defines these classes: ParticleGroup
00008 
00009 #ifndef _ParticleGroup_h
00010 #define _ParticleGroup_h
00011 
00012 // Particle.h includes vector
00013 #include "Particle.h"
00014 
00015 #include <vector>
00016 
00017 typedef void (*P_PARTICLE_CALLBACK)(struct Particle &particle, void *data); // Also defined in papi.h.
00018 
00019 typedef std::vector<Particle> ParticleList;
00020 
00021 class ParticleGroup
00022 {
00023     ParticleList list;
00024 
00025     size_t max_particles;       // Max particles allowed in group.
00026     P_PARTICLE_CALLBACK cb_birth;
00027     P_PARTICLE_CALLBACK cb_death;
00028     void *cb_birth_data;
00029     void *cb_death_data;
00030 
00031 public:
00032     ParticleGroup()
00033     {
00034         max_particles = 0;
00035         cb_birth = NULL;
00036         cb_death = NULL;
00037         cb_birth_data = NULL;
00038         cb_death_data = NULL;
00039     }
00040 
00041     ParticleGroup(size_t maxp) : max_particles(maxp)
00042     {
00043         cb_birth = NULL;
00044         cb_death = NULL;
00045         cb_birth_data = NULL;
00046         cb_death_data = NULL;
00047         list.reserve(max_particles);
00048     }
00049     ParticleGroup(const ParticleGroup &rhs) : list(rhs.list)
00050     {
00051         cb_birth = rhs.cb_birth;
00052         cb_death = rhs.cb_death;
00053         cb_birth_data = rhs.cb_birth_data;
00054         cb_death_data = rhs.cb_death_data;
00055         max_particles = rhs.max_particles;
00056     }
00057 
00058     ~ParticleGroup()
00059     {
00060         if (cb_death) {
00061             ParticleList::iterator it;
00062             for (it = list.begin(); it != list.end(); ++it)
00063                 (*cb_death)((*it), cb_death_data);
00064         }
00065     }
00066 
00067     ParticleGroup &operator=(const ParticleGroup &rhs)
00068     {
00069         if (this != &rhs) {
00070             if (cb_death) {
00071                 ParticleList::iterator it;
00072                 for (it = list.begin(); it != list.end(); ++it)
00073                     (*cb_death)((*it), cb_death_data);
00074             }
00075             list = rhs.list;
00076             cb_birth = rhs.cb_birth;
00077             cb_death = rhs.cb_death;
00078             cb_birth_data = rhs.cb_birth_data;
00079             cb_death_data = rhs.cb_death_data;
00080             max_particles = rhs.max_particles;
00081         }
00082         return *this;
00083     }
00084 
00085 inline size_t GetMaxParticles() { return max_particles; }
00086     inline ParticleList &GetList() { return list; }
00087 
00088     inline void SetBirthCallback(P_PARTICLE_CALLBACK cbb, void *cbb_data)
00089     {
00090         cb_birth = cbb;
00091         cb_birth_data = cbb_data;
00092     }
00093 
00094     inline void SetDeathCallback(P_PARTICLE_CALLBACK cbd, void *cbd_data)
00095     {
00096         cb_death = cbd;
00097         cb_death_data = cbd_data;
00098     }
00099 
00100     inline void SetMaxParticles(size_t maxp)
00101     {
00102         max_particles = maxp;
00103         if(list.size() > max_particles) {
00104             if (cb_death) {
00105                 ParticleList::iterator it;
00106                 for (it = list.begin() + max_particles; it != list.end(); ++it)
00107                     (*cb_death)((*it), cb_death_data);
00108             }
00109             list.resize(max_particles);
00110         }
00111         list.reserve(max_particles);
00112     }
00113 
00114 inline size_t size() const { return list.size(); }
00115     inline ParticleList::iterator begin() { return list.begin(); }
00116     inline ParticleList::iterator end() { return list.end(); }
00117 
00118     inline void Remove(ParticleList::iterator it)
00119     {
00120         if (cb_death)
00121             (*cb_death)((*it), cb_death_data);
00122 
00123         // Copy the one from the end to here.
00124         if(it != list.end() - 1) {
00125             *it = *(list.end() - 1);
00126         }
00127 
00128         // Delete the one at the end
00129         list.pop_back();
00130     }
00131 
00132     inline bool Add(const pVec &pos, const pVec &posB,
00133                     const pVec &up,
00134                     const pVec &vel, const pVec &rvel,
00135                     const pVec &size, const pVec &color,
00136                     const float alpha = 1.0f,
00137                     const float age = 0.0f,
00138                     const float mass = 1.0f,
00139                     const long data = 0)
00140     {
00141         if (list.size() >= max_particles)
00142             return false;
00143         else {
00144             list.push_back(Particle(pos, posB, up, up, vel, vel, rvel, rvel, size, color, alpha, age, mass, data, 0.0f));
00145             Particle &p = list.back();
00146             if (cb_birth)
00147                 (*cb_birth)(p, cb_birth_data);
00148             return true;
00149         }
00150     }
00151 
00152     inline bool Add(const Particle &P)
00153     {
00154         if (list.size() >= max_particles)
00155             return false;
00156         else {
00157             list.push_back(P);
00158             Particle &p = list.back();
00159             if (cb_birth)
00160                 (*cb_birth)(p, cb_birth_data);
00161             return true;
00162         }
00163     }
00164 };
00165 
00166 #endif

Generated on Sat Mar 15 22:55:58 2008 for Armagetron Advanced by  doxygen 1.5.4