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.

234 lines
6.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #include "cbase.h"
  14. #include "clientsideeffects.h"
  15. #include "tier0/vprof.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. bool g_FXCreationAllowed = false;
  19. //-----------------------------------------------------------------------------
  20. // Purpose:
  21. // Input : state -
  22. //-----------------------------------------------------------------------------
  23. void SetFXCreationAllowed( bool state )
  24. {
  25. g_FXCreationAllowed = state;
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Purpose:
  29. // Output : Returns true on success, false on failure.
  30. //-----------------------------------------------------------------------------
  31. bool FXCreationAllowed( void )
  32. {
  33. return g_FXCreationAllowed;
  34. }
  35. // TODO: Sort effects and their children back to front from view positon? At least with buckets or something.
  36. //
  37. //-----------------------------------------------------------------------------
  38. // Purpose: Construct and activate effect
  39. // Input : *name -
  40. //-----------------------------------------------------------------------------
  41. CClientSideEffect::CClientSideEffect( const char *name )
  42. {
  43. m_pszName = name;
  44. Assert( name );
  45. m_bActive = true;
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: Destroy effect
  49. //-----------------------------------------------------------------------------
  50. CClientSideEffect::~CClientSideEffect( void )
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: Get name of effect
  55. // Output : const char
  56. //-----------------------------------------------------------------------------
  57. const char *CClientSideEffect::GetName( void )
  58. {
  59. return m_pszName;
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose: Is effect still active?
  63. // Output : Returns true on success, false on failure.
  64. //-----------------------------------------------------------------------------
  65. bool CClientSideEffect::IsActive( void )
  66. {
  67. return m_bActive;
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose: Mark effect for destruction
  71. //-----------------------------------------------------------------------------
  72. void CClientSideEffect::Destroy( void )
  73. {
  74. m_bActive = false;
  75. }
  76. #define MAX_EFFECTS 256
  77. //-----------------------------------------------------------------------------
  78. // Purpose: Implements effects list interface
  79. //-----------------------------------------------------------------------------
  80. class CEffectsList : public IEffectsList
  81. {
  82. public:
  83. CEffectsList( void );
  84. virtual ~CEffectsList( void );
  85. // Add an effect to the effects list
  86. void AddEffect( CClientSideEffect *effect );
  87. // Remove the specified effect
  88. // Draw/update all effects in the current list
  89. void DrawEffects( double frametime );
  90. // Flush out all effects from the list
  91. void Flush( void );
  92. private:
  93. void RemoveEffect( int effectIndex );
  94. // Current number of effects
  95. int m_nEffects;
  96. // Pointers to current effects
  97. CClientSideEffect *m_rgEffects[ MAX_EFFECTS ];
  98. };
  99. // Implements effects list and exposes interface
  100. static CEffectsList g_EffectsList;
  101. // Public interface
  102. IEffectsList *clienteffects = ( IEffectsList * )&g_EffectsList;
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. //-----------------------------------------------------------------------------
  106. CEffectsList::CEffectsList( void )
  107. {
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Purpose:
  111. //-----------------------------------------------------------------------------
  112. CEffectsList::~CEffectsList( void )
  113. {
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Purpose: Add effect to effects list
  117. // Input : *effect -
  118. //-----------------------------------------------------------------------------
  119. void CEffectsList::AddEffect( CClientSideEffect *effect )
  120. {
  121. #if 0
  122. if ( FXCreationAllowed() == false )
  123. {
  124. //NOTENOTE: If you've hit this, you may not add a client effect where you have attempted to.
  125. // Most often this means that you have added it in an entity's DrawModel function.
  126. // Move this to the ClientThink function instead!
  127. Assert(0);
  128. return;
  129. }
  130. #endif
  131. if ( effect == NULL )
  132. return;
  133. if ( m_nEffects >= MAX_EFFECTS )
  134. {
  135. DevWarning( 1, "No room for effect %s\n", effect->GetName() );
  136. return;
  137. }
  138. m_rgEffects[ m_nEffects++ ] = effect;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: Remove specified effect by index
  142. // Input : effectIndex -
  143. //-----------------------------------------------------------------------------
  144. void CEffectsList::RemoveEffect( int effectIndex )
  145. {
  146. if ( effectIndex >= m_nEffects || effectIndex < 0 )
  147. return;
  148. CClientSideEffect *pEffect = m_rgEffects[effectIndex];
  149. m_nEffects--;
  150. if ( m_nEffects > 0 && effectIndex != m_nEffects )
  151. {
  152. // move the last one down to fill the empty slot
  153. m_rgEffects[effectIndex] = m_rgEffects[m_nEffects];
  154. }
  155. pEffect->Destroy();
  156. delete pEffect; //FIXME: Yes, no?
  157. }
  158. //-----------------------------------------------------------------------------
  159. // Purpose: Iterate through list and simulate/draw stuff
  160. // Input : frametime -
  161. //-----------------------------------------------------------------------------
  162. void CEffectsList::DrawEffects( double frametime )
  163. {
  164. VPROF_BUDGET( "CEffectsList::DrawEffects", VPROF_BUDGETGROUP_PARTICLE_RENDERING );
  165. int i;
  166. CClientSideEffect *effect;
  167. // Go backwards so deleting effects doesn't screw up
  168. for ( i = m_nEffects - 1 ; i >= 0; i-- )
  169. {
  170. effect = m_rgEffects[ i ];
  171. if ( !effect )
  172. continue;
  173. // Simulate
  174. effect->Draw( frametime );
  175. // Remove it if needed
  176. if ( !effect->IsActive() )
  177. {
  178. RemoveEffect( i );
  179. }
  180. }
  181. }
  182. //==================================================
  183. // Purpose:
  184. // Input:
  185. //==================================================
  186. void CEffectsList::Flush( void )
  187. {
  188. int i;
  189. CClientSideEffect *effect;
  190. // Go backwards so deleting effects doesn't screw up
  191. for ( i = m_nEffects - 1 ; i >= 0; i-- )
  192. {
  193. effect = m_rgEffects[ i ];
  194. if ( effect == NULL )
  195. continue;
  196. RemoveEffect( i );
  197. }
  198. }