c++ get elapsed time platform independent
c++, cross-platform
Solution
I don't think there is a high resolution clock built-in C++ before C++11. If you are unable to use C++11 you have to either fix your error with glut and glew or use the platform dependent timer functions.
#include <chrono>
class Timer {
public:
Timer() {
reset();
}
void reset() {
m_timestamp = std::chrono::high_resolution_clock::now();
}
float diff() {
std::chrono::duration<float> fs = std::chrono::high_resolution_clock::now() - m_timestamp;
return fs.count();
}
private:
std::chrono::high_resolution_clock::time_point m_timestamp;
};
Problem
For a game I wanna measure the time that has passed since the last frame. I used `glutGet(GLUT_ELAPSED_TIME)` to do that. But after including glew the compiler can't find the glutGet function anymore (strange). So I need an alternative. Most sites I found so far suggest using clock in ctime but that function only measures the cpu time of the program not the real time! The time function in ctime is only accurate to seconds. I need at least millisecond accuracy. I can use C++11.