BsTime.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 getFrameNumber() 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. /**
  67. * @brief Multiply with time in microseconds to get a time in seconds.
  68. */
  69. static const double MICROSEC_TO_SEC;
  70. private:
  71. float mFrameDelta; /**< Frame delta in seconds */
  72. float mTimeSinceStart; /**< Time since start in seconds */
  73. UINT64 mTimeSinceStartMs;
  74. UINT64 mAppStartTime; /**< Time the application started, in microseconds */
  75. unsigned long mLastFrameTime; /**< Time since last runOneFrame call, In microseconds */
  76. unsigned long mCurrentFrame;
  77. Timer* mTimer;
  78. };
  79. BS_UTILITY_EXPORT Time& gTime();
  80. }