Counter Strike : Global Offensive Source Code
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.

260 lines
7.8 KiB

  1. //===== Copyright � 1996-2005, 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. explicit 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. void SetShouldDrawForSplitScreenUser( int nSlot );
  139. // Overridables for variants like CEmberEffect.
  140. protected:
  141. explicit CSimpleEmitter( const char *pDebugName = NULL );
  142. virtual ~CSimpleEmitter();
  143. virtual float UpdateAlpha( const SimpleParticle *pParticle );
  144. virtual float UpdateScale( const SimpleParticle *pParticle );
  145. virtual float UpdateRoll( SimpleParticle *pParticle, float timeDelta );
  146. virtual void UpdateVelocity( SimpleParticle *pParticle, float timeDelta );
  147. virtual Vector UpdateColor( const SimpleParticle *pParticle );
  148. float m_flNearClipMin;
  149. float m_flNearClipMax;
  150. int m_nSplitScreenPlayerSlot;
  151. private:
  152. CSimpleEmitter( const CSimpleEmitter & ); // not defined, not accessible
  153. };
  154. //==================================================
  155. // EmberEffect
  156. //==================================================
  157. class CEmberEffect : public CSimpleEmitter
  158. {
  159. public:
  160. explicit CEmberEffect( const char *pDebugName );
  161. static CSmartPtr<CEmberEffect> Create( const char *pDebugName );
  162. virtual void UpdateVelocity( SimpleParticle *pParticle, float timeDelta );
  163. virtual Vector UpdateColor( const SimpleParticle *pParticle );
  164. private:
  165. CEmberEffect( const CEmberEffect & ); // not defined, not accessible
  166. };
  167. //==================================================
  168. // FireSmokeEffect
  169. //==================================================
  170. class CFireSmokeEffect : public CSimpleEmitter
  171. {
  172. public:
  173. explicit CFireSmokeEffect( const char *pDebugName );
  174. static CSmartPtr<CFireSmokeEffect> Create( const char *pDebugName );
  175. virtual void UpdateVelocity( SimpleParticle *pParticle, float timeDelta );
  176. virtual float UpdateAlpha( const SimpleParticle *pParticle );
  177. protected:
  178. VPlane m_planeClip;
  179. private:
  180. CFireSmokeEffect( const CFireSmokeEffect & ); // not defined, not accessible
  181. };
  182. //==================================================
  183. // CFireParticle
  184. //==================================================
  185. class CFireParticle : public CSimpleEmitter
  186. {
  187. public:
  188. explicit CFireParticle( const char *pDebugName );
  189. static CSmartPtr<CFireParticle> Create( const char *pDebugName );
  190. virtual Vector UpdateColor( const SimpleParticle *pParticle );
  191. private:
  192. CFireParticle( const CFireParticle & ); // not defined, not accessible
  193. };
  194. #endif // PARTICLES_SIMPLE_H