CmTime.h 2.5 KB

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