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

Basic tutorial on loading and showing images

From Allegro Wiki
Jump to: navigation, search

In this tutorial we'll learn the very basics about loading a bitmap and show it on a display.

#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_native_dialog.h"

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

   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_BITMAP  *image   = NULL;

   if(!al_init()) {
      al_show_native_message_box(display, "Error", "Error", "Failed to initialize allegro!", 
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      return 0;
   }

   if(!al_init_image_addon()) {
      al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!", 
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      return 0;
   }

   display = al_create_display(800,600);

   if(!display) {
      al_show_native_message_box(display, "Error", "Error", "Failed to initialize display!", 
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      return 0;
   }

   image = al_load_bitmap("image.png");

   if(!image) {
      al_show_native_message_box(display, "Error", "Error", "Failed to load image!", 
                                 NULL, ALLEGRO_MESSAGEBOX_ERROR);
      al_destroy_display(display);
      return 0;
   }

   al_draw_bitmap(image,200,200,0);

   al_flip_display();
   al_rest(2);

   al_destroy_display(display);
   al_destroy_bitmap(image);

   return 0;
}

Walk Through

   ALLEGRO_BITMAP  *image;

As the reference manual says: this is an abstract type representing a bitmap (2D image). After creating our display we load our image using the al_load_bitmap() function.

If you load an image before the creation of a display you would be loading it as a Memory Bitmap which is extremely slow, for that reason remember always to load bitmaps after the creation of a display. Future versions of Allegro 5 (Allegro 5.2), will have the possibility of changing from memory to video bitmaps any time in the code, but currently the only way to get a Video Bitmap is loading the image after the creation of a display.

al_load_bitmap() Will return false if it is not possible to load the image.

al_draw_bitmap(image,200,200,0);

This function will draw the image into the current display, but since Allegro 5 uses two bitmaps, the first one as the buffer bitmap, and the second one as the bitmap that the display would be showing to the user, you'll need to call al_flip_display() to show the image you have previously drawn. So before you call al_flip_display() all the drawing operations are going to be performed on the buffer, so they're not visible to the user. Once you call al_flip_display() Allegro will change the buffer, showing the buffer in which you were drawing previously, and hiding the bitmap that was previously visible to the user, becoming this the new buffer.

al_flip_display();

Using this method will allow you to get a better presentation of your games, since everything will look smoother than if you were using just one bitmap with no buffer.

al_rest(2);

After that we call this function to pause the program a little bit, otherwise the program would close very fast, making impossible to see the image.

al_destroy_bitmap(image);

And finally we use this function to free all memory used by the ALLEGRO_BITMAP struct.