Sprites: 128 Pictures and Two Line Buffers
Picture a busy game screen: your ship, a dozen enemies, a hail of bullets, an explosion or two. On the Next, every one of them can be a sprite, up to 128 at once, each moving on its own. A screen of 128 sprites is a picture in its own right, 320 × 256 dots, so you would expect the hardware to keep it somewhere. It does not. It holds two lines. So how do 128 sprites fit into two lines? I think the answer is one of the best bits of engineering in the whole machine, and by the end of the chapter I hope you will agree.
First, why bother with sprites at all? Almost everything that moves on a screen is one: the player, the enemies, the bullets and explosions of a game, and the pointer, cursor and dragged icon of an application. A machine without them, like the 48K Spectrum, makes the CPU draw every moving thing into screen memory itself, then put back what was behind it before drawing it again one step further on. That work ate most of a Spectrum game’s frame, and drawing into the bitmap is why moving objects took on the colours of the cells they crossed. Hardware sprites take all of it off the CPU, which is left free for the game itself. If you ask me, nothing on the Next saves a game programmer more of the CPU’s time.
A sprite is a picture that moves on its own. Your program says “put this 16 × 16 picture at X 140, Y 90”, and the hardware draws it there, in front of the background, without your program touching a byte of screen memory. Want to move it? Change two numbers. The Next has 128 of them, each up to 16 × 16 dots, enlarged up to eight times in either direction, turned and mirrored, and linked into objects far bigger than one sprite. The big picture chapter calls the sprites the troupe, a company of performers, each with a costume and a place of its own, and I will stay in that theatre here: you will meet their wings, their sketchbook and their cast list.
Read the overview chapter, the big picture, and the palettes chapter first: this chapter builds on the layers, the clip windows and the palette offset they explain.
The road ahead
We begin with what your program sets up: the square of positions a sprite lives in, the patterns that hold its pictures, and the four or five attribute bytes that say where and how each sprite is drawn, with the ports that upload them. Then the sprites go on the machine: eight orientations, four sizes, and anchors that drag whole groups of sprites along with them. After that comes the key idea, the two line buffers, and with them the answer to which sprite ends up in front. The heart of the chapter is the time budget of a line: how many sprites the hardware can draw before it runs out of time, and what it does when it does. If you read only one section closely, make it that one; it is the part I find most fascinating. We finish with sprites over the border, the clip window and collisions, and then gather it all up.
The pictures are the Next with the sprites alone on screen (the ULA switched off, the fallback colour black, unless a picture needs the border), at 128K/+3 timing and 50 Hz. Each picture comes from a checked test that first finds the dots the text describes. The time budget’s numbers come from the hardware’s sprite engine simulated clock by clock.
Sprite space
A sprite’s position is two 9-bit numbers, X and Y, each 0–511. They place the sprite’s top-left dot in a square of 512 × 512 positions, sprite space, of which only a corner is ever shown: the 320 × 256 wide area, X 0–319 and Y 0–255. The Spectrum’s display area sits inside it at X 32, Y 32, so a sprite at (32, 32) starts exactly at the top-left corner of the ULA’s picture, and one at (0, 0) at the top-left corner of the border. I always remember the 32 as “one border’s width” in both directions.
Why 512 positions for a 320-dot screen? So that a sprite can be partly off any edge. A 16-dot sprite moving left from X 0 goes to X 511, 510, …: positions wrap round at 512, so at X 500 its first 12 dots are past the right of sprite space, out of sight, and its last 4 come back in at X 0–3. To your program it is X −12, and the sprite never notices that it went round the back. I call the hidden part of sprite space the wings, as in a theatre: sprites wait there, out of the audience’s sight, for their cue. The same happens at the top with Y. With 8 bits, X could not even reach the right-hand side of a 320-dot screen; the ninth bit reaches it and leaves 192 hidden positions across and 256 down, more than the widest sprite. Roomy wings, if you ask me.
Patterns
A sprite’s picture is a pattern, and the patterns live apart from the sprites, in 16K of memory of their own inside the FPGA. Many sprites can show the same pattern, as many cells of the tilemap can show the same tile. Ten enemies of one kind cost you one pattern, not ten. I think of the pattern memory as the sketchbook: every sprite picks a drawing from it, and any number of them may use the same one. (The wardrobe, in this series, is the palette.)
A pattern is 16 × 16 dots, stored row after row, the left dot first, in one of two forms:
- 8-bit: a byte a dot, 256 bytes a pattern, so 16K holds 64 patterns. The byte is the palette index, and the sprite’s 4-bit palette offset is added to its upper four bits.
- 4-bit: a nibble a dot, two dots in a byte, the upper nibble on the left: 128 bytes a pattern, 128 patterns in the same 16K. The nibble is the low four bits of the index, and the palette offset becomes the upper four.
Here is how the two forms share one memory:
So the two forms share one memory, and your program mixes them freely (I find this one of the more generous design choices): 8-bit pattern 5 and 4-bit patterns 10 and 11 are the same bytes. A sprite names its pattern with six bits, N5–N0, which pick the 256-byte slot; that is the whole number of an 8-bit pattern. A 4-bit pattern needs one more bit, N6, which picks the half of the slot: 0 the first 128 bytes, 1 the second. So the 4-bit pattern’s number is 2 × slot + N6, and N6 is its lowest bit, even though it is the seventh one added. Let me warn you: this is the bit everyone puts in the wrong place the first time. Which form a sprite uses is a bit of its own (the H bit, which you meet with the attribute bytes below), not a setting of the memory.
A dot is transparent when its value, before the palette offset, equals the value in the Sprite
Transparency Index register, NextReg $4B ($E3 after a reset): the whole byte for an 8-bit
pattern, the low four bits of NextReg $4B for a 4-bit one. The colours come from one of the two
sprite palettes, chosen by bit 3 of the Palette Control register, NextReg $43. The palettes chapter’s
other layers
explains the offset and why the test comes before it.
The attribute bytes
Everything else about a sprite is in its attributes: four or five bytes for each of the 128 sprites, in a table of their own beside the pattern memory. If the patterns are the sketchbook, this table is the cast list: who is on stage, where they stand, and which drawing they use.
| Field | Where | Meaning |
|---|---|---|
| X | byte 0, and byte 2 bit 0 | 0–511, the left edge in sprite space |
| Y | byte 1, and byte 4 bit 0 | 0–511 (0–255 for a 4-byte sprite) |
| Palette offset | byte 2 bits 7–4 | added to an 8-bit dot’s upper four bits, or put above a 4-bit dot |
| Mirror and rotate | byte 2 bits 3–1 | X mirror, Y mirror, rotate 90 degrees clockwise (see Eight orientations) |
| Visible | byte 3 bit 7 | 0 hides the sprite (it still costs time: see The time budget of a line) |
| E | byte 3 bit 6 | 1: byte 4 is used |
| Pattern | byte 3 bits 5–0 (N5–N0), and byte 4 bit 6 (N6) for a 4-bit pattern | N5–N0 pick the 256-byte slot: for an 8-bit pattern that is its number, 0–63; for a 4-bit one, N6 picks the slot’s first or second 128 bytes, pattern 2 × slot + N6, 0–127 (see Patterns) |
| H | byte 4 bit 7 | 1: a 4-bit pattern |
| T | byte 4 bit 5 | for an anchor, how its relative sprites follow it (see Anchors and relative sprites) |
| Scale | byte 4 bits 4–3 (X), 2–1 (Y) | 1×, 2×, 4× or 8× (see Four sizes) |
Byte 4 bits 7 and 6 together have one more meaning. Look closely at the combination 01: H clear, so an
8-bit pattern, but N6 set, the half-slot bit that only a 4-bit pattern uses. It cannot mean anything
sensible as a pattern, so the hardware gives it a job: it makes the sprite a relative sprite, whose
bytes then mean something else (see Anchors and relative sprites). I love this kind of thrift: a
combination that would be nonsense becomes a feature.
Uploading through three ports
The pattern memory and the attribute table are not in the CPU’s memory map. Your program writes them through three ports, each with an index that steps on by itself, which I find a pleasure to use:
| Port | Name | What a write does |
|---|---|---|
port $303B | Sprite slot select | bits 6–0 pick the sprite whose attributes the next port $57 write sets, from its byte 0; bits 5–0 pick the 256-byte pattern slot the next port $5B write fills, and bit 7 starts it 128 bytes in, at the second 4-bit pattern |
port $57 | Sprite attributes | writes one attribute byte and steps to the next; after byte 4, or after byte 3 when its E bit is 0, it steps to the next sprite’s byte 0 |
port $5B | Sprite pattern | writes one pattern byte and steps to the next, through all 16K |
So you upload the whole table in one stream, four or five bytes a sprite, and a 4-byte sprite simply takes four. Its byte 4 is not written, and keeps whatever it held, but no part of the hardware reads it while E is 0:
The attributes can also be written through NextRegs, which the Copper can do in the middle of a frame.
The Sprite Number register, NextReg $34, picks the sprite (bits 6–0), and the Sprite Attribute 0–4
registers, NextRegs $35–$39, write its bytes 0–4 directly, in any order. The Sprite Attribute 0–4
with automatic post increment of Sprite Number registers, NextRegs $75–$79, write the same bytes
and then step NextReg $34 to the next sprite, so your program ends each sprite’s bytes with one of
them. Normally NextReg $34 and port $303B pick their sprites independently; bit 4
of the Peripheral 4 Setting register, NextReg $09, ties them, so that either one sets both (and then bit
7 of NextReg $34 is port $303B’s 128-byte bit). My advice is to pick one way in and stick to it
for a whole upload; mixing the two pointers is where the confusion starts.
Eight orientations
Bits 3–1 of byte 2 turn a pattern into any of its eight orientations, the same eight as a tile’s,
by the same rule: rotate 90 degrees clockwise first, then mirror. I always say it as a little
chant, “turn, then flip”, and that is all there is to remember. Meet the spaceship that flies
through the rest of the chapter: one pattern, a small ship pointing right with a big tail fin on top and
a small one underneath, drawn eight times with byte 2 from $00 to $0E. Its red nose tip shows which
way each one points:
As with tiles, nothing is moved, and I find that the neatest part. The hardware reads the pattern’s dots in a different order: for X mirror it walks each row from the right, for Y mirror it takes the rows from the bottom, and for rotate it walks a column where it would walk a row. The tilemap chapter’s eight orientations shows the rule worked out dot by dot; for a 16 × 16 pattern, replace its 7 by 15.
Four sizes
Bits 4–3 of byte 4 scale a sprite across and bits 2–1 scale it down, independently: 00 is 1×,
01 2×, 10 4× and 11 8×. Each dot is repeated, 2, 4 or 8 times, so an 8× sprite is 128 × 128
dots, two fifths of the screen’s width, drawn from the same 16 × 16 pattern. Here is the ship in six
sizes:
The scale belongs to the screen, not to the pattern: a sprite 4× across and 1× down is wide whether or not it is rotated, because the hardware rotates the pattern first and stretches the result. Scale is not free, though. You will see in the time budget of a line that a sprite twice as wide takes twice as long to draw. Big is cheap in memory and dear in time; keep that in mind.
Anchors and relative sprites
One sprite is at most 16 × 16 dots of detail. A boss, a mothership or a big character is several sprites moved together, and moving them one by one means rewriting every one’s position each frame. Tedious, and slow. I would not wish it on anyone. Relative sprites let the hardware do it for you: a sprite can be placed relative to an anchor, the last ordinary sprite before it in the table, and moves when the anchor moves. The name fits well: drop the anchor somewhere, and everything tied to it stays close by.
A sprite is relative when its bytes 3 and 4 say so: E set (five bytes) and byte 4 bits 7–6 01. Every
other sprite, visible or not, is an anchor, and the relative sprites that follow it in the table, up to
the next anchor, belong to it. A relative sprite’s bytes mean:
| Byte | In a relative sprite |
|---|---|
| 0, 1 | X and Y offsets from the anchor, signed: −128 to 127 |
| 2 | palette offset, X mirror, Y mirror, rotate; bit 0: add the anchor’s palette offset to its own |
| 3 | visible, E = 1, pattern N5–N0 |
| 4 | 01 in bits 7–6; bit 5: N6, the half-slot bit of a 4-bit pattern; bits 4–1: its own scale (composite only); bit 0: add the anchor’s pattern number to its own |
The anchor’s T bit (byte 4 bit 5) chooses how its relative sprites follow it:
- Composite (T = 0): the relatives take only the anchor’s position. Each keeps its own mirror, rotation and scale, so when the anchor turns, they do not. This is for objects made of separate parts: a ship whose turret turns on its own.
- Unified (T = 1): the group is one big sprite. The anchor’s rotation, mirroring and scale apply to the relatives’ offsets too; its rotation and mirroring are combined with each relative’s own, and its scale replaces theirs, so the whole object turns, mirrors and grows round the anchor as one picture.
In both kinds, a relative sprite is visible only if its anchor is, so hiding an object takes one bit, and it uses the anchor’s 4-bit or 8-bit form (H). Let’s see both kinds on the machine: four formations of three ships, all drawn from the same pattern, an anchor (grey hull) and two relatives at (+16, 0) (blue) and (0, +16) (green), each coloured by its own palette offset:
The unified group turns round the anchor because of how the offset is transformed. Rotation takes an offset (x, y) to (−y, x), a quarter turn clockwise; X mirror negates x, Y mirror negates y; then the scale multiplies both by 2, 4 or 8. Since every sprite is the same 16 × 16 and turns round its own middle, moving each corner like this keeps the pieces together: the blue sprite at (16, 0) goes to (0, 16), below the anchor, and the green one at (0, 16) to (−16, 0), to its left. Neat, isn’t it? One bit of one sprite, and the whole formation banks into the turn. I could watch it all day.
The two “add the anchor’s” bits make objects cheap to animate and recolour: with pattern-relative set, an object’s frames can be consecutive patterns, and changing the anchor’s pattern number moves every part to its next frame; with palette-relative set, one write to the anchor recolours the whole object. If you build big objects at all, my advice is to plan their patterns for this from the start.
Two lines, not a screen
Now for the key idea. The sprites do not have a picture memory. What they have is two line buffers, each 320 entries long, one for every dot of a line of the wide area. An entry is 9 bits: the 8-bit palette index of a sprite dot, and a ninth bit that says “a sprite dot is here”. I picture them as two easels: one stands in front of the audience, the other is backstage, being painted.
While the beam draws line v, one buffer is being shown: read out dot by dot at the 7 MHz dot rate, each entry cleared as soon as it has been read. At the same time the sprite engine fills the other buffer with line v + 1: it goes through the sprite table, sprite 0 first, and for every sprite that has a row on that line, writes the row’s dots into the buffer at the sprite’s X, one dot per 28 MHz clock. When the line ends, the two buffers swap, and the buffer just shown, empty again, is the next one filled. The easels change places, and the engine starts on a clean canvas:
The order of the table has nothing to do with the order of the dots. The engine can write any of the 320 entries at any time, so a sprite drawn later can land to the left of one drawn earlier: sprite 5 at X = 200 and sprite 6 at X = 40 are drawn in that order, and the line still comes out right, because nothing reads the buffer until the whole line is in it. Sprites never have to be sorted by X, or by anything else; their numbers only decide how long the line takes (see the time budget of a line, below) and which one is in front where two overlap. I find this the cleverest point of the design: the hardware never sorts anything, because a line buffer does not care in which order it is filled.
The swap happens when the wide area’s horizontal counter reaches 511, the dot just before the wide area’s first dot: the end of one line’s sprites is the start of the next’s. A line’s sprites are drawn in the line before it is shown, from the attribute table as it is at that moment. So a change to the table shows from the next line the engine draws: a sprite moved while the beam is on it shows its upper rows in the old place and the rest in the new, and a program that rewrites a sprite below the beam can use it again further down the same frame. One sprite, two appearances in a frame: a cheap trick worth keeping in your pocket, and the last chapter of the series puts it to work.
The buffer’s ninth bit is also what the rest of the machine sees. The sprite layer answers “opaque” for a
dot where it is set and “transparent” where it is not, and its index goes through the sprite palette to
the composition stage,
where the layer order decides whether a sprite is in front of Layer 2 or behind it. Bit 0 of the Sprite
and Layers System register, NextReg $15, which switches the sprites on, only gates this output.
The easel in front is covered, but the engine keeps drawing into the buffers when the sprites are switched off, and the collision flag (see
Collisions) keeps counting.
Which sprite is in front
When two sprites cover the same dot, both write it into the buffer, and the order of the writes decides.
The engine always works through the table from sprite 0 to sprite 127, so normally the later write
wins: sprite 127 is in front of sprite 126, and sprite 1 in front of sprite 0. Bit 6 of the Sprite and
Layers System register, NextReg $15, turns
it round: a dot is then written only where no sprite has written yet, so the first write wins and
sprite 0 is in front:
Either way the drawing order, and so the time it takes, is the same. Only what a write does to a dot already written changes. My preference is the “first write wins” setting for games, because then the sprite you care about most, at the top of the table, is also the one in front.
The time budget of a line
You have just seen that the engine draws a line’s sprites while the beam is showing the line before it. Its time for a line is fixed: from one swap of the line buffers to the next. Now let’s count how many sprites fit into that time, and what the engine does when they do not. Fair warning: this is the part of the sprite hardware I find most fascinating, and it takes a few sections.
The engine runs at 28 MHz, four clocks for every dot the beam moves. A 128K/+3 line is 456 dots, so the engine has 1,824 clocks from one swap to the next; at 48K or Pentagon timing, 448 dots and 1,792 clocks. All the numbers below are for 128K/+3 timing.
The beam is the dot the machine is showing at a given moment. Remember from the two line buffers that the beam shows line v while the engine draws line v + 1 into the other buffer. So when I say “the beam is at dot 316”, it means: at that moment the machine is showing dot 316 of line v. It does not mean anything about the line the engine is drawing. The beam is simply the engine’s clock face, the way to tell how far through its line’s time the engine is. I find it easiest to forget the picture here and think of the beam as the hand of a stopwatch.
The engine’s clock and the beam move together, so every clock has a beam position. The swap comes one dot before dot 0, so at clock c the beam is at dot c ÷ 4 − 1, rounded down. A few clocks you will meet again below:
- At clock 0 comes the swap, one dot before dot 0.
- At clock 1,156 the beam is at dot 288.
- At clock 1,268 the beam is at dot 316.
- At clock 1,284 the beam is at dot 320, the first dot after the wide area.
- At clock 1,824 comes the next swap.
What a sprite costs
The engine goes through the sprite table in order, from sprite 0 to sprite 127, starting 3 clocks after the swap:
- A sprite that is not on the line, because it is hidden or has no row there, costs 1 clock: the engine checks it and moves on.
- A sprite that is on the line costs 1 clock for the check and then 1 clock for every dot it walks, which is its width: 16, 32, 64 or 128. Transparent dots cost a clock like any other, and so do dots that fall off the left of the line. Only a sprite that runs off the right of the line stops early, at dot 320, unless it is far enough right to wrap round to the left edge. Parked wholly off the right, it costs 2.
So a 1× sprite on the line costs 17 clocks, and an 8× one 129. Dividing 1,824 by 17 suggests that about 107 1× sprites fit on a line. In fact only 75 do when every sprite is on it, because of one more rule, and it is the one I would ask you to read twice.
The cut-off window: when the engine gives up on a line
Near the end of every line there is a short stretch of time in which the engine is not allowed to begin drawing a sprite. I call it the sprite’s cut-off window: if the engine gets to a sprite during it, that sprite is not drawn at all, and the line is cut off there. There are four of them, one for each sprite scale, all ending just before the beam reaches dot 320:
The cut-off window belongs to the sprite the engine is about to draw: its length is that sprite’s own drawing time, so the wider the sprite, the longer its window, and the earlier it opens:
| Sprite scale | Cut-off window: the beam at | The engine’s clocks | Its length |
|---|---|---|---|
| 1× | dots 316–319 | 1,268–1,283 | 16 clocks |
| 2× | dots 312–319 | 1,252–1,283 | 32 clocks |
| 4× | dots 304–319 | 1,220–1,283 | 64 clocks |
| 8× | dots 288–319 | 1,156–1,283 | 128 clocks |
Nothing about it is uncertain: the same table always gives the same result, which I find reassuring. And it says nothing about where the sprite is on the screen: a 1× sprite at X = 10 and one at X = 300 have the same cut-off window. What decides is how late in the line the engine gets to the sprite, and that depends on how much work the sprites before it in the table took.
The rule is: each time the engine gets to a sprite that is on the line, it looks at the beam before drawing it. If the beam is in that sprite’s cut-off window, the engine gives up on the line: it draws neither this sprite nor any after it, and sets the “too many sprites” flag.
Three things follow from the rule, and the two examples below show each of them:
- What counts is the moment the engine gets to a sprite. A sprite it got to just before its cut-off window is drawn to its end, even though the beam moves into that window while it is being drawn.
- A sprite that is not on the line never makes the engine give up: its check does not look at the beam.
- Once the beam is past dot 319, the cut-off window is behind it, and the engine draws sprites again until the next swap. The cut-off window is not a deadline.
Example 1: every sprite on the line
Let’s start with the worst case, a crowd scene. All 128 sprites are 1× and on the same 16 lines, one every 2 dots: sprite k at X = 2k.
The figure has three parts on one scale. The picture at the top is the machine’s screen. The two rows below it show the same line in time: the beam row, where the beam is, and the engine row, what the engine is doing. Because the scale is shared, dot 320 is both the picture’s right edge and the point where the wide area ends for the beam.
How to read the rainbow. Every sprite is a solid 16 × 16 square, and each has one of 16 colours: sprite k gets colour k mod 16, so the colours repeat every 16 sprites, every 32 dots. Because the sprites are only 2 dots apart, each one covers all but the first 2 dots of the one before it (the later write wins, as you saw in Which sprite is in front). So:
- Each narrow stripe, 2 dots wide, is the left edge of one sprite: the rest of it is hidden under the sprites drawn after it. Counting the narrow stripes from the left counts the sprites, in table order.
- The wide block at the right end of a band is the last sprite drawn there, the only one nobody covered, so all 16 of its dots show.
- Black means no sprite wrote that dot.
Follow the engine row from the left:
- Sprite k starts at clock 3 + 17k.
- Sprite 74 starts at clock 1,261, with the beam at dot 314. That is before the cut-off window, so it is drawn.
- Sprite 75 comes up at clock 1,278, with the beam at dot 318. That is inside the cut-off window, so the engine gives up on the line: sprites 75–127 are not drawn.
That leaves 75 sprites on the line. In the picture, the band has 74 narrow stripes, for sprites 0–73, and ends with the wide block of sprite 74, at dots 148–163. Everything right of dot 164 is black: sprites 75–127 were never written.
Sprites 2×, 4× and 8× wide meet their wider cut-off windows sooner, after 38, 19 and 9 sprites.
Example 2: some sprites on other lines
Now a twist, and my favourite example in the chapter. The same sprites, but with numbers 74–99 moved 32 lines down:
Read the picture the same way as in Example 1:
- The top band, left part: the narrow stripes of sprites 0–72 and the wide block of sprite 73 at dots 146–161. Sprite 73 shows whole now, because sprite 74, which covered it in Example 1, is no longer on these lines.
- The black gap, dots 162–199, 38 dots: the place where sprites 74–99 were in Example 1. Their left edges run from X = 148 (sprite 74) to X = 198 (sprite 99), 2 dots apart, so their stripes would fill dots 148–199. Sprite 73 now shows whole and covers the first 14 of those dots, 148–161, which leaves 162–199 black. The gap is a stretch of the screen, not of time: how wide it is has nothing to do with how long the engine spends on these sprites (step 2 below).
- The top band, right part: sprites 100–127, from dot 200, ending with the wide white block of sprite 127 at dots 254–269. These are the sprites Example 1 lost.
- The lower band: the 26 moved sprites, 74–99, on their own lines, ending with the wide block of sprite 99. They have plenty of time there: only 26 sprites are on those lines.
Follow the engine row again:
- Sprites 0–73 are drawn as before. Sprite 73 starts at clock 1,244, at dot 310, and ends at clock 1,261.
- Sprites 74–99 are not on this line. The engine only checks each of them: 1 clock a sprite instead of the 17 it takes to draw one, so 26 clocks for all of them instead of 442. None of the checks looks at the beam, so these 26 clocks, 1,261 to 1,287, carry the engine across the cut-off window without it getting to a sprite on the line there.
- Sprite 100 comes up at clock 1,287, with the beam at dot 320. The cut-off window is behind it, so it is drawn.
- Sprites 100–127 take 17 clocks each, and the last one ends at clock 1,763, before the next swap.
That is 102 sprites on one line, 27 more than in Example 1, and no flag. Moving sprites away from a line let more of them onto it. Funny old world. I would never have guessed it without counting.
The next swap: the hard stop
The swap is the one real deadline. Wherever the engine is when it comes, it stops and starts the next line, even in the middle of a sprite. The dots it has already written stay, so a wide sprite late in the table can appear cut short on its right. That also sets the flag. Think of it as the curtain coming down mid-sentence: what was said stays said, the rest is never heard.
What a program can count on
Enough counting. What does all this mean for your program? Here is what I would rely on:
- The flag is bit 1 of port
$303Bwhen read (written, the same port selects a sprite slot). It is set the moment a line loses sprites: when the engine gives up in a cut-off window, or when the next swap stops it in the middle of a sprite. Once set, it stays set: later lines that lose nothing do not clear it. - Reading port
$303Breturns the flag and clears it, together with the collision flag, bit 0 (see Collisions), in the same read. A flag raised just after a read waits for the next one. A reset clears both as well. So the flag says that some line lost sprites since the last read; it does not say which line, or how many. - Sprites low in the table are safe: the engine always starts at sprite 0, so the sprites that matter most, the player, the bullets, go first. This is my first rule for any sprite table.
- Every sprite in the table costs at least one clock on every line, visible or not. A line on which all the work is done before clock 1,156, when the beam reaches dot 288, can never lose a sprite, whatever the scales.
- Wide sprites are expensive: one 8× sprite costs as much as seven and a half 1× ones. Scaling a sprite to fill the screen is cheap in memory and dear in time. I would spend 8× sprites like money.
Over the border, and clipping
Sprite space’s visible corner is the whole wide area, border included, but by default the sprites are
shown only over the Spectrum’s display area. Bit 1 of the Sprite and Layers System register, NextReg
$15, lets them over the border, and if you ask me, it belongs on in almost every game. Here
are two ships near the left edge, with the bit set and clear:
What is shown is decided as the buffer is read out, not as it is written: the engine draws every sprite
across the whole 320-dot line, and a window then says which dots of the line may show. The window comes
from the Clip Window Sprites register, NextReg $19 (four writes, X1, X2, Y1, Y2, as the overview
chapter’s
clip window
describes), and its meaning depends on two bits of NextReg $15:
NextReg $15 bit 1 (over the border) | bit 5 (clip over the border) | Where sprite dots show |
|---|---|---|
| 0 | either | inside the clip window, in display-area units: dots 32 + X1 to 32 + X2, lines 32 + Y1 to 32 + Y2, and never below the display area’s last line, 223 |
| 1 | 0 | the whole wide area, 320 × 256; the clip window is ignored |
| 1 | 1 | inside the clip window, in wide-area units, X doubled: dots 2 × X1 to 2 × X2 + 1, lines Y1 to Y2 |
After a reset the window is X 0–255, Y 0–191, the display area exactly. Without “over the border” a sprite can therefore never reach the border, however you set the window; with it, the clip bit decides whether the window still applies, now measured over the whole wide area in steps of two dots.
Over the border, sprites show even where the layer order puts the ULA in front of them: the composition chapter’s sprites over the border explains the exception that keeps the border from hiding them.
Collisions
Did your ship just fly into a rock? Bit 0 of port $303B, when read, says whether two sprites
collided: whether an opaque sprite dot was written into the line buffer where another sprite had
already written one. I call this bit the gossip: it always knows that something happened, and it
never tells you who was involved.
What counts as a touch. Two sprites touch only where both of them write the same dot of the same line. Each of these conditions must hold:
- Both dots are opaque. A dot whose value equals the value in the Sprite Transparency Index
register, NextReg
$4B(see Patterns), is never written into the buffer: it does not set the flag, and it leaves no mark for a later sprite to hit. So two sprites whose 16 × 16 squares overlap only where at least one of them is transparent do not collide. The flag is exact to the dot; it is not a test of the sprites’ boxes. - It is the same dot. Sprites side by side, even with no gap between them, do not collide.
- Both sprites are drawn on that line. A sprite the engine gave up on (see The time budget of a line) writes nothing, so it collides with nothing.
- Any two sprites count, including the parts of one object. An anchor and its relatives (see Anchors and relative sprites) that overlap set the flag on every frame. Let me warn you about this one: build your ship from overlapping pieces, and the flag is set every frame, rock or no rock.
Because the check is made in the line buffer, it follows the buffer’s rules, not the screen’s: dots
outside the clip window collide, overlaps hidden by “sprite 0 on top” collide, and sprites switched off
with the Sprite and Layers System register, NextReg $15 bit 0, still collide. Only dots inside the 320-dot line count. Both flags of port
$303B stay set until the port is read, and reading it clears both.
The gossip says that two sprites touched somewhere, in this dot-exact sense, not which two or where. A game reads it once a frame as a cheap “something hit something”, and only then compares the positions of the sprites it cares about. That is exactly how I would use it.
What to take home
Two easels and a stopwatch: that is the sprite hardware, and here it is once more in a few lines. The
Next has 128 sprites, the troupe, each placed by a 9-bit X and Y in a 512 × 512
sprite space, of which only the 320 × 256 wide area is shown. Positions wrap at 512, so a sprite can be
partly off any edge. Their pictures live in 16K of patterns, 64 8-bit ones of 256 bytes or 128 4-bit
ones of 128, mixed freely; a dot equal to NextReg $4B is transparent, and the palette offset recolours
the rest.
Each sprite has four or five attribute bytes, uploaded in one stream through ports $303B, $57 and
$5B, or through NextRegs $34–$39 and $75–$79. They give it eight orientations (rotate
clockwise, then mirror) and four sizes, 1× to 8×, across and down separately. A relative sprite
follows its anchor: a composite one takes only the anchor’s position, a unified one its position,
rotation, mirroring and scale, so the whole group moves as one picture.
The hardware never holds a screen of sprites, only two line buffers: it draws the next line’s sprites
into one while the other is shown, in table order, the later write winning unless NextReg $15 bit 6
says the first. Its time for a line is 1,824 clocks at 128K timing, and a 1× sprite on the line costs
17. If the engine gets to a sprite during that sprite’s cut-off window, the last stretch before
dot 320 that is as long as the sprite’s own drawing time, it gives up on the line: 75 1× sprites fit
when all of them are on it. The next swap stops the engine outright. Both set port $303B bit 1.
Sprites show over the border only with NextReg $15 bit 1, and the clip window, NextReg $19, is
applied as the buffer is read out. A collision, port $303B bit 0, is two opaque writes to one buffer
entry, counted before any clipping. If you remember only one piece of advice from me, make it this one:
put your player at sprite 0, and the rest of the table can fight over the time that is left.