b2Timer.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2011 Erin Catto http://box2d.org
  3. * Copyright (c) 2014 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include <Box2D/Common/b2Timer.h>
  20. #if defined(_WIN32)
  21. float64 b2Timer::s_invFrequency = 0.0f;
  22. #define WIN32_LEAN_AND_MEAN
  23. #include <windows.h>
  24. typedef BOOL (WINAPI *SystemGetTimeFunc)(_Out_ LARGE_INTEGER *lpFrequency);
  25. SystemGetTimeFunc systemGetTimeFunc = ::QueryPerformanceCounter;
  26. SystemGetTimeFunc systemGetFreqFunc = ::QueryPerformanceFrequency;
  27. int64 b2Timer::GetTicks()
  28. {
  29. LARGE_INTEGER largeInteger;
  30. systemGetTimeFunc(&largeInteger);
  31. return largeInteger.QuadPart;
  32. }
  33. b2Timer::b2Timer()
  34. {
  35. LARGE_INTEGER largeInteger;
  36. if (s_invFrequency == 0.0f)
  37. {
  38. systemGetFreqFunc(&largeInteger);
  39. s_invFrequency = float64(largeInteger.QuadPart);
  40. if (s_invFrequency > 0.0f)
  41. {
  42. s_invFrequency = 1000.0f / s_invFrequency;
  43. }
  44. }
  45. m_start = GetTicks();
  46. }
  47. void b2Timer::Reset()
  48. {
  49. m_start = GetTicks();
  50. }
  51. float32 b2Timer::GetMilliseconds() const
  52. {
  53. int64 elapsed = GetTicks() - m_start;
  54. return (float32)(s_invFrequency * elapsed);
  55. }
  56. #elif defined(__linux__) || defined (__APPLE__)
  57. #include <sys/time.h>
  58. #include <time.h>
  59. // systemGetTimeFunc is defined with external linkage to allow unit
  60. // test to mock out the system time function
  61. #if defined(__linux__)
  62. typedef int (*SystemGetTimeFunc)(clockid_t clk_id, struct timespec *tp);
  63. SystemGetTimeFunc systemGetTimeFunc = ::clock_gettime;
  64. #elif defined(__APPLE__)
  65. typedef int (*SystemGetTimeFunc)(struct timeval * tp, void * tzp);
  66. SystemGetTimeFunc systemGetTimeFunc = ::gettimeofday;
  67. #endif
  68. int64 b2Timer::GetTicks()
  69. {
  70. static const int NSEC_PER_SEC = 1000000000;
  71. #ifdef __linux__
  72. timespec ts;
  73. systemGetTimeFunc(CLOCK_MONOTONIC,&ts);
  74. return ((int64)ts.tv_sec) * NSEC_PER_SEC + ts.tv_nsec;
  75. #else
  76. timeval t;
  77. systemGetTimeFunc(&t, 0);
  78. return ((int64)t.tv_sec) * NSEC_PER_SEC + t.tv_usec * 1000;
  79. #endif
  80. }
  81. b2Timer::b2Timer()
  82. {
  83. Reset();
  84. }
  85. void b2Timer::Reset()
  86. {
  87. m_start = GetTicks();
  88. }
  89. float32 b2Timer::GetMilliseconds() const
  90. {
  91. static const float32 kTicksToMs = 0.000001f;
  92. return kTicksToMs * (float32)(GetTicks() - m_start);
  93. }
  94. #else
  95. b2Timer::b2Timer()
  96. {
  97. }
  98. void b2Timer::Reset()
  99. {
  100. }
  101. float32 b2Timer::GetMilliseconds() const
  102. {
  103. return 0.0f;
  104. }
  105. #endif