The Allegro Wiki is migrating to github at

Difference between revisions of "Double buffering"

From Allegro Wiki
Jump to: navigation, search
(Removed task and incomplete status.)
m (Notes: link update)
Line 24: Line 24:
* Double buffering uses only memory bitmaps, so you can safely use [[transcluency]] with it.
* Double buffering uses only memory bitmaps, so you can safely use [[Transcluent drawing|translucency]] with it.

Revision as of 13:44, August 2, 2007


Double buffering is one of the most basic methods of updating the display. This is often the first buffering technique adopted by new coders to combat flickering.

Double buffering uses a memory bitmap as a buffer to draw onto. The buffer is then drawn onto screen. If the objects were drawn directly to screen, the display could be updated mid-draw leaving some objects out. When the buffer, with all the objects already on it, is drawn onto screen, the new image will be drawn over the old one (screen should not be cleared). If the display gets updated before the drawing is complete, there may be a noticeable shear in the image, but all objects will be drawn. Shearing can be avoided by using vsync.


Double buffering is extremely easy to use. After initializing a graphics mode, you need to create the buffer:

<highlightSyntax language="c"> BITMAP *buffer;

buffer = create_bitmap(SCREEN_W, SCREEN_H); clear_bitmap(buffer); </highlightSyntax>

Then, do all your drawing onto buffer. Finally, you have to blit the buffer to screen:

<highlightSyntax language="c"> //In this example a sigle pixel is being drawn putpixel(buffer, pixel_x, pixel_y, pixel_color);

//After everything has been drawn onto buffer, buffer needs to be drawn onto screen blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); </highlightSyntax>


  • Double buffering uses only memory bitmaps, so you can safely use translucency with it.


<highlightSyntax language="c">

  1. include <allegro.h>

BITMAP *buffer;

void update_screen(void) { blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // blit the buffer to the screen }

int init_double_buffering() { // create the memory buffer buffer = create_bitmap(SCREEN_W, SCREEN_H);

if(!buffer) return FALSE;

return TRUE; }

int main() { int x, y;

// install allegro, set graphics mode, etc allegro_init(); install_keyboard();

set_color_depth(32); if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480, 0,0)) { allegro_message("Unable to set graphics mode.\n"); return 1; }

// initialize page flipping if (!init_double_buffering()) { set_gfx_mode(GFX_TEXT,0,0,0,0); allegro_message("Unable to initialize page flipping.\n"); return 1; }

// default coordinates for the rectangle x = SCREEN_W / 2 - 5; y = SCREEN_H / 2 - 5;

// main game loop while (!key[KEY_ESC]) { // game logic if (key[KEY_LEFT] && x) --x; if (key[KEY_RIGHT] && x < SCREEN_W-10) ++x; if (key[KEY_UP] && y) --y; if (key[KEY_DOWN] && y < SCREEN_H-10) ++y;

// you draw everything to buffer clear(buffer); rect(buffer, x,y, x+9,y+9, makecol(255,255,255)); textout_centre_ex(buffer, font, "Use arrow keys to move box. ESC to quit.", SCREEN_W/2, SCREEN_H/2, makecol(255,255,255), -1);

// show the offscreen page update_screen(); }

return 0; } END_OF_MAIN() </highlightSyntax>

See Also