Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Allegro: Sprite Animations
Author Message
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #1
Allegro: Sprite Animations
Now we're getting to some fun stuff.
For this tutorial you should use the image in the .zip at the bottom.

Notice the sprites are evenly spaced apart on the image. This is called a sprite sheet, and it's the easiest way to have a sprite animation.

To achieve the animation is rather simple if the images are the same distance apart.
We'll use the masked_blit() function for this.

masked_blit(Bitmap_to_draw, where_to_draw, source_x, source_y, destination_x, destination_y, width of blit, height of blit)

Big function right?

The individual sprites on that image are 32x32, so remember that.

Since the image is 96x128 I define the coordinates below. I'll let the code speak for itself.
Code:
#include <allegro.h>
#define down 0
#define left 32
#define right 64

BITMAP *Buffer = NULL;
BITMAP *Sprite = NULL;

void draw();
void logic();

short x = 0;
short y = 350;

short Sprite_X = left;
short Sprite_Y = down;



int main ()
{
// You always want to initialize everything before you do things in your program
allegro_init ();
install_keyboard ();
set_color_depth (32);
set_gfx_mode (GFX_AUTODETECT, 640, 480, 0, 0);//Screen resolution

Buffer = create_bitmap (SCREEN_W, SCREEN_H);
Sprite = load_bitmap ("Sprite.bmp", NULL);
if(!Sprite)
{
allegro_message("No Sprite.bmp in this directory...");
return 0;
}

while (!key [KEY_ESC])
{
logic();

// Always draw last
draw();

}
// End of Game Loop
clear(screen);
destroy_bitmap(Sprite);
destroy_bitmap(Buffer);
return 0;
}
END_OF_MAIN()

void draw()
{
    masked_blit (Sprite, Buffer, Sprite_X, Sprite_Y, x, y, 32, 32);
    draw_sprite(screen, Buffer, 0, 0);
    rest (50);
    clear_bitmap(Buffer);
}

void logic()
{
    Sprite_X += 32;//Sprite walk animation
    if(!key[KEY_RIGHT]&&!key[KEY_LEFT]&&!key[KEY_UP]&&!key[KEY_DOWN])
    Sprite_Y = down;
    if (key[KEY_RIGHT])
    {
    x += 5;
    Sprite_Y = right;
    }
    else if (key[KEY_LEFT])
    {
    x -= 5;
    Sprite_Y = left;
    }
    else
    {
    Sprite_X = left;
    }
    if (Sprite_X > right)
    Sprite_X = down;
}


Attached File(s)
.zip  Sprite Animations.zip (Size: 355.37 KB / Downloads: 98)
(This post was last modified: 01-28-2011 06:42 PM by Scorch.)
01-28-2011 06:42 PM
Find all posts by this user Quote this message in a reply
E___ Offline
New Member
**

Trade Count: (0)
Posts: 1
Joined: Dec 2012
Reputation: 0
Post: #2
RE: Allegro: Sprite Animations
Thanks man, that helped me a lot on understanding the basics of using a sprite sheet.

Keep up the good job!!
12-11-2012 02:20 PM
Find all posts by this user Quote this message in a reply
adsbox2014 Offline
New Member
**

Trade Count: (0)
Posts: 1
Joined: Mar 2014
Reputation: 0
Post: #3
Wink RE: Allegro: Sprite Animations
Really nice sample to get understand what teacher taught in class!

Thanks alot!
03-29-2014 10:41 PM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: