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.

262 lines
6.7 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "cbase.h"
  8. #include "clientalphaproperty.h"
  9. #include "const.h"
  10. #include "iclientshadowmgr.h"
  11. #include "iclientunknown.h"
  12. #include "iclientrenderable.h"
  13. // NOTE: This has to be the last file included!
  14. #include "tier0/memdbgon.h"
  15. //-----------------------------------------------------------------------------
  16. // Client alpha property starts here
  17. //-----------------------------------------------------------------------------
  18. CClientAlphaProperty::CClientAlphaProperty( )
  19. {
  20. m_nRenderFX = kRenderFxNone;
  21. m_nRenderMode = kRenderNormal;
  22. m_nDesyncOffset = 0;
  23. m_hShadowHandle = CLIENTSHADOW_INVALID_HANDLE;
  24. m_nAlpha = 255;
  25. m_flFadeScale = 0.0f; // By default, things don't fade out automagically
  26. m_nDistFadeStart = 0;
  27. m_nDistFadeEnd = 0;
  28. m_bAlphaOverride = false;
  29. m_bShadowAlphaOverride = false;
  30. m_nDistanceFadeMode = CLIENT_ALPHA_DISTANCE_FADE_USE_CENTER;
  31. }
  32. void CClientAlphaProperty::Init( IClientUnknown *pUnk )
  33. {
  34. m_pOuter = pUnk;
  35. }
  36. IClientUnknown* CClientAlphaProperty::GetIClientUnknown()
  37. {
  38. return m_pOuter;
  39. }
  40. void CClientAlphaProperty::SetShadowHandle( ClientShadowHandle_t hShadowHandle )
  41. {
  42. m_hShadowHandle = hShadowHandle;
  43. }
  44. void CClientAlphaProperty::SetAlphaModulation( uint8 a )
  45. {
  46. m_nAlpha = a;
  47. }
  48. void CClientAlphaProperty::EnableAlphaModulationOverride( bool bEnable )
  49. {
  50. m_bAlphaOverride = bEnable;
  51. }
  52. void CClientAlphaProperty::EnableShadowAlphaModulationOverride( bool bEnable )
  53. {
  54. m_bShadowAlphaOverride = bEnable;
  55. }
  56. // Sets an FX function
  57. void CClientAlphaProperty::SetRenderFX( RenderFx_t nRenderFx, RenderMode_t nRenderMode, float flStartTime, float flDuration )
  58. {
  59. bool bStartTimeUnspecified = ( flStartTime == FLT_MAX );
  60. bool bRenderFxChanged = ( m_nRenderFX != nRenderFx );
  61. switch( nRenderFx )
  62. {
  63. case kRenderFxFadeIn:
  64. case kRenderFxFadeOut:
  65. Assert( !bStartTimeUnspecified || !bRenderFxChanged );
  66. if ( bStartTimeUnspecified )
  67. {
  68. flStartTime = gpGlobals->curtime;
  69. }
  70. break;
  71. case kRenderFxFadeSlow:
  72. case kRenderFxSolidSlow:
  73. Assert( !bStartTimeUnspecified || !bRenderFxChanged );
  74. if ( bStartTimeUnspecified )
  75. {
  76. flStartTime = gpGlobals->curtime;
  77. }
  78. flDuration = 4.0f;
  79. break;
  80. case kRenderFxFadeFast:
  81. case kRenderFxSolidFast:
  82. Assert( !bStartTimeUnspecified || !bRenderFxChanged );
  83. if ( bStartTimeUnspecified )
  84. {
  85. flStartTime = gpGlobals->curtime;
  86. }
  87. flDuration = 1.0f;
  88. break;
  89. }
  90. m_nRenderMode = nRenderMode;
  91. m_nRenderFX = nRenderFx;
  92. if ( bRenderFxChanged || !bStartTimeUnspecified )
  93. {
  94. m_flRenderFxStartTime = flStartTime;
  95. m_flRenderFxDuration = flDuration;
  96. }
  97. }
  98. void CClientAlphaProperty::SetDesyncOffset( int nOffset )
  99. {
  100. m_nDesyncOffset = nOffset;
  101. }
  102. void CClientAlphaProperty::SetDistanceFadeMode( ClientAlphaDistanceFadeMode_t nFadeMode )
  103. {
  104. // Necessary since m_nDistanceFadeMode is stored in 1 bit
  105. COMPILE_TIME_ASSERT( CLIENT_ALPHA_DISTANCE_FADE_MODE_COUNT <= ( 1 << CLIENT_ALPHA_DISTANCE_FADE_MODE_BIT_COUNT ) );
  106. m_nDistanceFadeMode = nFadeMode;
  107. }
  108. // Sets fade parameters
  109. void CClientAlphaProperty::SetFade( float flGlobalFadeScale, float flDistFadeStart, float flDistFadeEnd )
  110. {
  111. if( flDistFadeStart > flDistFadeEnd )
  112. {
  113. V_swap( flDistFadeStart, flDistFadeEnd );
  114. }
  115. // If a negative value is provided for the min fade distance, then base it off the max.
  116. if( flDistFadeStart < 0 )
  117. {
  118. flDistFadeStart = flDistFadeEnd + flDistFadeStart;
  119. if( flDistFadeStart < 0 )
  120. {
  121. flDistFadeStart = 0;
  122. }
  123. }
  124. Assert( flDistFadeStart >= 0 && flDistFadeStart <= 65535 );
  125. Assert( flDistFadeEnd >= 0 && flDistFadeEnd <= 65535 );
  126. m_nDistFadeStart = (uint16)flDistFadeStart;
  127. m_nDistFadeEnd = (uint16)flDistFadeEnd;
  128. m_flFadeScale = flGlobalFadeScale;
  129. }
  130. int CClientAlphaProperty::ComputeRenderEffectBlend( int nRenderEffect ) const
  131. {
  132. int nBlend = 0;
  133. float flOffset = ((int)m_nDesyncOffset) * 363.0;// Use ent index to de-sync these fx
  134. switch( nRenderEffect )
  135. {
  136. case kRenderFxPulseSlowWide:
  137. nBlend = m_nAlpha + 0x40 * sin( gpGlobals->curtime * 2 + flOffset );
  138. break;
  139. case kRenderFxPulseFastWide:
  140. nBlend = m_nAlpha + 0x40 * sin( gpGlobals->curtime * 8 + flOffset );
  141. break;
  142. case kRenderFxPulseFastWider:
  143. nBlend = ( 0xff * fabs(sin( gpGlobals->curtime * 12 + flOffset ) ) );
  144. break;
  145. case kRenderFxPulseSlow:
  146. nBlend = m_nAlpha + 0x10 * sin( gpGlobals->curtime * 2 + flOffset );
  147. break;
  148. case kRenderFxPulseFast:
  149. nBlend = m_nAlpha + 0x10 * sin( gpGlobals->curtime * 8 + flOffset );
  150. break;
  151. case kRenderFxFadeOut:
  152. case kRenderFxFadeFast:
  153. case kRenderFxFadeSlow:
  154. {
  155. float flElapsed = gpGlobals->curtime - m_flRenderFxStartTime;
  156. float flVal = RemapValClamped( flElapsed, 0, m_flRenderFxDuration, m_nAlpha, 0 );
  157. flVal = clamp( flVal, 0, 255 );
  158. nBlend = (int)flVal;
  159. }
  160. break;
  161. case kRenderFxFadeIn:
  162. case kRenderFxSolidFast:
  163. case kRenderFxSolidSlow:
  164. {
  165. float flElapsed = gpGlobals->curtime - m_flRenderFxStartTime;
  166. float flVal = RemapValClamped( flElapsed, 0, m_flRenderFxDuration, 0, m_nAlpha );
  167. flVal = clamp( flVal, 0, 255 );
  168. nBlend = (int)flVal;
  169. }
  170. break;
  171. case kRenderFxStrobeSlow:
  172. nBlend = 20 * sin( gpGlobals->curtime * 4 + flOffset );
  173. nBlend = ( nBlend < 0 ) ? 0 : m_nAlpha;
  174. break;
  175. case kRenderFxStrobeFast:
  176. nBlend = 20 * sin( gpGlobals->curtime * 16 + flOffset );
  177. nBlend = ( nBlend < 0 ) ? 0 : m_nAlpha;
  178. break;
  179. case kRenderFxStrobeFaster:
  180. nBlend = 20 * sin( gpGlobals->curtime * 36 + flOffset );
  181. nBlend = ( nBlend < 0 ) ? 0 : m_nAlpha;
  182. break;
  183. case kRenderFxFlickerSlow:
  184. nBlend = 20 * (sin( gpGlobals->curtime * 2 ) + sin( gpGlobals->curtime * 17 + flOffset ));
  185. nBlend = ( nBlend < 0 ) ? 0 : m_nAlpha;
  186. break;
  187. case kRenderFxFlickerFast:
  188. nBlend = 20 * (sin( gpGlobals->curtime * 16 ) + sin( gpGlobals->curtime * 23 + flOffset ));
  189. nBlend = ( nBlend < 0 ) ? 0 : m_nAlpha;
  190. break;
  191. case kRenderFxNone:
  192. default:
  193. nBlend = ( m_nRenderMode == kRenderNormal ) ? 255 : m_nAlpha;
  194. break;
  195. }
  196. return nBlend;
  197. }
  198. //-----------------------------------------------------------------------------
  199. // Computes alpha value based on render fx
  200. //-----------------------------------------------------------------------------
  201. uint8 CClientAlphaProperty::ComputeRenderAlpha( ) const
  202. {
  203. if ( m_nRenderMode == kRenderNone || m_nRenderMode == kRenderEnvironmental )
  204. return 0;
  205. int nBlend = 0;
  206. if ( m_nRenderFX > kRenderNone && m_nRenderFX < kRenderFxMax )
  207. {
  208. nBlend = ComputeRenderEffectBlend( m_nRenderFX );
  209. }
  210. else
  211. {
  212. nBlend = ( m_nRenderMode == kRenderNormal ) ? 255 : m_nAlpha;
  213. }
  214. if ( m_bAlphaOverride )
  215. {
  216. nBlend = m_pOuter->GetClientRenderable()->OverrideAlphaModulation( m_nAlpha );
  217. }
  218. nBlend = clamp( nBlend, 0, 255 );
  219. return nBlend;
  220. }