I have some very old code lying about on my USB drive, which used old OpenGL and glut, the same principles for timing apply even in more modern versions, the draw code would be different though. The code is for imprecise timing, it is sufficient though for an illustration of how to roughly achieve a set FPS:
// throttle the drawing rate to a fixed FPS
//compile with: g++ yourfilenamehere.cpp -lGL -lglut
#include <cstdlib>
#include <iostream>
#include <GL/gl.h>
#include <GL/glut.h>
GLint FPS = 0;
void FPS(void) {
static GLint frameCounter = 0; // frames averaged over 1000mS
static GLuint currentClock; // [milliSeconds]
static GLuint previousClock = 0; // [milliSeconds]
static GLuint nextClock = 0; // [milliSeconds]
++frameCounter;
currentClock = glutGet(GLUT_ELAPSED_TIME); //has limited resolution, so average over 1000mS
if ( currentClock < nextClock ) return;
FPS = frameCounter/1; // store the averaged number of frames per second
previousClock = currentClock;
nextClock = currentClock+1000; // set the next clock to aim for as 1 second in the future (1000 ms)
frameCounter=0;
}
void idle() {
static GLuint previousClock=glutGet(GLUT_ELAPSED_TIME);
static GLuint currentClock=glutGet(GLUT_ELAPSED_TIME);
static GLfloat deltaT;
currentClock = glutGet(GLUT_ELAPSED_TIME);
deltaT=currentClock-previousClock;
if (deltaT < 35) {return;} else {previousClock=currentClock;}
// put your idle code here, and it will run at the designated fps (or as close as the machine can get
printf(".");
//end your idle code here
FPS(); //only call once per frame loop
glutPostRedisplay();
}
void display() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
// Set the drawing color (RGB: WHITE)
printf("FPS %d\n",FPS);
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_STRIP); {
glVertex3f(0.25,0.25,0.0);
glVertex3f(0.75,0.25,0.0);
glVertex3f(0.75,0.75,0.0);
glVertex3f(0.25,0.75,0.0);
glVertex3f(0.25,0.25,0.0);
}
glEnd();
glutSwapBuffers();
}
void init() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // escape key
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("FPS test");
glutIdleFunc(idle);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}
Hope this helps a bit :) let me know if you need more information.