Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Animation from a sprite sheet?

This thread is locked; no one can reply to it. rss feed Print
Animation from a sprite sheet?
eihn07
Member #11,634
January 2010

I figured out how to display where from the sprite sheet i want each movement to come from. but actually making it animate if you keep going right I cant seem to figure out :\

right now know WASD all has a single picture from the sprite sheet. How would i go about animating them to keep walking which way they are going?

Can someone show a quick example or edit one of mine

#SelectExpand
1 if (player.dir == 1) 2 { 3 masked_blit(player.bM,buffer,73,4,player.x,player.y,55,55); 4 } 5 if (player.dir == 0) 6 { 7 masked_blit(player.bM,buffer,73,69,player.x,player.y,55,55); 8 } 9 if (player.dir == 2) 10 { 11 masked_blit(player.bM,buffer,6,66,player.x,player.y,55,55); 12 } 13 if (player.dir == 3) 14 { 15 masked_blit(player.bM,buffer,140,67,player.x,player.y,55,55); 16 }

kazzmir
Member #1,786
December 2001
avatar

Do you mean you have multiple sprites for a "walking right" animation and you want to display them all as the character walks right?

If so you should calculate the dimensions of each sprite and put them into an array. Then just display each element of the array.

BITMAP * walk_right[10];
walk_right[0] = create_sub_bitmap(sprite_sheet, 23, 25, 90, 80); // whatever
walk_right[1] = ...;
...;
int right = 0;

while (...){
   if (player.dir == 1){
     blit(walk_right[right], buffer, ...);
     right = (right + 1) % 10; // go through the images and wrap around once you hit 10
   }
}

ImLeftFooted
Member #3,935
October 2003
avatar

You'll also need to figure out how many milliseconds to show each frame for.

eihn07
Member #11,634
January 2010

Okay I got the animation down. but its like spinning thru the images fast. so how do I do the timing part? Thanks guys I really appreciate helping me.

kazzmir
Member #1,786
December 2001
avatar

First you should split your code into a logic part and a drawing part. Look at this wiki article:

http://wiki.allegro.cc/index.php?title=Animation

Notice the while(speed_counter>0){...} and the if(draw){...}. The first is the logic part and the second is the draw part. So in your draw part you would blit the sprite to the screen. In the logic part you would check what the direction is and update the sprite to draw accordingly. Then you can just add in another variable to count a few frames before updating the current sprite.

#SelectExpand
1BITMAP * walk_left[] = ...; 2BITMAP * walk_right[] = ...; 3int counter = 0; 4BITMAP ** current = &walk_left[0]; 5int showing = 1; // 1 for left, 2 for right, 3 for up, 4 for down 6 7while (speed_counter > 0){ 8 if (player.direction == 1){ 9 if (showing != 1){ 10 current = &walk_left[0]; 11 showing = 1; 12 counter = 0; 13 } else if (counter > 5){ 14 current++; 15 counter = 0; 16 } else { 17 counter += 1; 18 } 19 } ... 20} 21 22if (draw){ 23 blit(*current, buffer, ...); 24}

ImLeftFooted
Member #3,935
October 2003
avatar

If you want instant gratification, add this code below the 'blit' code:

rest(35);

Neil Walker
Member #210
April 2000
avatar

As kazzmir - it might also serve better to encapsulate your animation in an object (or a struct) then instead of all those separate blit statements you would just set the current player's animation from your direction and have one blit statement. Also, if you create sub-bitmaps of the spritesheet rather than hard coding the location then it'll further simply things.

What I do is have an animation object that represents an animation as a whole (sequence of frames and frame timings, current frame, loop type, etc). Each frame object contains the sub-bitmap pointer, size (for quick access), etc. The player is then assigned an animation. This means the player simply changes animation and the relevant parts (e.g. animation) look after the updates, etc.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Go to: