setting max frame rate with glut?

How can I set my frame rate in glut? I’m looking for the most common/compatible way to do this because I’m compiling in windows and OSX… maybe Linux too in the future.

If you use GLUT library for cross platform support then you can use glutTimerFunc.

Note glutTimerFunc is a one shot timer and must be reset when called.

example:

glutTimerFunc( 100 (ms), My_timer_routine, 0);
glutMainLoop();
}

void My_timer_routine( int t )
{
// Animation code

// Update display
glutPostRedisplay();
// Reset timer
glutTimerFunc( 100, My_timer_routine, 0);
}

Originally posted by Rad:
How can I set my frame rate in glut? I’m looking for the most common/compatible way to do this because I’m compiling in windows and OSX… maybe Linux too in the future.

Perhaps another more universal way but not using Glut,
is simply using the gettimeofday function
(There is actually a more universal function for getting the time in ms but I just can’t really remember Anyone?)

Something like this.

float interval = 50; // ms
float t = gettimeofday();
while(run) {
if( (gettimeofday() - t) > interval ) {
usleep( gettimeofday()-t );
t = gettimeofday();
}

// Other stuff.
}

Then it’s only a matter of calculating how many frames you want and adjust interval accordingly.

Say you want 75fps max :

1s / 75 = 0.0133333…
0.013333… * 1000 = 13.3ms

Note: if for some reason a frame takes longer than the specified delay, the delay won’t be executed. (See the line with usleep())

At your service :wink:
Hope I could help.

Thanks Nexusone, looks good. How would you go about checking your actual refresh rate?

Also, I had been executing my animation code from my display function. From looking at your code I’d guess that I’ve been doing this wrong. But what if I reset the timer in the beginning of my display function and my timer function only contained glutPostRedisplay… then the timer would be ticking even during my rendering code. Would there be anything wrong with that?

[This message has been edited by Rad (edited 05-07-2003).]