Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » how to animate properly

This thread is locked; no one can reply to it. rss feed Print
how to animate properly
William Labbett
Member #4,486
March 2004
avatar

Hi,
my game's coming along nicely but I've need to learn how to animate properly.
At the moment the way it works is :-

  if(ball_moving) draw_ball();
  if(fan_turning) draw_fan();
  if(player_moving) draw_player();

In the draw_player() function there's a rest(10) call, so at the moment if the player is moving there's a rest(10) between every loop but if he's not the draw_ball() and draw_fan() get called quickly. I need to make so things are more consistent.
A bit of help would be greatly appreiciated.

Will

axilmar
Member #1,204
April 2001

Animation is not done at all in the way you describe. The appropriate approach is to do a balanced game loop like this:

void int_proc() {
    ++timer;
}

install_int(proc, FPS(60));
while (game) {
    while (timer) {
        game_logic();
        animate_stuff();
        --timer;
    }
    draw_stuff();
}

And then draw things in 'draw_stuff' and animate things in 'animate_stuff'.

Animating is easy. For example, if you have a bitmap animation, all you need is an array of bitmaps and an index into the array: by increasing the index, your game object will be drawn differently each time it is drawn, i.e. be animated.

Animation is all about altering the drawing parameters of a game object.

William Labbett
Member #4,486
March 2004
avatar

Thanks axilmar.

I think that should be

install_int(int_proc, FPS(60)); but I get what you mean.

I'll give this a go but I don't really understand what this code does.

Is this right ? - While draw_stuff() is doing it's thing the timer integer gets
increased. Then when execution gets back to the animate_stuff function it calls those functions decrementing timer each time until it's back to 0. Then draws again.
So the longer it takes to draw the more times the logic gets updated ?

Is that right ? Wouldn't this cause a lot of jumpy sprite drawing ?

Thanks

axilmar
Member #1,204
April 2001

It might cause some jumping in slower computers, but generally it is one of the most used ways to code a game loop.

William Labbett
Member #4,486
March 2004
avatar

Right.
It seems this necessitates doing all the non-drawing stuff all at once and then all the drawing all at once. This isn't how my game works but I should be able to adapt it into this way.

Go to: