Timer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/Object.h"
  5. namespace Urho3D
  6. {
  7. /// Low-resolution operating system timer.
  8. class URHO3D_API Timer
  9. {
  10. public:
  11. /// Construct. Get the starting clock value.
  12. Timer();
  13. /// Return elapsed milliseconds and optionally reset.
  14. unsigned GetMSec(bool reset);
  15. /// Reset the timer.
  16. void Reset();
  17. private:
  18. /// Starting clock value in milliseconds.
  19. unsigned startTime_{};
  20. };
  21. /// High-resolution operating system timer used in profiling.
  22. class URHO3D_API HiresTimer
  23. {
  24. friend class Time;
  25. public:
  26. /// Construct. Get the starting high-resolution clock value.
  27. HiresTimer();
  28. /// Return elapsed microseconds and optionally reset.
  29. long long GetUSec(bool reset);
  30. /// Reset the timer.
  31. void Reset();
  32. /// Return if high-resolution timer is supported.
  33. static bool IsSupported() { return supported; }
  34. /// Return high-resolution timer frequency if supported.
  35. static long long GetFrequency() { return frequency; }
  36. private:
  37. /// Starting clock value in CPU ticks.
  38. long long startTime_{};
  39. /// High-resolution timer support flag.
  40. static bool supported;
  41. /// High-resolution timer frequency.
  42. static long long frequency;
  43. };
  44. /// %Time and frame counter subsystem.
  45. class URHO3D_API Time : public Object
  46. {
  47. URHO3D_OBJECT(Time, Object);
  48. public:
  49. /// Construct.
  50. explicit Time(Context* context);
  51. /// Destruct. Reset the low-resolution timer period if set.
  52. ~Time() override;
  53. /// Begin new frame, with (last) frame duration in seconds and send frame start event.
  54. void BeginFrame(float timeStep);
  55. /// End frame. Increment total time and send frame end event.
  56. void EndFrame();
  57. /// Set the low-resolution timer period in milliseconds. 0 resets to the default period.
  58. void SetTimerPeriod(unsigned mSec);
  59. /// Return frame number, starting from 1 once BeginFrame() is called for the first time.
  60. /// @property
  61. i32 GetFrameNumber() const { return frameNumber_; }
  62. /// Return current frame timestep as seconds.
  63. /// @property
  64. float GetTimeStep() const { return timeStep_; }
  65. /// Return current low-resolution timer period in milliseconds.
  66. unsigned GetTimerPeriod() const { return timerPeriod_; }
  67. /// Return elapsed time from program start as seconds.
  68. /// @property
  69. float GetElapsedTime();
  70. /// Return current frames per second.
  71. /// @property
  72. float GetFramesPerSecond() const;
  73. /// Get system time as milliseconds.
  74. static unsigned GetSystemTime();
  75. /// Get system time as seconds since 1.1.1970.
  76. static unsigned GetTimeSinceEpoch();
  77. /// Get a date/time stamp as a string.
  78. static String GetTimeStamp();
  79. /// Sleep for a number of milliseconds.
  80. static void Sleep(unsigned mSec);
  81. private:
  82. /// Elapsed time since program start.
  83. Timer elapsedTime_;
  84. /// Frame number.
  85. i32 frameNumber_;
  86. /// Timestep in seconds.
  87. float timeStep_;
  88. /// Low-resolution timer period.
  89. unsigned timerPeriod_;
  90. };
  91. }