TIMER.CPP 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***************************************************************************
  19. ** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
  20. ***************************************************************************
  21. * *
  22. * Project Name : Temp timer for 32bit lib *
  23. * *
  24. * File Name : TIMER.CPP *
  25. * *
  26. * Programmer : Scott K. Bowen *
  27. * *
  28. * Start Date : July 6, 1994 *
  29. * *
  30. * Last Update : May 3, 1995 [SKB] *
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * TC::Time -- Return the time on the timer. *
  35. * TC::TimerClass -- Construct a timer class object. *
  36. * TC::Stop -- Stop the timer. *
  37. * TC::Start -- Start a timer. *
  38. * TC::Set -- Set the time of a timer. *
  39. * TC::Reset -- Clear the timer. *
  40. * TimerClass::Time -- Get the current time of timer. *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include <wwstd.h>
  43. #include "timer.H"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. /////////////////////////////////////////////////////////////////////////////////
  47. /////////////////////////////////// Code ////////////////////////////////////////
  48. /***************************************************************************
  49. * TC::TIMERCLASS -- Construct a timer class object. *
  50. * *
  51. * *
  52. * INPUT: *
  53. * *
  54. * OUTPUT: *
  55. * *
  56. * WARNINGS: *
  57. * *
  58. * HISTORY: *
  59. * 07/12/1994 SKB : Created. *
  60. *=========================================================================*/
  61. TimerClass::TimerClass(BaseTimerEnum timer, BOOL on)
  62. {
  63. Accumulated = 0;
  64. Started = 0;
  65. TickType=timer;
  66. if (on && TimerSystemOn) Start();
  67. }
  68. /***********************************************************************************************
  69. * TC:Get_Ticks -- return the number of ticks on the system or user timers *
  70. * *
  71. * *
  72. * *
  73. * INPUT: Nothing *
  74. * *
  75. * OUTPUT: tick count *
  76. * *
  77. * WARNINGS: None *
  78. * *
  79. * HISTORY: *
  80. * 10/5/95 4:17PM ST : Created *
  81. *=============================================================================================*/
  82. long TimerClass::Get_Ticks ( void )
  83. {
  84. if ( WindowsTimer ){
  85. switch ( TickType ){
  86. case BT_SYSTEM :
  87. return ( WindowsTimer->Get_System_Tick_Count() );
  88. case BT_USER :
  89. return ( WindowsTimer->Get_User_Tick_Count() );
  90. }
  91. }
  92. return 0;
  93. }
  94. /***************************************************************************
  95. * TIMERCLASS::TIME -- Get the current time of timer. *
  96. * *
  97. * *
  98. * *
  99. * INPUT: *
  100. * *
  101. * OUTPUT: *
  102. * *
  103. * WARNINGS: *
  104. * *
  105. * HISTORY: *
  106. * 05/03/1995 SKB : Created. *
  107. *=========================================================================*/
  108. long TimerClass::Time(void)
  109. {
  110. if (Started) {
  111. long ticks = Get_Ticks();
  112. Accumulated += ticks - (Started-1);
  113. Started = ticks+1;
  114. }
  115. return(Accumulated);
  116. }
  117. /***************************************************************************
  118. * TC::STOP -- Stop the timer. *
  119. * *
  120. * *
  121. * *
  122. * INPUT: *
  123. * *
  124. * OUTPUT: *
  125. * *
  126. * WARNINGS: *
  127. * *
  128. * HISTORY: *
  129. * 07/12/1994 SKB : Created. *
  130. *=========================================================================*/
  131. long TimerClass::Stop(void)
  132. {
  133. long time = Time();
  134. Started = 0;
  135. return(time);
  136. }
  137. /***************************************************************************
  138. * TC::START -- Start a timer. *
  139. * *
  140. * *
  141. * INPUT: *
  142. * *
  143. * OUTPUT: *
  144. * *
  145. * WARNINGS: *
  146. * *
  147. * HISTORY: *
  148. * 07/12/1994 SKB : Created. *
  149. *=========================================================================*/
  150. long TimerClass::Start(void)
  151. {
  152. if (!Started) {
  153. Started = Get_Ticks()+1;
  154. }
  155. return(Time());
  156. }
  157. /***************************************************************************
  158. * TC::SET -- Set the time of a timer. *
  159. * *
  160. * *
  161. * *
  162. * INPUT: long value to set timer at. *
  163. * *
  164. * OUTPUT: *
  165. * *
  166. * WARNINGS: *
  167. * *
  168. * HISTORY: *
  169. * 07/12/1994 SKB : Created. *
  170. * 05/03/1995 SKB : If start return Start since it returns Time *
  171. *=========================================================================*/
  172. long TimerClass::Set(long value, BOOL start)
  173. {
  174. Started = 0;
  175. Accumulated = value;
  176. if (start) return (Start());
  177. return(Time());
  178. }