Timer.cpp 5.0 KB

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