Timer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "CoreEvents.h"
  25. #include "Timer.h"
  26. #ifdef WIN32
  27. #include <windows.h>
  28. #include <mmsystem.h>
  29. #else
  30. #include <sys/time.h>
  31. #include <unistd.h>
  32. #endif
  33. #include "DebugNew.h"
  34. bool HiresTimer::supported(false);
  35. long long HiresTimer::frequency(1000);
  36. OBJECTTYPESTATIC(Time);
  37. Time::Time(Context* context) :
  38. Object(context),
  39. frameNumber_(0),
  40. timeStep_(0.0f),
  41. timerPeriod_(0)
  42. {
  43. #ifdef WIN32
  44. LARGE_INTEGER frequency;
  45. if (QueryPerformanceFrequency(&frequency))
  46. {
  47. HiresTimer::frequency = frequency.QuadPart;
  48. HiresTimer::supported = true;
  49. }
  50. #else
  51. HiresTimer::frequency = 1000000;
  52. HiresTimer::supported = true;
  53. #endif
  54. }
  55. Time::~Time()
  56. {
  57. SetTimerPeriod(0);
  58. }
  59. void Time::BeginFrame(float timeStep)
  60. {
  61. ++frameNumber_;
  62. if (!frameNumber_)
  63. ++frameNumber_;
  64. timeStep_ = timeStep;
  65. // Frame begin event
  66. using namespace BeginFrame;
  67. VariantMap eventData;
  68. eventData[P_FRAMENUMBER] = frameNumber_;
  69. eventData[P_TIMESTEP] = timeStep_;
  70. SendEvent(E_BEGINFRAME, eventData);
  71. }
  72. void Time::EndFrame()
  73. {
  74. // Frame end event
  75. SendEvent(E_ENDFRAME);
  76. }
  77. void Time::SetTimerPeriod(unsigned mSec)
  78. {
  79. #ifdef WIN32
  80. if (timerPeriod_ > 0)
  81. timeEndPeriod(timerPeriod_);
  82. timerPeriod_ = mSec;
  83. if (timerPeriod_ > 0)
  84. timeBeginPeriod(timerPeriod_);
  85. #endif
  86. }
  87. float Time::GetElapsedTime()
  88. {
  89. return elapsedTime_.GetMSec(false) / 1000.0f;
  90. }
  91. void Time::Sleep(unsigned mSec)
  92. {
  93. #ifdef WIN32
  94. ::Sleep(mSec);
  95. #else
  96. usleep(mSec * 1000);
  97. #endif
  98. }
  99. Timer::Timer()
  100. {
  101. Reset();
  102. }
  103. unsigned Timer::GetMSec(bool reset)
  104. {
  105. #ifdef WIN32
  106. unsigned currentTime = timeGetTime();
  107. #else
  108. struct timeval time;
  109. gettimeofday(&time, NULL);
  110. unsigned currentTime = time.tv_sec * 1000 + time.tv_usec / 1000;
  111. #endif
  112. unsigned elapsedTime = currentTime - startTime_;
  113. if (reset)
  114. startTime_ = currentTime;
  115. return elapsedTime;
  116. }
  117. void Timer::Reset()
  118. {
  119. #ifdef WIN32
  120. startTime_ = timeGetTime();
  121. #else
  122. struct timeval time;
  123. gettimeofday(&time, NULL);
  124. startTime_ = time.tv_sec * 1000 + time.tv_usec / 1000;
  125. #endif
  126. }
  127. HiresTimer::HiresTimer()
  128. {
  129. Reset();
  130. }
  131. long long HiresTimer::GetUSec(bool reset)
  132. {
  133. long long currentTime;
  134. #ifdef WIN32
  135. if (supported)
  136. {
  137. LARGE_INTEGER counter;
  138. QueryPerformanceCounter(&counter);
  139. currentTime = counter.QuadPart;
  140. }
  141. else
  142. currentTime = timeGetTime();
  143. #else
  144. struct timeval time;
  145. gettimeofday(&time, NULL);
  146. currentTime = time.tv_sec * 1000000LL + time.tv_usec;
  147. #endif
  148. long long elapsedTime = currentTime - startTime_;
  149. // Correct for possible weirdness with changing internal frequency
  150. if (elapsedTime < 0)
  151. elapsedTime = 0;
  152. if (reset)
  153. startTime_ = currentTime;
  154. return (elapsedTime * 1000000LL) / frequency;
  155. }
  156. void HiresTimer::Reset()
  157. {
  158. #ifdef WIN32
  159. if (supported)
  160. {
  161. LARGE_INTEGER counter;
  162. QueryPerformanceCounter(&counter);
  163. startTime_ = counter.QuadPart;
  164. }
  165. else
  166. startTime_ = timeGetTime();
  167. #else
  168. struct timeval time;
  169. gettimeofday(&time, NULL);
  170. startTime_ = time.tv_sec * 1000000LL + time.tv_usec;
  171. #endif
  172. }