Timer.h 3.8 KB

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