Timer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "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. timeStepMSec_(0),
  42. totalMSec_(0),
  43. timerPeriod_(0)
  44. {
  45. #ifdef _WIN32
  46. LARGE_INTEGER frequency;
  47. if (QueryPerformanceFrequency(&frequency))
  48. {
  49. HiresTimer::frequency = frequency.QuadPart;
  50. HiresTimer::supported = true;
  51. }
  52. #else
  53. HiresTimer::frequency = 1000000;
  54. HiresTimer::supported = true;
  55. #endif
  56. }
  57. Time::~Time()
  58. {
  59. SetTimerPeriod(0);
  60. }
  61. void Time::BeginFrame(unsigned mSec)
  62. {
  63. ++frameNumber_;
  64. if (!frameNumber_)
  65. ++frameNumber_;
  66. timeStep_ = (float)mSec / 1000.0f;
  67. timeStepMSec_ = mSec;
  68. {
  69. // Frame begin event
  70. using namespace BeginFrame;
  71. VariantMap eventData;
  72. eventData[P_FRAMENUMBER] = frameNumber_;
  73. eventData[P_TIMESTEP] = timeStep_;
  74. SendEvent(E_BEGINFRAME, eventData);
  75. }
  76. {
  77. // Logic update event
  78. using namespace Update;
  79. VariantMap eventData;
  80. eventData[P_TIMESTEP] = timeStep_;
  81. SendEvent(E_UPDATE, eventData);
  82. // Logic post-update event
  83. SendEvent(E_POSTUPDATE, eventData);
  84. // Rendering update event
  85. SendEvent(E_RENDERUPDATE, eventData);
  86. // Post-render update event
  87. SendEvent(E_POSTRENDERUPDATE, eventData);
  88. }
  89. }
  90. void Time::EndFrame()
  91. {
  92. totalMSec_ += timeStepMSec_;
  93. // Frame end event
  94. SendEvent(E_ENDFRAME);
  95. }
  96. void Time::SetTimerPeriod(unsigned mSec)
  97. {
  98. #ifdef _WIN32
  99. if (timerPeriod_ > 0)
  100. timeEndPeriod(timerPeriod_);
  101. timerPeriod_ = mSec;
  102. if (timerPeriod_ > 0)
  103. timeBeginPeriod(timerPeriod_);
  104. #endif
  105. }
  106. void Time::Sleep(unsigned mSec)
  107. {
  108. #ifdef _WIN32
  109. ::Sleep(mSec);
  110. #else
  111. usleep(mSec * 1000);
  112. #endif
  113. }
  114. Timer::Timer()
  115. {
  116. Reset();
  117. }
  118. unsigned Timer::GetMSec(bool reset)
  119. {
  120. #ifdef _WIN32
  121. unsigned currentTime = timeGetTime();
  122. #else
  123. struct timeval time;
  124. gettimeofday(&time, NULL);
  125. unsigned currentTime = time.tv_sec * 1000 + time.tv_usec / 1000;
  126. #endif
  127. unsigned elapsedTime = currentTime - startTime_;
  128. if (reset)
  129. startTime_ = currentTime;
  130. return elapsedTime;
  131. }
  132. void Timer::Reset()
  133. {
  134. #ifdef _WIN32
  135. startTime_ = timeGetTime();
  136. #else
  137. struct timeval time;
  138. gettimeofday(&time, NULL);
  139. startTime_ = time.tv_sec * 1000 + time.tv_usec / 1000;
  140. #endif
  141. }
  142. HiresTimer::HiresTimer()
  143. {
  144. Reset();
  145. }
  146. long long HiresTimer::GetUSec(bool reset)
  147. {
  148. long long currentTime;
  149. #ifdef _WIN32
  150. if (supported)
  151. {
  152. LARGE_INTEGER counter;
  153. QueryPerformanceCounter(&counter);
  154. currentTime = counter.QuadPart;
  155. }
  156. else
  157. currentTime = timeGetTime();
  158. #else
  159. struct timeval time;
  160. gettimeofday(&time, NULL);
  161. currentTime = time.tv_sec * 1000000LL + time.tv_usec;
  162. #endif
  163. long long elapsedTime = currentTime - startTime_;
  164. // Correct for possible weirdness with changing internal frequency
  165. if (elapsedTime < 0)
  166. elapsedTime = 0;
  167. if (reset)
  168. startTime_ = currentTime;
  169. return (elapsedTime * 1000000LL) / frequency;
  170. }
  171. void HiresTimer::Reset()
  172. {
  173. #ifdef _WIN32
  174. if (supported)
  175. {
  176. LARGE_INTEGER counter;
  177. QueryPerformanceCounter(&counter);
  178. startTime_ = counter.QuadPart;
  179. }
  180. else
  181. startTime_ = timeGetTime();
  182. #else
  183. struct timeval time;
  184. gettimeofday(&time, NULL);
  185. startTime_ = time.tv_sec * 1000000LL + time.tv_usec;
  186. #endif
  187. }