Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef PARTICLES_SIMPLE_H
  8. #define PARTICLES_SIMPLE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "particlemgr.h"
  13. #include "particlesphererenderer.h"
  14. #include "smartptr.h"
  15. // ------------------------------------------------------------------------------------------------ //
  16. // CParticleEffect is the base class that you can derive from to make a particle effect.
  17. // These can be used two ways:
  18. //
  19. // 1. Allocate a CParticleEffect-based object using the class's static Create() function. This gives
  20. // you back a smart pointer that handles the reference counting for you.
  21. //
  22. // 2. Contain a CParticleEffect object in your class.
  23. // ------------------------------------------------------------------------------------------------ //
  24. class CParticleEffect : public IParticleEffect
  25. {
  26. public:
  27. DECLARE_CLASS_NOBASE( CParticleEffect );
  28. friend class CRefCountAccessor;
  29. // Call this before adding a bunch of particles to give it a rough estimate of where
  30. // your particles are for sorting amongst other translucent entities.
  31. void SetSortOrigin( const Vector &vSortOrigin );
  32. PMaterialHandle GetPMaterial(const char *name);
  33. Particle* AddParticle( unsigned int particleSize, PMaterialHandle material, const Vector &origin );
  34. CParticleEffectBinding& GetBinding() { return m_ParticleEffect; }
  35. const char *GetEffectName();
  36. void AddFlags( int iFlags ) { m_Flags |= iFlags; }
  37. void RemoveFlags( int iFlags ) { m_Flags &= ~iFlags; }
  38. void SetDontRemove( bool bSet )
  39. {
  40. if( bSet )
  41. AddFlags( FLAG_DONT_REMOVE );
  42. else
  43. RemoveFlags( FLAG_DONT_REMOVE );
  44. }
  45. // IParticleEffect overrides
  46. public:
  47. virtual void SetParticleCullRadius( float radius );
  48. virtual void NotifyRemove( void );
  49. virtual const Vector & GetSortOrigin();
  50. virtual void NotifyDestroyParticle( Particle* pParticle );
  51. virtual void Update( float flTimeDelta );
  52. // All Create() functions should call this so the effect deletes itself
  53. // when it is removed from the particle manager.
  54. void SetDynamicallyAllocated( bool bDynamic=true );
  55. virtual bool ShouldSimulate() const { return m_bSimulate; }
  56. virtual void SetShouldSimulate( bool bSim ) { m_bSimulate = bSim; }
  57. int AllocateToolParticleEffectId();
  58. int GetToolParticleEffectId() const;
  59. protected:
  60. CParticleEffect( const char *pDebugName );
  61. virtual ~CParticleEffect();
  62. // Returns nonzero if Release() has been called.
  63. int IsReleased();
  64. enum
  65. {
  66. FLAG_ALLOCATED = (1<<1), // Most of the CParticleEffects are dynamically allocated but
  67. // some are member variables of a class. If they're member variables.
  68. FLAG_DONT_REMOVE = (1<<2),
  69. };
  70. // Used to track down bugs.
  71. char const *m_pDebugName;
  72. CParticleEffectBinding m_ParticleEffect;
  73. Vector m_vSortOrigin;
  74. int m_Flags; // Combination of CParticleEffect::FLAG_
  75. bool m_bSimulate;
  76. int m_nToolParticleEffectId;
  77. private:
  78. // Update the reference count.
  79. void AddRef();
  80. void Release();
  81. int m_RefCount; // When this goes to zero and the effect has no more active
  82. // particles, (and it's dynamically allocated), it will delete itself.
  83. CParticleEffect( const CParticleEffect & ); // not defined, not accessible
  84. };
  85. inline int CParticleEffect::GetToolParticleEffectId() const
  86. {
  87. return m_nToolParticleEffectId;
  88. }
  89. inline int CParticleEffect::AllocateToolParticleEffectId()
  90. {
  91. m_nToolParticleEffectId = ParticleMgr()->AllocateToolParticleEffectId();
  92. return m_nToolParticleEffectId;
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Particle flags
  96. //-----------------------------------------------------------------------------
  97. enum SimpleParticleFlag_t
  98. {
  99. SIMPLE_PARTICLE_FLAG_WINDBLOWN = 0x1,
  100. SIMPLE_PARTICLE_FLAG_NO_VEL_DECAY = 0x2 // Used by the blood spray emitter. By default, it decays the
  101. // particle velocity.
  102. };
  103. class SimpleParticle : public Particle
  104. {
  105. public:
  106. SimpleParticle() : m_iFlags(0) {}
  107. // AddSimpleParticle automatically initializes these fields.
  108. Vector m_vecVelocity;
  109. float m_flRoll;
  110. float m_flDieTime; // How long it lives for.
  111. float m_flLifetime; // How long it has been alive for so far.
  112. unsigned char m_uchColor[3];
  113. unsigned char m_uchStartAlpha;
  114. unsigned char m_uchEndAlpha;
  115. unsigned char m_uchStartSize;
  116. unsigned char m_uchEndSize;
  117. unsigned char m_iFlags; // See SimpleParticleFlag_t above
  118. float m_flRollDelta;
  119. };
  120. // CSimpleEmitter implements a common way to simulate and render particles.
  121. //
  122. // Effects can add particles to the particle manager and point at CSimpleEmitter
  123. // for the effect so they don't have to implement the simulation code. It simulates
  124. // velocity, and fades their alpha from invisible to solid and back to invisible over their lifetime.
  125. //
  126. // Particles you add using this effect must use the class CParticleSimple::Particle.
  127. class CSimpleEmitter : public CParticleEffect
  128. {
  129. // IParticleEffect overrides.
  130. public:
  131. DECLARE_CLASS( CSimpleEmitter, CParticleEffect );
  132. static CSmartPtr<CSimpleEmitter> Create( const char *pDebugName );
  133. virtual void SimulateParticles( CParticleSimulateIterator *pIterator );
  134. virtual void RenderParticles( CParticleRenderIterator *pIterator );
  135. void SetNearClip( float nearClipMin, float nearClipMax );
  136. void SetDrawBeforeViewModel( bool state = true );
  137. SimpleParticle* AddSimpleParticle( PMaterialHandle hMaterial, const Vector &vOrigin, float flDieTime=3, unsigned char uchSize=10 );
  138. // Overridables for variants like CEmberEffect.
  139. protected:
  140. CSimpleEmitter( const char *pDebugName = NULL );
  141. virtual ~CSimpleEmitter();
  142. virtual float UpdateAlpha( const SimpleParticle *pParticle );
  143. virtual float UpdateScale( const SimpleParticle *pParticle );
  144. virtual float UpdateRoll( SimpleParticle *pParticle, float timeDelta );
  145. virtual void UpdateVelocity( SimpleParticle *pParticle, float timeDelta );
  146. virtual Vector UpdateColor( const SimpleParticle *pParticle );
  147. float m_flNearClipMin;
  148. float m_flNearClipMax;
  149. private:
  150. CSimpleEmitter( const CSimpleEmitter & ); // not defined, not accessible
  151. };
  152. //==================================================
  153. // EmberEffect
  154. //==================================================
  155. class CEmberEffect : public CSimpleEmitter
  156. {
  157. public:
  158. CEmberEffect( const char *pDebugName );
  159. static CSmartPtr<CEmberEffect> Create( const char *pDebugName );
  160. virtual void UpdateVelocity( SimpleParticle *pParticle, float timeDelta );
  161. virtual Vector UpdateColor( const SimpleParticle *pParticle );
  162. private:
  163. CEmberEffect( const CEmberEffect & ); // not defined, not accessible
  164. };
  165. //==================================================
  166. // FireSmokeEffect
  167. //==================================================
  168. class CFireSmokeEffect : public CSimpleEmitter
  169. {
  170. public:
  171. CFireSmokeEffect( const char *pDebugName );
  172. static CSmartPtr<CFireSmokeEffect> Create( const char *pDebugName );
  173. virtual void UpdateVelocity( SimpleParticle *pParticle, float timeDelta );
  174. virtual float UpdateAlpha( const SimpleParticle *pParticle );
  175. protected:
  176. VPlane m_planeClip;
  177. private:
  178. CFireSmokeEffect( const CFireSmokeEffect & ); // not defined, not accessible
  179. };
  180. //==================================================
  181. // CFireParticle
  182. //==================================================
  183. class CFireParticle : public CSimpleEmitter
  184. {
  185. public:
  186. CFireParticle( const char *pDebugName );
  187. static CSmartPtr<CFireParticle> Create( const char *pDebugName );
  188. virtual Vector UpdateColor( const SimpleParticle *pParticle );
  189. private:
  190. CFireParticle( const CFireParticle & ); // not defined, not accessible
  191. };
  192. #endif // PARTICLES_SIMPLE_H