BsTime.h 2.7 KB

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