Tutorial Index

Tutorial 5- Using the SDL Timer


Introduction:

In this tutorial we use an SDL timer to build a game clock. The purpose of a game clock is to base all movements in the game according to a wall clock, rather than according to the speed of the processor. If movements are based on the speed of the processor then the game will run fast or slow depending on the speed of the processor. Using wall clock time, based on a game clock or game timer, allows you to build a game that will run at the same speed on all platforms.

In the tutorial, we have demonstrate how to create a scrolling background, where movement of the background sprite is based on an SDL timer. The important code is containted in the files: manager.h, manager.cpp, background.h, sprite.h, and sprite.cpp. These files contain classes: Manager, Background, and Sprite. The Manager class contains a function, play, which implements an event loop during which all aspects of the game are drawn, updated and moved. To perform the movements, the play function computes the number of clock ticks between each iteration of the event loop and uses these ticks to compute the velocity of the background sprite. The computation of the number of ticks is contained in the play function of Manager, and the computation of the velocity of the background is computed in the move function of Sprite.

This is a simple demonstration that consists of computing the number of ticks between iterations of the event loop and then using these ticks to compute the speed of all objects in the game. Using this technique, we allow the frames of the game to update as fast as the processor speed will allow. In tutorial 13, we demonstrate the use of the OSG timer; however, we use the OSG timer in tutorial 13 to create a delay in the event loop that will cause the game to run at the same speed on any platform. Various games use these techniques and it's your choice as to which technique you employ in your game.

Tutorial Code:

Tutorial Code