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