TimeoutTimer.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*=============================================================================
  2. Name : TimeoutTimer.c
  3. Purpose : Implementation of Timeout Timers
  4. Created 98/11/13 by gshaw
  5. Copyright Relic Entertainment, Inc. All rights reserved.
  6. =============================================================================*/
  7. // unfortunately need this for the timers
  8. #define WIN32_LEAN_AND_MEAN
  9. #include <windows.h>
  10. #include "types.h"
  11. #include "TimeoutTimer.h"
  12. #include "utility.h"
  13. extern LONGLONG utyTimerDivisor;
  14. void TTimerInit(TTimer *timer)
  15. {
  16. timer->enabled = FALSE;
  17. }
  18. void TTimerClose(TTimer *timer)
  19. {
  20. timer->enabled = FALSE;
  21. }
  22. void TTimerDisable(TTimer *timer)
  23. {
  24. timer->enabled = FALSE;
  25. }
  26. bool TTimerUpdate(TTimer *timer)
  27. {
  28. LARGE_INTEGER perftimer;
  29. LONGLONG difference;
  30. udword nTicks;
  31. if (!timer->enabled)
  32. {
  33. return FALSE;
  34. }
  35. if (timer->timedOut)
  36. {
  37. return TRUE;
  38. }
  39. QueryPerformanceCounter(&perftimer);
  40. difference = perftimer.QuadPart - timer->timerLast;
  41. nTicks = (udword)(difference / utyTimerDivisor);
  42. if (nTicks >= timer->timeoutTicks)
  43. {
  44. timer->timedOut = TRUE;
  45. return TRUE;
  46. }
  47. return FALSE;
  48. }
  49. bool TTimerIsTimedOut(TTimer *timer)
  50. {
  51. if (!timer->enabled)
  52. {
  53. return FALSE;
  54. }
  55. return timer->timedOut;
  56. }
  57. void TTimerReset(TTimer *timer)
  58. {
  59. LARGE_INTEGER perftimer;
  60. if (!timer->enabled)
  61. {
  62. return;
  63. }
  64. QueryPerformanceCounter(&perftimer);
  65. timer->timedOut = FALSE;
  66. timer->timerLast = perftimer.QuadPart;
  67. }
  68. void TTimerStart(TTimer *timer,real32 timeout)
  69. {
  70. LARGE_INTEGER perftimer;
  71. QueryPerformanceCounter(&perftimer);
  72. timer->enabled = TRUE;
  73. timer->timedOut = FALSE;
  74. timer->timerLast = perftimer.QuadPart;
  75. timer->timeoutTicks = (udword) (timeout * UTY_TimerResloutionMax);
  76. }
  77. void GetRawTime(sqword *time)
  78. {
  79. QueryPerformanceCounter((LARGE_INTEGER *)time);
  80. }