If you have chance to use C++11 and above, the best way to calculate the delta time is using std::chrono library. std::chrono library provides you a flexible collection of types that track time with varying degrees of precision.
std::chrono::steady_clock class is the best tool for measuring intervals. It represents a monotonic clock which will always be moving forward and will never decrease. std::chrono::steady_clock is not useful to tell real world time. It is only useful to measure the passage of time.It can be almost anything as long as it continues to move forward, such as the duration since the beginning of the program execution, since the computer has been turned on, an so on.
You can calculate the delta time as below:
1 2 3 4 5 |
// std::chrono::steady_clock::time_point lastUpdate; // float deltaTime auto now = std::chrono::steady_clock::now(); deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(now - lastUpdate).count() / 1000000.0f; lastUpdate = now; |