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 21:58, June 5, 2010 by Thomas Fjellstrom (talk | contribs) (Created page with '= Displays = == Abstract == 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 cre…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Displays

Abstract

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.

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.

#include <stdio.h>
#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.

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

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.

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

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.