The Double Buffering Technique using Allegro

Posted: July 30, 2008 in Allegro, Programming
Tags: , ,

We will learn how to use the double buffering technique to make things look smooth.
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 by the operating system at an unknown rate! 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 since you draw everything at once in one pass. 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.

#include             // You must include the Allegro Header file

volatile long speed_counter=0;  // A long integer which will store the value of the
                                // speed counter.
void increment_speed_counter()  // A function to increment the speed counter
    speed_counter++;            // This will just increment the speed counter by one
END_OF_FUNCTION(increment_speed_counter);    // Make sure you tell it that it's the end of the
                                             // function

int main(int argc, char *argv[])
    allegro_init();    // Initialize Allegro
    install_keyboard();    // Initialize keyboard routines
    install_timer();    // Initialize the timer routines
    LOCK_VARIABLE(speed_counter);    //Used to set the timer - which regulates the game's
    LOCK_FUNCTION(increment_speed_counter);    //speed.
    install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));    // Set our BPS
    set_color_depth(32);    // Set the color depth
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 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.

    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

    while(!key[KEY_ESC])    // If the user hits escape, quit the program
        if (speed_counter % 60 == 0)

        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
        clear_bitmap(buffer);    // Clear the contents of the buffer bitmap
    }     //This is the closing bracket for the (key[KEY_ESC]) test

    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.

Click below to download source of this lesson:
Download Source

  1. WesleyB says:

    Thanks for writing this I didnt get the timer to work for my game but the double buffering fixed the problem with my images flashing. I had done the same thing when i wrote a couple things for the PSP but i didnt know how to do it on the computer. I had looked around on a lot of other sites and non of them were as easy to read as yours so thanks a lot.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s