Timer.cpp 5.5 KB

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