Timer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/CoreEvents.h"
  24. #include "../Core/Profiler.h"
  25. #include <ctime>
  26. #ifdef _WIN32
  27. #include <windows.h>
  28. #include <mmsystem.h>
  29. #elif __EMSCRIPTEN__
  30. #include <emscripten/emscripten.h>
  31. #else
  32. #include <sys/time.h>
  33. #include <unistd.h>
  34. #endif
  35. #include "../DebugNew.h"
  36. namespace Urho3D
  37. {
  38. bool HiresTimer::supported(false);
  39. long long HiresTimer::frequency(1000);
  40. Time::Time(Context* context) :
  41. Object(context),
  42. frameNumber_(0),
  43. timeStep_(0.0f),
  44. timerPeriod_(0)
  45. {
  46. #ifdef _WIN32
  47. LARGE_INTEGER frequency;
  48. if (QueryPerformanceFrequency(&frequency))
  49. {
  50. HiresTimer::frequency = frequency.QuadPart;
  51. HiresTimer::supported = true;
  52. }
  53. #else
  54. HiresTimer::frequency = 1000000;
  55. HiresTimer::supported = true;
  56. #endif
  57. }
  58. Time::~Time()
  59. {
  60. SetTimerPeriod(0);
  61. }
  62. static unsigned Tick()
  63. {
  64. #ifdef _WIN32
  65. return (unsigned)timeGetTime();
  66. #elif __EMSCRIPTEN__
  67. return (unsigned)emscripten_get_now();
  68. #else
  69. struct timeval time;
  70. gettimeofday(&time, NULL);
  71. return (unsigned)(time.tv_sec * 1000 + time.tv_usec / 1000);
  72. #endif
  73. }
  74. static long long HiresTick()
  75. {
  76. #ifdef _WIN32
  77. if (HiresTimer::IsSupported())
  78. {
  79. LARGE_INTEGER counter;
  80. QueryPerformanceCounter(&counter);
  81. return counter.QuadPart;
  82. }
  83. else
  84. return timeGetTime();
  85. #elif __EMSCRIPTEN__
  86. return (unsigned)(emscripten_get_now()*1000.0);
  87. #else
  88. struct timeval time;
  89. gettimeofday(&time, NULL);
  90. return time.tv_sec * 1000000LL + time.tv_usec;
  91. #endif
  92. }
  93. void Time::BeginFrame(float timeStep)
  94. {
  95. ++frameNumber_;
  96. if (!frameNumber_)
  97. ++frameNumber_;
  98. timeStep_ = timeStep;
  99. auto* profiler = GetSubsystem<Profiler>();
  100. if (profiler)
  101. profiler->BeginFrame();
  102. {
  103. URHO3D_PROFILE(BeginFrame);
  104. // Frame begin event
  105. using namespace BeginFrame;
  106. VariantMap& eventData = GetEventDataMap();
  107. eventData[P_FRAMENUMBER] = frameNumber_;
  108. eventData[P_TIMESTEP] = timeStep_;
  109. SendEvent(E_BEGINFRAME, eventData);
  110. }
  111. }
  112. void Time::EndFrame()
  113. {
  114. {
  115. URHO3D_PROFILE(EndFrame);
  116. // Frame end event
  117. SendEvent(E_ENDFRAME);
  118. }
  119. auto* profiler = GetSubsystem<Profiler>();
  120. if (profiler)
  121. profiler->EndFrame();
  122. }
  123. void Time::SetTimerPeriod(unsigned mSec)
  124. {
  125. #ifdef _WIN32
  126. if (timerPeriod_ > 0)
  127. timeEndPeriod(timerPeriod_);
  128. timerPeriod_ = mSec;
  129. if (timerPeriod_ > 0)
  130. timeBeginPeriod(timerPeriod_);
  131. #endif
  132. }
  133. float Time::GetElapsedTime()
  134. {
  135. return elapsedTime_.GetMSec(false) / 1000.0f;
  136. }
  137. unsigned Time::GetSystemTime()
  138. {
  139. return Tick();
  140. }
  141. unsigned Time::GetTimeSinceEpoch()
  142. {
  143. return (unsigned)time(nullptr);
  144. }
  145. String Time::GetTimeStamp()
  146. {
  147. time_t sysTime;
  148. time(&sysTime);
  149. const char* dateTime = ctime(&sysTime);
  150. return String(dateTime).Replaced("\n", "");
  151. }
  152. void Time::Sleep(unsigned mSec)
  153. {
  154. #ifdef _WIN32
  155. ::Sleep(mSec);
  156. #else
  157. timespec time;
  158. time.tv_sec = mSec / 1000;
  159. time.tv_nsec = (mSec % 1000) * 1000000;
  160. nanosleep(&time, 0);
  161. #endif
  162. }
  163. float Time::GetFramesPerSecond() const
  164. {
  165. return 1.0f / timeStep_;
  166. }
  167. Timer::Timer()
  168. {
  169. Reset();
  170. }
  171. unsigned Timer::GetMSec(bool reset)
  172. {
  173. unsigned currentTime = Tick();
  174. unsigned elapsedTime = currentTime - startTime_;
  175. if (reset)
  176. startTime_ = currentTime;
  177. return elapsedTime;
  178. }
  179. void Timer::Reset()
  180. {
  181. startTime_ = Tick();
  182. }
  183. HiresTimer::HiresTimer()
  184. {
  185. Reset();
  186. }
  187. long long HiresTimer::GetUSec(bool reset)
  188. {
  189. long long currentTime = HiresTick();
  190. long long elapsedTime = currentTime - startTime_;
  191. // Correct for possible weirdness with changing internal frequency
  192. if (elapsedTime < 0)
  193. elapsedTime = 0;
  194. if (reset)
  195. startTime_ = currentTime;
  196. return (elapsedTime * 1000000LL) / frequency;
  197. }
  198. void HiresTimer::Reset()
  199. {
  200. startTime_ = HiresTick();
  201. }
  202. }