Archived

This topic is now archived and is closed to further replies.

nickelplate

Locking the frame rate.

Recommended Posts

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."

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites