HighRezTimerWindows.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2009-2021, 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. namespace anki {
  10. namespace {
  11. /// A dummy struct that inits the timer
  12. class DummyInitTimer
  13. {
  14. public:
  15. LARGE_INTEGER m_start;
  16. LARGE_INTEGER m_ticksPerSec;
  17. DummyInitTimer()
  18. {
  19. if(!QueryPerformanceFrequency(&m_ticksPerSec))
  20. {
  21. fprintf(stderr, "QueryPerformanceFrequency() failed\n");
  22. abort();
  23. }
  24. QueryPerformanceCounter(&m_start);
  25. }
  26. };
  27. DummyInitTimer init;
  28. } // namespace
  29. static U64 getMs()
  30. {
  31. LARGE_INTEGER now;
  32. QueryPerformanceCounter(&now);
  33. now.QuadPart -= init.m_start.QuadPart;
  34. now.QuadPart *= 1000;
  35. now.QuadPart /= init.m_ticksPerSec.QuadPart;
  36. return now.QuadPart;
  37. }
  38. void HighRezTimer::sleep(Second sec)
  39. {
  40. Sleep(U32(sec * 1000.0));
  41. }
  42. Second HighRezTimer::getCurrentTime()
  43. {
  44. // Second(ticks) / 1000.0
  45. return Second(getMs()) * 0.001;
  46. }
  47. } // end namespace anki