4

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.

relgukxilef
  • 343
  • 6
  • 13
  • Note: you should be able to use glew and glut together, there is a note about it [here](http://glew.sourceforge.net/install.html). Just make sure to include glew before glut. – Boris Dalstein Jul 15 '13 at 23:04
  • possible duplicate of [C++ Cross-Platform High-Resolution Timer](http://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer) – Ben Voigt Jul 15 '13 at 23:26
  • @Boris I made sure to include glew before glut. – relgukxilef Jul 15 '13 at 23:33

2 Answers2

5

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;
};
typ1232
  • 5,535
  • 6
  • 35
  • 51
  • Some platforms (Win32) will require writing your own clock class because the library one is broken (very poor resolution). But sticking to the C++11 interface at least will minimize the non-portable code. – Ben Voigt Jul 15 '13 at 23:23
  • [Here is such an implementation](http://stackoverflow.com/a/13266477/103167) and [another](http://stackoverflow.com/a/5524138/103167) – Ben Voigt Jul 15 '13 at 23:25
  • @BenVoigt I never had any problems with QueryPerformanceFrequency/QueryPerformanceCounter. Especially for a game there should be no problem. Very interesting bug and more interesting "fix" though! – typ1232 Jul 15 '13 at 23:26
  • `QueryPerformanceCounter` is the non-broken clock (but also non-portable). Visual C++'s `high_resolution_clock` uses the system tick which is ~15ms -- that's broken. See the linked answers for more information and the bug report. – Ben Voigt Jul 15 '13 at 23:28
  • You should really not rely on high_resolution_clock unless high_resolution_clock::is_steady() == true for duration calculation. – Iwan Aucamp Jul 18 '13 at 22:49
2
  1. Boost provides std::chrono like clocks: boost::chrono
  2. You should consider using std::chrono::steady_clock (or boost equivalent) as opposed to std::chrono::high_resolution_clock - or at least ensure std::chrono::steady_clock::is_steady() == true - if you want to use it to calculate duration, as the time returned by a non-steady clock might even decrease as physical time moves forward.
Iwan Aucamp
  • 1,469
  • 20
  • 21