BsWin32Timer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "BsTimer.h"
  2. #include "BsBitwise.h"
  3. #ifndef WIN32_LEAN_AND_MEAN
  4. # define WIN32_LEAN_AND_MEAN
  5. #endif
  6. #if !defined(NOMINMAX) && defined(_MSC_VER)
  7. # define NOMINMAX // Required to stop windows.h messing up std::min
  8. #endif
  9. #include "windows.h"
  10. namespace BansheeEngine
  11. {
  12. struct Timer::Data
  13. {
  14. clock_t zeroClock;
  15. LARGE_INTEGER startTime;
  16. LARGE_INTEGER frequency;
  17. };
  18. Timer::Timer()
  19. {
  20. m = bs_new<Data>();
  21. reset();
  22. }
  23. Timer::~Timer()
  24. {
  25. bs_delete(m);
  26. }
  27. void Timer::reset()
  28. {
  29. QueryPerformanceFrequency(&m->frequency);
  30. QueryPerformanceCounter(&m->startTime);
  31. m->zeroClock = clock();
  32. }
  33. unsigned long Timer::getMilliseconds()
  34. {
  35. LARGE_INTEGER curTime;
  36. QueryPerformanceCounter(&curTime);
  37. LONGLONG newTime = curTime.QuadPart - m->startTime.QuadPart;
  38. // Scale by 1000 for milliseconds
  39. unsigned long newTicks = (unsigned long)(1000 * newTime / m->frequency.QuadPart);
  40. return newTicks;
  41. }
  42. unsigned long Timer::getStartMs() const
  43. {
  44. unsigned long newTicks = (unsigned long)(1000 * m->startTime.QuadPart / m->frequency.QuadPart);
  45. return newTicks;
  46. }
  47. unsigned long Timer::getMicroseconds()
  48. {
  49. LARGE_INTEGER curTime;
  50. QueryPerformanceCounter(&curTime);
  51. LONGLONG newTime = curTime.QuadPart - m->startTime.QuadPart;
  52. // Scale by 1000000 for microseconds
  53. unsigned long newMicro = (unsigned long)(1000000 * newTime / m->frequency.QuadPart);
  54. return newMicro;
  55. }
  56. unsigned long Timer::getMillisecondsCPU()
  57. {
  58. clock_t newClock = clock();
  59. return (unsigned long)((float)(newClock - m->zeroClock) / ((float)CLOCKS_PER_SEC / 1000.0f));
  60. }
  61. unsigned long Timer::getMicrosecondsCPU()
  62. {
  63. clock_t newClock = clock();
  64. return (unsigned long)((float)(newClock - m->zeroClock) / ((float)CLOCKS_PER_SEC / 1000000.0f));
  65. }
  66. }