Topic : Bitmap Buffering
Author : Jay
Page : 1 Next >>
Go to page :


Lesson 5- Bitmap Buffering and the key[] structure


Double buffering: How to make things look 'smooth'.
                       How to use the key[] commands
                       create_bitmap(); function



Set up the main initializing 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  


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


Declare another bitmap - our 'buffer'

BITMAP *buffer; //Declare a BITMAP called buffer.

Here is a new function. As you can see, we have already declared a BITMAP called 'buffer'.
This bitmap will be our 'buffer' which will make things look smooth. We obviously don't want to load a bitmap into its memory - we are just going to be using it to be drawing bitmaps to it.
So we use the function create_bitmap();

create_bitmap takes two parameters,
      create_bitmap(width, height);

and will just create an empty bitmap in that memory location.

Pretty simple. So, lets create a bitmap with the dimensions of the screen
buffer = create_bitmap(640,480); //Create an empty bitmap.  

Delcare some integers for later use

int my_pic_x = 0;// Holds our pictures X coorinate
int my_pic_y = 0;// Holds our picture's Y coordinate


As you can see in this while loop, we will be using the key[] structure.

The key[] structure is simple. You can use it for testing - such as
if(key[KEY_UP])
{
   do_something();
}

For this IF loop, if the user hits the UP key, the loop will execute.

The same is for a while loop - such as the one following this comment. While the escape key is not pressed, keep looping.

A list of all the keys can be found in the allegro header files (which file depends on which version of allegro you are using. It is easy to spot the list of keys, just scroll through the header files - you'll KNOW when you've hit the list)

while(!key[KEY_ESC])//If the user hits escape, quit the program
{



Here is a series of tests- so we can move our bitmap around the screen

if(key[KEY_RIGHT])// If the user hits the right key, change the picture's X coordinate
{
   my_pic_x ++;// Moving right so up the X coordinate by 1
}
else if(key[KEY_LEFT])// Ditto' - only for left key
{
   my_pic_x --;// Moving left, so lower the X coordinate by 1
}
else if(key[KEY_UP])// If the user hits the up key, change the picture's Y coordinate
{
   my_pic_y --;// Moving up, so lower the Y coordinate by 1
}
else if(key[KEY_DOWN])// Ditto' - only for down
{
   my_pic_y ++;// Moving down, so up the Y coordinate by 1
}




Now, here is where the buffering comes into play.
As you can see, there are 3 lines of code. 2 regular lines, and 1 commented out.

draw_sprite(buffer, my_pic, my_pic_x, my_pic_y);
blit(buffer, screen, 0,0,0,0,640,480);


//draw_sprite(screen, my_pic, my_pic_x, my_pic_y);

The first two lines do the buffering.
If you comment out those two lines, and uncomment this line:

//draw_sprite(screen, my_pic, my_pic_x, my_pic_y);
The program will run without buffering.

Here is how buffering works:
  If you just draw everything to the screen, you will notice that you get a lot of flicker. This is because you are drawing DIRECTLY to the screen, which is updated god knows how many times! Buffering gets rid of flicker because it minimizes direct drawing to the screen. If you draw each component onto the 'buffer', and then the complete screen sized buffer to the screen: flicker is eliminated. It's that simple. You will also notice, in the UNBUFFERED mode, that the ball will ZOOM across the screen (if you have a relatively fast computer). Even in BUFFERED mode, things can move pretty fast (or slow - again depending on the speed of your computer). This problem is fixed using timers, which will be explained in the next lesson, so don't worry about this for now

acquire_screen();// Get the screen
draw_sprite(buffer, my_pic, my_pic_x, my_pic_y);//Draw the picture to the buffer
blit(buffer, screen, 0,0,0,0,640,480);//Draw the buffer to the screen
release_screen();// Release it

/* Uncomment the following 3 lines, and comment out the 4 previous lines in order to view an UNBUFFERED mode  of drawing. */
//acquire_screen();// Get the screen
//draw_sprite(screen, my_pic, my_pic_x, my_pic_y);
//release_screen();// Release it
}



Do all of the shut-down stuff

destroy_bitmap(my_pic);//Release the bitmap data
destroy_bitmap(buffer);//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.



