This post is a portion of Part 7 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 7.0 – Sprites
- 7.1 – Bitmap Transparency in Allegro 5
- 7.2 – Basic Animation in Allegro 5
- 7.3 – Using Allegro 5 to Create a Sprite Sheet
- 7.4 – Sprite Sheet Animation in Allegro 5
Now that we have our sprite sheets made, all that’s left is to alter our animation demo to utilize them. This video will conclude part 7 and pave the way for our Sprite struct which we will introduce in Part 8
Full source can be found here
Very great tutorial, I’m really enjoying watching your lessons!
In your included source, on line 93 you wrote frameWidth, where frameHeight should be.
Hope to see more videos from you soon!
nice catch! Fixed it.
Sprite Sheet Animation in Allegro 4 or 5?
Couldn’t you divide the imageWidth by maxFrames to get the individual image width? imageHeight could be directly recieved from the sprite sheet. Although this only works for a sprite ‘strip’ in this case.
You also forgot to do al_destroy_timer(timer); so there will be a small memory leak from the timer.
Er, sorry, that sounded like I was just criticizing you. Your tutorials are great!
No problem, thanks for pointing it out
You could certainly do that, Of course, it will only work if there is no space at the end of your sprite sheet. Also, it assumes that the sprite images are alone on the sheet and it isn’t shared. The way I do it, you only need a numerical offset and it can work for any type of sheet.
And yes, I often accidentally forget to clean up my memory. I can just never seem to remember
Hey Mike,
You’re a Godsend! Thank you so much for these videos, I’ll be donating here before too long and you can be sure I’ll spread the word to my Comp Sci friends.
If i use classes for player and comets.I can set the image as static ALLEGRO_BITMAP *image?
Yes, but you have to be careful. If one class object cleans up the memory by deleting the image, they all stop working. It can work but you must be careful when managing the memory.
OK.Thank you very much.
i had a question no matter what i do it seems that when I use the al_draw_bitmap_region function, my sprite sprite sheet seems to have continuous movement unlike how fluid the dragon moves in your tutorial.Essentially, I see at least 2 of my sprites in at a time moving forward in this continuous pattern…
#include”allegro5allegro.h”
#include”allegro5allegro_font.h”
#include”allegro5allegro_ttf.h”
#include”allegro5allegro_image.h”
#include”allegro5allegro_primitives.h”
int main(void)
{
int frame=0;
int maxframe=9;
int mframe=0;
int mmaxframe=4;
int framecounter=0;
int framedelay=5;
int framewidth=45;
int frameheight=45;
int x=0;
int xx=60;
int y=200;
int yy=250;
bool done=false;
al_init();
al_install_keyboard();
al_init_primitives_addon();
al_init_image_addon();
ALLEGRO_DISPLAY*display=NULL;
display=al_create_display(800,400);
ALLEGRO_BITMAP*image[9]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
ALLEGRO_BITMAP*pimage=NULL;
//ALLEGRO_BITMAP*image;
ALLEGRO_EVENT_QUEUE*event_queue=NULL;
ALLEGRO_TIMER*timer=NULL;
timer=al_create_timer (1.0/12);
event_queue=al_create_event_queue();
image[0]=al_load_bitmap(“frank.bmp”);
image[1]=al_load_bitmap(“frank2.bmp”);
image[2]=al_load_bitmap(“frank3.bmp”);
image[3]=al_load_bitmap(“frank4.bmp”);
image[4]=al_load_bitmap(“frank5.bmp”);
image[5]=al_load_bitmap(“frank6.bmp”);
image[6]=al_load_bitmap(“frank7.bmp”);
image[7]=al_load_bitmap(“frank8.bmp”);
image[8]=al_load_bitmap(“frank9.bmp”);
pimage=al_load_bitmap(“ShinobiSheet.gif”);
for(int i=0;i=framedelay)
{
if(++mframe>=mmaxframe)
mframe=0;
framecounter=0;
}
//////////////////////////////////////////////////////////////////
frame++;
if(frame>=maxframe)
{
frame=0;
}
x+=10;
if(x>800)
{
x=-40;
}
}
al_draw_filled_circle(x,y,20,al_map_rgb(255,0,0));
al_draw_bitmap(image[frame],x-60,y-60,0);
al_draw_bitmap_region(pimage, mframe*framewidth,153,framewidth,frameheight,xx,yy,0);
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
for(int i=0;i<9;i++)
{
al_destroy_bitmap(image[i]);
}
al_destroy_display(display);
al_destroy_timer(timer);
return 0;
}
Actually . I think the better question would be “Do the sprite sheets have to be perfect and do framewidth variable in the draw function have to match up twice? One being for the frame width and the other being where at the function will start drawing from the source.
Yes, that is correct. If they don’t line up, you will need a collection to store the various dimensions.
You must be cautious about that. However,
the interest rates are not worth consolidating because it either does not lower the rate or it even increases the rate.
A consolidate debt loans specialist will allow you to take bad credit consolidate debt loans.
The bottom line is that a person who finds themselves in this situation to begin with?
But it has to consolidate debt loans be wise in choosing the right one.
Students have to pay $12, 000.
My page: webpage
Hi, I love these videos!!! However, I have a question. All the videos have been fine untill now and I am having a problem with this one. I have an image and a sprite but whenever i build it just crashes at “al_convert_mask_to_alpha(sprite, al_map_rgb(0,0,0));”.
If I comment this out it just crashes at the next line:
“al_draw_bitmap_region(sprite,frame_number*frame_width,0,frame_width,frame_height,body.man_body_x,body.man_body_y,0);”.
If i comment out both lines the program opens without a problem.
All the variables etc are correct. It just crashes upon opening.
Any idea what the problem may be?
Many thanks,
James