b2Timer.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2011 Erin Catto http://box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <Box2D/Common/b2Timer.h>
  19. #if defined(_WIN32)
  20. float64 b2Timer::s_invFrequency = 0.0f;
  21. #define WIN32_LEAN_AND_MEAN
  22. #include <windows.h>
  23. b2Timer::b2Timer()
  24. {
  25. LARGE_INTEGER largeInteger;
  26. if (s_invFrequency == 0.0f)
  27. {
  28. QueryPerformanceFrequency(&largeInteger);
  29. s_invFrequency = float64(largeInteger.QuadPart);
  30. if (s_invFrequency > 0.0f)
  31. {
  32. s_invFrequency = 1000.0f / s_invFrequency;
  33. }
  34. }
  35. QueryPerformanceCounter(&largeInteger);
  36. m_start = float64(largeInteger.QuadPart);
  37. }
  38. void b2Timer::Reset()
  39. {
  40. LARGE_INTEGER largeInteger;
  41. QueryPerformanceCounter(&largeInteger);
  42. m_start = float64(largeInteger.QuadPart);
  43. }
  44. float32 b2Timer::GetMilliseconds() const
  45. {
  46. LARGE_INTEGER largeInteger;
  47. QueryPerformanceCounter(&largeInteger);
  48. float64 count = float64(largeInteger.QuadPart);
  49. float32 ms = float32(s_invFrequency * (count - m_start));
  50. return ms;
  51. }
  52. #elif defined(__linux__) || defined (__APPLE__)
  53. #include <sys/time.h>
  54. b2Timer::b2Timer()
  55. {
  56. Reset();
  57. }
  58. void b2Timer::Reset()
  59. {
  60. timeval t;
  61. gettimeofday(&t, 0);
  62. m_start_sec = t.tv_sec;
  63. m_start_usec = t.tv_usec;
  64. }
  65. float32 b2Timer::GetMilliseconds() const
  66. {
  67. timeval t;
  68. gettimeofday(&t, 0);
  69. return 1000.0f * (t.tv_sec - m_start_sec) + 0.001f * (t.tv_usec - m_start_usec);
  70. }
  71. #else
  72. b2Timer::b2Timer()
  73. {
  74. }
  75. void b2Timer::Reset()
  76. {
  77. }
  78. float32 b2Timer::GetMilliseconds() const
  79. {
  80. return 0.0f;
  81. }
  82. #endif