BsTimer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsTimer.h"
  5. #include "BsBitwise.h"
  6. namespace BansheeEngine
  7. {
  8. Timer::Timer()
  9. {
  10. reset();
  11. }
  12. Timer::~Timer()
  13. {
  14. }
  15. void Timer::reset()
  16. {
  17. QueryPerformanceFrequency(&mFrequency);
  18. QueryPerformanceCounter(&mStartTime);
  19. mZeroClock = clock();
  20. }
  21. unsigned long Timer::getMilliseconds()
  22. {
  23. LARGE_INTEGER curTime;
  24. QueryPerformanceCounter(&curTime);
  25. LONGLONG newTime = curTime.QuadPart - mStartTime.QuadPart;
  26. // Scale by 1000 for milliseconds
  27. unsigned long newTicks = (unsigned long) (1000 * newTime / mFrequency.QuadPart);
  28. return newTicks;
  29. }
  30. unsigned long Timer::getStartMs() const
  31. {
  32. unsigned long newTicks = (unsigned long) (1000 * mStartTime.QuadPart / mFrequency.QuadPart);
  33. return newTicks;
  34. }
  35. unsigned long Timer::getMicroseconds()
  36. {
  37. LARGE_INTEGER curTime;
  38. QueryPerformanceCounter(&curTime);
  39. LONGLONG newTime = curTime.QuadPart - mStartTime.QuadPart;
  40. // Scale by 1000000 for microseconds
  41. unsigned long newMicro = (unsigned long) (1000000 * newTime / mFrequency.QuadPart);
  42. return newMicro;
  43. }
  44. unsigned long Timer::getMillisecondsCPU()
  45. {
  46. clock_t newClock = clock();
  47. return (unsigned long)((float)(newClock - mZeroClock) / ((float)CLOCKS_PER_SEC / 1000.0f));
  48. }
  49. unsigned long Timer::getMicrosecondsCPU()
  50. {
  51. clock_t newClock = clock();
  52. return (unsigned long)((float)(newClock - mZeroClock) / ((float)CLOCKS_PER_SEC / 1000000.0f));
  53. }
  54. }