Sprites
<lynx/_suzy.h>. It is the companion to the
Collision detection page, which owns the collision
half of the same hardware, and the Graphics page,
whose gfx_sprite paints the
SCBs described here.1. The hardware sprite model
The Lynx draws nothing a pixel at a time. Every visible thing is a sprite that Suzy paints into the video buffer, and the software's job is to hand Suzy a list of sprites to paint each frame. Two features set this engine apart from an ordinary blitter. First, sprites are sized in real time as they are painted — reduced or enlarged, horizontally and vertically by independent factors — so one image can be drawn at any scale. Second, each scan line of sprite pixels is encoded as either packed (run-length compressed) or literal (verbatim) data in RAM, and Suzy decodes it on the fly.
The hardware fixes a few limits. A sprite's vertical size is unlimited, but a
single scan line of source data may be at most 254 bytes (about
508 unscaled pixels). Each pixel carries a 1, 2, 3 or 4 bit
pen index (the BPP_1…BPP_4 selectors in
SPRCTL0). Pixels that fall off-screen are clipped by the hardware;
sprites can be flipped horizontally and/or vertically about their reference
point; and any individual sprite can be skipped. Neither the SCB nor the
sprite data may live in Mikey ROM — both are reached by pointer and may sit
anywhere else in RAM.
In lynxcc you rarely start the engine by hand.
gfx_sprite (see the
Graphics page) takes a pointer to an SCB, performs the
register writes, and busy-waits until Suzy has finished. This page describes the
data structure gfx_sprite consumes, and what you are building when you
author an SCB directly — as
spritetest.c,
packtest.c,
spriteslice.c and
spritesheet.c all do.
2. The sprite control block
A sprite control block is a small structure with a fixed field order the
hardware requires. It has three parts: a five-field fixed head
that is always present and always loaded; a reloadable body of
position, size, stretch and tilt whose presence is governed by the reload bits in
SPRCTL1 (section 2.2); and a
pen palette, which the hardware treats as a separate reloadable
block. <lynx/_suzy.h> (pulled in by <lynx/lynx.h>)
declares one C struct for every legal shape; the fullest is
SCB_REHVST_PAL:
typedef struct SCB_REHVST_PAL { /* SCB with every attribute present */
unsigned char sprctl0; /* $FC80: bpp, H/V flip, sprite type */
unsigned char sprctl1; /* $FC81: pack, reload, reuse-pal, skip */
unsigned char sprcoll; /* $FC82: collision number + no-collide */
char *next; /* -> next SCB in the list, 0 = last */
unsigned char *data; /* -> packed sprite pixel data */
signed int hpos, vpos; /* screen position of the reference pt */
unsigned int hsize, vsize; /* 8.8 fixed-point scale (256 = 1x) */
unsigned int stretch, tilt; /* per-scan-line H-size / H-pos adders */
unsigned char penpal[8]; /* pen-index palette (4bpp = 8 bytes) */
} SCB_REHVST_PAL;
The first three bytes are control (detailed in sections
2.2, 3 and — for
sprcoll — the Collision detection
page). Then come two 16-bit pointers: next, the address of the
following SCB, and data, the address of the packed pixel data. The
body holds the signed screen position (hpos, vpos),
the unsigned 8.8 scale (hsize, vsize), and the
per-scan-line stretch and tilt adders. The palette closes
the block.
SCB_REHVST_PAL: the fixed head, the reloadable body, and the
separately reloadable pen palette. The penpal cell is drawn wider to hint that it
is eight bytes.2.1. Linking and painter's order
SCBs form a singly linked list. Each block's next field holds the
address of the following SCB; the engine walks the chain until it reads a
next of 0, which marks the last sprite. You start a
frame by giving Suzy the address of the first SCB; the hardware follows the
pointers from there. Because every block is reached by pointer, SCBs and their data
may be scattered anywhere in RAM.
The list is painted in painter's order: the first SCB is drawn first and each later sprite overwrites whatever it overlaps, so the chain runs back-to-front in depth. This is also the order that makes collision numbers meaningful — a sprite only ever discovers things drawn before it (see the Collision detection page). One SCB is needed for each on-screen occurrence.
Each SCB carries its own data pointer, so the usual case is that
every sprite in a chain points at a different pixel block — the first
diagram below shows three SCBs each with its own image. Sharing is an option, not a
requirement: because the data field is just a pointer, several SCBs
may aim at the same block when you want the same picture more than
once. A repeated tile or a sprite sheet then costs one small control block per
instance and a single copy of the pixels — the second diagram shows SCB 0
and SCB 1 sharing one block while SCB 2 keeps its own
(spritesheet.c and
spriteslice.c show this). The
two cases can be mixed freely within one chain.
next, the last terminating
with 0, and each pointing at its own data block.
A skipped sprite (SKIP bit) is still read far enough to follow
its next, so skipping never breaks the chain.data block to draw the same picture twice, while SCB 2 still points
at its own. Sharing and distinct data can be mixed freely in one chain.2.2. The eight struct variants and the reload bits
Why does <lynx/_suzy.h> declare eight SCB structs? Because the
hardware lets an SCB omit its trailing fields. The reload field in
SPRCTL1 selects how much of the size/stretch/tilt tail this SCB
carries; anything it does not reload keeps the value left in Suzy's registers by
the previous sprite. A separate bit, REUSEPAL, drops the pen
palette the same way. Each struct is the exact C shape of one legal combination, so
picking a struct is picking the reload bits:
SPRCTL1 reload | Body fields loaded from this SCB | Palette | _suzy.h struct |
|---|---|---|---|
REHVST ($30) | hpos, vpos, hsize, vsize, stretch, tilt | own | SCB_REHVST_PAL |
REHVST + REUSEPAL | hpos, vpos, hsize, vsize, stretch, tilt | reused | SCB_REHVST |
REHVS ($20) | hpos, vpos, hsize, vsize, stretch | own | SCB_REHVS_PAL |
REHVS + REUSEPAL | hpos, vpos, hsize, vsize, stretch | reused | SCB_REHVS |
REHV ($10) | hpos, vpos, hsize, vsize | own | SCB_REHV_PAL |
REHV + REUSEPAL | hpos, vpos, hsize, vsize | reused | SCB_REHV |
RENONE ($00) | hpos, vpos | own | SCB_RENONE_PAL |
RENONE + REUSEPAL | hpos, vpos | reused | SCB_RENONE |
Every variant still carries the five-byte head and hpos/vpos;
the ladder is only about how much of the size/stretch/tilt tail and the
palette travel with this block. Shorter SCBs are smaller in RAM and paint a
touch faster, so a natural pattern is: the first sprite of a group uses a
_PAL struct with full reload to load the registers, and its followers
reuse those values with a shorter, non-_PAL struct. The
REUSEPAL half of the name is literally “reuse the previous
sprite's palette”: pick the _PAL struct to carry a palette, the
plain struct to inherit one. Reusing a palette that was never loaded (for instance
on the very first sprite) is undefined — give the first sprite its own.
Two more SPRCTL1 bits round out the byte: the draw-direction bits
DRAWUP and DRAWLEFT (which pick the starting quadrant, see
section 6) and ALGO3, a legacy
packing-algorithm select left at 0 for the standard data format.
RENONE ⊂ REHV ⊂ REHVS ⊂ REHVST
choose how much of the body loads; REUSEPAL independently drops the
palette. Each pairing is one _suzy.h struct.3. Sprite types
The low bits of SPRCTL0 select the sprite type
(the TYPE_* constants). The type decides which pen numbers are
transparent, which are collideable, and whether the video data is overwritten or
combined. Pens 1–13 are always opaque and collideable; the type only
changes the treatment of pen 0, pen 14 (shadow) and pen 15.
| Type | Constant | Value | Behaviour |
|---|---|---|---|
| Normal | TYPE_NORMAL | $04 | The workhorse. Pen 0 transparent and non-colliding; all other pens opaque and collideable. |
| Boundary | TYPE_BOUNDARY | $03 | Normal, but pen 15 is transparent yet still collideable — an invisible wall a ball bounces off. |
| Shadow | TYPE_SHADOW | $07 | Normal, but pen 14 is non-collideable yet still opaque — a drawn shadow that does not register hits. |
| Boundary‑shadow | TYPE_BSHADOW | $02 | Both of the above: pen 15 transparent-collideable, pen 14 opaque-non-collideable. |
| Background | TYPE_BACKGROUND | $00 | Overwrites video and collision buffers (pens 0 and 15 no longer transparent); no collision detection. Used to lay down a scene. Paints ~¼ faster. |
| Background‑non‑collide | TYPE_BACKNONCOLL | $01 | Background, but leaves the collision buffer untouched entirely. Paints ~⅓ faster than a background sprite. |
| Non‑collideable | TYPE_NONCOLL | $05 | Never touches the collision buffer; pen 15 is non-collideable. For HUD, clouds, cockpits. Paints ~¼ faster. |
| Exclusive‑or | TYPE_XOR | $06 | Normal collision, but the sprite data is XORed with the video buffer. The read-modify-write can cost up to ~¼ more time. |
The hardware realises these from a small set of flags. The truth table below is
the authoritative version — and it already incorporates a hardware quirk: the
shadow generator is missing an inverter, so TYPE_XOR and the two
background types come out with shadow enabled whether they asked for it or
not. The practical consequence is that pen 14 is non-collideable on those
types, so a background sprite used to clear the collision buffer will not
clear pixels drawn in pen 14. See the
Collision detection page for how the type and
its override bits gate collision.
| Shadow | B‑shadow | Normal | Boundary | Bg‑shadow | Bg‑no‑coll | Non‑coll | XOR‑shadow | |
|---|---|---|---|---|---|---|---|---|
| 15 is opaque | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 |
| 14 is collideable | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
| 0 is opaque & collideable | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
| collision detect | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
| collision buffer access | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 |
| XOR the video data | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
The third SCB byte, sprcoll, carries the sprite's 4-bit collision
number and a per-sprite don't-collide bit (NO_COLLIDE,
$20). That is the whole of what this page says about collision; the
numbering scheme, the depository byte and the COLLOFF register are on
the Collision detection page.
4. The pen palette
A sprite's pixels do not store colours directly. Each pixel holds a small pen index — 0 to 2bpp−1 — and the sprite's own pen palette maps that index to a 4-bit pen number (0–15). The pen number then indexes the 16-entry display palette to produce the actual colour (documented on the Graphics page). So there are two lookups: the per-sprite penpal in the SCB, then the global display palette.
The palette is 2bpp nibbles long, packed two nibbles
to a byte, which is exactly why _suzy.h ships four penpal structs:
| Depth | Pens | Palette size | Struct |
|---|---|---|---|
BPP_1 | 2 | 1 byte | PENPAL_1 |
BPP_2 | 4 | 2 bytes | PENPAL_2 |
BPP_3 | 8 | 4 bytes | PENPAL_3 |
BPP_4 | 16 | 8 bytes | PENPAL_4 |
The generic SCB structs declare penpal[8] because that covers the
4bpp case; when you hand-build a lower-depth sprite you populate only the first
2bpp−1 bytes and the SCB is that much shorter. Within a byte, pen
index 0's pen number is the upper nibble.
packtest.c uses an identity
penpal {0x01,0x23,…,0xEF} (pen index k maps to pen
number k);
spritetest.c gives pen
index 0 a pen that the display palette leaves transparent.
Because the palette is a separately reloadable block, consecutive sprites can
share it via REUSEPAL (section 2.2). One
alignment trap: an 8-byte penpal must not begin at address $xxFA —
a hardware bug drops its last two bytes, leaving pens 12–15 stale —
so align or pad the SCB (gfx_sprite notes this too; the
design/LYNX_GFX_DESIGN.md rationale doc analyses it).
5. Pixel and data format
In the frame buffer two pixels share a byte: on an un-flipped, right-drawing sprite the high nibble is the left pixel, and bytes ascend from left to right along the line (the same order the penpal nibbles follow). The source data a sprite points at is separately encoded — a stream of offsets and pixel runs that Suzy unpacks as it paints.
Each scan line of source data is either packed or
literal, chosen by the PACKED/LITERAL
bit in SPRCTL1. Packed data run-length-encodes repeated pixel groups;
literal data stores every pixel verbatim, which is convenient for text and score
digits you rewrite often. The two produce identical output —
packtest.c confirms it at all four
depths. The full encoding, and how the sp65 tool emits it, are on the
sp65 page; the sprpck packer
produces the same packed data from BMP and ASCII SPS art. This page does not
re-derive the encoding.
One authoring rule does belong here, because it is a Suzy trap you hit when
building SCBs: the sprite-data shift register drops the final pixel group of any
scan line whose data ends on a byte boundary. Literal lines always end that way, so
every literal line needs a trailing pad byte (and its offset
incremented) whose pixels resolve to pen 0 — usually value $00,
but a value the penpal maps to pen 0 if you have repurposed value 0. Packed
lines only need the pad in the roughly one-in-eight case where the last meaningful
bit lands on bit 0 of a byte. The design/LYNX_SPRITE_PADBYTE_DESIGN.md
note and the sprctl1 comment in _suzy.h have the full
story; lynxcc's sp65 and
sprpck converters both handle the padding for data they
generate.
6. Sizing, stretch, tilt and flip
hsize and vsize are 8.8 fixed-point scale factors:
$0100 is 1×, $0080 is half size, $0200
double. Horizontal and vertical scale are independent, and Suzy resizes the image
as it paints — there is no pre-scaled copy in RAM.
Two 16-bit adders modify the sprite per scan line and are only applied
when the reload bits carry them (REHVS for stretch,
REHVST for stretch and tilt):
Stretch (
stretch) is added tohsizeafter each line, so the sprite widens (or narrows, past a wrap) down its height — a flat-topped trapezoid. The value is 8 bits of size plus 8 of fraction, so the per-line step runs from1/256of a unit up to 128 units; a value over 128 units wraps the ALU and turns into a size reduction.Vertical stretch — setting the
VSTRETCHbit (0x10) inSPRSYSfeeds that samestretchvalue intovsizeas well, so each scan line enlarges the sprite in both axes at once: a uniform zoom down its length rather than a flat-topped trapezoid. There is no separate vertical adder — horizontal and vertical share the onestretchfield.VSTRETCHis a global mode bit inSPRSYS, not an SCB field, so it stays set until you clear it and applies to every sprite drawn while it is on; leave it off (the default) to holdvsizesteady. A change to the vertical size only takes effect at the next source-line fetch.Tilt (
tilt) is added, through the tilt accumulator, to the horizontal position each line, shearing the sprite sideways; positive values lean it right. Stretch and tilt together let you paint arbitrary quadrilaterals, which is howspriteslice.cand theraycasterexample draw scaled wall slices.
hsize/vsize are whole-sprite 8.8 scale
factors; stretch and tilt are per-scan-line adders that
turn the rectangle into a trapezoid or parallelogram.stretch adder alone widens the sprite each scan line (left).
Setting VSTRETCH in SPRSYS applies the same adder to
vsize as well, so the sprite grows in both directions down its length
(right).A sprite is painted outward from a fixed reference point —
chosen when the image is compacted — into one of four quadrants. The starting
quadrant is set at run time by DRAWUP and DRAWLEFT in
SPRCTL1; with neither bit set the sprite paints down and to the right of
the reference point. HFLIP and VFLIP in SPRCTL0
mirror the image left/right and up/down about that same point. Changing the
starting quadrant shifts the sprite's position slightly, which is what the
hsizeoff/vsizeoff registers exist to balance: at
initialisation they are loaded with the “magic” $007F on the
right and down directions (and 0 on left and up) to cancel the one-pixel bump at the
reference point of a scaled multi-quadrant sprite. They are shared by all sprites, so
restore them if you change them.
DRAWUP/DRAWLEFT, and HFLIP/VFLIP
mirror the image about the two axes.6.1. Where the action point sits
Because the reference point is a fixed screen pixel, it is the pivot
every one of these transforms turns about: hsize, vsize,
stretch and tilt all grow or shear the image
outward from that pixel along the current draw direction. So the same
transform paints the result in a different place depending on where the action
point sits inside the artwork — the ax,ay
action point you pick when the sprite is compacted.
Move it, and a doubled or sheared sprite lands somewhere else even though the SCB
scale fields are byte-for-byte identical.
One caveat decides what “middle” even means: a plain
single-quadrant sprite can only anchor at a corner — which
corner is chosen by DRAWUP/DRAWLEFT. To pivot about the
centre of the image you must draw it as a multi-quadrant sprite
whose four quadrants share one reference point; that is exactly the case the
hsizeoff/vsizeoff $007F correction above is
there to serve. The figure below applies one 2× scale and one
tilt shear at three action points to show the pivot staying put while
the picture moves around it.
2× scale (top) and one tilt shear (bottom),
each applied unchanged at three action points. The dashed 1× base and the
pinned dot stay fixed in every panel; only where the action point sits in the
artwork changes, and the transformed result grows or shears outward from that
pixel — down-right from a top-left corner, symmetrically from a centred
multi-quadrant anchor, up-left from a bottom-right corner.The spritefx.c sample turns this
figure into something you can drive: it animates hsize, vsize,
stretch and tilt on a choice of shapes and lets you cycle the
action point through all five spots — the four corners and the centre —
with a magenta cross pinned at the anchor so you can watch each shape grow or shear
around it exactly as drawn here.
6.2. Flipping about the reference point
Two bits in SPRCTL0 mirror a sprite as Suzy paints it, so one image
in RAM can face either way without a second copy:
HFLIP(0x20) mirrors the sprite left↔right. Suzy paints each scan line from the reference point in the opposite horizontal direction, so the pixel that was on the left edge ends up on the right. A character sprite drawn facing right is flipped to face left by setting this one bit — the classic use.VFLIP(0x10) mirrors the sprite up↓down. The scan lines are emitted from the reference point in the opposite vertical order, so the top row of the image paints at the bottom.
Both flips are about the reference point, not the sprite's
centre or a screen axis, so the sprite stays anchored where you positioned it and
only its contents mirror — the reference pixel never moves. This is the same
point DRAWUP/DRAWLEFT pick a starting quadrant around
(above); flipping and quadrant choice are independent, and both leave hpos
/vpos untouched. Setting HFLIP and VFLIP
together mirrors on both axes at once, which is a 180° rotation of the image.
Flip does not interact with hsize/vsize or the
stretch/tilt adders — a flipped sprite scales and
shears exactly like an un-flipped one.
F glyph flipped about its reference point (the teal dot).
HFLIP mirrors left↔right, VFLIP up↓down, and the
two together give a 180° rotation. The reference point never moves, so
hpos/vpos stay put — only the image mirrors.7. Driving the engine
Normally you never touch these registers — gfx_init
does the one-time setup and gfx_sprite
runs each sprite list — but the struct __suzy register map in
_suzy.h shows what they do, and a custom sprite loop needs them. Once,
after power-up, the engine is initialised: the control byte sprsys, the
init byte sprinit, the screen-edge offsets hoff/voff
(which pan the display window — see the Graphics
page), the collision buffer base colbase and its offset colloff
(both covered on the Collision detection page), the
size offsets hsizeoff/vsizeoff, and finally
suzybusen = 1 to grant Suzy the RAM bus.
To paint a frame you set the video build address (vidadr/sprbase),
write the address of the first SCB into scbnext, and start the engine
by writing SPRITE_GO ($01, or EVER_ON | SPRITE_GO
= $05) to sprgo. Suzy then walks the chain until a
next of 0. The read side of sprsys reports progress:
SPRITEWORKING is set while the engine runs, SPRITETOSTOP
while a requested stop is pending, and UNSAFE_ACCESS flags a register
access collision; VSTRETCHING and LEFTHANDED read back the
current state of the two mode bits below. The write side offers
SPRITESTOP (finish the current sprite and halt), CLR_UNSAFE,
the global NO_COLLIDE disable, VSTRETCH (the vertical-stretch
mode of §6), and LEFTHAND. The engine only ever restarts from the
beginning of the list — it cannot resume from where it stopped.
LEFTHAND (0x08) selects the console's drawing
handedness. It reverses the direction Suzy fills the video buffer so the picture
comes out correct when the unit is held in its flipped orientation, and it pairs with
the matching flip of the joypad axes (the JOYSTICK up/down and
left/right bits swap sense with this bit). It is a global console-orientation
setting written once at init, not a per-sprite control; leave it as
gfx_init sets it unless you are
implementing screen flip. The read-back LEFTHANDED reports the bit's
current value.
The SIGNMATH and ACCUMULATE bits in the same register
belong to Suzy's hardware multiply/divide, documented under suzymath.h
in the function reference, not to sprites; their read-side
counterparts MATHWORKING, MATHWARNING, and
MATHCARRY report math status, again not sprite state.
8. License
The Lynx runtime, like the rest of the package, is distributed under the cc65 zlib-style license. The full text, together with every other copyright notice in the distribution, is reproduced verbatim on the consolidated Licenses and copyright notices page.