CmTime.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Called every frame. Should only be called by Application.
  55. */
  56. void update();
  57. private:
  58. float mFrameDelta; // Frame delta in seconds
  59. float mTimeSinceStart; // Time since start in seconds
  60. UINT64 mTimeSinceStartMs;
  61. unsigned long mAppStartTime; // Time the application started, in microseconds
  62. unsigned long mLastFrameTime; // Time since last runOneFrame call, In microseconds
  63. unsigned long mCurrentFrame;
  64. Timer* mTimer;
  65. static const double MICROSEC_TO_SEC;
  66. };
  67. CM_UTILITY_EXPORT Time& gTime();
  68. }