Defining a Sprite Data Structure
A sprite is a graphic object that moves independently on the screen. We need a memory structure to hold all its properties in one place. This structure is often called a Control Block or a Sprite Descriptor.
Example Sprite Descriptor (10 Bytes):
Offset | Size (Bytes) | Purpose |
---|---|---|
+0 |
2 | X-Coordinate (16-bit) |
+2 |
2 | Y-Coordinate (16-bit) |
+4 |
2 | Sprite Graphic Address (Pointer to the pixel data) |
+6 |
1 | Frame Number (If sprite is animated) |
+7 |
1 | Status (Bitwise flags: Active, Firing, Hit) |
+8 |
2 | Pointer to Next Sprite (For a linked list) |
Reading Sprite Properties
We use the Index Registers ($IX$ or $IY$) for fast, organized access to these properties.
Example: Getting X and Y Coordinates
LD IX, SPRITE_1_ADDR ; IX points to the start of the descriptor
; Read the 16-bit X-coordinate (Offset +0)
LD L, (IX + 0) ; Load Low Byte of X
LD H, (IX + 1) ; Load High Byte of X (HL now holds X)
; Read the 8-bit Status Flag (Offset +7)
LD A, (IX + 7) ; A now holds the Status byte
The Core Drawing Routine
The drawing routine must convert the sprite’s (X, Y) coordinates into a screen memory address, then copy the pixel data to that location.
Steps to Draw:
- Calculate Screen Address: Use the sprite’s X and Y to find the starting address on the screen (Part 23).
- Set Source: Point a pointer (e.g.,
HL
) to the Sprite Graphic Data (address from offset +4). - Set Destination: Point another pointer (e.g.,
DE
) to the calculated Screen Address. - Transfer: Use
LDIR
to copy the graphic data row by row.
Challenge: Transparency
In simple Z80 systems, LDIR
is too fast; it overwrites the background. A proper sprite routine must:
- Read the Background byte.
- AND the Background with the inverse of the Sprite Mask (the transparent area).
- OR the result with the Sprite Data.
- Write the combined byte back. This is slower but creates proper transparency.