Timer.h 3.8 KB

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