BasicTimer.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #pragma once
  11. #include <wrl.h>
  12. // Helper class for basic timing.
  13. ref class BasicTimer sealed
  14. {
  15. public:
  16. // Initializes internal timer values.
  17. BasicTimer()
  18. {
  19. if (!QueryPerformanceFrequency(&m_frequency))
  20. {
  21. throw ref new Platform::FailureException();
  22. }
  23. Reset();
  24. }
  25. // Reset the timer to initial values.
  26. void Reset()
  27. {
  28. Update();
  29. m_startTime = m_currentTime;
  30. m_total = 0.0f;
  31. m_delta = 1.0f / 60.0f;
  32. }
  33. // Update the timer's internal values.
  34. void Update()
  35. {
  36. if (!QueryPerformanceCounter(&m_currentTime))
  37. {
  38. throw ref new Platform::FailureException();
  39. }
  40. m_total = static_cast<float>(
  41. static_cast<double>(m_currentTime.QuadPart - m_startTime.QuadPart) /
  42. static_cast<double>(m_frequency.QuadPart)
  43. );
  44. if (m_lastTime.QuadPart == m_startTime.QuadPart)
  45. {
  46. // If the timer was just reset, report a time delta equivalent to 60Hz frame time.
  47. m_delta = 1.0f / 60.0f;
  48. }
  49. else
  50. {
  51. m_delta = static_cast<float>(
  52. static_cast<double>(m_currentTime.QuadPart - m_lastTime.QuadPart) /
  53. static_cast<double>(m_frequency.QuadPart)
  54. );
  55. }
  56. m_lastTime = m_currentTime;
  57. }
  58. // Duration in seconds between the last call to Reset() and the last call to Update().
  59. property float Total
  60. {
  61. float get() { return m_total; }
  62. }
  63. // Duration in seconds between the previous two calls to Update().
  64. property float Delta
  65. {
  66. float get() { return m_delta; }
  67. }
  68. private:
  69. LARGE_INTEGER m_frequency;
  70. LARGE_INTEGER m_currentTime;
  71. LARGE_INTEGER m_startTime;
  72. LARGE_INTEGER m_lastTime;
  73. float m_total;
  74. float m_delta;
  75. };
粤ICP备19079148号