// Allegro library
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
//Screen resolution
#define SCREEN_W 640
#define SCREEN_H 480
// Game fps
#define FPS 60
int main()
{
al_init(); // Start Allegro
ALLEGRO_DISPLAY *screen = NULL; // Main display
ALLEGRO_EVENT_QUEUE *event_queue = NULL; // Event queue to maintain events
ALLEGRO_TIMER *timer = NULL; // Timer to control game fps
// Initializing screen, event queue and timer
screen = al_create_display(SCREEN_W, SCREEN_H);
event_queue = al_create_event_queue();
timer = al_create_timer(1.0/FPS); // Timer set to tick at 60 frames per second
// Register event sources to be handled in the event queue
al_register_event_source(event_queue,al_get_display_event_source(screen));
al_register_event_source(event_queue,al_get_timer_event_source(timer));
al_init_image_addon(); // Initialize Allegro image addon
al_start_timer(timer); // Start timer
// ALLEGRO BITMAP 'image' to hold the image
ALLEGRO_BITMAP *image = NULL;
// Load bitmap
image = al_load_bitmap("Pacman_Sprite.png");
// Convert image background to transparent
al_convert_mask_to_alpha(image,al_map_rgb(255, 0, 255));
int x=0; // x coordinate of the image
while(1) //Game Loop
{
ALLEGRO_EVENT event; // ALLEGRO_EVENT to hold events
al_wait_for_event(event_queue,&event); // Wait for events to occur
// Display handler
// Draw if timer counter has incremented and there are no events to handle
// and event queue is empty
if(event.type == ALLEGRO_EVENT_TIMER && al_is_event_queue_empty(event_queue))
{
al_draw_bitmap(image, x, SCREEN_H/2, 0); // Draw image on the backbuffer
al_flip_display(); // Display backbuffer on to the main screen
al_clear_to_color(al_map_rgb(0, 0, 0)); // Clear display to black
}
//Event handlers
if (event.type == ALLEGRO_EVENT_TIMER)
{
// Increment x coordinate of the image
// When position of the image is more than the screen width, reset x coordinate to 0
if(++x>=SCREEN_W)x=0;
}else if(event.type==ALLEGRO_EVENT_DISPLAY_CLOSE)
{
break; // End game loop on closing display window
}
}// End of Game Loop
al_destroy_display(screen); // Destroy display
return 0;
}
Explanation:▪ Image Background Transparency
// ALLEGRO BITMAP 'image' to hold the image
ALLEGRO_BITMAP *image = NULL;
// Load bitmap
image = al_load_bitmap("Pacman_Sprite.png");
// Convert image background to transparent
al_convert_mask_to_alpha(image,al_map_rgb(255, 0, 255));
Image background transparency is achieved with the Allegro routine al_convert_mask_to_alpha(ALLEGRO_BITMAP *bitmap, ALLEGRO_COLOR mask_color) which takes 2 parameters:
■ ALLEGRO_BITMAP structure which holds our image.
■ ALLEGRO_COLOR structure, in our case we gave al_map_rgb(255, 0, 255) which is the rgb value for magenta, which is the background color of our image.
▪ Game FPS■ ALLEGRO_COLOR structure, in our case we gave al_map_rgb(255, 0, 255) which is the rgb value for magenta, which is the background color of our image.
// Game fps #define FPS 60 ALLEGRO_TIMER *timer = NULL; // Timer to control game fps timer = al_create_timer(1.0/FPS); // Timer set to tick at 60 frames per second al_register_event_source(event_queue,al_get_timer_event_source(timer)); al_start_timer(timer); // Start timer
0 comments:
Post a Comment