BsTime.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. #include "BsModule.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup General
  9. * @{
  10. */
  11. /**
  12. * Manages all time related functionality.
  13. *
  14. * @note Sim thread only unless where specified otherwise.
  15. */
  16. class BS_UTILITY_EXPORT Time : public Module<Time>
  17. {
  18. public:
  19. Time();
  20. ~Time();
  21. /**
  22. * Gets the time elapsed since application start. Only gets updated once per frame.
  23. *
  24. * @return The time since application start, in seconds.
  25. */
  26. float getTime() const { return mTimeSinceStart; }
  27. /**
  28. * Gets the time elapsed since application start. 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. * Gets the time since last frame was executed. Only gets updated once per frame.
  35. *
  36. * @return Time since last frame was executed, in seconds.
  37. */
  38. float getFrameDelta() const { return mFrameDelta; }
  39. /**
  40. * Returns the sequential index of the current frame. First frame is 0.
  41. *
  42. * @return The current frame.
  43. *
  44. * @note Thread safe, but only counts sim thread frames.
  45. */
  46. UINT64 getFrameIdx() const { return mCurrentFrame.load(); }
  47. /**
  48. * Returns the precise time since application start, in microseconds. Unlike other time methods this is not only
  49. * updated every frame, but will return exact time at the moment it is called.
  50. *
  51. * @return Time in microseconds.
  52. *
  53. * @note
  54. * You will generally only want to use this for performance measurements and similar. Use non-precise methods in
  55. * majority of code as it is useful to keep the time value equal in all methods during a single frame.
  56. */
  57. UINT64 getTimePrecise() const;
  58. /**
  59. * Gets the time at which the application was started, counting from system start.
  60. *
  61. * @return The time since system to application start, in milliseconds.
  62. */
  63. UINT64 getStartTimeMs() const { return mAppStartTime; }
  64. /** @name Internal
  65. * @{
  66. */
  67. /** Called every frame. Should only be called by Application. */
  68. void _update();
  69. /** @} */
  70. /** Multiply with time in microseconds to get a time in seconds. */
  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. UINT64 mLastFrameTime; /**< Time since last runOneFrame call, In microseconds */
  78. std::atomic<unsigned long> mCurrentFrame;
  79. Timer* mTimer;
  80. };
  81. /** Easier way to access the Time module. */
  82. BS_UTILITY_EXPORT Time& gTime();
  83. /** @} */
  84. }