A Quick and Dirty VCL Sprite Engine - Part V

By: John Ray Thomas

Abstract: Run through a series of images that comprise an animation cycle.

Run through a series of images that comprise an animation cycle.

This piece of the sprite engine can be best represented by a list or an array of TBitmap's. I use an array becuase it's faster and I can use the [ ] operator which makes accessing the elements easier. The following is an example of loading a number of bitmaps with TBitmap that comprise a complete animation cycle.
void TankSprite::LoadImages(AnsiString BaseName, int NumImages)
    name = BaseName;
    numImages = NumImages;

    for (int i = 0; i < NumImages; i++)
        Graphics::TBitmap *image = new Graphics::TBitmap();
        AnsiString file(name);
        file += i + 1;
        file += ".bmp";
        image->Transparent = true;
        imageList[i] = image;
    Position = TPoint(imageList[0]->Width2,imageList[0]->Height2);
    clipRect.Top = 0;
    clipRect.Left = 0;
    spriteWidth = imageList[0]->Width;
    spriteHeight = imageList[0]->Height;
    clipRect.Right = imageList[0]->Width;
    clipRect.Bottom = imageList[0]->Height;
The drawing of the sprite will look something like the following:

imageList[currentImage]->Draw(Position.x, Position.y, backbuffer);

where currentImage is an int that keeps track of which sprite frame should be displayed and gets updated, usually according to some time lapse.

The Sprite that is used for the example is not an animation but an image of a tank that faces 8 directions. For this Sprite I calculate the directions in radians and populate a lookup table.

float step = 45.0;

    for (int i = 0; i < NumImages; i++)
        angleTable[i] = (step * i)  * (3.1415)180.0;

I then use an enum to represent the directions and the location in the image list.


For the tank sprite I load the up-facing sprite first (NORTH) then work clockwise around unit circle and load the appropriate-facing sprite into the image list. When it comes time to draw, I use the direction type to make things clearer.

Server Response from: ETNASC03