Frame-Rate Independent Animation Using SDL and OpenGL (With Frame-Rate Limiting)
Like the GLUT frame-rate independent animation template, this template keeps animated objects moving at the same speed regardless of the speed of the CPU, or the CPU load. This is absolutely essential for games/applications that must operate consistently on a wide range of computers/consoles with different specifications. Without it, a game may be unbearably slow on one machine, and ridiculously fast on another. Either way, the result is something unplayable.
This template includes a project file for Visual Studio 2008 (i.e., Windows), and a makefile for Amiga OS 4.x+. Users of other systems should be able to modify the makefile for their systems easily.
Achieving Frame-Rate Independent Animation
As with the equivalent GLUT template, the key to frame-rate independent animation is to measure the elapsed time since the last frame was drawn. SDL offers the SDL_GetTicks() function to retrieve the time since the SDL application was started. The code for extracting the elapsed time and responding to it is:
int currTime = SDL_GetTicks();
int timeElapsed = currTime - prevTime;
prevTime = currTime;
animate(timeElapsed);
The variable timeElapsed is the time in milliseconds since the previous frame occurred. Animate() is the function that is responsible for updating the positions of all objects. In the case of this template, it rotates a triangle at constant angular velocity. This is achieved by updating the triangle's angle with the amount that it has rotated since the previous frame, i.e.,:
rot += ROTRATE * elapsedTime / 1000;
Limiting the Frame Rate
It is pointless to render images at hundreds of frames per second, it is worth limiting the frame rate. This is achieved via the SDL_Delay() function. Thus, the elapsed time code at the top of the page becomes:
int currTime = SDL_GetTicks();
int timeElapsed = currTime - prevTime;
if(timeElapsed < MIN_FRAMETIME_MSECS)
{
// Not enough time has elapsed. Let's limit the frame rate
SDL_Delay(MIN_FRAMETIME_MSECS - timeElapsed);
currTime = SDL_GetTicks();
timeElapsed = currTime - prevTime;
}
prevTime = currTime;
animate(timeElapsed);
Compiling
Simply download (here) and decompress the archive to a directory. Windows users can open the Visual Studio solution file. Amiga users should open a shell (console) window and enter that directory, and type gmake. Provided that you have the SDK and the MiniGL header files installed, it will compile and produce a binary called GLUT-basic. You can run the template application by typing GLUT-basic and pressing enter. An example of the DOS commands is:
cd SDL-GL-animate
gmake
SDL-GL-animate
Pressing ESCAPE quits the template application.
IMPORTANT: Amiga developers should use gmake, not make, to build this template.Download
Learning More About OpenGL
Whilst online tutorials and template code such as the above are important aids to learning OpenGL programming, having the right books is equally important. A list of recommended books can be found here.
Articles » Amiga OS 4 Articles » OpenGL/MiniGL Templates » Frame-Rate Independent Animation Using SDL and OpenGL (With Frame-Rate Limiting)