The Allegro Wiki is migrating to github at

Allegro 5 Tutorial/Displays

From Allegro Wiki
Jump to: navigation, search



In this section we teach you how to create a display, and set 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

Now to someone new to Allegro 5, that may look a little intimidating, so we will go through it line by line.

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 used allegro functions.

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

Just your standard main function definition. Note both arguments are unused, which may cause a compiler warning, it is safe to ignore this warning for now.

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

Here we initialize the allegro library. Should it fail, it will return false, and the if() block will execute, printing a message to standard error, and exiting.

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 exit.

Now to the more interesting bits.

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

This here is a two-for-one. 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.

And al_clear_to_color clears the current display [1] to a given color.

21 al_flip_display();

This is somewhat interesting. If you're familiar with Allegro 4, it will be totally alien to you. What al_flip_display does is make sure what you did to the display actually shows up. Typically Allegro 5 displays are by default double (or more) buffered. So when you draw to a display, it won't actually show up, you were actually drawing to what is called the "backbuffer". And in order to show that backbuffer, you need to "flip" the buffers.

23 al_rest(10.0);

Here we rest, or sleep for 10 seconds. al_rest takes a floating point integer specifying how many seconds to sleep. And yes, it is perfectly valid to choose 0.5 seconds, or any other floating point number. It will try its best to sleep for the amount of time you picked, but may not be 100% accurate.

25 al_destroy_display(display);

This (obviously?) destroys our display. The display will disappear, and you can't use it any longer.

And that concludes our basic introduction to Allegro 5 displays.

Display Flags

Displays support a number of special boolean flags, which enable or disable certain features.

void al_set_new_display_flags(int flags)

You must call this function, with a bitwise OR of the flags you want to set, prior to creating your display.

al_set_new_display_flags(ALLEGRO_FULLSCREEN | ALLEGRO_OPENGL);

Here is a list of some common flags, and what they mean:

    This flag tells Allegro to create a windowed display. This is currently the default mode.
    This will enable the user to resize the created window. Only applicable with ALLEGRO_WINDOWED.
    Tells allegro to create a window without a frame or window controls.
    This will make Allegro create Fullscreen displays. This will change the monitor's resolution if you select a display size which isn't equal to the current desktop mode.
    This is a special mode where Allegro will ignore your chosen resolution upon display creation, and will create a frameless window the size of the current desktop resolution.
    Tells Allegro to use an OpenGL context. Currently the default on Unix, Linux and OSX. Does work on Windows.
    Tells Allegro to use a Direct3D context. Currently the default on Windows. Only applicable on windows.
    This tells Allegro to generate expose events. Can be used to detect when to update.

Display Options

Display options are similar to Display flags, except they can take a number of values, and priorities. Most you probably shouldn't touch unless you absolutely know what you're doing.

void al_set_new_display_option(int option, int value, int importance)
int al_get_new_display_option(int option, int *importance)


Force VSYNC on:

al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE)</code>

Fetch max bitmap size:
<source lang="c">int importance = 0;
int size = al_get_new_display_option(ALLEGRO_MAX_BITMAP_SIZE, &importance);

Some useful options:

    Set whether the display is synced to the monitor's vertical sync.


  1. the one we just created, but more on this later