TIMER.CPP 10 KB

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