yatime.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // yatime.h
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 01/22/97 MJR Started.
  23. //
  24. // 04/18/97 JMI Added Suspend() and Resume().
  25. // I wasn't sure how to handle when Update() was passed a
  26. // forced elapsed time while the time was suspended. The two
  27. // options are:
  28. // 1) Ignore the forced time and keep suspended anyway.
  29. // 2) Force the time to advance even though we're suspended.
  30. // I chose 1.
  31. //
  32. // 05/08/97 JMI Set MaxElapsedRealTime to 200 (was 1250).
  33. //
  34. // 06/05/97 JMI Changed MaxElapsedRealTime to 67 (15 FPS) (was 200).
  35. //
  36. // 06/09/97 JMI Put MaxElapsedRealTime back to 200.
  37. //
  38. // 06/26/97 MJR MaxElapsedRealTime only applies to real time (its name was
  39. // changed to reflect this). When a forced elapsed time is
  40. // being used, that forced time is not limited in any way.
  41. //
  42. // 07/14/97 MJR Renamed module to yatime.cpp/.h to avoid conflicts with
  43. // <time.h>, which are only conflicts because the VC++
  44. // compiler doesn't properly differentiate between #include's
  45. // using <> and "".
  46. //
  47. ////////////////////////////////////////////////////////////////////////////////
  48. #ifndef YATIME_H
  49. #define YATIME_H
  50. #include "Blue.h"
  51. class CTime
  52. {
  53. //---------------------------------------------------------------------------
  54. // Types, enums, etc.
  55. //---------------------------------------------------------------------------
  56. public:
  57. enum
  58. {
  59. // This determine the maximum amount of elapsed real time that can occur
  60. // between calls to Update() before it will fudge elapsed time. The
  61. // primarary reason for this is to make debugging possible. Without it,
  62. // every time you stopped in the debugger, a huge amount of real time
  63. // would elapse, and when you resumed the game, everything that was
  64. // time-based would react to that huge passage of time, which would be
  65. // rediculous. Instead, if more than this time elapses, we pretend
  66. // just some fixed amount of time has elapsed.
  67. //
  68. // THIS ONLY APPLIES WHEN USING REAL TIME!
  69. MaxElapsedRealTime = 200,
  70. // If the MaxElapsedRealTime is exceeded, then this is the value that is
  71. // used to fudge the elapsed time. At some point we might want to
  72. // calculate and and then use the average elapsed time, but hardwiring
  73. // it seems to work pretty well.
  74. //
  75. // THIS ONLY APPLIES WHEN USING REAL TIME!
  76. DefaultElapsedRealTime = 100
  77. };
  78. //---------------------------------------------------------------------------
  79. // Variables
  80. //---------------------------------------------------------------------------
  81. protected:
  82. long m_lResetTime;
  83. long m_lLastTime;
  84. long m_lGameTime;
  85. long m_lForceInterval;
  86. short m_sNumSuspends; // Number of Suspend()s that have occurred w/o
  87. // corresponding Resume()s.
  88. //---------------------------------------------------------------------------
  89. // Functions
  90. //---------------------------------------------------------------------------
  91. public:
  92. ////////////////////////////////////////////////////////////////////////////////
  93. // Constructor
  94. ////////////////////////////////////////////////////////////////////////////////
  95. CTime(void)
  96. {
  97. Reset();
  98. }
  99. ////////////////////////////////////////////////////////////////////////////////
  100. // Destructor
  101. ////////////////////////////////////////////////////////////////////////////////
  102. ~CTime(void)
  103. {
  104. Reset();
  105. }
  106. ////////////////////////////////////////////////////////////////////////////////
  107. // Reset game time to 0.
  108. ////////////////////////////////////////////////////////////////////////////////
  109. void Reset(void)
  110. {
  111. m_lResetTime = rspGetMilliseconds();
  112. m_lLastTime = m_lResetTime;
  113. m_lGameTime = 0;
  114. m_lForceInterval = 0;
  115. m_sNumSuspends = 0;
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. // Suspend game time.
  119. ////////////////////////////////////////////////////////////////////////////////
  120. void Suspend(void)
  121. {
  122. m_sNumSuspends++;
  123. }
  124. ////////////////////////////////////////////////////////////////////////////////
  125. // Resume game time.
  126. ////////////////////////////////////////////////////////////////////////////////
  127. void Resume(void)
  128. {
  129. m_sNumSuspends--;
  130. }
  131. ////////////////////////////////////////////////////////////////////////////////
  132. // Update game time. This must be called once per game loop. If the real
  133. // elapsed time between calls to this function exceeds MaxElapsedRealTime, then the
  134. // game time will only be moved forward by DefaultElapsedRealTime.
  135. ////////////////////////////////////////////////////////////////////////////////
  136. void Update(
  137. long lForceElapsed = 0)
  138. {
  139. long lNewTime;
  140. if (lForceElapsed == 0)
  141. {
  142. // Get current time
  143. lNewTime = rspGetMilliseconds();
  144. }
  145. else
  146. {
  147. // Used specified elapsed time to create a "new" time
  148. lNewTime = m_lLastTime + lForceElapsed;
  149. }
  150. // If suspended . . .
  151. if (m_sNumSuspends > 0)
  152. {
  153. m_lLastTime = lNewTime;
  154. }
  155. // Calculate elapsed time since last update
  156. long lElapsedTime = lNewTime - m_lLastTime;
  157. // If we're using real time, we might need to limit the elapsed time
  158. if (lForceElapsed == 0)
  159. {
  160. // If elapsed time is too long, set it to the default elapsed time
  161. if (lElapsedTime > MaxElapsedRealTime)
  162. lElapsedTime = DefaultElapsedRealTime;
  163. }
  164. // Move game time forward by the elapsed time
  165. m_lGameTime += lElapsedTime;
  166. // Save current time for next update
  167. m_lLastTime = lNewTime;
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////
  170. // Get game time since last Reset(). Note that this is NOT compatible with
  171. // the time returned by rspGetMilliseconds()!
  172. ////////////////////////////////////////////////////////////////////////////////
  173. long GetGameTime(void)
  174. {
  175. // Return current game time
  176. return m_lGameTime;
  177. }
  178. ////////////////////////////////////////////////////////////////////////////////
  179. // Get real time since last Reset().
  180. ////////////////////////////////////////////////////////////////////////////////
  181. long GetRealTime(void)
  182. {
  183. // Return real elapsed time since last reset
  184. return rspGetMilliseconds() - m_lResetTime;
  185. }
  186. };
  187. #endif // YATIME_H
  188. ////////////////////////////////////////////////////////////////////////////////
  189. // EOF
  190. ////////////////////////////////////////////////////////////////////////////////