BsTime.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /** Called every frame. Should only be called by Application. */
  65. void update();
  66. /** Multiply with time in microseconds to get a time in seconds. */
  67. static const double MICROSEC_TO_SEC;
  68. private:
  69. float mFrameDelta; /**< Frame delta in seconds */
  70. float mTimeSinceStart; /**< Time since start in seconds */
  71. UINT64 mTimeSinceStartMs;
  72. UINT64 mAppStartTime; /**< Time the application started, in microseconds */
  73. unsigned long mLastFrameTime; /**< Time since last runOneFrame call, In microseconds */
  74. std::atomic<unsigned long> mCurrentFrame;
  75. Timer* mTimer;
  76. };
  77. /** Easier way to access the Time module. */
  78. BS_UTILITY_EXPORT Time& gTime();
  79. /** @} */
  80. }