Timer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #pragma once
  24. #include "Object.h"
  25. /// %Time and frame counter subsystem.
  26. class Time : public Object
  27. {
  28. OBJECT(Time);
  29. public:
  30. /// Construct.
  31. Time(Context* context);
  32. /// Destruct. Reset the low-resolution timer period if set.
  33. virtual ~Time();
  34. /// Begin new frame, with (last) frame duration in milliseconds and send frame start event.
  35. void BeginFrame(unsigned mSec);
  36. /// End frame. Increment total time and send frame end event.
  37. void EndFrame();
  38. /// %Set the low-resolution timer period in milliseconds. 0 resets to the default period.
  39. void SetTimerPeriod(unsigned mSec);
  40. /// Return frame number, starting from 1 once BeginFrame() is called for the first time.
  41. unsigned GetFrameNumber() const { return frameNumber_; }
  42. /// Return current frame timestep as seconds.
  43. float GetTimeStep() const { return timeStep_; }
  44. /// Return current frame timestep as milliseconds.
  45. unsigned GetTimeStepMSec() const { return timeStepMSec_; }
  46. /// Return total elapsed time of frames in milliseconds.
  47. unsigned GetTotalMSec() const { return totalMSec_; }
  48. /// Return current low-resolution timer period in milliseconds.
  49. unsigned GetTimerPeriod() const { return timerPeriod_; }
  50. /// Sleep for a number of milliseconds.
  51. static void Sleep(unsigned mSec);
  52. private:
  53. /// Frame number.
  54. unsigned frameNumber_;
  55. /// Timestep in seconds.
  56. float timeStep_;
  57. /// Timestep in milliseconds.
  58. unsigned timeStepMSec_;
  59. /// Total elapsed time in milliseconds.
  60. unsigned totalMSec_;
  61. /// Low-resolution timer period.
  62. unsigned timerPeriod_;
  63. };
  64. /// Low-resolution operating system timer.
  65. class Timer
  66. {
  67. public:
  68. /// Construct. Get the starting clock value.
  69. Timer();
  70. /// Return elapsed milliseconds and optionally reset.
  71. unsigned GetMSec(bool reset);
  72. /// Reset the timer.
  73. void Reset();
  74. private:
  75. /// Starting clock value in milliseconds.
  76. unsigned startTime_;
  77. };
  78. /// High-resolution operating system timer used in profiling.
  79. class HiresTimer
  80. {
  81. friend class Time;
  82. public:
  83. /// Construct. Get the starting high-resolution clock value.
  84. HiresTimer();
  85. /// Return elapsed microseconds and optionally reset.
  86. long long GetUSec(bool reset);
  87. /// Reset the timer.
  88. void Reset();
  89. /// Return if high-resolution timer is supported.
  90. static bool IsSupported() { return supported; }
  91. /// Return high-resolution timer frequency if supported.
  92. static long long GetFrequency() { return frequency; }
  93. private:
  94. /// Starting clock value in CPU ticks.
  95. long long startTime_;
  96. /// High-resolution timer support flag.
  97. static bool supported;
  98. /// High-resolution timer frequency.
  99. static long long frequency;
  100. };