Look Here First

Curious why I created this blog? The following two posts explain it all. Click on the titles below to read more.

What this Blog is about.

Project Description.

Sunday, April 10, 2011

Game Design: Event Loop Timer

Consider the pseudo code below.

while ( not done)
{
ReadInput();
MovementLogic();
DrawScreen();
for (counter = 0;  counter < LargeNumber; counter++)
{
NOP();
}
}

The code above, represents a simple event loop that could be used for a game.  But it has a weakness that it will always 'wait' the same amount of time between iterations of the loop no matter how long each job takes to complete.

Imagine that the time taken (TT) to complete ReadInput(), MovementLogic() and  DrawScreen() is much less then LargeNumber... (TT << LargeNumber).  Then the time taken for one complete iteration of the while loop is about LargeNumber.  Now imagine TT approaches LargeNumber,  the time needed to complete one iteration of the while loop would be 2 * Large Number.  If the designer of a game was trying to achieve a certain amount of frames per second, the game loop above could take up to twice the target time per frame, even though the operations needed to complete a draw cycle take less time than the target time. The code above will always wait a certain amount of time no matter how large TT is.

Perhaps a better way to code a loop is shown below

while ( not done)
{
// current time is set to the on chip timer
if (currentTime >= targetTime)
{
targetTime = currentTime + LargeNumber;
ReadInput();
MovementLogic();
DrawScreen();
}
else
{
NOP();
}
}

In the pseudo code above if TT approaches LargeNumber, the time taken for each screen draw is still around LargeNumber instead of 2*LargeNumber.   The LPC2148 has two 32 bit timers.  The student will use one to implement the type of code in the second example.

Phillips provides the files timer.h and timer.c to allow use of a timer.  The student will be including these files in the game.

No comments:

Post a Comment