Topic : Allegro: Your First Bitmap
Author : Jay
Page : 1 Next >>
Go to page :


This lesson will initialize allegro, set up keyboard support, show an single bitmap, then wait for a keypress. Upon the keypress the program will then quit.

As before, you must link the allegro library to the project. Do this now.


Here is your first line. You must include the allegro header file or else nothing will work :)

#include <allegro.h> // You must include the Allegro Header file

Initialize the main function.  
int main(int argc, char *argv[])
{
  

Now we want to initialize allegro and set up keyboard support, just as we did in our last lesson  
allegro_init();        // Initialize Allegro
install_keyboard(); // Initialize keyboard routines


This next step is something new. When you actually draw bitmaps to the screen you must specify a color depth that the screen will have.
set_color_depth will change the current color depth.
Available depths are 8,15,16,32.
A color depth of 8 requires a pallete to be loaded with each bitmap, so we will not use it.
A color depth of 32 can use a lot of resources - so we will either use 15 or 16 in order to ensure maximum compatabiliy between machines.
Make sure that you change the color depth before you change the screen resolution. If you fail to do so, your bitmaps will not display with the correct colors  
set_color_depth(16); // Set the color depth
set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480
  

This is the BITMAP structure. Every time you want to make a picture to display, or to draw to, you must declare it with BITMAP - just like when you want an integer, you declare it with int. Since BITMAP in the allegro libarary is a POINTER - every instance of a bitmap you create must be preceded by a *

BITMAP *my_pic; //Declare a BITMAP called my_pic

Now, the bitmap my_pic is empty. In order to load a picture into it, you must call the function load_bitmap(); Next, we will load a bitmap image from the hard disk into the bitmap my_pic.
Here are what the parameters mean:
  load_bitmap("Location of the bitmap", pallete);
In place of the pallete, we will pass NULL - meaning that no pallete is needed.
However, we can't just call the function load_bitmap by itself.
We must specify that we are loading the bitmap into the memory of my_pic. It is done like so: (note, the bitmap must actually exist at this location. If not, the program will crash when trying to load it)

my_pic = load_bitmap("picture.bmp", NULL); // Load our picture

Now that our bitmap is loaded into memory, we want to display it onto the screen. To do this we use a function called blit. Here are the parameters blit(bitmap to draw, destination bitmap (usually the screen), clip x, clip y, x position, y position, width, height); As you can see - blit offers a lot of functionality. The clipx and clipy will be the upper left hand corner of the bitmap that you want to copy. For instance, if you use 0,0 - you will get the bitmap from the corner exactly. However, if you use 50,50 - you will get the bitmap from pixel 50,50 being the upper left hand corner down. You also need to specity the width and height. This will allow you to clip the image from the other sides. Don't worry, we will demonstrate all the capabilities of blitting in the upcoming lessons.

You will also see the two functions aquire_screen/release_screen. These are specific to making windows programs. Because the windows version of allegro uses directX - each bitmap must be 'acquired' before you "draw" it. So, before any blitting to the screen, we want to acquire it. After we blit, we must also release it - or else any other commands like keyboard input/etc cannot execute

Now, lets just display the WHOLE bitmap in the upper left hand corner
acquire_screen();
blit(my_pic, screen, 0,0,0,0,480,360);//Draw the whole bitmap to
the screen at (0,0)
release_screen();  

We now want to wait for a keypress before we exit the program
readkey();// Wait untill a key is pressed  

The next step is critical before exiting the program.
The destroy_bitmap function is used to clear out the space in memory that a bitmap was using. Make sure you destroy all bitmaps that you used in your program, so jumbled data doesn't stay behind in memory  
destroy_bitmap(my_pic);//Release the bitmap data

Return 0 at the end of main, indicating a exit without errors
return(0);// Exit with no errors
}


Call the allegro specific END_OF_MAIN() function.  
END_OF_MAIN(); // This must be called right after the closing bracket of your MAIN function.
                      // It is Allegro specific.


This concludes Lesson 2. You now know how to set up an allegro environment and change the resolution of the screen and display a bitmap.

Lesson 2 code:


