Timer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Timer.h"
  25. #include <windows.h>
  26. #include <mmsystem.h>
  27. #include "DebugNew.h"
  28. unsigned timerPeriod = 0;
  29. bool hiresTimerSupported = false;
  30. long long hiresTimerFrequency = 1000;
  31. Timer::Timer()
  32. {
  33. mStartTime = timeGetTime();
  34. }
  35. unsigned Timer::getMSec(bool reset)
  36. {
  37. unsigned currentTime = timeGetTime();
  38. unsigned elapsedTime = currentTime - mStartTime;
  39. if (reset)
  40. mStartTime = currentTime;
  41. return elapsedTime;
  42. }
  43. void Timer::reset()
  44. {
  45. mStartTime = timeGetTime();
  46. }
  47. HiresTimer::HiresTimer()
  48. {
  49. if (hiresTimerSupported)
  50. {
  51. LARGE_INTEGER counter;
  52. QueryPerformanceCounter(&counter);
  53. mStartTime = counter.QuadPart;
  54. }
  55. else
  56. mStartTime = timeGetTime();
  57. }
  58. long long HiresTimer::getUSec(bool reset)
  59. {
  60. long long currentTime;
  61. if (hiresTimerSupported)
  62. {
  63. LARGE_INTEGER counter;
  64. QueryPerformanceCounter(&counter);
  65. currentTime = counter.QuadPart;
  66. }
  67. else
  68. currentTime = timeGetTime();
  69. long long elapsedTime = currentTime - mStartTime;
  70. // Correct for possible weirdness with changing internal frequency
  71. if (elapsedTime < 0)
  72. elapsedTime = 0;
  73. if (reset)
  74. mStartTime = currentTime;
  75. return (elapsedTime * 1000000LL) / hiresTimerFrequency;
  76. }
  77. void HiresTimer::reset()
  78. {
  79. if (hiresTimerSupported)
  80. {
  81. LARGE_INTEGER counter;
  82. QueryPerformanceCounter(&counter);
  83. mStartTime = counter.QuadPart;
  84. }
  85. else
  86. mStartTime = timeGetTime();
  87. }
  88. void setTimerPeriod(unsigned period)
  89. {
  90. if (timerPeriod > 0)
  91. timeEndPeriod(timerPeriod);
  92. timerPeriod = period;
  93. if (timerPeriod > 0)
  94. timeBeginPeriod(timerPeriod);
  95. }
  96. unsigned getTimerPeriod()
  97. {
  98. return timerPeriod;
  99. }
  100. void initHiresTimer()
  101. {
  102. LARGE_INTEGER frequency;
  103. if (QueryPerformanceFrequency(&frequency))
  104. {
  105. hiresTimerFrequency = frequency.QuadPart;
  106. hiresTimerSupported = true;
  107. }
  108. }
  109. bool isHiresTimerSupported()
  110. {
  111. return hiresTimerSupported;
  112. }
  113. long long getHiresTimerFrequency()
  114. {
  115. return hiresTimerFrequency;
  116. }