The Allegro Wiki is migrating to github at https://github.com/liballeg/allegro_wiki/wiki

Allegro 5 Tutorial/Displays

From Allegro Wiki
< Allegro 5 Tutorial
Revision as of 09:17, June 6, 2010 by Archon (talk | contribs) (Abstract)
Jump to: navigation, search

Displays

Abstract

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.

Code

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

int main(int argc, char **argv)
{
   ALLEGRO_DISPLAY *display = NULL;

   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;
   }

   al_clear_to_color(al_map_rgb(0,0,0));
   
   al_flip_display();

   al_rest(10.0);

   al_destroy_display(display);

   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.

Next >> Events

Notes

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