ModDB Wiki
ModDB Wiki

Loading and Displaying Bitmaps with Allegro[]

Memory Bitmap[]

A memory bitmap is the basic way of loading and displaying a bitmap in allegro.

This is nowhere near as fast as a bitmap stored in video memory, but in some instances you need to use a memory bitmap rather than a video bitmap.

  1. include <allegro.h>

using namespace std;

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

   BITMAP *image;
   PALETTE pal;
   
   allegro_init();
   install_keyboard();
   
   set_color_depth(16);
   
   //For more info on set_gfx_mode > http://www.allegro.cc/manual/view_function.php?_id=1152
   if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0) //GFX_SAFE can also be used, that enables software mode with no video card support.
   {
       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
       allegro_message("Unable to set resolution\n%s\n", allegro_error);
       return 1;
   }
   
   //Load the Bitmap. Allegro dose not support .GIF files, but you can download libarys to load them.
   image = load_bitmap("gpwiki.bmp", pal);
   if (!image) 
   {
       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
       allegro_message("Error reading bitmap file '%s'\n", allegro_error);
       return 1;
   }
   
   /* blit the image onto the screen */
   blit(image, screen, 0, 0, (SCREEN_W-image->w)/2, (SCREEN_H-image->h)/2, image->w, image->h);
   
   /* destroy the bitmap */
   destroy_bitmap(image);
   
   readkey();
   return 0;

}

END_OF_MAIN();

Video Bitmap[]

  1. include <allegro.h>

using namespace std;

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

   BITMAP *image;
   BITMAP *bTemp;
   PALETTE pal;
   
   allegro_init();
   install_keyboard();
   
   set_color_depth(16);
   
   if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0) //GFX_SAFE can also be used, that enables software mode with no video card support.
   {
       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
       allegro_message("Unable to set resolution\n%s\n", allegro_error);
       return 1;
   }
   
   bTemp=load_bitmap("gpwiki.bmp", pal);
   image=create_video_bitmap(bTemp->w, bTemp->h);
   blit(bTemp, image, 0, 0, 0, 0, bTemp->w, bTemp->h);
   if (!image) 
   {
       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
       allegro_message("Error reading bitmap file '%s'\n", allegro_error);
       return 1;
   }
   
   blit(image, screen, 0, 0, (SCREEN_W-image->w)/2, (SCREEN_H-image->h)/2, image->w, image->h);
   
   destroy_bitmap(image);
   
   readkey();
   return 0;

}

END_OF_MAIN();

My Custom Load_Surface() Function[]

Image=Load_Surface(Image, "Filename.bmp",0);

  1. define cStr std::string

BITMAP *Load_Surface(BITMAP *tBitmap, string Path, int Hardware) {

   BITMAP *btemp;
   PALETTE pal;
   
   if (Hardware!=0)
   {
       btemp=load_bitmap(Path.c_str(), pal);
       tBitmap=create_video_bitmap(btemp->w, btemp->h);
       blit(btemp, tBitmap, 0, 0, 0, 0, btemp->w, btemp->h);
   }
   else
   {
       tBitmap=load_bitmap(Path.c_str(), pal);
   }
   
   if (!tBitmap)
   {
       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
       allegro_message("Not enough memory could be found when loading up surfaces, Read log for deatils.\n");
       return NULL;
   }
   
   destroy_bitmap(btemp);
   
   return tBitmap;

}


- Chris Fesko "cdfbr"

Added May 14, 2005