Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) Double buffering


Technique to prevent flicker when working with graphics (it has nothing to do with the data type double).


The idea of double buffering is to first paint all screen graphics to an invisible buffer, then drawing this buffer to screen.


Drawing to screen directly is called single buffering and causes flicker. Single buffering and double buffering can be compared using the program DemoBuffering.


To clean a buffer, often the background is drawn on the buffer first.


In C++ Builder, this looks somewhat like the code below (but see the program DemoBuffering for a complete program):


#include <cassert>
#include <vcl.h>
#pragma hdrstop

#include "UnitFormDoubleBuffering.h"

#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)

void __fastcall TForm1::Timer1Timer(TObject *Sender)
  //Clean the buffer by drawing the background on the buffer

  //Draw the sprites on the buffer
  const int nSprites = 100;
  for (int i=0; i!=nSprites; ++i)
    const int x = std::rand() % (ClientWidth - ImageSprite->Width );
    const int y = std::rand() % (ClientHeight - ImageSprite->Height);

  //Finally, copy the buffer to the Form

void __fastcall TForm1::FormResize(TObject *Sender)
  //Assume all Images are invisible
  //Set the Buffer and Background to the right size
  ImageBuffer->Picture->Bitmap->Width = ClientWidth;
  ImageBuffer->Picture->Bitmap->Height = ClientHeight;
  ImageBackground->Picture->Bitmap->Width = ClientWidth;
  ImageBackground->Picture->Bitmap->Height = ClientHeight;






Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict