Sunday, October 30, 2011

2D C++ Game: Part 3 - Getting Started

So, by now everything must be in place and working. Let's start, with a very basic program to check if Allegro is working properly.

Objective: Initialize a window and fill it with colour.
#include <allegro5/allegro.h>

#define SCREEN_W 640
#define SCREEN_H 480
int main()
{
    al_init();
    ALLEGRO_DISPLAY *screen = NULL;
    screen=al_create_display(SCREEN_W, SCREEN_H);
    al_clear_to_color(al_map_rgb(0,0,255));
    al_flip_display();

    al_rest(5.0);
    al_destroy_display(screen);
    return 0;
}
Explanation:
#include <allegro5/allegro.h>
This is the core library of Allegro, which is required to use the Allegro functions. Just the way we include library files like iostream.h or stdio.h inorder to use cout or printf function.

▪ #define SCREEN_W 640
Then we define a constant screen width and assign value 640.
▪ #define SCREEN_H 480
Then we define a constant screen height and assign value 480, both are later used in the program.

al_init();
This is to initialize the Allegro system and has be to initialized before using any other Allegro function.

ALLEGRO_DISPLAY *screen = NULL;
Here we declare a pointer 'screen' which will be our window/display and then initialize it to NULL.

display = al_create_display(SCREEN_W, SCREEN H);
Then we initialize screen with specified dimensions which we had defined in the very beginning of our program i.e 640, 480. For this, we use al_create_display(int width, int height), which returns NULL if an error has occured.

al_clear_to_color(al_map_rgb(0,0,255));
Then we clear the display to a specific colour using al_clear_to_color(ALLEGRO_COLOR color), An ALLEGRO_COLOR structure describes a color in a device. So, we need ALLEGRO_COLOR structure as parameter, for which we use al_map_rgb(unsigned char r, unsigned char g, unsigned char b) which takes three arguments, the value for the red, green, and blue components of the color ranging from 0-255 respectively, and will return a ALLEGRO_COLOR structure.

al_flip_display();
In Allegro, there are two image buffers, one is the front buffer and another is back buffer. Front buffer is the one displayed currently on the screen and back buffer is the one being drawn in the code. Once we are done drawing on the back buffer, we have to call al_flip_display() which updates the back buffer to the front buffer. This is done inorder to prevent the flickering effect that is caused due to drawing directly on to the front buffer, this technique in computer graphics terminology is called as double buffering.

al_rest(5.0);
Then we used al_rest(double seconds) to pause the system for 5 seconds, orelse the program would execute the next line of the code and window will close before you can even see it.

al_destroy_display(screen);
This should be always called at the end of the program to destroy our window and to free up the memory.

return 0;
Finally, we return 0 to the main function signifying the successful execution of the program.

Output:

0 comments:

Post a Comment