Topic : Allegro: Masking a bitmap
Author : Jay
Page : 1

LESSON 4 : All initializtation, blitting a masked bitmap, and sprite. Also - learn how to clear a bitmap, and clear the keybuffer.

Initialize the general stuff.

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

int main(int argc, char *argv[])
{
   allegro_init(); // Initialize Allegro
   install_keyboard(); // Initialize keyboard routines
   set_color_depth(16); // Set the color depth
   set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480


Create a bitmap, and load the picture into memory  
BITMAP *my_pic; //Declare a BITMAP called my_pic
my_pic = load_bitmap("picture.bmp", NULL); // Load our picture  


Here we will blit a MASKED bitmap - that is, pixels that we want will be shown as transparent. In order to make transparent parts in a bitmap, you must use the RGB color:

Red 255
Green 0
Blue 255

This color will be the BRIGHTEST pink. Any pixel in a bitmap that is this color when you draw a bitmap using masked_bitmap or draw_sprite will be transparent. masked_blit takes the SAME parameters as blit - so you can perform clipping if you wish.
acquire_screen();
masked_blit(my_pic, screen, 0,0,0,0,572,473);//Draw the whole bitmap to the screen at (0,0)
release_screen();
readkey();// Wait untill a key is pressed


The clear_keybuf(); function will just clear the keybuffer of any information. We are calling this because we don't want the program to think a key is pressed when we call readkey for the second time. Some computers are so fast that they may draw the sprite - and think that a key is still pressed from the previous keypress.  
clear_keybuf();  

Here we will be using two new functions.
As always, before doing any drawing or routines with bitmaps, we must aqcuire the screen.
The next function, clear_bitmap(); does exactly that - it will clear a bitmap. In this case, we want to clear the SCREEN - which is a special kind of bitmap (but a bitmap nonetheless).
After clearing the screen, we will be using the function draw_sprite();. Draw sprite is exactly the same as masked_blit except for the fact that it has no clipping capabilities.

draw_sprite() will always draw the FULL, masked, bitmap. Also, notice that the source bitmap and destination bitmap are reversed. When calling draw_sprite - put the bitmap you wish to draw to first, and the bitmap you are actually DRAWING second.

When you run the program, you won't be able to tell the difference between masked_blit and draw_sprite.

acquire_screen();
clear_bitmap(screen);
draw_sprite(screen, my_pic, 0,0);//Draw the bitmap at 0,0.
release_screen();
readkey();//Wait for a keypress  


Destroy the bitmap, and exit the program

destroy_bitmap(my_pic);//Release the bitmap data
return(0);// Exit with no errors
}
END_OF_MAIN(); // This must be called right after the closing bracket of your MAIN function. // It is Allegro specific.  


This concludes Lesson 4. You now know how to draw masked bitmaps using two different functions, clear a bitmap, and clear the keyboard buffer.  



Lesson 4 code:

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

  by
LoomSoft

http://loomsoft.cjb.net
*******************/
/**************************************************
LESSON 4 :
All initializtation, blitting a masked bitmap, and
sprite.
Also - learn how to clear a bitmap, and clear the
keybuffer
***************************************************/

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



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

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

   
   
   set_color_depth(16); // Set the color depth
                     
   set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480


   BITMAP *my_pic; //Declare a BITMAP called my_pic
                                   

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


   /*****************
   Here we will blit a MASKED bitmap -
   that is, pixels that we want will
   be shown as transparent.

   In order to make transparent parts
   in a bitmap, you must use the RGB color
   Red   255
   Green 0
   Blue  255

    This color will be the BRIGHTEST pink.
   Any pixel in a bitmap that is this color
   when you draw a bitmap using masked_bitmap
   or draw_sprite will be transparent.

    masked_blit takes the SAME parameters as
   blit - so you can perform clipping if you
   wish.
    ******************/

   acquire_screen();
   masked_blit(my_pic, screen, 0,0,0,0,572,473);//Draw the whole bitmap to the screen at (0,0)
   release_screen();


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

   
   /********************
   The clear_keybuf(); function
   will just clear the keybuffer
   of any information.

    We are calling this because   we don't want the
   program to think a key is pressed when we call
   readkey for the second time.

    Some computers are so fast that they
   may draw the sprite - and think that
   a key is still pressed from the previous
   keypress.
   ********************/
   clear_keybuf();

   /********************************
   Here we will be using two new functions.
   As always, before doing any drawing or
   routines with bitmaps, we must aqcuire the
   screen.

    The next function, clear_bitmap();
   does exactly that - it will clear
   a bitmap. In this case, we want to
   clear the SCREEN - which is a special
   kind of bitmap (but a bitmap nonetheless).

    After clearing the screen, we will be
   using the function draw_sprite();.

    Draw sprite is exactly the same as
   masked_blit except for the fact that it
   has no clipping capabilities. draw_sprite()
   will always draw the FULL, masked, bitmap.

   Also, notice that the source bitmap and destination bitmap
   are reversed. When calling draw_sprite - put the bitmap you
   wish to draw to first, and the bitmap you are actually DRAWING
   second.

   When you run the program, you won't be able
   to tell the difference between masked_blit
   and draw_sprite.
   ********************************/
   acquire_screen();
   clear_bitmap(screen);
   draw_sprite(screen, my_pic, 0,0);//Draw the bitmap at 0,0.
   release_screen();
   

   readkey();//Wait for a keypress

   destroy_bitmap(my_pic);//Release the bitmap data


   return(0);// Exit with no errors
}
END_OF_MAIN(); // This must be called right after the closing bracket of your MAIN function.
               // It is Allegro specific.


Page : 1