I had an error with the vertex buffer (used D3DPOOL_MANAGED for some reason) and there is not any exception errors now, but I can't see any of the particles.
Here's the new code:
particleSystem.h
#ifndef _PARTICLE_#define _PARTICLE_#include <d3d9.h>#include <d3dx9.h>#include <math.h>#include <vector>#include "gfx_dx9.h"using namespace std;#define FVF_CUSTOM (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)enum ParticleState{ PSTATE_QUEUED, PSTATE_ACTIVE, PSTATE_DEAD};struct Vertex{ D3DXVECTOR3 pos; DWORD color; Vertex() { pos=D3DXVECTOR3(0.0f,0.0f,0.0f); color=D3DXCOLOR(1.0f,1.0f,1.0f,1.0f); }};inline DWORD FtoDW( FLOAT f ) { return *((DWORD*)&f); }struct Particle{ D3DXVECTOR3 position; D3DCOLOR color; float speed; float friction; float gravity; float direction; int life; ParticleState state; Particle() { position=D3DXVECTOR3( 0,0,0); color=D3DCOLOR_ARGB( 255,0, 0, 0); speed=friction=gravity=direction=0; state=PSTATE_QUEUED; } void update() { life -=1; if (life < 0){state=PSTATE_DEAD;} float x_speed=cos( (D3DX_PI/ 180) * direction)*speed-friction; float y_speed=sin( (D3DX_PI/ 180) * direction)*speed-gravity; position+=D3DXVECTOR3( x_speed, y_speed, 0); }};class emitter{public: emitter( LPDIRECT3DDEVICE9 device, const char *tex, D3DXVECTOR3 pos,float speed, float friction, float gravity, int life); ~emitter(); inline float getRandomMinMax( float fMin, float fMax ) { float fRandNum = (float)rand () / RAND_MAX; return fMin + (fMax - fMin) * fRandNum; } void emit_stream( int count, D3DXVECTOR3 pos, float direction); void emit_burst( int count, D3DXVECTOR3 pos, float min_direction, float max_direction); void update();private: LPDIRECT3DDEVICE9 pDevice; LPDIRECT3DVERTEXBUFFER9 pVertexBuffer; LPDIRECT3DTEXTURE9 texture; int MaxParticles; int ParticleCount; DWORD RenderCount; D3DXVECTOR3 xyz; vector<Particle *> ParticleArray;};#endif
particleSystem.cpp
[source lang="cpp]#include "particleSystem.h"emitter::emitter( LPDIRECT3DDEVICE9 device, const char *tex, D3DXVECTOR3 pos,float speed, float friction, float gravity, int life): pDevice( device), xyz(pos){ MaxParticles = 256; if (FAILED(pDevice->CreateVertexBuffer( 256*sizeof(Vertex), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY | D3DUSAGE_POINTS, FVF_CUSTOM, D3DPOOL_DEFAULT, &pVertexBuffer, NULL))) { MessageBox( NULL, "Failed To Create Vertex Buffer", "Error", MB_OK); } if(FAILED(D3DXCreateTextureFromFile( pDevice, tex, &texture))) { MessageBox(NULL, "Could Not Load Particle Texture", "Error", MB_OK); } for ( int i = 0; i <= MaxParticles; ++i) { Particle *pParticle = new Particle(); pParticle->position=xyz; pParticle->speed=speed; pParticle->friction=friction; pParticle->gravity=gravity; pParticle->life=life; pParticle->state=PSTATE_QUEUED; ParticleArray.push_back( pParticle); }}emitter::~emitter(){ if (texture){texture->Release();} if (pVertexBuffer){pVertexBuffer->Release();} for (int i = 0; i <=MaxParticles;++i) { delete ParticleArray; ParticleArray = NULL; } ParticleArray.clear();}void emitter::emit_stream( int count, D3DXVECTOR3 pos, float direction){ for (int i = 0; i < count; ++i) { ParticleArray.at(ParticleCount)->position=pos; ParticleArray.at(ParticleCount)->direction=direction; ParticleCount+=1; }}void emitter::emit_burst( int count, D3DXVECTOR3 pos, float min_direction, float max_direction){ for (int i = 0; i < count; ++i) { float direction = getRandomMinMax( min_direction, max_direction); ParticleArray[ParticleCount]->position=pos; ParticleArray[ParticleCount]->direction=direction; ParticleCount+=1; }}void emitter::update(){ for (int i = 0; i < ParticleCount; ++i) { if (ParticleArray->state = PSTATE_QUEUED) { if (ParticleArray->position!=xyz){ParticleArray->position=xyz;} ParticleArray->update(); ++RenderCount; } if (ParticleArray->state = PSTATE_ACTIVE) { ParticleArray->update(); ++RenderCount; } if (ParticleArray->state = PSTATE_DEAD) { ParticleArray.erase(ParticleArray.begin()+i); Particle *temp = new Particle(); ParticleArray.insert(ParticleArray.begin()+i+1, temp); } } pDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE ); // Turn on point sprites pDevice->SetRenderState( D3DRS_POINTSCALEENABLE, TRUE ); // Allow sprites to be scaled with distance pDevice->SetRenderState( D3DRS_POINTSIZE, FtoDW(1.0f) ); // Float value that specifies the size to use for point size computation in cases where point size is not specified for each vertex. pDevice->SetRenderState( D3DRS_POINTSIZE_MIN, FtoDW(1.0f) ); // Float value that specifies the minimum size of point primitives. Point primitives are clamped to this size during rendering. pDevice->SetRenderState( D3DRS_POINTSCALE_A, FtoDW(1.0f) ); pDevice->SetRenderState( D3DRS_POINTSCALE_B, FtoDW(1.0f) ); pDevice->SetRenderState( D3DRS_POINTSCALE_C, FtoDW(0.0f) ); pDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE ); pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); pDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); // fountain particles pDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); Vertex *pVertices; pVertexBuffer->Lock(0, ParticleArray.size()*sizeof(Vertex), (void**)&pVertices, D3DLOCK_DISCARD); for (UINT i = 0; i < ParticleArray.size(); ++i) { if (ParticleArray->state != PSTATE_DEAD) { pVertices->pos=ParticleArray->position; pVertices->color=ParticleArray->color; ++pVertices; RenderCount++; } } pVertexBuffer->Unlock(); if (RenderCount) { pDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof(Vertex)); pDevice->SetFVF(FVF_CUSTOM); pDevice->SetTexture( 0,texture); pDevice->DrawPrimitive(D3DPT_POINTLIST, 0, RenderCount); } pDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, FALSE ); pDevice->SetRenderState( D3DRS_POINTSCALEENABLE, FALSE ); pDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE ); pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );}
EDIT:
I'm also getting a lot of these errors:
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9B9FBD (exceeded 7ffdefff)
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BA1BF (exceeded 7ffdefff)
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BA3C1 (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BA5C3 (exceeded 7ffdefff)
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BA7C5 (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BA9C7 (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BABC9 (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BADCB (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BAFCD (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BB1CF (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in SADE_2D.exe: Microsoft C++ exception: long at memory location 0x0012f800..
HEAP[SADE_2D.exe]: Invalid allocation size - 9B9BB3D1 (exceeded 7ffdefff)