Bit sprites

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Bit sprites

Post by angros47 »

There are several ways to create a sprite, and drawing it while preserving the background. One way is to redraw the whole frame (in case of animated background, it has to be redrawn anyway), another is to redraw only the part where the sprite has been drawn, another one consists in using some sort of overlay, and drawing the sprite on it.

If a single bit depth sprite is needed, and indexed color mode is used, a cheap solution could be to use one bit of the color depth to store the sprite informations: by setting, or cleaning that bit, the sprite can be drawn or erased without touching the rest of the image. By setting the color palette appropriately, it is possible to have all the pixel with that bit set of the same color (that will be the sprite color).

Such a solution can be extremely efficient if planar graphic mode is used (as on the Amiga, or on the old VGA in SCREEN 12), because it would require to work only on one bit plane, and would make erasing a sprite a very quick operation. In Basic, the PUT command with OR can be used to draw, and the PUT command with AND can be used to erase the sprite.

This solution allows to have single color sprites, with no limit in their number, using a single page. Only color 0-7 can be used for the background. It is not much impressive, but it can be used in QBASIC, on the limited hardware where that language was supposed to be used.

The following demo works in FreeBasic, and in original QBASIC, with no changes:

Code: Select all

'$LANG:"QB"
SCREEN 12
DIM Sprite(150)
DIM Eraser(150)
PALETTE 8, 4144959
PALETTE 9, 4144959
PALETTE 10, 4144959
PALETTE 11, 4144959
PALETTE 12, 4144959
PALETTE 13, 4144959
PALETTE 14, 4144959
PALETTE 15, 4144959

COLOR 8
CIRCLE (10, 10), 10
CIRCLE (10, 10), 10
CIRCLE (5, 7), 2: CIRCLE (15, 7), 2
CIRCLE (10, 13), 5, , 3.1415, 6.28

GET (0, 0)-(20, 20), Sprite
LINE (0, 0)-(20, 20), 7, BF
GET (0, 0)-(20, 20), Eraser

FOR I = 0 TO 200
  COLOR I MOD 7
  PRINT "Background";
NEXT

FOR I = 0 TO 400
  X = SIN(I / 5) * 200 + 320
  Y = I
  PUT (X, Y), Sprite, OR
  SLEEP 1
  PUT (X, Y), Eraser, AND
NEXT
Post Reply