A Quick and Dirty VCL Sprite Engine - Part II

By: John Ray Thomas

Abstract: Draw a background image and foreground sprites without flicker.

Draw a background image and foreground sprites without flicker.

To properly update and display the screen without flicker requires the use of a technique called double-buffering. Double-buffering means that the scene is first drawn into memory. When it is finished drawing the complete scene, it BLT's the entire contents into the visible area as a single image. The order of drawing is the backgound image first then all sprites that are to be displayed on top of it. If a bitmap's rectangular region overlaps another bitmap and it is drawn after it, it will be dispalyed on top of it. The following VCL code shows this in practice.

Graphics::TBitmap* backbuffer = new Graphics::TBitmap(); 
Graphics::TBitmap* background = new Graphics::TBitmap(); 
//Contsruct scene by first drawing the background then all of the sprites in their z-order 
backbuffer->Canvas->Draw(0, 0, background); 
backbuffer->Canvas->Draw(someX, someY, someBitmap); 
//"Flip" finished scene from the backbuffer to the frontbuffer. 
Form1->Canvas->Draw(0, 0, backbuffer);

This shown technique actually replaces the entire background every frame to erase previously drawn sprites. You can gain a few frames by drawing as little of the background as possible. One technique used to do this is called "dirty rectangle" animation. Essentially, instead of redrawing the whole background to erase the last scene, only the rectangular region that the sprite occupied in the previous frame is redrawn with the corresponding rect in the background bitmap. Then the sprite is drawn in it's new position.

Server Response from: ETNASC03