Code:
#include <allegro.h> // You must include the Allegro Header file
void increase_speed();
void init_all();
void clean_all();
void clean_bitmap(BITMAP* page);
void flip_page(BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP* active_page);
volatile long speed_counter = 0;//A long integer which will store the value of the speed counter.
int main(int argc, char *argv[])
{
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(150));//Set our BPS
BITMAP *ship, *page1, *page2, *page3, *active_page, *background, *bullet;
page1 = create_video_bitmap(SCREEN_W, SCREEN_H);
page2 = create_video_bitmap(SCREEN_W, SCREEN_H);
page3 = create_video_bitmap(SCREEN_W, SCREEN_H);
background = load_bitmap("lee.bmp", NULL);
ship = load_bitmap("ship.bmp", NULL); // Load our picture
clean_bitmap(page1);
clean_bitmap(page2);
clean_bitmap(page3);
active_page = page1;
int x = 0;
int y = 0;
bool validness = true;
/*///////////////////////////////////////////////////////////////////////////////////*/
/*////// Name: Main Loop ////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////////////////////////*/
while(!key[KEY_ESC])
{
while(speed_counter > 0)
{
validness = true;
if(key[KEY_UP] && key[KEY_RIGHT])
{
y--;// Moving down, so up the Y coordinate by 1
x++;
if(y < 0)
{
y++;
}
if(x > 604)
{
x--;
}
}
else if(key[KEY_UP] && key[KEY_LEFT])
{
y--;// Moving down, so up the Y coordinate by 1
x--;
if(y < 0)
{
y++;
}
if(x < 0)
{
x++;
}
}
else if(key[KEY_DOWN] && key[KEY_LEFT])
{
y++;// Moving down, so up the Y coordinate by 1
x--;
if(y > 440)
{
y--;
}
if(x < 0)
{
x++;
}
}
else if(key[KEY_DOWN] && key[KEY_RIGHT])
{
y++;// Moving down, so up the Y coordinate by 1
x++;
if(y > 440)
{
y--;
}
if(x > 604)
{
x--;
}
}
else if(key[KEY_RIGHT])
{
x++;// Moving right so up the X coordinate by 1
if(x > 604)
{
validness = false;
x--;
}
}
else if(key[KEY_LEFT])// Ditto' - only for left key
{
x--;// Moving left, so lower the X coordinate by 1
if(x < 0)
{
validness = false;
x++;
}
}
else if(key[KEY_UP])// If the user hits the up key, change the picture's Y coordinate
{
y--;// Moving up, so lower the Y coordinate by 1
if(y < 0)
{
validness = false;
y++;
}
}
else if(key[KEY_DOWN])// Ditto' - only for down
{
y++;// Moving down, so up the Y coordinate by 1
if(y > 440)
{
validness = false;
y--;
}
}
speed_counter--;
};
if(validness == true)
{
acquire_screen();// Get the screen
clean_bitmap(page1);
clean_bitmap(page2);
clean_bitmap(page3);
masked_blit(ship, active_page, 0,0, x, y,640,480);
show_video_bitmap(active_page);
release_screen();// Release it
flip_page(page1, page2, page3, active_page);
}
};
destroy_bitmap(ship);//Release the bitmap data
destroy_bitmap(background);
destroy_bitmap(page1);
destroy_bitmap(page2);
destroy_bitmap(page3);
destroy_bitmap(bullet);
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 increase_speed()
{
speed_counter++;
}
END_OF_FUNCTION(increase_speed);
void clean_bitmap(BITMAP* page)
{
acquire_bitmap(page);
clear_bitmap(page);
release_bitmap(page);
}
END_OF_FUNCTION(clear_bitmap);
void flip_page(BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP* active_page)
{
if(active_page == page1)
active_page = page2;
if(active_page == page2)
active_page = page3;
if(active_page == page3)
active_page = page1;
}
END_OF_FUNCTION(flip_page);