Game Programming with DGJPP
Graphics by The Dark Angel

The Animations
How is possible to simulate the movement of the personages in the games? This is done by drawing a sequence of frames of the personage in differents positions. Is simple to say, but hard to done.....

The logic of the animations
The logic of animations isn't very hard: for each character we have a sequence of frames in differents positions (walk, jump, and so on). The player hit some keys, and the frames will be shown accordly to those keys. How can we implement this in C code? First we must define the differents status that the character to animate can assume. For example a character in a platform game can look, walk, get, hit and jump:

#define STAT_LOOK  0
#define STAT_WALK  1   
#define STAT_GET   2   
#define STAT_HIT   3   
#define STAT_JUMP  4   
And, logically, can walk or jump left or right, with different speed:

#define RIGHT 0
#define LEFT  1
#define STEP_WALK 5    /* Pixel increment when walking */
#define STEP_JUMP 10   /* Pixel increment when jumping */
For syncronizing the speed of the animations to the speed of the computer, we use the function timer_step() (see Handling Interrupts), but the speed is still too high, and we must dealay a bit before changing the status (e.g. wait few timer clocks from a hit to another):

#define DELAY_FRAME 5   /* Numbers of timer clock to delay before             */
                        /* advance one frame (frame_time)                     */
#define DELAY_GET   15  /* Number of colcks to wait for be ready to get  */
#define DELAY_HIT   10  /* Number of clocks to wait for be ready to hit  */
#define DELAY_JUMP  8   /* Number of clocks to wait for be ready to jump */

#define COUNTER_FRAME 0
#define COUNTER_GET   1
#define COUNTER_HIT   2
#define COUNTER_JUMP  3
Well, we assume to load all the frames in an array declared as struct IMAGE frame[19], so we can define the variables for memorizing the entire status of the character:

short status     = STAT_LOOK;
short direction  = RIGHT;		
short frameindex = 7;		
short delay_counter[4];
short posx = START_POSX, posy = START_POSY;
The code implementation
Now is necessary to write a code that do this:
  1. Hook the keyboard and the timer; initialize the VGA.
  2. Process the inputs from keyboard (and from joystick if you want...) to determiate the status of the character to animate (enter in a status).
  3. Update the delay counters.
  4. Draw the right frame
  5. Control the advancement of the frames (in agreement of the inputs)
  6. Syncronize the speed with the internal timer
  7. Reprocessing the inputs (exit from a status).
  8. Jump to point 2 until exit.
  9. Restore the keyboard and the timer handlers; restore the video mode.
A simply implementation of the algorithm is done in this demo program.



[main] [prec] [next] [email]

Site Made and Maintained by The Dark Angel <DarkAngel@tin.it>
My ICQ Number is 2063026
You Visited This Page times


This page hosted by Geocities Icon Get your own Free Home Page