CmTime.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "CmTime.h"
  2. #include "CmTimer.h"
  3. namespace CamelotFramework
  4. {
  5. const double Time::MICROSEC_TO_SEC = 1.0/1000000.0;
  6. Time::Time()
  7. :mAppStartTime(0), mLastFrameTime(0), mFrameDelta(0.0f), mTimeSinceStart(0.0f), mCurrentFrame(0)
  8. {
  9. mTimer = cm_new<Timer>();
  10. mAppStartTime = mTimer->getMicroseconds();
  11. }
  12. Time::~Time()
  13. {
  14. cm_delete(mTimer);
  15. }
  16. void Time::update()
  17. {
  18. unsigned long currentFrameTime = mTimer->getMicroseconds();
  19. mFrameDelta = (float)((currentFrameTime - mLastFrameTime) * MICROSEC_TO_SEC);
  20. mTimeSinceStart = (float)(currentFrameTime * MICROSEC_TO_SEC);
  21. mTimeSinceStartMs = (UINT64)(currentFrameTime / 1000);
  22. mLastFrameTime = currentFrameTime;
  23. mCurrentFrame++;
  24. }
  25. UINT64 Time::getTimePrecise() const
  26. {
  27. // TODO Low priority - Timer internally calls high performance OS specific methods. We can go a step further and use CPU specific instructions, which would
  28. // (likely) give even more precise measurements in cycles. (RDTSC instruction - although that might not be valid with todays variable CPU clocks)
  29. return mTimer->getMicroseconds();
  30. }
  31. Time& gTime()
  32. {
  33. return Time::instance();
  34. }
  35. }