The Allegro Wiki is migrating to github at

Difference between revisions of "Allegro 5 Tutorial/Displays"

From Allegro Wiki
Jump to: navigation, search
(Walk through)
Line 89: Line 89:
And that concludes our basic introduction to Allegro 5 displays.
And that concludes our basic introduction to Allegro 5 displays.
Next >> [[../Events|Events]]
[[../|Up]] >> [[../Events|Next]]
= Notes =
= Notes =

Revision as of 01:03, June 7, 2010



In this section we will walk through a simple example, explaining how to create a display and setting optional properties for the display.

Basic Example

Here we demonstrate how to create a display, clear the display, display it for 10 seconds, and exit.


#include <stdio.h>
#include <allegro5/allegro.h>

int main(int argc, char **argv)

   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;

   display = al_create_display(640, 480);
   if(!display) {
      fprintf(stderr, "failed to create display!\n");
      return -1;




   return 0;

Walk through

These walk throughs will explain important lines of code in the examples.

1 #include <stdio.h>
2 #include <allegro5/allegro.h>

Here we include the headers necessary for this example. stdio.h is required for the fprintf function, and allegro5/allegro.h is necessary for all of the allegro functions that are used in this example.

4 int main(int argc, char **argv)
5 {

A standard main function, with the argument count and argument vector parameters.

 8 if(!al_init()) {
 9       fprintf(stderr, "failed to initialize allegro!\n");
10       return -1;
11 }

Here we initialize the Allegro library. If it should fail, it will return false, and the if() block will execute, printing a message to standard error, and then exit.

13 display = al_create_display(640, 480);

al_create_display will create a display with the given width and height. Should al_create_display fail to create a display for some reason, it will return NULL.

14    if(!display) {
15       fprintf(stderr, "failed to create display!\n");
16       return -1;
17    }

Should al_create_display fail, we trigger this if block, print a failure message, and then exit.

19 al_clear_to_color(al_map_rgb(0,0,0));

al_map_rgb takes three arguments, the value for the red, green, and blue components of the color respectively, and will return a ALLEGRO_COLOR structure. al_clear_to_color clears the current display [1] to a given color.

21 al_flip_display();

Allegro, by default, creates two image buffers -- the one being displayed on the screen, and the one being drawn on in the code. When you have finished drawing to one of the image buffers for one loop, then you are ready to display its result to the screen for the user. al_flip_display is called to swap the two image buffers around so that the first image buffer is now displayed to the user while the second image buffer becomes the one you will draw on. Failing to call this function will result in only the single, blank image buffer being shown on the screen.

23 al_rest(10.0);

Here we rest the program for 10 seconds. al_rest takes a floating point integer specifying how many seconds to sleep. Using any valid floating point number is valid, including numbers less than 1.0. Allegro will attempt to sleep for the amount of time specified, but it does not have perfect accuracy.

25 al_destroy_display(display);

This function destroys our display and frees the memory and should be called when you are about to shut down the program.

And that concludes our basic introduction to Allegro 5 displays.

Up >> Next


  1. the one we just created