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.

95 lines
2.9 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // A class representing a procedural texture
  4. //
  5. //=============================================================================
  6. #include "movieobjects/dmecycle.h"
  7. #include "datamodel/dmelementfactoryhelper.h"
  8. #include "tier0/dbg.h"
  9. #include "mathlib/mathlib.h"
  10. #include <math.h>
  11. //-----------------------------------------------------------------------------
  12. // Consts
  13. //-----------------------------------------------------------------------------
  14. #define INT_FLOAT_SCALE 0.000001f
  15. //#define USE_NEW_WAY
  16. //-----------------------------------------------------------------------------
  17. // Expose this class to the scene database
  18. //-----------------------------------------------------------------------------
  19. IMPLEMENT_ELEMENT_FACTORY( DmeCycle, CDmeCycle );
  20. //-----------------------------------------------------------------------------
  21. // Implementation
  22. //-----------------------------------------------------------------------------
  23. void CDmeCycle::OnConstruction()
  24. {
  25. m_cycleRate.Init( this, "cycleRate" );
  26. m_prevCycle.Init( this, "prevCycle" );
  27. m_lastCycleResetTime.Init( this, "lastCycleResetTime" );
  28. m_lastCycleResetValue.Init( this, "lastCycleResetValue" );
  29. }
  30. void CDmeCycle::OnDestruction()
  31. {
  32. }
  33. void CDmeCycle::SetCycleRate( float flCycleRate )
  34. {
  35. m_cycleRate = flCycleRate;
  36. }
  37. void CDmeCycle::SetPrevCycle( float flPrevCycle )
  38. {
  39. m_prevCycle = (int)(flPrevCycle / INT_FLOAT_SCALE);
  40. }
  41. void CDmeCycle::SetCycle( float flCycle, float flCurTime )
  42. {
  43. #ifdef USE_NEW_WAY
  44. float const flCycleDelta = fabs( flCycle - GetPrevCycle() );
  45. // NOTE: Overlays depend on this logic - record if cycle==0. There may be a better way to
  46. // do this, but if an overlay is at 0 while it's dormant (ie its weight==0), we need to
  47. // record at the moment its weight>0.
  48. bool bForceCycleRecord = flCycle == 0.0f;
  49. if ( bForceCycleRecord || flCycleDelta >= 0.1f )
  50. {
  51. // Store this time
  52. m_lastCycleResetTime = (int)(flCurTime / INT_FLOAT_SCALE);
  53. // For this method, m_cycle is only recorded when a reset of some kind has occurred
  54. m_lastCycleResetValue = (int)(flCycle / INT_FLOAT_SCALE);
  55. DevMsg(" resetting cycle at: time=%f cycle=%f\n", flCurTime, flCycle );
  56. }
  57. #else
  58. m_lastCycleResetValue = (int)(flCycle / INT_FLOAT_SCALE);
  59. #endif
  60. // Store as previous cycle
  61. SetPrevCycle( flCycle );
  62. }
  63. float CDmeCycle::GetCycleRate() const
  64. {
  65. return m_cycleRate;
  66. }
  67. float CDmeCycle::GetPrevCycle() const
  68. {
  69. return m_prevCycle * INT_FLOAT_SCALE;
  70. }
  71. float CDmeCycle::GetCycle( float flCurTime ) const
  72. {
  73. #ifdef USE_NEW_WAY
  74. float const dt = flCurTime - INT_FLOAT_SCALE * m_lastCycleResetTime;
  75. float const flCycle = INT_FLOAT_SCALE * m_lastCycleResetValue + GetCycleRate() * dt; Assert( flCycle >= 0.0f && flCycle <= 2.0f );
  76. return fmod( flCycle, 1.0f ); // In case it's 1.0f
  77. #else
  78. return INT_FLOAT_SCALE * m_lastCycleResetValue;
  79. #endif
  80. }