And that's it! Double-Buffering (bitmap buffering) is pretty easy, eh?

Draw everything to the buffer, and then the buffer to the screen. :D



Lesson 5's Code:
/*******************
Allegro Newbie Tutorial

  by
LoomSoft

http://loomsoft.cjb.net
*******************/
/**************************************************
LESSON 5 :
Double buffering: How to make things look 'smooth'
and how to use the key[] commands

Also the: create_bitmap(); function
***************************************************/

#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

   BITMAP *buffer; //Declare a BITMAP called buffer.

   /************************************
   Here is a new function. As you
   can see, we have already declared
   a BITMAP called 'buffer'. This bitmap
   will be our 'buffer' which will make
   things look smooth.
   We obviously don't want to load a
   bitmap into its memory - we are just
   going to be using it to be drawing bitmaps
   to it.

   So we use the function
      create_bitmap();

   create_bitmap takes two parameters,
      create_bitmap(width, height);
   and will just create an empty bitmap
   in that memory location.

    Pretty simple. So, lets create a bitmap
   with the dimensions of the screen
   ************************************/
   buffer = create_bitmap(640,480); //Create an empty bitmap.

   /************************************
    Declare some integers for later use
   ***********************************/
   int my_pic_x = 0;// Holds our pictures X coorinate
   int my_pic_y = 0;// Holds our picture's Y coordinate



   /****************************************
   As you can see in this while loop, we
   will be using the key[] structure.

   The key[] structure is simple. You can use
   it for testing - such as

   if(key[KEY_UP])
   {
     do_something();
   }

   For this IF loop, if the user hits the UP
   key, the loop will execute.


   The same is for a while loop - such as
   the one following this comment.
   While the escape key is not pressed, keep
   looping.

   A list of all the keys can be found in
   the allegro header files (which file depends on
   which version of allegro you are using. It is easy
   to spot the list of keys, just scroll through the
   header files - you'll KNOW when you've hit the list)
   ****************************************/
   while(!key[KEY_ESC])//If the user hits escape, quit the program
   {
      /*****************************
      Here is a series of tests - so we can move
      our bitmap around the screen
      ****************************/
      if(key[KEY_RIGHT])// If the user hits the right key, change the picture's X coordinate
      {
         my_pic_x ++;// Moving right so up the X coordinate by 1
      }
      else if(key[KEY_LEFT])// Ditto' - only for left key
      {
         my_pic_x --;// Moving left, so lower the X coordinate by 1
      }
      else if(key[KEY_UP])// If the user hits the up key, change the picture's Y coordinate
      {
         my_pic_y --;// Moving up, so lower the Y coordinate by 1
      }
      else if(key[KEY_DOWN])// Ditto' - only for down
      {
         my_pic_y ++;// Moving down, so up the Y coordinate by 1
      }
      
      /***************************************************************
      Now, here is where the buffering comes
      into play.

      As you can see, there are 3 lines of code.
      2 regular lines, and 1 commented out.

      draw_sprite(buffer, my_pic, my_pic_x, my_pic_y);
       blit(buffer, screen, 0,0,0,0,640,480);

      //draw_sprite(screen, my_pic, my_pic_x, my_pic_y);

      The first two lines do the buffering. If you comment
      out those two lines, and uncomment this line:
      
      //draw_sprite(screen, my_pic, my_pic_x, my_pic_y);

      The program will run without buffering.

      Here is how buffering works.
      If you just draw everything to the screen, you
      will notice that you get a lot of flicker. This is
      because you are drawing DIRECTLY to the screen, which
      is updated god knows how many times!

      Buffering gets rid of flicker because it minimizes
      direct drawing to the screen.
      If you draw each component onto the 'buffer', and
      then the complete screen sized buffer to the screen:
      flicker is eliminated. It's that simple.

      You will also notice, in the UNBUFFERED mode,  that the
      ball will ZOOM across the screen (if you have a relatively fast
      computer).
      Even in BUFFERED mode, things can move pretty fast (or slow - again
      depending on the speed of your computer).

      This problem is fixed using timers, which will be


Page : 1 Next >>