HighRezTimerWindows.cpp 1.1 KB

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