BsWin32Timer.cpp 2.1 KB

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