Locking the frame rate.

Started by
2 comments, last by nickelplate 20 years, 6 months ago
How do you go about locking the frame rate under Linux? I used the gettimeofday() function, but apparently, there is something missing. Here is my code. bool LockFrameRate() { timeval tval; static double lastTime = 0.0; double currentTime; gettimeofday(&tval, NULL); // Convert the current time in seconds currentTime = (double)tval.tv_sec + (double)tval.tv_usec*0.000001; // Check if (1.0/FRAME_RATE) seconds have elapsed since the last time lastTime was set if ((currentTime - lastTime) > (1.0/FRAME_RATE)) { cout << "Frame rate unlocked" << endl; lastTime = currentTime; return false; } cout << "Frame rate locked" << endl; return true; } The function returns false when it''s ok to draw. I use it like this at the end of my displayUpdate() function void displayUpdate() { ...... while(LockFrameRate() == true); } Any help is appreciated. "Don''t hate me, it''s just my opinion."
Advertisement
insert some debug info. output what 1.0/FRAME_RATE is. check what currentTime and lastTime are.

in that function. output every time it loops. see what happens.

Oh, and what do you mean besides "it doesn''t work"?
currentTime and lastTime always have the same values, i.e. currentTime==lastTime every time the function is called. So my approach is probably wrong, or the gettimeofday() function might not be the best way to go at it.

It''s weird, because I use the exact same logic under Windows, the only difference is that I use GetTickCount() instead of gettimeofday().

Any suggestion?



"Don''t hate me, it''s just my opinion."
msec = millisecond = second/1000
usec = microsecond = second/1000000
Therefore, to get the time in msec since the Epoch:

gettimeofday(&tv, NULL);
msec = tv.sec * 1000 + tv.usec / 1000;

NOTE: msec should probably be a 64-bit (or larger) variable.

[edited by - dvogel on October 19, 2003 12:07:40 AM]
Regards,Drew Vogel

This topic is closed to new replies.

Advertisement