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;