ParticleSystem.h
#ifndef _PARTICLE_#define _PARTICLE_#include <d3d9.h>#include <d3dx9.h>#include <math.h>#include <vector>using namespace std;#define FVF_CUSTOM (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)struct POINT_SPRITE{ float x, y, z; DWORD PSIZE; DWORD Diffuse;};class particle{private:protected:public: D3DXVECTOR3 m_pos; float m_direction; DWORD m_size; DWORD m_color; DWORD m_duration; DWORD m_life; // age DWORD m_start_time; DWORD m_speed; particle() { m_pos = D3DXVECTOR3( 0, 0 ,0); m_color = D3DCOLOR_XRGB( 0, 0 ,0); m_size=m_duration=m_life=m_speed=m_start_time =0; } void update() { float x = cos(m_direction * 3.14f /180.0f) * m_speed; float y = sin(m_direction * 3.14f /180.0f) * m_speed; m_life = timeGetTime() - m_start_time; m_pos += D3DXVECTOR3( x, y, 0); }};class emitter{public: emitter( LPDIRECT3DDEVICE9 dev, const char *fname); ~emitter(); void Release(); void update( float x, float y, float direction, float speed, int life);private: LPDIRECT3DDEVICE9 lpDirect3dDevice9; LPDIRECT3DVERTEXBUFFER9 lpVertexBuffer9; LPDIRECT3DTEXTURE9 texture; D3DXVECTOR3 xyz; int max_particles; particle *pParticles;};struct emitter_type{ const char* fname; float m_direction; DWORD m_size; //DWORD m_color; DWORD m_duration; int m_life; DWORD m_speed;};class particle_system{public: particle_system( LPDIRECT3DDEVICE9 dev); ~particle_system(); int create_emitter( emitter_type &type); void destroy_emitter( int index); void update();private: LPDIRECT3DDEVICE9 pDevice; vector<emitter> emitter_list; vector<emitter_type> type_list;};#endif
ParticleSystem.cpp
#include "particleSystem.h"//-------------------------------------------------------------------------//Particle System//-------------------------------------------------------------------------POINT_SPRITE pVertices[] ={ {0.0f, 0.0f, 0.0f, 10, 0xffff0000}, {1.0f, 1.0f, 0.0f, 10, 0xff00ff00}, {0.0f, 1.0f, 0.0f, 10, 0x0000ffff}, {1.0f, 0.0f, 0.0f, 10, 0x0000ffff}};particle_system::particle_system( LPDIRECT3DDEVICE9 dev): pDevice( dev){}particle_system::~particle_system(){ for (vector<emitter>::iterator i = emitter_list.begin(); i != emitter_list.end(); ++i) { i->Release(); } delete &emitter_list; delete &type_list;}int particle_system::create_emitter( emitter_type &type){ emitter *temp = new emitter( pDevice, type.fname); return emitter_list.size(); emitter_list.push_back( *temp); type_list.push_back( type);}void particle_system::destroy_emitter( int index){ emitter_list.erase( emitter_list.begin() + index);}void particle_system::update(){ for (vector<emitter>::iterator i = emitter_list.begin(); i != emitter_list.end(); ++i){ for (vector<emitter_type>::iterator o = type_list.begin(); o != type_list.end(); ++o) { i->update( 320, 240, o->m_direction, o->m_speed, o->m_life); } }}//-------------------------------------------------------------------------//Emitter//-------------------------------------------------------------------------emitter::emitter( LPDIRECT3DDEVICE9 dev, const char *fname): lpDirect3dDevice9( dev){ max_particles = 256; pParticles = new particle[max_particles]; lpDirect3dDevice9->CreateVertexBuffer( max_particles*sizeof(POINT_SPRITE), 0, FVF_CUSTOM, D3DPOOL_DEFAULT, &lpVertexBuffer9, NULL); VOID* vertices; lpVertexBuffer9->Lock(128*sizeof(pVertices), 0, (void**)&vertices, NULL); memcpy( pVertices, &vertices, sizeof(pVertices)); lpVertexBuffer9->Unlock(); D3DXCreateTextureFromFile( lpDirect3dDevice9, fname, &texture);}emitter::~emitter(){}void emitter::Release(){ if (lpVertexBuffer9) {lpVertexBuffer9->Release();} if (texture) {texture->Release();} if (pParticles) {delete [] pParticles;}}void emitter::update(float x, float y, float direction, float speed, int life){ DWORD time = timeGetTime(); POINT_SPRITE *POINT_SPRITEs = new POINT_SPRITE[max_particles]; xyz = D3DXVECTOR3(x,y,0); for (DWORD counter = 0; counter < max_particles; ++counter) { if (pParticles[counter].m_life >= pParticles[counter].m_duration) { pParticles[counter].m_start_time = timeGetTime(); pParticles[counter].m_pos = xyz; pParticles[counter].m_direction = direction; pParticles[counter].m_speed = speed; pParticles[counter].m_duration = life; } pParticles[counter].update(); POINT_SPRITEs[counter].x=pParticles[counter].m_pos.x; POINT_SPRITEs[counter].y=pParticles[counter].m_pos.y; POINT_SPRITEs[counter].z=0; } VOID* vertices; lpVertexBuffer9->Lock(0 ,128*sizeof(pVertices), (void**)&vertices, NULL); memcpy( vertices, POINT_SPRITEs, sizeof(POINT_SPRITEs)); lpVertexBuffer9->Unlock(); float MinSize=0.5f; float Scale=1; lpDirect3dDevice9->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); lpDirect3dDevice9->SetRenderState(D3DRS_POINTSCALEENABLE, TRUE); lpDirect3dDevice9->SetRenderState(D3DRS_POINTSIZE_MIN,*((DWORD*)&MinSize)); lpDirect3dDevice9->SetRenderState(D3DRS_POINTSCALE_A, *((DWORD*)&Scale)); lpDirect3dDevice9->SetRenderState(D3DRS_POINTSCALE_B, *((DWORD*)&Scale)); lpDirect3dDevice9->SetRenderState(D3DRS_POINTSCALE_C, *((DWORD*)&Scale)); lpDirect3dDevice9->SetStreamSource(0, lpVertexBuffer9, 0, sizeof(POINT_SPRITE)); lpDirect3dDevice9->SetFVF(FVF_CUSTOM); lpDirect3dDevice9->SetTexture(0, texture); lpDirect3dDevice9->DrawPrimitive(D3DPT_POINTLIST, 0, max_particles);}
Help will be much appreciated.
Thanks,
~Justin12343