BsTime.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsModule.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Manages all time related functionality.
  8. *
  9. * @note Sim thread only unless where specified otherwise.
  10. */
  11. class BS_UTILITY_EXPORT Time : public Module<Time>
  12. {
  13. public:
  14. Time();
  15. ~Time();
  16. /**
  17. * @brief Gets the time elapsed since application start.
  18. * Only gets updated once per frame.
  19. *
  20. * @return The time since application start, in seconds.
  21. */
  22. float getTime() const { return mTimeSinceStart; }
  23. /**
  24. * @brief Gets the time elapsed since application start.
  25. * Only gets updated once per frame.
  26. *
  27. * @return The time since application start, in miliseconds.
  28. */
  29. UINT64 getTimeMs() const { return mTimeSinceStartMs; }
  30. /**
  31. * @brief Gets the time since last frame was executed.
  32. * Only gets updated once per frame.
  33. *
  34. * @return Time since last frame was executed, in seconds.
  35. */
  36. float getFrameDelta() const { return mFrameDelta; }
  37. /**
  38. * @brief Returns the sequential index of the current frame. First frame is 0.
  39. *
  40. * @return The current frame.
  41. *
  42. * @note Thread safe, but only counts sim thread frames.
  43. */
  44. UINT64 getFrameIdx() const { return mCurrentFrame.load(); }
  45. /**
  46. * @brief Returns the precise time since application start, in microseconds.
  47. * Unlike other time methods this is not only updated every frame,
  48. * but will return exact time at the moment it is called.
  49. *
  50. * @note You will generally only want to use this for performance measurements and similar.
  51. * Use non-precise methods in majority of code as it is useful to keep the time value equal
  52. * in all methods during a single frame.
  53. *
  54. * @return Time in microseconds.
  55. */
  56. UINT64 getTimePrecise() const;
  57. /**
  58. * @brief Gets the time at which the application was started, counting
  59. * from system start.
  60. *
  61. * @return The time since system to application start, in milliseconds.
  62. */
  63. UINT64 getStartTimeMs() const { return mAppStartTime; }
  64. /**
  65. * @brief Called every frame. Should only be called by Application.
  66. */
  67. void update();
  68. /**
  69. * @brief Multiply with time in microseconds to get a time in seconds.
  70. */
  71. static const double MICROSEC_TO_SEC;
  72. private:
  73. float mFrameDelta; /**< Frame delta in seconds */
  74. float mTimeSinceStart; /**< Time since start in seconds */
  75. UINT64 mTimeSinceStartMs;
  76. UINT64 mAppStartTime; /**< Time the application started, in microseconds */
  77. unsigned long mLastFrameTime; /**< Time since last runOneFrame call, In microseconds */
  78. std::atomic<unsigned long> mCurrentFrame;
  79. Timer* mTimer;
  80. };
  81. BS_UTILITY_EXPORT Time& gTime();
  82. }