Timer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "Timer.h"
  26. #include <Windows.h>
  27. #include <MMSystem.h>
  28. #include "DebugNew.h"
  29. bool HiresTimer::supported(false);
  30. long long HiresTimer::frequency(1000);
  31. OBJECTTYPESTATIC(Time);
  32. Time::Time(Context* context) :
  33. Object(context),
  34. frameNumber_(0),
  35. timeStep_(0.0f),
  36. timeStepMSec_(0),
  37. totalMSec_(0),
  38. timerPeriod_(0)
  39. {
  40. LARGE_INTEGER frequency;
  41. if (QueryPerformanceFrequency(&frequency))
  42. {
  43. HiresTimer::frequency = frequency.QuadPart;
  44. HiresTimer::supported = true;
  45. }
  46. }
  47. Time::~Time()
  48. {
  49. SetTimerPeriod(0);
  50. }
  51. void Time::BeginFrame(unsigned mSec)
  52. {
  53. ++frameNumber_;
  54. if (!frameNumber_)
  55. ++frameNumber_;
  56. timeStep_ = (float)mSec / 1000.0f;
  57. timeStepMSec_ = mSec;
  58. {
  59. // Frame begin event
  60. using namespace BeginFrame;
  61. VariantMap eventData;
  62. eventData[P_FRAMENUMBER] = frameNumber_;
  63. eventData[P_TIMESTEP] = timeStep_;
  64. SendEvent(E_BEGINFRAME, eventData);
  65. }
  66. {
  67. // Logic update event
  68. using namespace Update;
  69. VariantMap eventData;
  70. eventData[P_TIMESTEP] = timeStep_;
  71. SendEvent(E_UPDATE, eventData);
  72. // Logic post-update event
  73. SendEvent(E_POSTUPDATE, eventData);
  74. // Rendering update event
  75. SendEvent(E_RENDERUPDATE, eventData);
  76. // Post-render update event
  77. SendEvent(E_POSTRENDERUPDATE, eventData);
  78. }
  79. }
  80. void Time::EndFrame()
  81. {
  82. totalMSec_ += timeStepMSec_;
  83. // Frame end event
  84. SendEvent(E_ENDFRAME);
  85. }
  86. void Time::SetTimerPeriod(unsigned mSec)
  87. {
  88. if (timerPeriod_ > 0)
  89. timeEndPeriod(timerPeriod_);
  90. timerPeriod_ = mSec;
  91. if (timerPeriod_ > 0)
  92. timeBeginPeriod(timerPeriod_);
  93. }
  94. Timer::Timer()
  95. {
  96. Reset();
  97. }
  98. unsigned Timer::GetMSec(bool reset)
  99. {
  100. unsigned currentTime = timeGetTime();
  101. unsigned elapsedTime = currentTime - startTime_;
  102. if (reset)
  103. startTime_ = currentTime;
  104. return elapsedTime;
  105. }
  106. void Timer::Reset()
  107. {
  108. startTime_ = timeGetTime();
  109. }
  110. HiresTimer::HiresTimer()
  111. {
  112. Reset();
  113. }
  114. long long HiresTimer::GetUSec(bool reset)
  115. {
  116. long long currentTime;
  117. if (supported)
  118. {
  119. LARGE_INTEGER counter;
  120. QueryPerformanceCounter(&counter);
  121. currentTime = counter.QuadPart;
  122. }
  123. else
  124. currentTime = timeGetTime();
  125. long long elapsedTime = currentTime - startTime_;
  126. // Correct for possible weirdness with changing internal frequency
  127. if (elapsedTime < 0)
  128. elapsedTime = 0;
  129. if (reset)
  130. startTime_ = currentTime;
  131. return (elapsedTime * 1000000LL) / frequency;
  132. }
  133. void HiresTimer::Reset()
  134. {
  135. if (supported)
  136. {
  137. LARGE_INTEGER counter;
  138. QueryPerformanceCounter(&counter);
  139. startTime_ = counter.QuadPart;
  140. }
  141. else
  142. startTime_ = timeGetTime();
  143. }