Re : Forget

Thank you Pierre for this advice.

Actually, it’s a breakout game.


----- Message d’origine ----De : Pierre Phaneuf
? : A list for developers using the SDL library. (includes SDL-announce)
Envoy? le : Mercredi, 2 Juillet 2008, 16h05mn 25s
Objet : Re: [SDL] Forget

On Wed, Jul 2, 2008 at 8:45 AM, julien CLEMENT <@Julien_Clement1> wrote:

Then, last thing, it seems that SDL_GetTicks() is more precise because it
takes in account the time needed to perform one game step while SDL_Delay()
just delays in every case.

It’s more precise because there’s no actual sleeping done. This also
means that your CPU will be pegged at 100%. You should replace the
"while ( SDL_GetTicks() - ticks < 5 )" with the following:

Uint32 now = SDL_GetTicks();
if (now < ticks + 5)
SDL_Delay(ticks + 5 - now);

Supposing that the game drawing is somewhere between that code and the
"ticks = SDL_GetTicks();", that will set a target frame rate of 200
fps (I’d personally replace the 5 with 10, making the target frame
rate 100 fps, it’s plenty and will actually manage to sleep once in a
while!). After getting the events and drawing, it will check how early
it is, and only if it’s ahead of time, sleep a little bit (just the
right amount). This might go past, of course, but that will be
smoothed out on the next frame, where it might be a tiny bit late, so
there wouldn’t be any sleeping.

If it’s running on a computer that is insufficiently fast, it will
never sleep, but that’s kind of okay, I suppose.

You could have an “else SDL_Delay(0);”, doing that seems to hint some
platforms’ OS scheduler that here would be a good time for letting
other processes go. If your game doesn’t aim for a target frame rate,
and really just wants to go as fast as possible, you could also do
just “SDL_Delay(0);”, and not bother with the whole SDL_GetTicks()

Note that many OS schedulers reward processes that sleep (for example,
the “interactivity bonus” on Linux). In practice, that means that
SDL_Delay() becomes more accurate, because you get put closer to the
front of the run queue when the delay is over, so your process becomes
runnable more quickly (that’s why it’s called the “interactivity

If you write a backgammon game (or something like that) where there is
no animations while you wait for the next move, using SDL_WaitEvent()
is best. Ideally, SDL_WaitEvent() would take a (optional) timeout,
which would allow you to do animations while sleeping as much as
possible, but for now, we have to fake it with a loop of
SDL_PollEvent() (which is okay for now, since that’s what
SDL_WaitEvent does anyway! check out the source).

SDL mailing list
SDL at


Envoyez avec Yahoo! Mail. Une boite mail plus intelligente