Our perception of the world and events has to do with time. Everything happens at some speed and that is what we need. A car covers some distance at a specific time interval while a free falling object accelerates at a standard rate. Another thing is the processing speed of different computer systems. In the old days it used to be impossible to play some games on fast computers because they ran like hell. Game programmers were relying on the processing power of the system to keep track of elapsed time and so when you got a faster computer it was impossible to run the game.
This make clear that what we need to make our games realistic is to know how much time has elapsed since the last time we updated our virtual world and advance everything accordingly. Assume that we have a car that moves at 72 kph. That converts to 20 meters per second. So if our game runs at 100 frames per second we should advance our car 20 cm per frame.
What we actually need is a precision stop watch. We will start it before we enter the main loop and query the elapsed time on every iteration. Then we will be able to calculate all the changes in our virtual world based on real time. This will make our game behave in the same way regardless of the processing power of the computer it runs on.
The code is in the file clf_timer.cpp and the functions and class definitions are in clf_timer.h. The timer can also be used in Linux (note the #ifdef _WIN32 directives). It has been tested under Windows and Linux actually and it is used in some games I have developed as is and without any problems. I don't find it necessary to go into the details of the systems in order to explain how it keeps track of time. It would be too time and space consuming. Anyway you can find a lot of information about the details on the Internet or in books. My main goal is to show the need of time keeping and provide you with a solid basis to start your own games.
We will use the simple application of the first tutorial. What we will do is rotate the triangle around the Z (depth) axis. First we add a couple of global variables to deal with the rotation. Those are the current position, aka rotation angle, and the rotating speed in degrees. We use degrees because OpenGL uses degrees for the angles.
static float fRotation = 0.f; // current rotation angle static float fAngularSpeed = 30.f; // degrees/second radial speed
Then before the main loop we start the timer
In the main loop now and before we call frame_render we call the scene update function
This should be responsible for updating the status of our game world. In our sample it only calculates the new rotation angle based on the formula of simple angular speed
fRotation += fAngularSpeed*fElapsed;
In the rendering function we added an OpenGL command to show the rotation.
glRotatef(fRotation, 0, 0, 1);
After we exit the program loop we stop the timer.