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.

300 lines
7.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implements visual effects entities: sprites, beams, bubbles, etc.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "shake.h"
  9. #ifdef INFESTED_DLL
  10. #include "asw_marine.h"
  11. #include "asw_player.h"
  12. #endif
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. class CEnvFade : public CLogicalEntity
  16. {
  17. private:
  18. float m_Duration;
  19. float m_HoldTime;
  20. COutputEvent m_OnBeginFade;
  21. DECLARE_DATADESC();
  22. float m_flFadeStartTime;
  23. float m_flReverseFadeStartTime;
  24. float m_flReverseFadeDuration;
  25. public:
  26. DECLARE_CLASS( CEnvFade, CLogicalEntity );
  27. CEnvFade();
  28. virtual void Spawn( void );
  29. inline float Duration( void ) { return m_Duration; }
  30. inline float HoldTime( void ) { return m_HoldTime; }
  31. inline void SetDuration( float duration ) { m_Duration = duration; }
  32. inline void SetHoldTime( float hold ) { m_HoldTime = hold; }
  33. int DrawDebugTextOverlays(void);
  34. // Inputs
  35. void InputFade( inputdata_t &inputdata );
  36. void InputReverseFade( inputdata_t &inputdata );
  37. };
  38. LINK_ENTITY_TO_CLASS( env_fade, CEnvFade );
  39. BEGIN_DATADESC( CEnvFade )
  40. DEFINE_KEYFIELD( m_Duration, FIELD_FLOAT, "duration" ),
  41. DEFINE_KEYFIELD( m_HoldTime, FIELD_FLOAT, "holdtime" ),
  42. DEFINE_KEYFIELD( m_flReverseFadeDuration, FIELD_FLOAT, "ReverseFadeDuration" ),
  43. DEFINE_INPUTFUNC( FIELD_VOID, "Fade", InputFade ),
  44. DEFINE_INPUTFUNC( FIELD_VOID, "FadeReverse", InputReverseFade ),
  45. DEFINE_OUTPUT( m_OnBeginFade, "OnBeginFade"),
  46. END_DATADESC()
  47. #define SF_FADE_IN 0x0001 // Fade in, not out
  48. #define SF_FADE_MODULATE 0x0002 // Modulate, don't blend
  49. #define SF_FADE_ONLYONE 0x0004
  50. #define SF_FADE_STAYOUT 0x0008
  51. CEnvFade::CEnvFade()
  52. : m_flFadeStartTime(0.0f),
  53. m_flReverseFadeStartTime(0.0f)
  54. {
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. //-----------------------------------------------------------------------------
  59. void CEnvFade::Spawn( void )
  60. {
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose: Input handler that does the screen fade.
  64. //-----------------------------------------------------------------------------
  65. void CEnvFade::InputFade( inputdata_t &inputdata )
  66. {
  67. int fadeFlags = 0;
  68. if ( m_spawnflags & SF_FADE_IN )
  69. {
  70. fadeFlags |= FFADE_IN;
  71. }
  72. else
  73. {
  74. fadeFlags |= FFADE_OUT;
  75. }
  76. if ( m_spawnflags & SF_FADE_MODULATE )
  77. {
  78. fadeFlags |= FFADE_MODULATE;
  79. }
  80. if ( m_spawnflags & SF_FADE_STAYOUT )
  81. {
  82. fadeFlags |= FFADE_STAYOUT;
  83. }
  84. //color32 fadeColor = m_clrRender;
  85. //if( m_flReverseFadeStartTime != 0.0f )
  86. //{
  87. // float flCurrentFadeTime = gpGlobals->curtime - m_flReverseFadeStartTime;
  88. // //Change the fade alpha to match the alpha of the current fade to prevent a pop
  89. // if( flCurrentFadeTime < m_Duration )
  90. // {
  91. // fadeColor.a = m_clrRender.GetA() * ( flCurrentFadeTime )/ m_flReverseFadeDuration;
  92. // }
  93. //}
  94. if ( m_spawnflags & SF_FADE_ONLYONE )
  95. {
  96. #ifdef INFESTED_DLL
  97. if ( inputdata.pActivator->Classify() == CLASS_ASW_MARINE )
  98. {
  99. CASW_Marine *pMarine = static_cast<CASW_Marine*>( inputdata.pActivator );
  100. CASW_Player *pPlayer = pMarine->GetCommander();
  101. if ( pPlayer && pMarine->IsInhabited() )
  102. {
  103. UTIL_ScreenFade( pPlayer, m_clrRender, Duration(), HoldTime(), fadeFlags );
  104. }
  105. }
  106. #else
  107. if ( inputdata.pActivator->IsNetClient() )
  108. {
  109. UTIL_ScreenFade( inputdata.pActivator, m_clrRender, Duration(), HoldTime(), fadeFlags );
  110. }
  111. #endif
  112. }
  113. else
  114. {
  115. UTIL_ScreenFadeAll( m_clrRender, Duration(), HoldTime(), fadeFlags|FFADE_PURGE );
  116. }
  117. m_flFadeStartTime = gpGlobals->curtime;
  118. m_OnBeginFade.FireOutput( inputdata.pActivator, this );
  119. }
  120. void CEnvFade::InputReverseFade( inputdata_t &inputdata )
  121. {
  122. int fadeFlags = 0;
  123. if ( m_spawnflags & SF_FADE_IN )
  124. {
  125. fadeFlags |= FFADE_OUT;
  126. }
  127. else
  128. {
  129. fadeFlags |= FFADE_IN;
  130. }
  131. if ( m_spawnflags & SF_FADE_MODULATE )
  132. {
  133. fadeFlags |= FFADE_MODULATE;
  134. }
  135. if ( m_spawnflags & SF_FADE_STAYOUT )
  136. {
  137. fadeFlags |= FFADE_STAYOUT;
  138. }
  139. color32 fadeColor = m_clrRender;
  140. if( m_flFadeStartTime != 0.0f )
  141. {
  142. float flCurrentFadeTime = gpGlobals->curtime - m_flFadeStartTime;
  143. //Change the fade alpha to match the alpha of the current fade to prevent a pop
  144. if( flCurrentFadeTime < m_Duration )
  145. {
  146. fadeColor.a = m_clrRender.GetA() * ( flCurrentFadeTime )/ m_Duration;
  147. }
  148. }
  149. if ( m_spawnflags & SF_FADE_ONLYONE )
  150. {
  151. if ( inputdata.pActivator->IsNetClient() )
  152. {
  153. UTIL_ScreenFade( inputdata.pActivator, fadeColor, m_flReverseFadeDuration, HoldTime(), fadeFlags );
  154. }
  155. }
  156. else
  157. {
  158. UTIL_ScreenFadeAll( fadeColor, m_flReverseFadeDuration, HoldTime(), fadeFlags|FFADE_PURGE );
  159. }
  160. m_flReverseFadeStartTime = gpGlobals->curtime;
  161. m_OnBeginFade.FireOutput( inputdata.pActivator, this );
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Purpose: Fetches the arguments from the command line for the fadein and fadeout
  165. // console commands.
  166. // Input : flTime - Returns the fade time in seconds (the time to fade in or out)
  167. // clrFade - Returns the color to fade to or from.
  168. //-----------------------------------------------------------------------------
  169. static void GetFadeParms( const CCommand &args, float &flTime, color32 &clrFade)
  170. {
  171. flTime = 2.0f;
  172. if ( args.ArgC() > 1 )
  173. {
  174. flTime = atof( args[1] );
  175. }
  176. clrFade.r = 0;
  177. clrFade.g = 0;
  178. clrFade.b = 0;
  179. clrFade.a = 255;
  180. if ( args.ArgC() > 4 )
  181. {
  182. clrFade.r = atoi( args[2] );
  183. clrFade.g = atoi( args[3] );
  184. clrFade.b = atoi( args[4] );
  185. if ( args.ArgC() == 5 )
  186. {
  187. clrFade.a = atoi( args[5] );
  188. }
  189. }
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Purpose: Console command to fade out to a given color.
  193. //-----------------------------------------------------------------------------
  194. static void CC_FadeOut( const CCommand &args )
  195. {
  196. float flTime;
  197. color32 clrFade;
  198. GetFadeParms( args, flTime, clrFade );
  199. CBasePlayer *pPlayer = UTIL_GetCommandClient();
  200. UTIL_ScreenFade( pPlayer, clrFade, flTime, 0, FFADE_OUT | FFADE_PURGE | FFADE_STAYOUT );
  201. }
  202. static ConCommand fadeout("fadeout", CC_FadeOut, "fadeout {time r g b}: Fades the screen to black or to the specified color over the given number of seconds.", FCVAR_CHEAT );
  203. //-----------------------------------------------------------------------------
  204. // Purpose: Console command to fade in from a given color.
  205. //-----------------------------------------------------------------------------
  206. static void CC_FadeIn( const CCommand &args )
  207. {
  208. float flTime;
  209. color32 clrFade;
  210. GetFadeParms( args, flTime, clrFade );
  211. CBasePlayer *pPlayer = UTIL_GetCommandClient();
  212. UTIL_ScreenFade( pPlayer, clrFade, flTime, 0, FFADE_IN | FFADE_PURGE );
  213. }
  214. static ConCommand fadein("fadein", CC_FadeIn, "fadein {time r g b}: Fades the screen in from black or from the specified color over the given number of seconds.", FCVAR_CHEAT );
  215. //-----------------------------------------------------------------------------
  216. // Purpose: Draw any debug text overlays
  217. // Output : Current text offset from the top
  218. //-----------------------------------------------------------------------------
  219. int CEnvFade::DrawDebugTextOverlays( void )
  220. {
  221. int text_offset = BaseClass::DrawDebugTextOverlays();
  222. if (m_debugOverlays & OVERLAY_TEXT_BIT)
  223. {
  224. char tempstr[512];
  225. // print duration
  226. Q_snprintf(tempstr,sizeof(tempstr)," duration: %f", m_Duration);
  227. EntityText(text_offset,tempstr,0);
  228. text_offset++;
  229. // print hold time
  230. Q_snprintf(tempstr,sizeof(tempstr)," hold time: %f", m_HoldTime);
  231. EntityText(text_offset,tempstr,0);
  232. text_offset++;
  233. }
  234. return text_offset;
  235. }