/*******************
Allegro Newbie Tutorial

  by
LoomSoft

http://loomsoft.cjb.net
*******************/
/**************************************************
LESSON 2 :
Initialize allegro - set up keyboard support -
Set up color depth
Change the screen color to white - display a
simple bitmap - and read a key to exit
***************************************************/

#include <allegro.h> // You must include the Allegro Header file


/*
Allegro uses the Win32 style application, NOT the console application.
Make sure your MAIN always looks like the following.
Don't worry if you aren't used to it, soon enough you will memorize this
*/
int main(int argc, char argv[])
{

   
   
   allegro_init(); // Initialize Allegro
   install_keyboard(); // Initialize keyboard routines

   
   
   /***************************
   set_color_depth will change the current color
   depth.
   Available depths are 8,15,16,32.

    A color depth of 8 requires a pallete to be
   loaded with each bitmap, so we will not use
   it.
   A color depth of 32 can use a lot of resources -
   so we will either use 15 or 16 in order to ensure
   maximum compatabiliy between machines
   ***************************/
   set_color_depth(16); // Set the color depth
                  
   
                  
   set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480

   
   /*************************
   This is the BITMAP structure.
   Every time you want to make a
   picture to display, or to draw
   to, you must declare it with
   BITMAP - just like when you want an
   integer, you declare it with int.

   Since BITMAP in the allegro libarary
   is a POINTER - every instance of a
   bitmap you create must be preceded by a *
   *************************/
   BITMAP *my_pic; //Declare a BITMAP called my_pic
                                   
   
   /**************************
   Now, the bitmap my_pic is empty.
   In order to load a picture into
   it, you must call the function
   load_bitmap();

    When you load bitmaps, you also
   need a pallete (I know I said no palletes
   in 8 bit mode - that's because allegro
   does everything for you... you just need
   to tell it that the pallete exists)

    So, let's create a pallete.
   It's simple
   **************************/

   PALLETE pal; //Declare a pallete called pal

   /****************************
   Next, we will load a bitmap image
   from the hard disk into the bitmap
   my_pic

    Here are what the parameters mean
   load_bitmap("Location of the bitmap", pallete);
   
   However, we can't just call the function load_bitmap
   by itself. We must specify that we are loading the
   bitmap into the memory of my_pic.

    It is done like so
   ****************************/

   my_pic = load_bitmap("picture.bmp", pal); // Load our picture



   /***********************
   Now that our bitmap is
   loaded into memory, we
   want to display it onto
   the screen.
   To do this we use a function
   called blit.

    Here are the parameters

    blit(bitmap to draw, destination bitmap (usually the screen), clip x, clip y, x position, y position, width, height);

    As you can see - blit offers a lot of functionality.

    The clipx and clipy will be the upper left hand corner of the bitmap
   that you want to copy.
   For instance, if you use 0,0 - you will get the bitmap from the corner exactly.

    However, if you use 50,50 - you will get the bitmap from pixel 50,50 being the upper
   left hand corner down.

    You also need to specity the width and height. This will allow you to clip the image from
   the other sides.

    Don't worry, we will demonstrate all the capabilities of blitting in the upcoming
   lessons.

   You will also see the two functions aquire_screen/release_screen. These are
   specific to making windows programs. Because the windows version of allegro
   uses directX - each bitmap must be 'acquired' before you "draw" it.
   So, before any blitting to the screen, we want to acquire it. After we
   blit, we must also release it - or else any other commands like keyboard
   input/etc cannot execute

    Now, lets just display the WHOLE bitmap in the upper left hand corner
   ************************/
   acquire_screen();
   blit(my_pic, screen, 0,0,0,0,640,480);//Draw the whole bitmap to the screen at (0,0)
   release_screen();


   
   readkey();// Wait untill a key is pressed

   /************************
   The destroy_bitmap function is used to
   clear out the space in memory that
   a bitmap was using.
   Make sure you destroy all bitmaps that
   you used in your program, so jumbled
   data doesn't stay behind in memory
   ************************/
   destroy_bitmap(my_pic);//Release the bitmap data


   return(0);// Exit with no errors
}
END_OF_MAIN(); // This


Page : 1 Next >>