Using SDL_AddTimer in games

What is the most efficient(read: consumes the least possible amount of CPU
time) way to use the timer in a game loop? Make a 10ms timer execute
’timer++’ and then wait for handling moving and drawing until it reaches
the wanted about of msecs and then reset it again? Or what way do you
recommend? A simple gameloop would be like this?

create timer

while (timer < time_to_wait_until_next_frame);

move objects;

draw objects;

reset timer;

Is there a better way?

I have tried this approach in a project I’m working on (sdlstella, an
Atari 2600 emulator). The only problem is that since you are tied to a
10 ms timeslice, you will not be able to get an exact framerate.

For example, assume that the rest of your game takes 4 ms and you have to
wait for 6 ms. When you try to wait for 6 ms, you will actually be
forced to wait for 10, effectively slowing the framerate down.

If your game is not sensitive to framerate, then this may be acceptable.

Good luck,
SteveOn February 3, 2002 09:31 am, you wrote:

What is the most efficient(read: consumes the least possible amount of
CPU time) way to use the timer in a game loop? Make a 10ms timer
execute ‘timer++’ and then wait for handling moving and drawing until
it reaches the wanted about of msecs and then reset it again? Or what
way do you recommend? A simple gameloop would be like this?

create timer

while (timer < time_to_wait_until_next_frame);

move objects;

draw objects;

reset timer;

Is there a better way?