BsTime.h 2.8 KB

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