Timer.cpp 5.5 KB

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