Team Fortress 2 Source Code as on 22/4/2020
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.

158 lines
5.6 KiB

  1. //========= Copyright 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 "platform.h"
  14. #include "tier0/dbg.h"
  15. class DmeTime_t;
  16. #define DMETIME_TO_SECONDS( t ) ((t) * 0.0001f)
  17. class DmeFramerate_t
  18. {
  19. public:
  20. DmeFramerate_t( float fps );
  21. DmeFramerate_t( int fps = 0 );
  22. DmeFramerate_t( const DmeFramerate_t& src ) : m_num( src.m_num ), m_den( src.m_den ) {}
  23. void SetFramerate( float flFrameRate );
  24. void SetFramerate( int fps );
  25. // other (uncommon) options besides 30(29.97 - ntsc video) are 24 (23.976 - ntsc film) and 60 (59.94 - ntsc progressive)
  26. void SetFramerateNTSC( int multiplier = 30 );
  27. float GetFramesPerSecond() const;
  28. bool operator==( DmeFramerate_t f ) const { return m_num == f.m_num && m_den == f.m_den; }
  29. bool operator!=( DmeFramerate_t f ) const { return m_num != f.m_num && m_den != f.m_den; }
  30. bool operator< ( DmeFramerate_t f ) const { return m_num * ( int )f.m_den < f.m_num * ( int )m_den; }
  31. bool operator> ( DmeFramerate_t f ) const { return m_num * ( int )f.m_den > f.m_num * ( int )m_den; }
  32. bool operator<=( DmeFramerate_t f ) const { return m_num * ( int )f.m_den <= f.m_num * ( int )m_den; }
  33. bool operator>=( DmeFramerate_t f ) const { return m_num * ( int )f.m_den >= f.m_num * ( int )m_den; }
  34. DmeFramerate_t operator*( int i ) const { return DmeFramerate_t( m_num * i, m_den ); }
  35. DmeFramerate_t operator/( int i ) const { return DmeFramerate_t( m_num, m_den * i ); }
  36. unsigned short abs( unsigned short i ) { return i >= 0 ? i : -i; }
  37. DmeFramerate_t operator*=( int i ) { Assert( abs( m_num * i ) <= USHRT_MAX ); m_num *= ( unsigned short )i; return *this; }
  38. DmeFramerate_t operator/=( int i ) { Assert( abs( m_den * i ) <= USHRT_MAX ); m_den *= ( unsigned short )i; return *this; }
  39. private:
  40. DmeFramerate_t( int nNumerator, int nDenominator );
  41. unsigned short m_num;
  42. unsigned short m_den;
  43. friend class DmeTime_t;
  44. };
  45. #define DMETIME_ZERO DmeTime_t(0)
  46. #define DMETIME_MINDELTA DmeTime_t::MinTimeDelta()
  47. #define DMETIME_MINTIME DmeTime_t::MinTime()
  48. #define DMETIME_MAXTIME DmeTime_t::MaxTime()
  49. #define DMETIME_INVALID DmeTime_t::InvalidTime()
  50. class DmeTime_t
  51. {
  52. public:
  53. DmeTime_t() : m_tms( INT_MIN ) {} // invalid time
  54. explicit DmeTime_t( int tms ) : m_tms( tms ) {}
  55. explicit DmeTime_t( float sec ) : m_tms( RoundSecondsToTMS( sec ) ) {}
  56. explicit DmeTime_t( double sec ) : m_tms( RoundSecondsToTMS( sec ) ) {}
  57. DmeTime_t( int frame, DmeFramerate_t framerate );
  58. // time operators
  59. bool operator==( DmeTime_t t ) const { return m_tms == t.m_tms; }
  60. bool operator!=( DmeTime_t t ) const { return m_tms != t.m_tms; }
  61. bool operator< ( DmeTime_t t ) const { return m_tms < t.m_tms; }
  62. bool operator> ( DmeTime_t t ) const { return m_tms > t.m_tms; }
  63. bool operator<=( DmeTime_t t ) const { return m_tms <= t.m_tms; }
  64. bool operator>=( DmeTime_t t ) const { return m_tms >= t.m_tms; }
  65. DmeTime_t operator%( DmeTime_t t ) const { return DmeTime_t( m_tms % t.m_tms ); }
  66. DmeTime_t operator+( DmeTime_t t ) const { return DmeTime_t( m_tms + t.m_tms ); }
  67. DmeTime_t operator-( DmeTime_t t ) const { return DmeTime_t( m_tms - t.m_tms ); }
  68. DmeTime_t operator-() const { return DmeTime_t( -m_tms ); }
  69. DmeTime_t operator+=( DmeTime_t t ) { m_tms += t.m_tms; return *this; }
  70. DmeTime_t operator-=( DmeTime_t t ) { m_tms -= t.m_tms; return *this; }
  71. // float operators - comment these out to find potentially incorrect uses of DmeTime_t
  72. friend DmeTime_t operator*( DmeTime_t t, float f ) { t *= f; return t; }
  73. friend DmeTime_t operator*( float f, DmeTime_t t ) { t *= f; return t; }
  74. friend DmeTime_t operator/( DmeTime_t t, float f ) { t /= f; return t; }
  75. friend float operator/( DmeTime_t n, DmeTime_t d ) { return float( n.m_tms / double( d.m_tms ) ); }
  76. DmeTime_t operator*=( float f );
  77. DmeTime_t operator/=( float f );
  78. // limits, special values and validity
  79. bool IsValid() const { return m_tms != INT_MIN; }
  80. static DmeTime_t InvalidTime() { return DmeTime_t( INT_MIN ); }
  81. static DmeTime_t MinTime() { return DmeTime_t( INT_MIN + 1 ); }
  82. static DmeTime_t MaxTime() { return DmeTime_t( INT_MAX ); }
  83. static DmeTime_t MinTimeDelta() { return DmeTime_t( 1 ); }
  84. // conversions between other time representations
  85. int GetTenthsOfMS() const { return m_tms; }
  86. float GetSeconds() const { return DMETIME_TO_SECONDS( m_tms ); }
  87. void SetTenthsOfMS( int tms ) { m_tms = tms; }
  88. void SetSeconds( float sec ) { m_tms = RoundSecondsToTMS( sec ); }
  89. // helper methods
  90. void Clamp( DmeTime_t lo, DmeTime_t hi );
  91. bool IsInRange( DmeTime_t lo, DmeTime_t hi ) const;
  92. // helper functions
  93. friend float GetFractionOfTime( DmeTime_t t, DmeTime_t duration, bool bClamp );
  94. friend int FrameForTime( DmeTime_t t, DmeFramerate_t framerate );
  95. // standard arithmetic methods
  96. friend DmeTime_t abs( DmeTime_t t ) { return t.m_tms >= 0 ? t : -t; }
  97. // framerate-dependent conversions to/from frames
  98. int CurrentFrame( DmeFramerate_t framerate, bool bRoundDown = true ) const;
  99. DmeTime_t TimeAtCurrentFrame( DmeFramerate_t framerate, bool bRoundDown = true ) const;
  100. DmeTime_t TimeAtNextFrame( DmeFramerate_t framerate ) const;
  101. DmeTime_t TimeAtPrevFrame( DmeFramerate_t framerate ) const;
  102. private:
  103. DmeTime_t( int64 tms ) : m_tms( int( tms ) ) {}
  104. // conversion helper methods - implementation
  105. static int RoundSecondsToTMS( float sec );
  106. static int RoundSecondsToTMS( double sec );
  107. int m_tms;
  108. };
  109. float GetFractionOfTimeBetween( DmeTime_t t, DmeTime_t start, DmeTime_t end, bool bClamp = false );
  110. #endif // TIMEUTILS_H