[SDL] Per-frame updates
Todd Klaus
tklaus at yahoo.com
Fri Apr 18 15:26:01 PDT 2003
I guess the fundamental thing is to decouple the screen updates from the rest of the game logic.
That's what I was missing from the abstract examples in books, they usually just lump everything
together in one 'game logic' function, I was just assuming that the two were tied together.
Thanks for the clarification.
Todd
--- Max Watson <max at blackholesun.com> wrote:
> On Friday 18 April 2003 12:37, Todd Klaus wrote:
> >
> > It seems to me (intuitively, I haven't written any games yet) that a better
> > way to do this would be to not have 'do_game_logic()' in the main loop at
> > all, but instead use a timer and have the game logic execute in the
> > callback for that timer. However, I've seen a lot of anecdotal comments on
> > boards and in tutorials that say basically, 'don't use timers, they will
> > kill your frame rate!' Is there really a big downside to using timers this
> > way? I can see that if you are developing a 3d FPS or flight sim you would
> > want to get as many frames as possible, and having the game logic called
> > every frame makes sense for those games, but why do it for 2d sprite games?
>
> You are correct. There are a few reasons not to do your game logic inside the
> timer callback. The one that always bites me is that SDL timer callbacks run
> in a separate thread (under linux). It is possible to use a timer to
> regulate the game logic, what you have to do is decouple the game logic from
> the display update. This allows the logic to run at a steady speed, and slow
> computers can skip a screen update when necessary while fast computers can
> get super high numbers of screen refreshes. The only real downside to this is
> that very slow computers will skip every screen update if your game logic
> takes too long. But we were trying to future-proof the game for faster
> computers, right?
> The way I like to handle this is ripped straight from the allegro toolkit FAQ
> http://alleg.sourceforge.net/faq.html (with a little modification to work
> with SDL):
>
>
> Q: How can I make my game run at the same speed on any computer?
>
> A:You need to make sure the game logic gets updated at a regular rate, but
> skip the screen refresh every now and then if the computer is too slow to
> keep up. This can be done by installing a timer handler that will increment a
> global variable at your game logic speed, eg:
>
> Uint32 speed_counter = 0;
> Uint32 increment_speed_counter(Uint32 interval, void *param) {
> speed_counter++;
> }
>
> void play_the_game() {
> SDL_AddTimer(1000/DESIRED_FPS, increment_speed_counter, NULL);
> while (!game_over) {
> while (speed_counter > 0) {
> update_game_logic();
> speed_counter--;
> }
> update_display();
> }
> }
>
> --
> Hope that was useful,
> Max Watson <max at blackholesun.com>
>
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com
More information about the SDL
mailing list