HighRezTimerWindows.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Util/HighRezTimer.h>
  6. #include <AnKi/Util/Assert.h>
  7. #include <AnKi/Util/Win32Minimal.h>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. namespace anki {
  11. namespace {
  12. /// A dummy struct that inits the timer
  13. class DummyInitTimer
  14. {
  15. public:
  16. LARGE_INTEGER m_start;
  17. LARGE_INTEGER m_ticksPerSec;
  18. DummyInitTimer()
  19. {
  20. if(!QueryPerformanceFrequency(&m_ticksPerSec))
  21. {
  22. fprintf(stderr, "QueryPerformanceFrequency() failed\n");
  23. abort();
  24. }
  25. QueryPerformanceCounter(&m_start);
  26. }
  27. };
  28. static DummyInitTimer g_init;
  29. } // namespace
  30. void HighRezTimer::sleep(Second sec)
  31. {
  32. Sleep(U32(sec * 1000.0));
  33. }
  34. U64 HighRezTimer::getCurrentTimeMs()
  35. {
  36. LARGE_INTEGER now;
  37. QueryPerformanceCounter(&now);
  38. now.QuadPart -= g_init.m_start.QuadPart;
  39. now.QuadPart *= 1000;
  40. now.QuadPart /= g_init.m_ticksPerSec.QuadPart;
  41. return now.QuadPart;
  42. }
  43. U64 HighRezTimer::getCurrentTimeUs()
  44. {
  45. LARGE_INTEGER now;
  46. QueryPerformanceCounter(&now);
  47. now.QuadPart -= g_init.m_start.QuadPart;
  48. now.QuadPart *= 1000000;
  49. now.QuadPart /= g_init.m_ticksPerSec.QuadPart;
  50. return now.QuadPart;
  51. }
  52. Second HighRezTimer::getCurrentTime()
  53. {
  54. return Second(getCurrentTimeUs()) / 1000000.0;
  55. }
  56. } // end namespace anki