Code:
#include <allegro.h> // You must include the Allegro Header file
void buffdraw(BITMAP *buffer, BITMAP *buffer2, BITMAP **primary_buffer, BITMAP *t_Bitmap_obj, int x, int y);
void increase_speed();
volatile long speed_counter = 0;//A long integer which will store the value of the
//speed counter.
int main(int argc, char *argv[])
{
bool validness;
int wBuffer=1;
allegro_init(); // Initialize Allegro
install_keyboard(); // Initialize keyboard routines
set_color_depth(16); // Set the color depth
set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480
install_timer();
LOCK_VARIABLE(speed_counter);
LOCK_FUNCTION(increase_speed);
install_int_ex(increase_speed, BPS_TO_TIMER(100));//Set our BPS
BITMAP *my_pic; //Declare a BITMAP called my_pic
my_pic = load_bitmap("picture.bmp", NULL); // Load our picture
BITMAP *buffer; //Declare a BITMAP called buffer.
BITMAP *buffer2;
BITMAP **primary_buffer = &buffer;
BITMAP *background;
background = load_bitmap("lee.bmp", NULL);
buffer = create_bitmap(640,480); //Create an empty bitmap.
buffer2 = create_bitmap(640, 480);
int my_pic_x = 0;// Holds our pictures X coorinate
int my_pic_y = 0;// Holds our picture's Y coordinate
while(!key[KEY_ESC])//If the user hits escape, quit the program
{
while(speed_counter > 0)
{
validness = true;
/*****************************
Here is a series of tests - so we can move
our bitmap around the screen
****************************/
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
if(my_pic_x > 608)
{
validness = false;
my_pic_x--;
}
}
else if(key[KEY_LEFT])// Ditto' - only for left key
{
my_pic_x--;// Moving left, so lower the X coordinate by 1
if(my_pic_x < 0)
{
validness = false;
my_pic_x++;
}
}
else 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
if(my_pic_y < 0)
{
validness = false;
my_pic_y++;
}
}
else if(key[KEY_DOWN])// Ditto' - only for down
{
my_pic_y++;// Moving down, so up the Y coordinate by 1
if(my_pic_y > 448)
{
validness = false;
my_pic_y--;
}
}
speed_counter--;
};
if(validness == true)
{
acquire_screen();// Get the screen
buffdraw(buffer, buffer2, primary_buffer, background, 0, 0);
buffdraw(buffer, buffer2, primary_buffer, my_pic, my_pic_x, my_pic_y);
if(*primary_buffer == buffer)
{
primary_buffer = &buffer2;
wBuffer = 2;
}
if(*primary_buffer == buffer2)
{
primary_buffer = &buffer;
wBuffer = 1;
}
release_screen();// Release it
}
};
destroy_bitmap(my_pic);//Release the bitmap data
destroy_bitmap(buffer);//Release the bitmap data
destroy_bitmap(background);
return(0);// Exit with no errors
}
END_OF_MAIN(); // This must be called right after the closing bracket of your MAIN function.
// It is Allegro specific.
void buffdraw(BITMAP *buffer, BITMAP *buffer2, BITMAP **primary_buffer, BITMAP *t_Bitmap_obj, int x, int y)
{
draw_sprite(*primary_buffer, t_Bitmap_obj, x, y);
if(*primary_buffer == buffer2)
blit(buffer, screen, 0,0,0,0,640,480);
if(*primary_buffer == buffer)
blit(buffer2, screen, 0,0,0,0,640,480);
}
END_OF_FUNCTION(buffdraw);
void increase_speed()
{
speed_counter++;
}
END_OF_FUNCTION(increase_speed);