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.

173 lines
6.3 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef TIMEUTILS_H
  7. #define TIMEUTILS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include <limits.h>
  12. #include <math.h>
  13. #include "tier0/dbg.h"
  14. enum RoundStyle_t
  15. {
  16. ROUND_NEAREST,
  17. ROUND_DOWN,
  18. ROUND_UP,
  19. };
  20. class DmeTime_t;
  21. class DmeFramerate_t
  22. {
  23. public:
  24. DmeFramerate_t( float fps );
  25. DmeFramerate_t( int fps = 0 );
  26. DmeFramerate_t( const DmeFramerate_t& src ) : m_num( src.m_num ), m_den( src.m_den ) {}
  27. void SetFramerate( float flFrameRate );
  28. void SetFramerate( int fps );
  29. // other (uncommon) options besides 30(29.97 - ntsc video) are 24 (23.976 - ntsc film) and 60 (59.94 - ntsc progressive)
  30. void SetFramerateNTSC( int multiplier = 30 );
  31. float GetFramesPerSecond() const;
  32. DmeTime_t GetTimePerFrame() const;
  33. bool operator==( DmeFramerate_t f ) const { return m_num == f.m_num && m_den == f.m_den; }
  34. bool operator!=( DmeFramerate_t f ) const { return m_num != f.m_num && m_den != f.m_den; }
  35. bool operator< ( DmeFramerate_t f ) const { return m_num * ( int )f.m_den < f.m_num * ( int )m_den; }
  36. bool operator> ( DmeFramerate_t f ) const { return m_num * ( int )f.m_den > f.m_num * ( int )m_den; }
  37. bool operator<=( DmeFramerate_t f ) const { return m_num * ( int )f.m_den <= f.m_num * ( int )m_den; }
  38. bool operator>=( DmeFramerate_t f ) const { return m_num * ( int )f.m_den >= f.m_num * ( int )m_den; }
  39. DmeFramerate_t operator*( int i ) const { return DmeFramerate_t( m_num * i, m_den ); }
  40. DmeFramerate_t operator/( int i ) const { return DmeFramerate_t( m_num, m_den * i ); }
  41. DmeFramerate_t operator*=( int i ) { Assert( abs( m_num * i ) <= USHRT_MAX ); m_num *= ( unsigned short )i; return *this; }
  42. DmeFramerate_t operator/=( int i ) { Assert( abs( m_den * i ) <= USHRT_MAX ); m_den *= ( unsigned short )i; return *this; }
  43. private:
  44. DmeFramerate_t( int nNumerator, int nDenominator );
  45. unsigned short m_num;
  46. unsigned short m_den;
  47. friend class DmeTime_t;
  48. };
  49. #define DMETIME_ZERO DmeTime_t(0)
  50. #define DMETIME_MINDELTA DmeTime_t::MinTimeDelta()
  51. #define DMETIME_MINTIME DmeTime_t::MinTime()
  52. #define DMETIME_MAXTIME DmeTime_t::MaxTime()
  53. #define DMETIME_INVALID DmeTime_t::InvalidTime()
  54. class DmeTime_t
  55. {
  56. public:
  57. DmeTime_t() : m_tms( INT_MIN ) {} // invalid time
  58. explicit DmeTime_t( int tms ) : m_tms( tms ) {}
  59. explicit DmeTime_t( float sec ) : m_tms( RoundSecondsToTMS( sec ) ) {}
  60. explicit DmeTime_t( double sec ) : m_tms( RoundSecondsToTMS( sec ) ) {}
  61. DmeTime_t( int frame, DmeFramerate_t framerate );
  62. // time operators
  63. friend bool operator==( DmeTime_t a, DmeTime_t b ) { return a.m_tms == b.m_tms; }
  64. friend bool operator!=( DmeTime_t a, DmeTime_t b ) { return a.m_tms != b.m_tms; }
  65. friend bool operator< ( DmeTime_t a, DmeTime_t b ) { return a.m_tms < b.m_tms; }
  66. friend bool operator> ( DmeTime_t a, DmeTime_t b ) { return a.m_tms > b.m_tms; }
  67. friend bool operator<=( DmeTime_t a, DmeTime_t b ) { return a.m_tms <= b.m_tms; }
  68. friend bool operator>=( DmeTime_t a, DmeTime_t b ) { return a.m_tms >= b.m_tms; }
  69. friend DmeTime_t operator%( DmeTime_t a, DmeTime_t b ) { return DmeTime_t( a.m_tms % b.m_tms ); }
  70. friend DmeTime_t operator+( DmeTime_t a, DmeTime_t b ) { return DmeTime_t( a.m_tms + b.m_tms ); }
  71. friend DmeTime_t operator-( DmeTime_t a, DmeTime_t b ) { return DmeTime_t( a.m_tms - b.m_tms ); }
  72. DmeTime_t operator-() const { return DmeTime_t( -m_tms ); }
  73. DmeTime_t operator+=( DmeTime_t t ) { m_tms += t.m_tms; return *this; }
  74. DmeTime_t operator-=( DmeTime_t t ) { m_tms -= t.m_tms; return *this; }
  75. // float operators - comment these out to find potentially incorrect uses of DmeTime_t
  76. friend DmeTime_t operator*( DmeTime_t t, float f ) { t *= f; return t; }
  77. friend DmeTime_t operator*( float f, DmeTime_t t ) { t *= f; return t; }
  78. friend DmeTime_t operator/( DmeTime_t t, float f ) { t /= f; return t; }
  79. friend float operator/( DmeTime_t n, DmeTime_t d ) { return float( n.m_tms / double( d.m_tms ) ); }
  80. DmeTime_t operator*=( float f );
  81. DmeTime_t operator/=( float f );
  82. DmeTime_t operator++() { ++m_tms; return *this; }
  83. DmeTime_t operator--() { --m_tms; return *this; }
  84. DmeTime_t operator++( int ) { DmeTime_t t = *this; ++m_tms; return t; } // postfix version..
  85. DmeTime_t operator--( int ) { DmeTime_t t = *this; --m_tms; return t; } // postfix version..
  86. // limits, special values and validity
  87. bool IsValid() const { return m_tms != INT_MIN; }
  88. static DmeTime_t InvalidTime() { return DmeTime_t( INT_MIN ); }
  89. static DmeTime_t MinTime() { return DmeTime_t( INT_MIN + 1 ); }
  90. static DmeTime_t MaxTime() { return DmeTime_t( INT_MAX ); }
  91. static DmeTime_t MinTimeDelta() { return DmeTime_t( 1 ); }
  92. // conversions between other time representations
  93. int GetTenthsOfMS() const { return m_tms; }
  94. float GetSeconds() const { return m_tms * 0.0001f; }
  95. void SetTenthsOfMS( int tms ) { m_tms = tms; }
  96. void SetSeconds( float sec ) { m_tms = RoundSecondsToTMS( sec ); }
  97. // helper methods
  98. void Clamp( DmeTime_t lo, DmeTime_t hi );
  99. bool IsInRange( DmeTime_t lo, DmeTime_t hi ) const;
  100. // helper functions
  101. friend float GetFractionOfTimeBetween( DmeTime_t t, DmeTime_t start, DmeTime_t end, bool bClamp );
  102. friend float GetFractionOfTime( DmeTime_t t, DmeTime_t duration, bool bClamp );
  103. friend int FrameForTime( DmeTime_t t, DmeFramerate_t framerate );
  104. // standard arithmetic methods
  105. friend DmeTime_t abs( DmeTime_t t ) { return t.m_tms >= 0 ? t : -t; }
  106. // framerate-dependent conversions to/from frames
  107. int CurrentFrame( DmeFramerate_t framerate, RoundStyle_t roundStyle = ROUND_DOWN ) const;
  108. DmeTime_t TimeAtCurrentFrame( DmeFramerate_t framerate, RoundStyle_t roundStyle = ROUND_DOWN ) const;
  109. DmeTime_t TimeAtNextFrame( DmeFramerate_t framerate ) const;
  110. DmeTime_t TimeAtPrevFrame( DmeFramerate_t framerate ) const;
  111. private:
  112. explicit DmeTime_t( int64 tms ) : m_tms( int( tms ) ) {}
  113. // conversion helper methods - implementation
  114. static int RoundSecondsToTMS( float sec );
  115. static int RoundSecondsToTMS( double sec );
  116. int m_tms;
  117. };
  118. float GetFractionOfTimeBetween( DmeTime_t t, DmeTime_t start, DmeTime_t end, bool bClamp = false );
  119. float GetFractionOfTime( DmeTime_t t, DmeTime_t duration, bool bClamp = false );
  120. class CUtlBuffer;
  121. bool Serialize( CUtlBuffer &buf, const DmeTime_t &src );
  122. bool Unserialize( CUtlBuffer &buf, DmeTime_t &dest );
  123. #endif // TIMEUTILS_H