Jump to content
  • Advertisement
Sign in to follow this  
MrDuskling

Allegro 4.0 Sprite animation :o

This topic is 2858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make the left animations to play when your moving left, and the right animations to play when your moving right. I'm approaching this by using if statements and booleans. but when i run the program it just closes, and im not sure if this even works.
PS: im testing out the code from http://www.loomsoft.net/resources/alltut/alltut_lesson7.htm
************************************************************************
/* This stuff should be pretty much memorized by now: */
#include <allegro.h> // You must include the Allegro Header file

/* Timer stuff */
volatile long speed_counter = 0; //A long integer which will store the value of the
//speed counter.

void increment_speed_counter() // A function to increment the speed counter
{
speed_counter++; // This will just increment the speed counter by one.
}
END_OF_FUNCTION(increment_speed_counter);// Make sure you tell it that it's the end of the
// function

int main(int argc, char *argv[])
{
allegro_init(); // Initialize Allegro
install_keyboard(); // Initialize keyboard routines
install_timer(); // Initialize the timer routines

LOCK_VARIABLE(speed_counter); //Used to set the timer - which regulates the game's
LOCK_FUNCTION(increment_speed_counter); //speed.
install_int_ex(increment_speed_counter, BPS_TO_TIMER(60)); //Set our BPS

set_color_depth(16); // Set the color depth
set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480

/* Here we create 3 bitmaps. One bitmap per frame of animation, and we have a total of
* three different frames for the animation, so hence: frame1, frame2, and frame3.
*/
BITMAP *Left1 = load_bitmap("Left1.bmp", NULL); // Declare a bitmap and Load our picture in one single statement
BITMAP *Left2 = load_bitmap("Left2.bmp", NULL);
BITMAP *Left3 = load_bitmap("Left3.bmp", NULL);
BITMAP *Right1 = load_bitmap("Right1.bmp", NULL); // Declare a bitmap and Load our picture in one single statement
BITMAP *Right2 = load_bitmap("Right2.bmp", NULL);
BITMAP *Right3 = load_bitmap("Right3.bmp", NULL);

/* This code is the same as in the previous lesson. Create the buffer, and location
* holders for the position of the frames.
*/
BITMAP *buffer = create_bitmap(640,480); // Declare and create an empty bitmap in one statment.

/* Declare some integers for later use */
int my_pic_x = 0; // Holds our pictures X coorinate
int my_pic_y = 0; // Holds our picture's Y coordinate
bool ismoving = false;
bool left = false;
bool right = false;

/* Here is a simple integer that we are going to increment on each logic loop,
* therefore it will be incremented at the speed of the speed counter, which
* (in this program) is set at 60bps. That means when our frame_counter == 60,
* one full second has passed. Keep that in mind, as you will see how we use it later on.
*/
int frame_counter = 0;// A counter for which frame to draw

/* This code stays the same as the previous lessons */
while(!key[KEY_ESC])//If the user hits escape, quit the program
{
while(speed_counter > 0)
{
if(key[KEY_RIGHT])// If the user hits the right key, change the picture's X coordinate
{
my_pic_x ++;// Moving right so up the X coordinate by 1
ismoving = true;
right = true;
}
if(key[KEY_LEFT])// Ditto' - only for left key
{
my_pic_x --;// Moving left, so lower the X coordinate by 1
ismoving = true;
left = true;
}
if(key[KEY_UP])// If the user hits the up key, change the picture's Y coordinate
{
my_pic_y --;// Moving up, so lower the Y coordinate by 1
ismoving = true;
}
if(key[KEY_DOWN])// Ditto' - only for down
{
my_pic_y ++;// Moving down, so up the Y coordinate by 1
ismoving = true;
}

speed_counter --;

/* Increment our frame counter at the same speed of the speed counter. */
frame_counter ++;

/* Here we test to see if the frame counter is over 4 seconds.
* If it is, we set it back to zero in order to restart the animation. We are only
* testing for four seconds because we are only drawing 1 frame a second, and we're
* drawing a total of four frames. The next big chunk of explanation will clear
* up any confusion regarding this.
*/

if(frame_counter > 240) // 60 * 4 = 240 (When frame counter = 60, 1 second has passed)
{
frame_counter = 0;
}

} //Closing bracket for the while(speed_counter > 0) statment

/* Here is where things get a little tricky. There are multiple ways to do this,
* but I'll show you the one requiring the least lines of code.
*
* As you read earlier, when frame_counter == 60, one second
* has passed, so we will draw each frame at one second.
*/

// In the first second, draw the first frame
if(frame_counter < 60) // Less than a full second
{
if (left == true){
draw_sprite(buffer, Left1, my_pic_x, my_pic_y);//Draw the first frame
}
else if (right == true){
draw_sprite(buffer, Right1, my_pic_x, my_pic_y);//Draw the first frame
}
}
else if(frame_counter >= 60 && frame_counter < 120)//Between 1 and 2 seconds
{
if (left == true){
draw_sprite(buffer, Left2, my_pic_x, my_pic_y);//Draw the second frame
}
else if (right == true){
draw_sprite(buffer, Right2, my_pic_x, my_pic_y);
}
else if(frame_counter >= 120 && frame_counter < 180)//If we are between 2 and 3 seconds
{
draw_sprite(buffer, Left1, my_pic_x, my_pic_y);//Draw the first frame again,
//to acheive better effect
}
else // If we are over 3 seconds
{
if (left == true){
draw_sprite(buffer, Left3, my_pic_x, my_pic_y); //Draw the last frame.
}
else if (right == true){
draw_sprite(buffer, Right3, my_pic_x, my_pic_y);
}
}

/* End of the drawing portion -- same as the previous lesson's */
blit(buffer, screen, 0,0,0,0,640,480); //Draw the buffer to the screen
clear_bitmap(buffer);
}

/* Don't forget to destroy all the bitmaps we created, and do the rest of the deinitializing. */
destroy_bitmap(Left1); //Release the bitmap data
destroy_bitmap(Left2); //Release the bitmap data
destroy_bitmap(Left3); //Release the bitmap data
destroy_bitmap(Right1); //Release the bitmap data
destroy_bitmap(Right2); //Release the bitmap data
destroy_bitmap(Right3); //Release the bitmap data
destroy_bitmap(buffer); //Release the bitmap data
}
}
END_OF_MAIN()

Share this post


Link to post
Share on other sites
Advertisement
Please put your code in source tags, as described here. Trying to read so much unformatted source code is not pleasant.

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
Quote:
Original post by _fastcall
Also, be sure that you check the return values of allegro_init, install_keyboard, install_timer, install_mouse, load_bitmap, and install_sound for any errors, and retrieve the error with allegro_error.


Next to that- also try checking if the bitmaps you are trying to draw aren't set to NULL (meaning the load was unsuccesfull - perhaps because of a typo or a wrong path set to the image).

This can easily by doing by adding this small piece of code:
if ( Left1 = NULL )
// Load was unsuccesfull

These checks should always be in your program, as they will help to save some time while debugging.

And next to that, I advice you to use sprites instead of just 6 different images, 1 should be enough. Then you can use the 'masked_blit' function to exactly say what part of the image you wish to blit to your buffer.

Share this post


Link to post
Share on other sites
When I do an incremental search for "?" in the browser, it shows no results. So this thread must have been answered already.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!