systimer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. ** Command & Conquer Generals(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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/systimer.h $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 12/09/01 6:41p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #pragma once
  36. #ifndef _SYSTIMER_H
  37. #include "always.h"
  38. #include <windows.h>
  39. #include "mmsys.h"
  40. #define TIMEGETTIME SystemTime.Get
  41. /*
  42. ** Class that just wraps around timeGetTime()
  43. **
  44. **
  45. */
  46. class SysTimeClass
  47. {
  48. public:
  49. SysTimeClass(void); //default constructor
  50. ~SysTimeClass(); //default destructor
  51. /*
  52. ** Get. Use everywhere you would use timeGetTime
  53. */
  54. WWINLINE unsigned long Get(void);
  55. WWINLINE unsigned long operator () (void) {return(Get());}
  56. WWINLINE operator unsigned long(void) {return(Get());}
  57. /*
  58. ** Use periodically (like every few days!) to make sure the timer doesn't wrap.
  59. */
  60. void Reset(void);
  61. /*
  62. ** See if the timer is about to wrap.
  63. */
  64. bool Is_Getting_Late(void);
  65. private:
  66. /*
  67. ** Time we were first called.
  68. */
  69. unsigned long StartTime;
  70. /*
  71. ** Time to add after timer wraps.
  72. */
  73. unsigned long WrapAdd;
  74. };
  75. extern SysTimeClass SystemTime;
  76. /***********************************************************************************************
  77. * SysTimeClass::Get -- Wrapper around system timeGetTime() api call *
  78. * *
  79. * *
  80. * *
  81. * INPUT: Nothing *
  82. * *
  83. * OUTPUT: Current system time in ms *
  84. * *
  85. * WARNINGS: None *
  86. * *
  87. * HISTORY: *
  88. * 10/25/2001 1:38PM ST : Created *
  89. *=============================================================================================*/
  90. WWINLINE unsigned long SysTimeClass::Get(void)
  91. {
  92. /*
  93. ** This has to be static here since we don't know if we will get called in a global constructor of another object before our
  94. ** constructor gets called. In fact, we don't even have a constructor because it's pointless.
  95. */
  96. static bool is_init = false;
  97. if (!is_init) {
  98. Reset();
  99. is_init = true;
  100. }
  101. unsigned long time = timeGetTime();
  102. if (time > StartTime) {
  103. return(time - StartTime);
  104. }
  105. /*
  106. ** Timer wrapped around. Eeek.
  107. */
  108. return(time + WrapAdd);
  109. }
  110. #ifdef timeGetTime
  111. #undef timeGetTime
  112. #define timeGetTime SystemTime.Get
  113. #endif //timeGetTime
  114. #endif //_SYSTIMER_H