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.

129 lines
3.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "baseanimatedtextureproxy.h"
  9. #include "materialsystem/imaterial.h"
  10. #include "materialsystem/imaterialvar.h"
  11. #include "materialsystem/itexture.h"
  12. #include "tier1/keyvalues.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. //-----------------------------------------------------------------------------
  16. // Constructor, destructor:
  17. //-----------------------------------------------------------------------------
  18. CBaseAnimatedTextureProxy::CBaseAnimatedTextureProxy()
  19. {
  20. Cleanup();
  21. }
  22. CBaseAnimatedTextureProxy::~CBaseAnimatedTextureProxy()
  23. {
  24. Cleanup();
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Initialization, shutdown
  28. //-----------------------------------------------------------------------------
  29. bool CBaseAnimatedTextureProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  30. {
  31. char const* pAnimatedTextureVarName = pKeyValues->GetString( "animatedTextureVar" );
  32. if( !pAnimatedTextureVarName )
  33. return false;
  34. bool foundVar;
  35. m_AnimatedTextureVar = pMaterial->FindVar( pAnimatedTextureVarName, &foundVar, false );
  36. if( !foundVar )
  37. return false;
  38. char const* pAnimatedTextureFrameNumVarName = pKeyValues->GetString( "animatedTextureFrameNumVar" );
  39. if( !pAnimatedTextureFrameNumVarName )
  40. return false;
  41. m_AnimatedTextureFrameNumVar = pMaterial->FindVar( pAnimatedTextureFrameNumVarName, &foundVar, false );
  42. if( !foundVar )
  43. return false;
  44. m_FrameRate = pKeyValues->GetFloat( "animatedTextureFrameRate", 15 );
  45. m_WrapAnimation = !pKeyValues->GetInt( "animationNoWrap", 0 );
  46. return true;
  47. }
  48. void CBaseAnimatedTextureProxy::Cleanup()
  49. {
  50. m_AnimatedTextureVar = NULL;
  51. m_AnimatedTextureFrameNumVar = NULL;
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Does the dirty deed
  55. //-----------------------------------------------------------------------------
  56. void CBaseAnimatedTextureProxy::OnBind( void *pEntity )
  57. {
  58. Assert ( m_AnimatedTextureVar );
  59. if( m_AnimatedTextureVar->GetType() != MATERIAL_VAR_TYPE_TEXTURE )
  60. {
  61. return;
  62. }
  63. ITexture *pTexture;
  64. pTexture = m_AnimatedTextureVar->GetTextureValue();
  65. int numFrames = pTexture->GetNumAnimationFrames();
  66. if ( numFrames <= 0 )
  67. {
  68. Assert( !"0 frames in material calling animated texture proxy" );
  69. return;
  70. }
  71. // NOTE: Must not use relative time based methods here
  72. // because the bind proxy can be called many times per frame.
  73. // Prevent multiple Wrap callbacks to be sent for no wrap mode
  74. float startTime = GetAnimationStartTime(pEntity);
  75. float deltaTime = gpGlobals->curtime - startTime;
  76. float prevTime = deltaTime - gpGlobals->frametime;
  77. // Clamp..
  78. if (deltaTime < 0.0f)
  79. deltaTime = 0.0f;
  80. if (prevTime < 0.0f)
  81. prevTime = 0.0f;
  82. float frame = m_FrameRate * deltaTime;
  83. float prevFrame = m_FrameRate * prevTime;
  84. int intFrame = ((int)frame) % numFrames;
  85. int intPrevFrame = ((int)prevFrame) % numFrames;
  86. // Report wrap situation...
  87. if (intPrevFrame > intFrame)
  88. {
  89. if (m_WrapAnimation)
  90. {
  91. AnimationWrapped( pEntity );
  92. }
  93. else
  94. {
  95. // Only sent the wrapped message once.
  96. // when we're in non-wrapping mode
  97. if (prevFrame < numFrames)
  98. AnimationWrapped( pEntity );
  99. intFrame = numFrames - 1;
  100. }
  101. }
  102. m_AnimatedTextureFrameNumVar->SetIntValue( intFrame );
  103. }
  104. IMaterial *CBaseAnimatedTextureProxy::GetMaterial()
  105. {
  106. return m_AnimatedTextureVar->GetOwningMaterial();
  107. }