timer.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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 : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/timer.h $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 5/04/01 8:08p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * BasicTimerClass<T>::BasicTimerClass -- Constructor for basic timer class. *
  35. * BasicTimerClass<T>::operator () -- Function operator for timer object. *
  36. * BasicTimerClass<T>::operator long -- Conversion to long operator. *
  37. * BasicTimerClass<T>::~BasicTimerClass -- Destructor for basic timer object. *
  38. * TTimerClass<T>::Is_Active -- Checks to see if the timer is counting. *
  39. * TTimerClass<T>::Start -- Starts (resumes) a stopped timer. *
  40. * TTimerClass<T>::Stop -- Stops the current timer from incrementing. *
  41. * TTimerClass<T>::TTimerClass -- Constructor for timer class object. *
  42. * TTimerClass<T>::operator () -- Function operator for timer object. *
  43. * TTimerClass<T>::operator long -- Conversion operator for timer object. *
  44. * CDTimerClass<T>::CDTimerClass -- Constructor for count down timer. *
  45. * CDTimerClass<T>::Is_Active -- Checks to see if the timer object is active. *
  46. * CDTimerClass<T>::Start -- Starts (resumes) the count down timer. *
  47. * CDTimerClass<T>::Stop -- Stops (pauses) the count down timer. *
  48. * CDTimerClass<T>::operator () -- Function operator for the count down timer. *
  49. * CDTimerClass<T>::operator long -- Conversion to long operator function. *
  50. * CDTimerClass<T>::~CDTimerClass -- Destructor for the count down timer object. *
  51. * TTimerClass<T>::Value -- Returns with the current value of the timer. *
  52. * CDTimerClass<T>::Value -- Fetches the current value of the countdown timer. *
  53. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  54. #ifndef TIMER_H
  55. #define TIMER_H
  56. #include "noinit.h"
  57. /*
  58. ** This is a timer class that watches a constant rate timer (specified by the parameter
  59. ** type class) and provides basic timer functionality. It is possible to set the start value
  60. ** WITHOUT damaging or otherwise affecting any other timer that may be built upon the same
  61. ** specified timer class object. Treat an object of this type as if it were a "magic" integral
  62. ** long that automatically advances at the speed of the timer class object controlling it.
  63. */
  64. // Let lint know that non-virtual destructor is OK for this class.
  65. //lint -esym(1509,BasicTimerClass)
  66. template<class T>
  67. class BasicTimerClass {
  68. public:
  69. // Constructor allows assignment as if class was integral 'long' type.
  70. BasicTimerClass(int set=0);
  71. BasicTimerClass(NoInitClass const & );
  72. virtual ~BasicTimerClass(void);
  73. // Fetch current value of timer.
  74. int Value(void) const {return(Timer()-Started);}
  75. // Conversion operator to allow consistent treatment with integral types.
  76. operator int(void) const;
  77. // Function operator to allow timer object definition to be cascaded.
  78. int operator () (void) const;
  79. protected:
  80. T Timer; // Timer regulator (ticks at constant rate).
  81. int Started; // Time started.
  82. };
  83. template<class T>
  84. inline BasicTimerClass<T>::BasicTimerClass(NoInitClass const & )
  85. {
  86. };
  87. /***********************************************************************************************
  88. * BasicTimerClass<T>::BasicTimerClass -- Constructor for basic timer class. *
  89. * *
  90. * This is the constructor for the basic timer class object. It sets the timer counting *
  91. * up from zero at the rate of the controlling timer class object. *
  92. * *
  93. * INPUT: set -- Alternate initial start value for the counter. If not specified, then *
  94. * the timer is assumed to start at zero and count upwards. *
  95. * *
  96. * OUTPUT: none *
  97. * *
  98. * WARNINGS: none *
  99. * *
  100. * HISTORY: *
  101. * 02/05/1996 JLB : Created. *
  102. *=============================================================================================*/
  103. //lint -esym(1403,BasicTimerClass<class FrameTimerClass>::Timer)
  104. //lint -esym(1403,BasicTimerClass<class SystemTimerClass>::Timer)
  105. template<class T>
  106. inline BasicTimerClass<T>::BasicTimerClass(int set) :
  107. Started(Timer()-set)
  108. {
  109. }
  110. /***********************************************************************************************
  111. * BasicTimerClass<T>::~BasicTimerClass -- Destructor for basic timer object. *
  112. * *
  113. * The destructor for the basic timer object doesn't have to do anything. *
  114. * *
  115. * INPUT: none *
  116. * *
  117. * OUTPUT: none *
  118. * *
  119. * WARNINGS: none *
  120. * *
  121. * HISTORY: *
  122. * 02/05/1996 JLB : Created. *
  123. *=============================================================================================*/
  124. template<class T>
  125. inline BasicTimerClass<T>::~BasicTimerClass(void)
  126. {
  127. }
  128. //template<class T>
  129. //inline unsigned long BasicTimerClass<T>::Value(void) const
  130. //{
  131. // return(Timer()-Started);
  132. //}
  133. /***********************************************************************************************
  134. * BasicTimerClass<T>::operator long -- Conversion to long operator. *
  135. * *
  136. * This conversion operator allows the basic timer object to function in much the same *
  137. * manner as the integral "long" type. One can assign a long with a timer object and the *
  138. * actual value of the timer is extracted from the object and used. *
  139. * *
  140. * INPUT: none *
  141. * *
  142. * OUTPUT: Returns with the timer value expressed as a long. *
  143. * *
  144. * WARNINGS: none *
  145. * *
  146. * HISTORY: *
  147. * 02/05/1996 JLB : Created. *
  148. *=============================================================================================*/
  149. template<class T>
  150. inline BasicTimerClass<T>::operator int(void) const
  151. {
  152. return(Timer()-Started);
  153. }
  154. /***********************************************************************************************
  155. * BasicTimerClass<T>::operator () -- Function operator for timer object. *
  156. * *
  157. * This function operator allows the timer to also serve as the parameter type class for *
  158. * additional timer objects. This allows one to instantiate a controlling timer class that *
  159. * can control (e.g., turn on or off) all timers that are based upon it. *
  160. * *
  161. * INPUT: none *
  162. * *
  163. * OUTPUT: Returns the current timer value expressed as a long. *
  164. * *
  165. * WARNINGS: none *
  166. * *
  167. * HISTORY: *
  168. * 02/05/1996 JLB : Created. *
  169. *=============================================================================================*/
  170. template<class T>
  171. inline int BasicTimerClass<T>::operator () (void) const
  172. {
  173. return(Timer()-Started);
  174. }
  175. /*
  176. ** This timer class functions similarly to the basic timer class. In addition to the
  177. ** normal timer operation, this class has the ability to be stopped and started at
  178. ** will. If you have no need to start or stop the timer, then use the basic timer
  179. ** class instead.
  180. */
  181. template<class T>
  182. class TTimerClass : public BasicTimerClass<T> {
  183. public:
  184. // Constructor allows assignment as if class was integral 'long' type.
  185. TTimerClass(int set=0);
  186. TTimerClass(NoInitClass const & x);
  187. ~TTimerClass(void) {};
  188. // Fetches current value of timer.
  189. int Value(void) const;
  190. // Conversion operator to allow consistent treatment with integral types.
  191. operator int(void) const;
  192. // Function operator to allow timer object definition to be cascaded.
  193. int operator () (void) const;
  194. // Stops (pauses) the timer.
  195. void Stop(void);
  196. // Starts (resumes) the timer.
  197. void Start(void);
  198. // Queries whether the timer is currently active.
  199. bool Is_Active(void) const;
  200. private:
  201. int Accumulated; // Total accumulated ticks.
  202. };
  203. template<class T>
  204. inline TTimerClass<T>::TTimerClass(NoInitClass const & x) :
  205. BasicTimerClass<T>(x)
  206. {
  207. }
  208. /***********************************************************************************************
  209. * TTimerClass<T>::TTimerClass -- Constructor for timer class object. *
  210. * *
  211. * This is the constructor for the advanced timer class object. This object class can start *
  212. * or stop the timer under user control. *
  213. * *
  214. * INPUT: set -- The initial value to set the timer to. If no value is specified, then *
  215. * the timer is assumed to start from zero. *
  216. * *
  217. * OUTPUT: none *
  218. * *
  219. * WARNINGS: none *
  220. * *
  221. * HISTORY: *
  222. * 02/05/1996 JLB : Created. *
  223. *=============================================================================================*/
  224. template<class T>
  225. inline TTimerClass<T>::TTimerClass(int set) :
  226. BasicTimerClass<T>(set),
  227. Accumulated(0)
  228. {
  229. }
  230. /***********************************************************************************************
  231. * TTimerClass<T>::Value -- Returns with the current value of the timer. *
  232. * *
  233. * This routine will return with the current value of the timer. It takes into account *
  234. * whether the timer has stopped or not so as to always return the correct value regardless *
  235. * of that condition. *
  236. * *
  237. * INPUT: none *
  238. * *
  239. * OUTPUT: Returns with the current value of the timer. *
  240. * *
  241. * WARNINGS: none *
  242. * *
  243. * HISTORY: *
  244. * 07/06/1996 JLB : Created. *
  245. *=============================================================================================*/
  246. template<class T>
  247. inline int TTimerClass<T>::Value(void) const
  248. {
  249. int value = Accumulated;
  250. if (Started != -1) {
  251. value += BasicTimerClass<T>::Value();
  252. }
  253. return(value);
  254. }
  255. /***********************************************************************************************
  256. * TTimerClass<T>::operator long -- Conversion operator for timer object. *
  257. * *
  258. * This conversion operator allows this timer object to function as an "rvalue" of a "long" *
  259. * type. This is consistent with the integral "long" value. It is possible to assign a *
  260. * timer object to a long and have the long initialized with the current value of the *
  261. * timer. *
  262. * *
  263. * INPUT: none *
  264. * *
  265. * OUTPUT: Returns with the current time value expressed as a long. *
  266. * *
  267. * WARNINGS: none *
  268. * *
  269. * HISTORY: *
  270. * 02/05/1996 JLB : Created. *
  271. *=============================================================================================*/
  272. template<class T>
  273. inline TTimerClass<T>::operator int(void) const
  274. {
  275. int value = Accumulated;
  276. if (Started != -1) {
  277. value += BasicTimerClass<T>::Value();
  278. }
  279. return(value);
  280. }
  281. /***********************************************************************************************
  282. * TTimerClass<T>::operator () -- Function operator for timer object. *
  283. * *
  284. * This function operator for the timer class allows this timer class to be used as the *
  285. * template parameter for other timer class objects. With this ability, one can control *
  286. * several timers (e.g., start or stop them) by using a single controlling timer class *
  287. * that other timers are instantiated from. *
  288. * *
  289. * INPUT: none *
  290. * *
  291. * OUTPUT: Returns with the current time expressed as a long. *
  292. * *
  293. * WARNINGS: none *
  294. * *
  295. * HISTORY: *
  296. * 02/05/1996 JLB : Created. *
  297. *=============================================================================================*/
  298. template<class T>
  299. inline int TTimerClass<T>::operator () (void) const
  300. {
  301. int value = Accumulated;
  302. if (Started != -1) {
  303. value += BasicTimerClass<T>::Value();
  304. }
  305. return(value);
  306. }
  307. /***********************************************************************************************
  308. * TTimerClass<T>::Stop -- Stops the current timer from incrementing. *
  309. * *
  310. * This routine will stop (pause) the timer from further increments. To cause the timer *
  311. * to begin anew, call the Start() function. *
  312. * *
  313. * *
  314. * INPUT: *
  315. * *
  316. * OUTPUT: *
  317. * *
  318. * WARNINGS: *
  319. * *
  320. * HISTORY: *
  321. * 02/05/1996 JLB : Created. *
  322. *=============================================================================================*/
  323. template<class T>
  324. void TTimerClass<T>::Stop(void)
  325. {
  326. if (Started != -1) {
  327. Accumulated += BasicTimerClass<T>::Value();
  328. Started = -1;
  329. }
  330. }
  331. /***********************************************************************************************
  332. * TTimerClass<T>::Start -- Starts (resumes) a stopped timer. *
  333. * *
  334. * This routine will resume a timer that was previously stopped with the Stop() function. *
  335. * *
  336. * INPUT: none *
  337. * *
  338. * OUTPUT: none *
  339. * *
  340. * WARNINGS: none *
  341. * *
  342. * HISTORY: *
  343. * 02/06/1996 JLB : Created. *
  344. *=============================================================================================*/
  345. template<class T>
  346. void TTimerClass<T>::Start(void)
  347. {
  348. if (Started == -1) {
  349. Started = Timer();
  350. }
  351. }
  352. /***********************************************************************************************
  353. * TTimerClass<T>::Is_Active -- Checks to see if the timer is counting. *
  354. * *
  355. * Since this timer can be paused, this routine is used to examine the timer to see if it *
  356. * is currently paused or active. If the timer is active, then the return value will be *
  357. * true. *
  358. * *
  359. * INPUT: none *
  360. * *
  361. * OUTPUT: bool; Is this timer currently active? *
  362. * *
  363. * WARNINGS: none *
  364. * *
  365. * HISTORY: *
  366. * 02/06/1996 JLB : Created. *
  367. *=============================================================================================*/
  368. template<class T>
  369. inline bool TTimerClass<T>::Is_Active(void) const
  370. {
  371. return(Started != -1);
  372. }
  373. /*
  374. ** This timer counts down from the specified (or constructed) value down towards zero.
  375. ** The countdown rate is controlled by the timer object specified. This timer object can
  376. ** be started or stopped. It can also be tested to see if it has expired or not. An expired
  377. ** count down timer is one that has value of zero. You can treat this class object as if it
  378. ** were an integral "magic" long that automatically counts down toward zero.
  379. */
  380. template<class T>
  381. class CDTimerClass : public BasicTimerClass<T> {
  382. public:
  383. // Constructor allows assignment as if class was integral 'long' type.
  384. CDTimerClass(int set=0);
  385. CDTimerClass(NoInitClass const & x);
  386. ~CDTimerClass(void);
  387. // Fetches current value of count down timer.
  388. int Value(void) const;
  389. // Conversion operator to allow consistent treatment with integral types.
  390. operator int(void) const;
  391. // Function operator to allow timer object definition to be cascaded.
  392. int operator () (void) const;
  393. // Stops (pauses) the timer.
  394. void Stop(void);
  395. // Starts (resumes) the timer.
  396. void Start(void);
  397. // Queries whether the timer is currently active.
  398. bool Is_Active(void) const;
  399. private:
  400. int DelayTime; // Ticks remaining before countdown timer expires.
  401. };
  402. template<class T>
  403. inline CDTimerClass<T>::CDTimerClass(NoInitClass const & x) :
  404. BasicTimerClass<T>(x)
  405. {
  406. }
  407. /***********************************************************************************************
  408. * CDTimerClass<T>::CDTimerClass -- Constructor for count down timer. *
  409. * *
  410. * This is the constructor for the count down timer object. The optional starting value *
  411. * can be used to initiate the timer. Because of this constructor it is possible to assign *
  412. * a long to a count down timer object in order to begin the countdown process. *
  413. * *
  414. * INPUT: set -- The initial starting value for the countdown timer. *
  415. * *
  416. * OUTPUT: none *
  417. * *
  418. * WARNINGS: none *
  419. * *
  420. * HISTORY: *
  421. * 02/06/1996 JLB : Created. *
  422. *=============================================================================================*/
  423. template<class T>
  424. inline CDTimerClass<T>::CDTimerClass(int set) :
  425. BasicTimerClass<T>(0),
  426. DelayTime(set)
  427. {
  428. }
  429. /***********************************************************************************************
  430. * CDTimerClass<T>::~CDTimerClass -- Destructor for the count down timer object. *
  431. * *
  432. * The destructor for the count down timer object does nothing. *
  433. * *
  434. * INPUT: none *
  435. * *
  436. * OUTPUT: none *
  437. * *
  438. * WARNINGS: none *
  439. * *
  440. * HISTORY: *
  441. * 02/06/1996 JLB : Created. *
  442. *=============================================================================================*/
  443. template<class T>
  444. inline CDTimerClass<T>::~CDTimerClass(void)
  445. {
  446. }
  447. /***********************************************************************************************
  448. * CDTimerClass<T>::Value -- Fetches the current value of the countdown timer. *
  449. * *
  450. * Use this routine to fetch the current value of the timer. It takes into consideration *
  451. * whether the timer has been stopped or not. It returns the correct value regardless of *
  452. * this condition. *
  453. * *
  454. * INPUT: none *
  455. * *
  456. * OUTPUT: Returns with the correct value of this count down timer. *
  457. * *
  458. * WARNINGS: none *
  459. * *
  460. * HISTORY: *
  461. * 07/06/1996 JLB : Created. *
  462. *=============================================================================================*/
  463. template<class T>
  464. inline int CDTimerClass<T>::Value(void) const
  465. {
  466. int remain = DelayTime;
  467. if (Started != -1) {
  468. int value = BasicTimerClass<T>::Value();
  469. if (value < remain) {
  470. return(remain - value);
  471. } else {
  472. return(0);
  473. }
  474. }
  475. return(remain);
  476. }
  477. /***********************************************************************************************
  478. * CDTimerClass<T>::operator long -- Conversion to long operator function. *
  479. * *
  480. * This conversion operator allows the count down timer object to be used as if it were *
  481. * a "magic" long that automatically counted downward at the controller class tick rate. *
  482. * The count down object can be used in any place that an rvalue long could be used. *
  483. * *
  484. * INPUT: none *
  485. * *
  486. * OUTPUT: Returns with the current count down time expressed in the form of a long value. *
  487. * *
  488. * WARNINGS: none *
  489. * *
  490. * HISTORY: *
  491. * 02/06/1996 JLB : Created. *
  492. *=============================================================================================*/
  493. template<class T>
  494. inline CDTimerClass<T>::operator int(void) const
  495. {
  496. int remain = DelayTime;
  497. if (Started != 0xFFFFFFFFU) {
  498. int value = BasicTimerClass<T>::Value();
  499. if (value < remain) {
  500. return(remain - value);
  501. } else {
  502. return(0);
  503. }
  504. }
  505. return(remain);
  506. }
  507. /***********************************************************************************************
  508. * CDTimerClass<T>::operator () -- Function operator for the count down timer. *
  509. * *
  510. * This is the function operator for the count down timer object. By supporting this *
  511. * function operator, this class (or one derived from this class) could be used as the *
  512. * controlling timer to the timer templates. *
  513. * *
  514. * INPUT: none *
  515. * *
  516. * OUTPUT: Returns with the current count down time expressed in the form of a long. *
  517. * *
  518. * WARNINGS: none *
  519. * *
  520. * HISTORY: *
  521. * 02/06/1996 JLB : Created. *
  522. *=============================================================================================*/
  523. template<class T>
  524. inline int CDTimerClass<T>::operator () (void) const
  525. {
  526. int remain = DelayTime;
  527. if (Started != -1) {
  528. int value = BasicTimerClass<T>::Value();
  529. if (value < remain) {
  530. return(remain - value);
  531. } else {
  532. return(0);
  533. }
  534. }
  535. return(remain);
  536. }
  537. /***********************************************************************************************
  538. * CDTimerClass<T>::Stop -- Stops (pauses) the count down timer. *
  539. * *
  540. * This routine is used to stop (pause) the count down timer object. A timer object paused *
  541. * in this fashion will be resumed by a call to Start() or by assigning a new count down *
  542. * value to the timer. *
  543. * *
  544. * INPUT: none *
  545. * *
  546. * OUTPUT: none *
  547. * *
  548. * WARNINGS: none *
  549. * *
  550. * HISTORY: *
  551. * 02/06/1996 JLB : Created. *
  552. *=============================================================================================*/
  553. template<class T>
  554. void CDTimerClass<T>::Stop(void)
  555. {
  556. if (Started != -1) {
  557. DelayTime = *this;
  558. Started = -1;
  559. }
  560. }
  561. /***********************************************************************************************
  562. * CDTimerClass<T>::Start -- Starts (resumes) the count down timer. *
  563. * *
  564. * This routine is used to start (resume) the count down timer that was previously stopped *
  565. * with the Stop() function. The timer will also resume when a new timer value is assigned. *
  566. * *
  567. * INPUT: none *
  568. * *
  569. * OUTPUT: none *
  570. * *
  571. * WARNINGS: none *
  572. * *
  573. * HISTORY: *
  574. * 02/06/1996 JLB : Created. *
  575. *=============================================================================================*/
  576. template<class T>
  577. void CDTimerClass<T>::Start(void)
  578. {
  579. if (Started == -1) {
  580. Started = Timer();
  581. }
  582. }
  583. /***********************************************************************************************
  584. * CDTimerClass<T>::Is_Active -- Checks to see if the timer object is active. *
  585. * *
  586. * Because the timer object counting can be stopped, this routine is used to determine *
  587. * if the timer is currently paused or active. *
  588. * *
  589. * INPUT: none *
  590. * *
  591. * OUTPUT: bool; Is the timer currently active? *
  592. * *
  593. * WARNINGS: Note that if the timer has counted down to zero, then it may be active, but *
  594. * the value will, naturally, not change. *
  595. * *
  596. * HISTORY: *
  597. * 02/06/1996 JLB : Created. *
  598. *=============================================================================================*/
  599. template<class T>
  600. inline bool CDTimerClass<T>::Is_Active(void) const
  601. {
  602. return(Started != -1);
  603. }
  604. #endif