Timer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. Send frame start event, then the update events
  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. private:
  51. /// Frame number
  52. unsigned frameNumber_;
  53. /// Timestep in seconds
  54. float timeStep_;
  55. /// Timestep in milliseconds
  56. unsigned timeStepMSec_;
  57. /// Total elapsed time in milliseconds
  58. unsigned totalMSec_;
  59. /// Low-resolution timer period
  60. unsigned timerPeriod_;
  61. };
  62. /// Low-resolution operating system timer
  63. class Timer
  64. {
  65. public:
  66. /// Construct. Get the starting clock value
  67. Timer();
  68. /// Return elapsed milliseconds and optionally reset
  69. unsigned GetMSec(bool reset);
  70. /// Reset the timer
  71. void Reset();
  72. private:
  73. /// Starting clock value in milliseconds
  74. unsigned startTime_;
  75. };
  76. /// High-resolution operating system timer used in profiling
  77. class HiresTimer
  78. {
  79. friend class Time;
  80. public:
  81. /// Construct. Get the starting high-resolution clock value
  82. HiresTimer();
  83. /// Return elapsed microseconds and optionally reset
  84. long long GetUSec(bool reset);
  85. /// Reset the timer
  86. void Reset();
  87. /// Return if high-resolution timer is supported
  88. static bool IsSupported() { return supported; }
  89. /// Return high-resolution timer frequency if supported
  90. static long long GetFrequency() { return frequency; }
  91. private:
  92. /// Starting clock value in CPU ticks
  93. long long startTime_;
  94. /// High-resolution timer support flag
  95. static bool supported;
  96. /// High-resolution timer frequency
  97. static long long frequency;
  98. };