BsTime.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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
  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 number of the current frame. First frame is 0.
  39. *
  40. * @return The current frame.
  41. */
  42. unsigned long getCurrentFrameNumber() const { return mCurrentFrame; }
  43. /**
  44. * @brief Returns the precise time since application start, in microseconds.
  45. * Unlike other getTime methods this time is not only updated every frame,
  46. * but will return exact time at the moment it is called.
  47. *
  48. * @note You will generally only want to use this for performance measurements and similar.
  49. * Use non-precise methods in majority of code as it is useful to keep the time value equal
  50. * in all methods during a single frame.
  51. *
  52. * @return Time in microseconds.
  53. */
  54. UINT64 getTimePrecise() const;
  55. /**
  56. * @brief Gets the time at which the application was started, counting
  57. * from system start.
  58. *
  59. * @return The time since system to application start, in milliseconds.
  60. */
  61. UINT64 getStartTimeMs() const { return mAppStartTime; }
  62. /**
  63. * @brief Called every frame. Should only be called by Application.
  64. */
  65. void update();
  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. unsigned long mCurrentFrame;
  73. Timer* mTimer;
  74. static const double MICROSEC_TO_SEC;
  75. };
  76. BS_UTILITY_EXPORT Time& gTime();
  77. }