The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.3KB

  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 "b2Timer.h"
  19. #if defined(_WIN32)
  20. float64 b2Timer::s_invFrequency = 0.0f;
  21. #include <windows.h>
  22. b2Timer::b2Timer()
  23. {
  24. LARGE_INTEGER largeInteger;
  25. if (s_invFrequency == 0.0f)
  26. {
  27. QueryPerformanceFrequency(&largeInteger);
  28. s_invFrequency = float64(largeInteger.QuadPart);
  29. if (s_invFrequency > 0.0f)
  30. {
  31. s_invFrequency = 1000.0f / s_invFrequency;
  32. }
  33. }
  34. QueryPerformanceCounter(&largeInteger);
  35. m_start = float64(largeInteger.QuadPart);
  36. }
  37. void b2Timer::Reset()
  38. {
  39. LARGE_INTEGER largeInteger;
  40. QueryPerformanceCounter(&largeInteger);
  41. m_start = float64(largeInteger.QuadPart);
  42. }
  43. float32 b2Timer::GetMilliseconds() const
  44. {
  45. LARGE_INTEGER largeInteger;
  46. QueryPerformanceCounter(&largeInteger);
  47. float64 count = float64(largeInteger.QuadPart);
  48. float32 ms = float32(s_invFrequency * (count - m_start));
  49. return ms;
  50. }
  51. #elif defined(__linux__) || defined (__APPLE__)
  52. #include <sys/time.h>
  53. b2Timer::b2Timer()
  54. {
  55. Reset();
  56. }
  57. void b2Timer::Reset()
  58. {
  59. timeval t;
  60. gettimeofday(&t, 0);
  61. m_start_sec = t.tv_sec;
  62. m_start_msec = t.tv_usec * 0.001f;
  63. }
  64. float32 b2Timer::GetMilliseconds() const
  65. {
  66. timeval t;
  67. gettimeofday(&t, 0);
  68. return (t.tv_sec - m_start_sec) * 1000 + t.tv_usec * 0.001f - m_start_msec;
  69. }
  70. #else
  71. b2Timer::b2Timer()
  72. {
  73. }
  74. void b2Timer::Reset()
  75. {
  76. }
  77. float32 b2Timer::GetMilliseconds() const
  78. {
  79. return 0.0f;
  80. }
  81. #endif