ztime.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef _time_h_
  2. #define _time_h_
  3. /////////////////////////////////////////////////////////////////////////////
  4. // ztime.h : Declaration and implementation of the Time class.
  5. //
  6. #include "TlsValue.h"
  7. /////////////////////////////////////////////////////////////////////////////
  8. //
  9. // Time
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. class Time {
  13. private:
  14. DWORD m_dwTime;
  15. friend class DTime;
  16. static tlsDWORD s_dwPauseStart;
  17. static tlsDWORD s_dwNegativeOffset;
  18. #ifdef _DEBUG_TRAINING
  19. static tlsDWORD s_dwLastTime;
  20. static tlsDWORD s_dwAccumulatedTime;
  21. static tlsINT s_iShift;
  22. static tlsDWORD s_dwLastClockTime;
  23. #endif
  24. public:
  25. static float fResolution (void) {return 1.0e3f;}
  26. static void Pause (void);
  27. static bool IsPaused (void);
  28. static void Continue (void);
  29. #ifdef _DEBUG_TRAINING
  30. static int GetShift (void);
  31. static void SetShift (int iShift);
  32. #endif
  33. static inline Time Now(void)
  34. {
  35. #ifdef _DEBUG_TRAINING
  36. // compute the amount of time elapsed since the last frame
  37. DWORD dwRealTime = timeGetTime ();
  38. assert (dwRealTime >= s_dwLastTime);
  39. DWORD dwDeltaTime = dwRealTime - s_dwLastTime;
  40. s_dwLastTime = dwRealTime;
  41. // compute a maximum allowable frame time
  42. DWORD dwMaxDeltaTime = static_cast<DWORD> (fResolution () * 0.25f); // 4 FPS
  43. dwDeltaTime = (dwDeltaTime > dwMaxDeltaTime) ? dwMaxDeltaTime : dwDeltaTime;
  44. // scale the elapsed time using the shift factor, and
  45. // accumulate it into the game clock
  46. DWORD dwShiftedTime = (s_iShift >= 0) ? (dwDeltaTime << s_iShift) : (dwDeltaTime >> -s_iShift);
  47. s_dwAccumulatedTime += dwShiftedTime;
  48. // compute the current time, accounting for whether or
  49. // not the clock is paused, and whether or not it has
  50. // been paused in the past.
  51. DWORD dwCurrentClockTime = ((s_dwPauseStart != 0) ? s_dwPauseStart : s_dwAccumulatedTime) - s_dwNegativeOffset;
  52. Time now (dwCurrentClockTime);
  53. // check that time is strictly increasing
  54. if (s_dwLastClockTime != 0)
  55. assert (dwCurrentClockTime >= s_dwLastClockTime);
  56. s_dwLastClockTime = dwCurrentClockTime;
  57. #else
  58. DWORD dwCurrentClockTime = ((s_dwPauseStart != 0) ? s_dwPauseStart : timeGetTime()) - s_dwNegativeOffset;
  59. Time now (dwCurrentClockTime);
  60. #endif
  61. return now;
  62. }
  63. inline Time(void)
  64. {
  65. }
  66. inline Time(DWORD dwTime) :
  67. m_dwTime(dwTime)
  68. {
  69. }
  70. inline Time(const Time& t) :
  71. m_dwTime(t.m_dwTime)
  72. {
  73. }
  74. inline Time Before(void) const
  75. {
  76. return Time(m_dwTime - 1);
  77. }
  78. inline Time After(void) const
  79. {
  80. return Time(m_dwTime + 0x7fffffff);
  81. }
  82. inline bool operator > (const Time t) const
  83. {
  84. return ((int)(m_dwTime - t.m_dwTime) > 0);
  85. }
  86. inline bool operator >= (const Time t) const
  87. {
  88. return ((int)(m_dwTime - t.m_dwTime) >= 0);
  89. }
  90. inline bool operator < (const Time t) const
  91. {
  92. return ((int)(m_dwTime - t.m_dwTime) < 0);
  93. }
  94. inline bool operator <= (const Time t) const
  95. {
  96. return ((int)(m_dwTime - t.m_dwTime) <= 0);
  97. }
  98. inline bool operator == (const Time t) const
  99. {
  100. return m_dwTime == t.m_dwTime;
  101. }
  102. inline bool operator != (const Time t) const
  103. {
  104. return m_dwTime != t.m_dwTime;
  105. }
  106. inline Time operator = (DWORD tick)
  107. {
  108. this->m_dwTime = tick;
  109. return *this;
  110. }
  111. inline Time operator + (float d) const
  112. {
  113. Time t(m_dwTime + (int)floor(d * fResolution () + 0.5));
  114. return t;
  115. }
  116. inline Time operator += (float d)
  117. {
  118. m_dwTime += (int)floor((d * fResolution ()) + 0.5);
  119. return *this;
  120. }
  121. inline Time operator - (float d) const
  122. {
  123. Time t(m_dwTime - (int)floor((d * fResolution ()) + 0.5));
  124. return t;
  125. }
  126. inline Time operator -= (float d)
  127. {
  128. m_dwTime -= (int)floor((d * fResolution ()) + 0.5);
  129. return *this;
  130. }
  131. inline float operator - (const Time t) const
  132. {
  133. return ((float)((int)(m_dwTime - t.m_dwTime))) / fResolution ();
  134. }
  135. inline DWORD clock(void) const
  136. {
  137. return m_dwTime;
  138. }
  139. inline void clock(DWORD c)
  140. {
  141. m_dwTime = c;
  142. }
  143. };
  144. #endif