2007-05-07 18:33:24 +02:00
|
|
|
/**
|
|
|
|
* =========================================================================
|
|
|
|
* File : ParticleEmitter.h
|
|
|
|
* Project : 0 A.D.
|
|
|
|
* Description : Particle and Emitter base classes.
|
|
|
|
* =========================================================================
|
|
|
|
*/
|
2006-01-19 12:19:55 +01:00
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_PARTICLEEMITTER
|
|
|
|
#define INCLUDED_PARTICLEEMITTER
|
2006-01-19 12:19:55 +01:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
#include "maths/Vector3D.h"
|
2009-02-19 01:19:06 +01:00
|
|
|
#include "ps/CStr.h"
|
2006-02-17 03:02:18 +01:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
class CTexture;
|
2006-01-19 12:19:55 +01:00
|
|
|
|
|
|
|
class CEmitter
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2006-01-21 21:01:17 +01:00
|
|
|
static const int HALF_RAND = (RAND_MAX / 2);
|
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
public:
|
2006-01-19 12:19:55 +01:00
|
|
|
struct tColor
|
|
|
|
{
|
|
|
|
unsigned char r, g, b;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tParticle
|
|
|
|
{
|
|
|
|
// base stuff
|
2007-05-02 14:07:08 +02:00
|
|
|
CVector3D pos; // Current position 12
|
|
|
|
CVector3D dir; // Current direction with speed 12
|
2006-01-19 12:19:55 +01:00
|
|
|
float alpha; // Fade value 4
|
|
|
|
float alphaDelta; // Change of fade 4
|
|
|
|
tColor color; // Current color of particle 3
|
|
|
|
tColor deltaColor; // Change of color 3
|
|
|
|
short life; // How long it will last 2
|
|
|
|
|
|
|
|
// particle text stuff
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
CVector3D endPos; // For particle texture 12
|
2006-01-19 12:19:55 +01:00
|
|
|
bool inPos; // 1
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
tParticle* next; // pointer for link lists 4
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
tParticle()
|
|
|
|
{
|
|
|
|
next = 0;
|
|
|
|
}
|
|
|
|
};
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
//struct tParticleNode
|
|
|
|
//{
|
2007-05-02 14:07:08 +02:00
|
|
|
// tParticle* pParticle;
|
|
|
|
// tParticleNode* next;
|
2006-01-19 12:19:55 +01:00
|
|
|
//};
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
protected:
|
2009-02-19 01:19:06 +01:00
|
|
|
|
|
|
|
CStrW m_tag;
|
|
|
|
int m_maxParticles; // Maximum particles emitter can put out
|
|
|
|
int m_particleCount; // Total emitted right now
|
|
|
|
int m_emitsPerFrame, m_emitsVar; // Emits per frame and variation
|
|
|
|
int m_emitterLife; // Life of the emitter
|
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
bool isFinished; // tells the engine it's ready to be deleted
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
// Transformation Info
|
2009-02-19 01:19:06 +01:00
|
|
|
CVector3D m_pos; // XYZ Position of emitter
|
|
|
|
float m_yaw, m_yawVar; // Yaw of emitted particles
|
|
|
|
float m_pitch, m_pitchVar; // Pitch of emitted particles
|
|
|
|
float m_speed, m_speedVar; // Speed of emitted particles
|
|
|
|
|
|
|
|
// Particle linked lists
|
|
|
|
tParticle* m_heap; // Pointer to beginning of array
|
|
|
|
tParticle* m_openList; // linked list of unused particles
|
|
|
|
tParticle* m_usedList; // linked list of used particles
|
|
|
|
|
|
|
|
//Particle appearence
|
|
|
|
CTexture* m_texture; // Texture
|
|
|
|
float m_size; // size of the particles (if point sprites is not enabled)
|
|
|
|
tColor m_startColor, m_startColorVar; // Current color of particle
|
|
|
|
tColor m_endColor, m_endColorVar; // End color of particle
|
|
|
|
int m_blendMode; // Method used to blend particles
|
|
|
|
int m_alpha, m_alphaVar; // Alpha value for particles
|
|
|
|
int m_life, m_lifeVar; // Life count and variation (in Frames)
|
|
|
|
bool m_decrementLife; // Controls whether or not the particles life is decremented every update.
|
|
|
|
bool m_decrementAlpha; // Controls whether or not the particles alpha is decremented every update.
|
|
|
|
bool m_renderParticles; // Controls the rendering of the particles.
|
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
// Physics
|
2009-02-19 01:19:06 +01:00
|
|
|
CVector3D m_force; // Forces that affect the particles
|
2006-01-19 12:19:55 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
CEmitter(const int MAX_PARTICLES = 4000, const int lifetime = -1, int textureID = 0);
|
2007-05-02 14:07:08 +02:00
|
|
|
virtual ~CEmitter(void);
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2009-02-19 01:19:06 +01:00
|
|
|
// note: methods are virtual and overridable so as to suit the
|
2007-05-02 14:07:08 +02:00
|
|
|
// specific particle needs.
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2009-02-19 01:19:06 +01:00
|
|
|
virtual bool LoadFromXML(const CStr& filename );
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
virtual bool Setup() { return false; }
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
virtual bool AddParticle();
|
|
|
|
|
2009-02-19 01:19:06 +01:00
|
|
|
virtual bool Update();
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
virtual bool Render();
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2009-02-19 01:19:06 +01:00
|
|
|
// Helper functions
|
2006-01-19 12:19:55 +01:00
|
|
|
inline float RandomNum()
|
|
|
|
{
|
|
|
|
int rn;
|
|
|
|
rn = rand();
|
|
|
|
return ((float)(rn - HALF_RAND) / (float)HALF_RAND);
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
inline char RandomChar()
|
|
|
|
{
|
|
|
|
return (unsigned char)(rand() >> 24);
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
inline void RotationToDirection(float pitch, float yaw, CVector3D* direction)
|
2006-01-19 12:19:55 +01:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
direction->X = (float)(-sin(yaw)* cos(pitch));
|
|
|
|
direction->Y = (float)sin(pitch);
|
|
|
|
direction->Z = (float)(cos(pitch)* cos(yaw));
|
2006-01-19 12:19:55 +01:00
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Accessors
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////
|
2009-02-19 01:19:06 +01:00
|
|
|
CStrW GetTag() { return m_tag; }
|
|
|
|
float GetPosX() { return m_pos.X; }
|
|
|
|
float GetPosY() { return m_pos.Y; }
|
|
|
|
float GetPosZ() { return m_pos.Z; }
|
|
|
|
CVector3D GetPosVec() { return m_pos; }
|
2007-05-02 14:07:08 +02:00
|
|
|
bool IsFinished(void) { return isFinished; }
|
2009-02-19 01:19:06 +01:00
|
|
|
int GetEmitterLife() { return m_emitterLife; }
|
|
|
|
int GetParticleCount() { return m_particleCount; }
|
|
|
|
int GetMaxParticles(void) { return m_maxParticles; }
|
|
|
|
tColor GetStartColor(void) { return m_startColor; }
|
|
|
|
tColor GetStartColorVar(void) { return m_startColorVar; }
|
|
|
|
tColor GetEndColor(void) { return m_endColor; }
|
|
|
|
tColor GetEndColorVar(void) { return m_endColorVar; }
|
|
|
|
int GetBlendMode(void) { return m_blendMode; }
|
|
|
|
float GetSize(void) { return m_size; }
|
|
|
|
float GetYaw(void) { return m_yaw; }
|
|
|
|
float GetYawVar(void) { return m_yawVar; }
|
|
|
|
float GetPitch(void) { return m_pitch; }
|
|
|
|
float GetPitchVar(void) { return m_pitchVar; }
|
|
|
|
float GetSpeed(void) { return m_speed; }
|
|
|
|
float GetSpeedVar(void) { return m_speedVar; }
|
|
|
|
int GetEmitsPerFrame(void) { return m_emitsPerFrame; }
|
|
|
|
int GetEmitVar(void) { return m_emitsVar; }
|
|
|
|
int GetLife(void) { return m_life; }
|
|
|
|
int GetLifeVar(void) { return m_lifeVar; }
|
|
|
|
float GetForceX(void) { return m_force.X; }
|
|
|
|
float GetForceY(void) { return m_force.Y; }
|
|
|
|
float GetForceZ(void) { return m_force.Z; }
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
|
2006-01-19 12:19:55 +01:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Mutators
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////
|
2009-02-19 01:19:06 +01:00
|
|
|
void SetTag(CStrW tag) { m_tag = tag; }
|
|
|
|
void SetPosX(float posX) { m_pos.X = posX; }
|
|
|
|
void SetPosY(float posY) { m_pos.Y = posY; }
|
|
|
|
void SetPosZ(float posZ) { m_pos.Z = posZ; }
|
2007-05-02 14:07:08 +02:00
|
|
|
inline void SetPosVec(const CVector3D& newPos)
|
2006-01-19 12:19:55 +01:00
|
|
|
{
|
2009-02-19 01:19:06 +01:00
|
|
|
m_pos = newPos;
|
2006-01-19 12:19:55 +01:00
|
|
|
}
|
2009-02-19 01:19:06 +01:00
|
|
|
void SetTexture(CTexture* id) { m_texture = id; }
|
2007-05-02 14:07:08 +02:00
|
|
|
void SetIsFinished(bool finished) { isFinished = finished; }
|
2009-02-19 01:19:06 +01:00
|
|
|
void SetEmitterLife(int life) { m_emitterLife = life; }
|
|
|
|
void SetLife(int newlife) { m_life = newlife; }
|
|
|
|
void SetLifeVar(int newlifevar) { m_lifeVar = newlifevar; }
|
|
|
|
void SetSpeed(float newspeed) { m_speed = newspeed; }
|
|
|
|
void SetSpeedVar(float newspeedvar) { m_speedVar = newspeedvar; }
|
|
|
|
void SetYaw(float newyaw) { m_yaw = newyaw; }
|
|
|
|
void SetYawVar(float newyawvar) { m_yawVar = newyawvar; }
|
|
|
|
void SetPitch(float newpitch) { m_pitch = newpitch; }
|
|
|
|
void SetPitchVar(float newpitchvar) { m_pitchVar = newpitchvar; }
|
|
|
|
void SetStartColor(tColor newColor) { m_startColor = newColor; }
|
|
|
|
void SetStartColorVar(tColor newColorVar) { m_startColorVar = newColorVar; }
|
|
|
|
void SetEndColor(tColor newColor) { m_endColor = newColor; }
|
|
|
|
void SetEndColorVar(tColor newColorVar) { m_endColorVar = newColorVar; }
|
|
|
|
void SetStartColorR(int newColorR) { m_startColor.r = newColorR; }
|
|
|
|
void SetStartColorG(int newColorG) { m_startColor.g = newColorG; }
|
|
|
|
void SetStartColorB(int newColorB) { m_startColor.b = newColorB; }
|
|
|
|
void SetStartColorVarR(int newColorVarR) { m_startColorVar.r = newColorVarR; }
|
|
|
|
void SetStartColorVarG(int newColorVarG) { m_startColorVar.g = newColorVarG; }
|
|
|
|
void SetStartColorVarB(int newColorVarB) { m_startColorVar.b = newColorVarB; }
|
|
|
|
void SetEndColorR(int newColorR) { m_endColor.r = newColorR; }
|
|
|
|
void SetEndColorG(int newColorG) { m_endColor.g = newColorG; }
|
|
|
|
void SetEndColorB(int newColorB) { m_endColor.b = newColorB; }
|
|
|
|
void SetEndColorVarR(int newColorVarR) { m_endColorVar.r = newColorVarR; }
|
|
|
|
void SetEndColorVarG(int newColorVarG) { m_endColorVar.g = newColorVarG; }
|
|
|
|
void SetEndColorVarB(int newColorVarB) { m_endColorVar.b = newColorVarB; }
|
2007-05-02 14:07:08 +02:00
|
|
|
inline void SetBlendMode(int blendmode)
|
2006-01-19 12:19:55 +01:00
|
|
|
{
|
|
|
|
if(blendmode >= 1 && blendmode <= 4)
|
2009-02-19 01:19:06 +01:00
|
|
|
m_blendMode = blendmode;
|
2006-01-19 12:19:55 +01:00
|
|
|
else
|
2009-02-19 01:19:06 +01:00
|
|
|
m_blendMode = 1;
|
2006-01-19 12:19:55 +01:00
|
|
|
}
|
2009-02-19 01:19:06 +01:00
|
|
|
void SetEmitsPerFrame(int emitsperframe) { m_emitsPerFrame = emitsperframe; }
|
|
|
|
void SetEmitVar(int emitvar) { m_emitsVar = emitvar; }
|
|
|
|
void SetForceX(float forceX) { m_force.X = forceX; }
|
|
|
|
void SetForceY(float forceY) { m_force.Y = forceY; }
|
|
|
|
void SetForceZ(float forceZ) { m_force.Z = forceZ; }
|
|
|
|
void SetSize(float newSize) { m_size = newSize; }
|
|
|
|
void SetRenderParticles(bool render) { m_renderParticles = render; }
|
2004-05-29 22:56:24 +02:00
|
|
|
};
|
|
|
|
|
2006-01-21 21:01:17 +01:00
|
|
|
#endif
|