#include "CommonFuncs.h"
#include "D3DHelperFuncs.h"
#include "Ch18p1_Particle.h"
#include "RecyclingArray.h"

#define NUMPARTICLES 500

typedef struct 
{
    D3DXVECTOR3 position;
    float       pointsize;
    D3DCOLOR    color;
} VERTEX_PARTICLE;

#define D3DFVF_PARTICLE (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_PSIZE)

class CParticleEmitter
{
public:
  CParticleEmitter();
  virtual ~CParticleEmitter();

  virtual void Update(float fElapsedTime, float fTimeDelta);

  virtual HRESULT Render();

  virtual HRESULT RestoreDeviceObjects(LPDIRECT3DDEVICE8 pDev, 
    const char *strTextureFilename);

  virtual void InvalidateDeviceObjects();

  // attributes
  D3DXVECTOR3 GetGravity(void) const { return(m_vGravity); }
  void SetGravity(const D3DXVECTOR3 &data) { m_vGravity = data; }

  D3DXVECTOR3 GetPos(void) const { return(m_vPos); }
  void SetPos(const D3DXVECTOR3 &data) { m_vPos = data; }

  float GetMinEmitRate(void) const { return(m_fMinEmitRate); }
  void SetMinEmitRate(const float data) { m_fMinEmitRate = data; }

  float GetMaxEmitRate(void) const { return(m_fMaxEmitRate); }
  void SetMaxEmitRate(const float data) { m_fMaxEmitRate = data; }

  D3DXCOLOR GetColor1(void) const { return(m_Color1); }
  void SetColor1(const D3DXCOLOR &data) { m_Color1 = data; }

  D3DXCOLOR GetColor2(void) const { return(m_Color2); }
  void SetColor2(const D3DXCOLOR &data) { m_Color2 = data; }

  float GetMinSize(void) const { return(m_fMinSize); }
  void SetMinSize(const float data) { m_fMinSize = data; }

  float GetMaxSize(void) const { return(m_fMaxSize); }
  void SetMaxSize(const float data) { m_fMaxSize = data; }

  D3DXVECTOR3 GetSpawnDir1(void) const { return(m_vSpawnDir1); }
  void SetSpawnDir1(const D3DXVECTOR3 &data) { m_vSpawnDir1 = data; }

  D3DXVECTOR3 GetSpawnDir2(void) const { return(m_vSpawnDir2); }
  void SetSpawnDir2(const D3DXVECTOR3 &data) { m_vSpawnDir2 = data; }

  int GetVBSize(void) const { return(m_iVBSize); }
  void SetVBSize(const int data) { m_iVBSize = data; }
  
private:

  // particle system attributes
  D3DXVECTOR3 m_vGravity;
  D3DXVECTOR3 m_vPos; // position of emitter
  float m_fMinEmitRate; // in particles/sec
  float m_fMaxEmitRate; // in particles/sec

  // these vars determine starting values for each particle
  D3DXCOLOR m_Color1;
  D3DXCOLOR m_Color2;
  
  float m_fMinSize;
  float m_fMaxSize;

  D3DXVECTOR3 m_vSpawnDir1;
  D3DXVECTOR3 m_vSpawnDir2;

  // other stuff
  float m_fNumNewPartsExcess;
  int m_iVBSize; // size of the vertex buffer (can and should be less than NUMPARTICLES)
  LPDIRECT3DDEVICE8 m_pd3dDevice;
  LPDIRECT3DVERTEXBUFFER8 m_vbParticles;
  LPDIRECT3DTEXTURE8 m_texParticle;

  CRecyclingArray<CParticle, NUMPARTICLES> m_Particles;
};

