Hardware sprites: 32 x 16x16 from 64 shapes, collisions, frame counter
rtl/sprites.v draws sprites between the graphics and the text from a pair of line buffers filled one graphics line ahead. Ports 70h-77h set position, shape, flip and on/off and load shapes; 78h is a frame counter; 80h-87h report sprite-sprite and sprite-graphics collisions per frame. The graphics fill (CLS) turns sprites off unless bit 7 is set. Testbench checks pixels, priority, flip, edge clipping, collisions and CLS. Manual, README and basic/examples/sprites.bas demo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c78073320b
commit
13373244e8
9 files changed
+480
-7
No files matched your search
+69
-1
@@ -441,6 +441,72 @@ place. Use `TIM` to set the pace:
|
||||
70 GOTO 20
|
||||
```
|
||||
|
||||
### Sprites
|
||||
|
||||
Sprites are small pictures that the hardware draws over the graphics wherever
|
||||
you put them. To move one, give it a new position: nothing needs erasing or
|
||||
redrawing, nothing flickers, and the Z-80 does no drawing work. They are what
|
||||
moving things in a game should be made of.
|
||||
|
||||
- **32 sprites**, numbered 0–31, each 16 × 16 graphics pixels.
|
||||
- **64 shapes**, numbered 0–63, that the sprites show. Several sprites can
|
||||
show the same shape.
|
||||
- **Colours:** each pixel of a shape is one of the 16 colours; colour 0 is
|
||||
see-through.
|
||||
- **Layers:** sprites are in front of the graphics and behind the text. Where
|
||||
sprites overlap, the lower number is in front.
|
||||
- **Collisions:** the hardware notices when a sprite touches another sprite,
|
||||
or touches a graphics pixel that isn't colour 0.
|
||||
|
||||
BASIC drives them with `OUT` and `INP`:
|
||||
|
||||
| Port | `OUT` | `INP` |
|
||||
|---|---|---|
|
||||
| 112 | Pick sprite *n* (0–31) for ports 113–117 | — |
|
||||
| 113, 114 | X: `X AND 255`, then `INT(X / 256)` | — |
|
||||
| 115 | Y; the sprite moves to the new X and Y together | — |
|
||||
| 116 | Shape (0–63) | — |
|
||||
| 117 | 1 = show, 0 = hide; add 2 to flip left-right, 4 to flip upside down | — |
|
||||
| 118 | Start defining shape *n* (0–63) | — |
|
||||
| 119 | The next two pixels of the shape: 16 × left colour + right colour | — |
|
||||
| 120 | — | Frame counter: goes up by one 60 times a second, as each picture is finished |
|
||||
| 128–131 | — | Sprites that touched another sprite in the last frame: 128 has a bit for sprites 0–7 (1, 2, 4 … 128), 129 for 8–15, 130 for 16–23, 131 for 24–31 |
|
||||
| 132–135 | — | The same for sprites that touched the graphics |
|
||||
|
||||
**Defining a shape.** `OUT 118, n` and then 128 `OUT 119`s: the 16 rows from
|
||||
the top, each as 8 bytes of two pixels. A shape stays defined until the power
|
||||
goes off, even through `NEW`, so define it once.
|
||||
|
||||
**Placing a sprite.** X and Y are its top-left pixel, the same as for `PLOT`.
|
||||
X can go from 0 to 511 and Y from 0 to 255; values past the edge wrap round, so
|
||||
X = 500 puts the sprite 12 pixels off the left edge, half showing.
|
||||
|
||||
```
|
||||
10 CLS
|
||||
20 OUT 118, 0: FOR I = 1 TO 128: OUT 119, 204: NEXT I : REM SHAPE 0: COLOUR 12
|
||||
30 OUT 112, 0: OUT 116, 0: OUT 117, 1 : REM SPRITE 0 SHOWS IT
|
||||
40 FOR X = 0 TO 304
|
||||
50 OUT 112, 0: OUT 113, X AND 255: OUT 114, INT(X / 256): OUT 115, 100
|
||||
60 F = INP(120)
|
||||
70 IF INP(120) = F THEN 70 : REM WAIT FOR THE NEXT FRAME
|
||||
80 NEXT X
|
||||
```
|
||||
|
||||
Waiting for the frame counter to change, as in lines 60–70, keeps movement
|
||||
smooth: one step per frame, 60 a second.
|
||||
|
||||
**Collisions.** `IF INP(128) AND 1 THEN …` asks whether sprite 0 touched
|
||||
another sprite in the last frame; `INP(132) AND 1` whether it touched the
|
||||
graphics, such as a wall drawn with `FILL`. A collision only counts where
|
||||
pixels that aren't colour 0 overlap, so the see-through corners of a round
|
||||
shape don't count.
|
||||
|
||||
**Turning them off.** `CLS` hides every sprite (the shapes are kept), and so
|
||||
does restarting the Z-80. `OUT 117, 0` hides the picked one.
|
||||
|
||||
`basic/examples/sprites.bas` is a complete example: a robot steered with
|
||||
W A S D among bouncing balls, with shapes written as rows of hex digits.
|
||||
|
||||
### Speed
|
||||
|
||||
`CLS` clears the graphics in under a millisecond, and a line right across the
|
||||
@@ -502,12 +568,13 @@ kept free for it.
|
||||
| 33 | — | left two digits of the 7-segment display |
|
||||
| 48, 49, 50 | — | graphics X (low byte, high bit), Y |
|
||||
| 51 | colour at X, Y | plot a pixel at X, Y and move right |
|
||||
| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour |
|
||||
| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour; also hides the sprites, unless 128 is added |
|
||||
| 64 | bit 0: flash byte in progress; bit 1: last flash command refused | 1 selects the flash and starts a command, 0 ends it |
|
||||
| 65 | the byte the flash sent back during the last `OUT 65` | a byte to the flash |
|
||||
| 96 | why the monitor was entered (used by the monitor) | monitor bank and single step (used by the monitor) |
|
||||
| 97 | — | enter the monitor (what `MONITOR` does) |
|
||||
| 98–105 | memory read and breakpoint registers (used by the monitor) | |
|
||||
| 112–120, 128–135 | sprites (section 7) | sprites (section 7) |
|
||||
|
||||
```
|
||||
10 FOR I = 0 TO 255
|
||||
@@ -984,6 +1051,7 @@ bouncing ball and a Mandelbrot set, then prints how long it all took.
|
||||
| User functions | FNA–FNZ, one argument each |
|
||||
| FOR nesting / GOSUB nesting | 16 / 32 |
|
||||
| Graphics | 320 × 240, 16 colours |
|
||||
| Sprites | 32 on screen, 64 shapes, 16 × 16 pixels each |
|
||||
| Text | 80 × 32, or 132 × 64 |
|
||||
| Files in flash | 64, 1,784 KB in all, 32 KB each |
|
||||
| Open data files | 3 |
|
||||
Reference in new issue
Block a user