Timer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // Copyright (c) 2008-2014 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 "CoreEvents.h"
  24. #include "Profiler.h"
  25. #include "Timer.h"
  26. #include <ctime>
  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. namespace Urho3D
  36. {
  37. bool HiresTimer::supported(false);
  38. long long HiresTimer::frequency(1000);
  39. Time::Time(Context* context) :
  40. Object(context),
  41. frameNumber_(0),
  42. timeStep_(0.0f),
  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(float timeStep)
  62. {
  63. ++frameNumber_;
  64. if (!frameNumber_)
  65. ++frameNumber_;
  66. timeStep_ = timeStep;
  67. Profiler* profiler = GetSubsystem<Profiler>();
  68. if (profiler)
  69. profiler->BeginFrame();
  70. {
  71. PROFILE(BeginFrame);
  72. // Frame begin event
  73. using namespace BeginFrame;
  74. VariantMap& eventData = GetEventDataMap();
  75. eventData[P_FRAMENUMBER] = frameNumber_;
  76. eventData[P_TIMESTEP] = timeStep_;
  77. SendEvent(E_BEGINFRAME, eventData);
  78. }
  79. }
  80. void Time::EndFrame()
  81. {
  82. {
  83. PROFILE(EndFrame);
  84. // Frame end event
  85. SendEvent(E_ENDFRAME);
  86. }
  87. Profiler* profiler = GetSubsystem<Profiler>();
  88. if (profiler)
  89. profiler->EndFrame();
  90. }
  91. void Time::SetTimerPeriod(unsigned mSec)
  92. {
  93. #ifdef WIN32
  94. if (timerPeriod_ > 0)
  95. timeEndPeriod(timerPeriod_);
  96. timerPeriod_ = mSec;
  97. if (timerPeriod_ > 0)
  98. timeBeginPeriod(timerPeriod_);
  99. #endif
  100. }
  101. float Time::GetElapsedTime()
  102. {
  103. return elapsedTime_.GetMSec(false) / 1000.0f;
  104. }
  105. unsigned Time::GetSystemTime()
  106. {
  107. #ifdef WIN32
  108. unsigned currentTime = timeGetTime();
  109. #else
  110. struct timeval time;
  111. gettimeofday(&time, NULL);
  112. unsigned currentTime = time.tv_sec * 1000 + time.tv_usec / 1000;
  113. #endif
  114. return currentTime;
  115. }
  116. String Time::GetTimeStamp()
  117. {
  118. time_t sysTime;
  119. time(&sysTime);
  120. const char* dateTime = ctime(&sysTime);
  121. return String(dateTime).Replaced("\n", "");
  122. }
  123. void Time::Sleep(unsigned mSec)
  124. {
  125. #ifdef WIN32
  126. ::Sleep(mSec);
  127. #else
  128. usleep(mSec * 1000);
  129. #endif
  130. }
  131. Timer::Timer()
  132. {
  133. Reset();
  134. }
  135. unsigned Timer::GetMSec(bool reset)
  136. {
  137. #ifdef WIN32
  138. unsigned currentTime = timeGetTime();
  139. #else
  140. struct timeval time;
  141. gettimeofday(&time, NULL);
  142. unsigned currentTime = time.tv_sec * 1000 + time.tv_usec / 1000;
  143. #endif
  144. unsigned elapsedTime = currentTime - startTime_;
  145. if (reset)
  146. startTime_ = currentTime;
  147. return elapsedTime;
  148. }
  149. void Timer::Reset()
  150. {
  151. #ifdef WIN32
  152. startTime_ = timeGetTime();
  153. #else
  154. struct timeval time;
  155. gettimeofday(&time, NULL);
  156. startTime_ = time.tv_sec * 1000 + time.tv_usec / 1000;
  157. #endif
  158. }
  159. HiresTimer::HiresTimer()
  160. {
  161. Reset();
  162. }
  163. long long HiresTimer::GetUSec(bool reset)
  164. {
  165. long long currentTime;
  166. #ifdef WIN32
  167. if (supported)
  168. {
  169. LARGE_INTEGER counter;
  170. QueryPerformanceCounter(&counter);
  171. currentTime = counter.QuadPart;
  172. }
  173. else
  174. currentTime = timeGetTime();
  175. #else
  176. struct timeval time;
  177. gettimeofday(&time, NULL);
  178. currentTime = time.tv_sec * 1000000LL + time.tv_usec;
  179. #endif
  180. long long elapsedTime = currentTime - startTime_;
  181. // Correct for possible weirdness with changing internal frequency
  182. if (elapsedTime < 0)
  183. elapsedTime = 0;
  184. if (reset)
  185. startTime_ = currentTime;
  186. return (elapsedTime * 1000000LL) / frequency;
  187. }
  188. void HiresTimer::Reset()
  189. {
  190. #ifdef WIN32
  191. if (supported)
  192. {
  193. LARGE_INTEGER counter;
  194. QueryPerformanceCounter(&counter);
  195. startTime_ = counter.QuadPart;
  196. }
  197. else
  198. startTime_ = timeGetTime();
  199. #else
  200. struct timeval time;
  201. gettimeofday(&time, NULL);
  202. startTime_ = time.tv_sec * 1000000LL + time.tv_usec;
  203. #endif
  204. }
  205. }