1 #pragma once 2 3 #include <wrl.h> 4 5 // Helper class for basic timing. 6 ref class BasicTimer sealed 7 { 8 public: 9 // Initializes internal timer values. 10 BasicTimer() 11 { 12 if (!QueryPerformanceFrequency(&m_frequency)) 13 { 14 throw ref new Platform::FailureException(); 15 } 16 Reset(); 17 } 18 19 // Reset the timer to initial values. 20 void Reset() 21 { 22 Update(); 23 m_startTime = m_currentTime; 24 m_total = 0.0f; 25 m_delta = 1.0f / 60.0f; 26 } 27 28 // Update the timer's internal values. 29 void Update() 30 { 31 if (!QueryPerformanceCounter(&m_currentTime)) 32 { 33 throw ref new Platform::FailureException(); 34 } 35 36 m_total = static_cast<float>( 37 static_cast<double>(m_currentTime.QuadPart - m_startTime.QuadPart) / 38 static_cast<double>(m_frequency.QuadPart) 39 ); 40 41 if (m_lastTime.QuadPart == m_startTime.QuadPart) 42 { 43 // If the timer was just reset, report a time delta equivalent to 60Hz frame time. 44 m_delta = 1.0f / 60.0f; 45 } 46 else 47 { 48 m_delta = static_cast<float>( 49 static_cast<double>(m_currentTime.QuadPart - m_lastTime.QuadPart) / 50 static_cast<double>(m_frequency.QuadPart) 51 ); 52 } 53 54 m_lastTime = m_currentTime; 55 } 56 57 // Duration in seconds between the last call to Reset() and the last call to Update(). 58 property float Total 59 { 60 float get() { return m_total; } 61 } 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 69 private: 70 LARGE_INTEGER m_frequency; 71 LARGE_INTEGER m_currentTime; 72 LARGE_INTEGER m_startTime; 73 LARGE_INTEGER m_lastTime; 74 float m_total; 75 float m_delta; 76 }; 77