Timer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Object.h"
  24. namespace Urho3D
  25. {
  26. /// Low-resolution operating system timer.
  27. class URHO3D_API Timer
  28. {
  29. public:
  30. /// Construct. Get the starting clock value.
  31. Timer();
  32. /// Return elapsed milliseconds and optionally reset.
  33. unsigned GetMSec(bool reset);
  34. /// Reset the timer.
  35. void Reset();
  36. private:
  37. /// Starting clock value in milliseconds.
  38. unsigned startTime_{};
  39. };
  40. /// High-resolution operating system timer used in profiling.
  41. class URHO3D_API HiresTimer
  42. {
  43. friend class Time;
  44. public:
  45. /// Construct. Get the starting high-resolution clock value.
  46. HiresTimer();
  47. /// Return elapsed microseconds and optionally reset.
  48. long long GetUSec(bool reset);
  49. /// Reset the timer.
  50. void Reset();
  51. /// Return if high-resolution timer is supported.
  52. static bool IsSupported() { return supported; }
  53. /// Return high-resolution timer frequency if supported.
  54. static long long GetFrequency() { return frequency; }
  55. private:
  56. /// Starting clock value in CPU ticks.
  57. long long startTime_{};
  58. /// High-resolution timer support flag.
  59. static bool supported;
  60. /// High-resolution timer frequency.
  61. static long long frequency;
  62. };
  63. /// %Time and frame counter subsystem.
  64. class URHO3D_API Time : public Object
  65. {
  66. URHO3D_OBJECT(Time, Object);
  67. public:
  68. /// Construct.
  69. explicit Time(Context* context);
  70. /// Destruct. Reset the low-resolution timer period if set.
  71. ~Time() override;
  72. /// Begin new frame, with (last) frame duration in seconds and send frame start event.
  73. void BeginFrame(float timeStep);
  74. /// End frame. Increment total time and send frame end event.
  75. void EndFrame();
  76. /// Set the low-resolution timer period in milliseconds. 0 resets to the default period.
  77. void SetTimerPeriod(unsigned mSec);
  78. /// Return frame number, starting from 1 once BeginFrame() is called for the first time.
  79. /// @property
  80. unsigned GetFrameNumber() const { return frameNumber_; }
  81. /// Return current frame timestep as seconds.
  82. /// @property
  83. float GetTimeStep() const { return timeStep_; }
  84. /// Return current low-resolution timer period in milliseconds.
  85. unsigned GetTimerPeriod() const { return timerPeriod_; }
  86. /// Return elapsed time from program start as seconds.
  87. /// @property
  88. float GetElapsedTime();
  89. /// Return current frames per second.
  90. /// @property
  91. float GetFramesPerSecond() const;
  92. /// Get system time as milliseconds.
  93. static unsigned GetSystemTime();
  94. /// Get system time as seconds since 1.1.1970.
  95. static unsigned GetTimeSinceEpoch();
  96. /// Get a date/time stamp as a string.
  97. static String GetTimeStamp();
  98. /// Sleep for a number of milliseconds.
  99. static void Sleep(unsigned mSec);
  100. private:
  101. /// Elapsed time since program start.
  102. Timer elapsedTime_;
  103. /// Frame number.
  104. unsigned frameNumber_;
  105. /// Timestep in seconds.
  106. float timeStep_;
  107. /// Low-resolution timer period.
  108. unsigned timerPeriod_;
  109. };
  110. }