Sprites

Table of Contents:
What are sprites?
The Sprite structure
Creating and deleting sprites
Example program


What are sprites?

A sprite is a common term for a rectangular graphic stored in memory that can be transfered to the screen. They are commonly used in side scrolling, role playing, fighting, shoot-em-ups, and about every other type of game. For our usage, a sprite has the following attributes:
  • A height and a width
  • A chunk of memory that holds the pixel data
  • An implied clear color
With these things, we can make a flexible package well suited to animation. Of course for real time animation though, it has to be fast. In the next couple of articles, we will develop sprite capabilities and add some animation functions to our graphics unit.


The Sprite structure

To make keeping track of our sprites easy, we will have a structure that holds all of the pertinent information about our sprite. It looks like this:

TYPE
  SpriteType = RECORD            { Each record is convieniently 8 bytes long }
    Height, Width : INTEGER;     { Height and Width of sprite }
    Data          : POINTER;     { Pointer to the color data  }
  END;
This structure allows us to load a sprite in the beginning of our program and not have to worry about keeping track of it until we delete it. This means that we need some procedures to create and delete sprites!


Creating and freeing sprites

Below are some simple procedures that we can use to create and delete sprites. The biggest part is allocating and deleting the memory for the sprite:

PROCEDURE KillSprite(VAR Sprite : SpriteType);
BEGIN
  WITH Sprite DO
  BEGIN
    FREEMEM(Data, Width*Height);
    Width := 0; Height := 0; Data := NIL;
  END;
END;

PROCEDURE CreateSprite(VAR Sprite : SpriteType; Width, Height : INTEGER);
BEGIN
  IF Sprite.Data <> NIL THEN KillSprite(Sprite);
  Sprite.Width := Width;
  Sprite.Height := Height;
  GETMEM(Sprite.Data, Width*Height);
END;
As you can see, the fundemental sprite unit is the Sprite, not a pointer to a sprite. At once, I was considering using pointers to the SpriteType (^SpriteType) instead of passing sprites as VAR parameters. But then I realized that each time you do NEW(Sprite), you only allocate eight bytes! Therefore, in my programs, I use an array of sprites, and the data for the sprite is allocated at run time.


Example Program

Sorry, no example program for this stuff!


  • Created by Chris Lattner