A Quick and Dirty VCL Sprite Engine - Part I

By: John Ray Thomas

Abstract: Draw an image to a specific position and region.

Draw an image to a specific position and region.

The simple overview of the steps of placing images on a drawing area with Windows API is
  • load a bitmap (.bmp) file as a device independant bitmap,
  • associate it to an in-memory HDC to become a device-dependant bitmap,
  • handle a WM_PAINT message and use BitBlt to draw it to an in-view HDC.
Using the VCL these steps become
  • create a TBitmap object and call it's member function LoadFromFile or LoadFromResource,
  • which automatically creates a device-dependant bitmap to the in-memory HDC, TBitmap::Canvas,
  • place it on the TForm at a specific postion by calling the TForm's Canvas::Draw function during an OnPaint event.
These steps look like this in VCL code.
Graphics::TBitmap *bmp = new Graphics::TBitmap();
Canvas->Draw(0,0, bmp);

This places the top-left pixel of the bitmap at the location (0,0) on the main forms Canvas (our visible HDC) and covers the rectangular region with the image from (0,0) to (bmp->Left, bmp->Bottom).

Server Response from: ETNASC01