// cd /blog/pc3-warp-affine-span
Raster effects on the Pico Computer 3: WARPH, WARPV, AFFINE and SPAN
Hills scrolling behind hills, a racing road that curves and climbs, a map that turns and zooms, the ground of a kart track rushing towards you, an iris closing on the hero. The arcade machines and consoles of the 1980s and 1990s made these with special video chips. PicoMite V8.00.00b3 gives the Pico Computer 3 four FRAMEBUFFER commands that do the same jobs in firmware. Each copies a picture from one framebuffer to another, and moves, turns, scales or cuts every row or column of it by its own entry in a table. This article explains them, taking apart the demo that comes with them scene by scene.
Where the ideas come from
A television draws its picture one line at a time, top to bottom, 50 or 60 times a second. The home computers and consoles of the 1980s had too little memory and too slow a processor to build every frame from scratch, but their video chips read the scroll position again for each line. Change it between one line and the next, and each line moves on its own. The Atari 2600 had no frame buffer at all, so its programs drew each line as the beam went past, which programmers called racing the beam. The Commodore 64 had raster interrupts to split the screen, and the Amiga had the Copper, a co-processor whose job was to change the video registers at chosen places down the screen.
The arcades made the most of it. Irem's Moon Patrol (1982) is usually credited with bringing parallax scrolling to the arcades: hills behind hills, each moving more slowly than the one in front. Namco's Pole Position (1982) made a road curve away by moving each line of a straight road sideways. Sega's Out Run (1986) had its own hardware to draw the road, with its hills and its stripes of kerb. Sega's Mega Drive (1988) brought the same ideas home: its video chip could move every line sideways, and every 16-pixel column up or down.
Nintendo's Super NES (1990) went further. Its Mode 7 turned and scaled a background through a 2×2 matrix, and its HDMA could load new values into the video registers on every line. Changing the matrix line by line made the background lie down as a floor in perspective: the tracks of F-Zero and Super Mario Kart, the ground of Pilotwings. HDMA also moved the edges of the SNES's windows on every line, which is how the circle closed at the end of a level in Super Mario World, and a mosaic register broke the screen into blocks. The Game Boy Advance (2001) could turn and scale sprites as well.
Not every trick needed a chip. In 1986 Alan Paeth showed that any rotation can be done as three shears: slide the rows, then the columns, then the rows again, never moving anything but whole rows and columns. And in 1993 Doom ended each level by melting the screen, every column sliding down at its own speed, using nothing but the PC's processor.
| Effect | Then | On the PC3 |
|---|---|---|
| Parallax scrolling | Irem's Moon Patrol (1982); the Amiga's Copper | WARPH, a band for each layer |
| A road with curves, hills and stripes | Namco's Pole Position (1982), Sega's Out Run (1986) | WARPH with rows%() |
| Row and column scroll | Sega's Mega Drive (1988) | WARPH and WARPV |
| Turning and zooming, a floor in perspective | Mode 7 on the Super NES (1990): F-Zero, Pilotwings, Super Mario Kart | AFFINE |
| Turned sprites | The Game Boy Advance (2001) | AFFINE with left and width |
| An iris, a spotlight | The Super NES's windows, moved on every line by HDMA | SPAN |
| A mosaic | The Super NES's mosaic register | BLIT RESIZE and WARPH |
| Rotation by three shears | Alan Paeth (1986) | WARPH, WARPV, WARPH |
| The screen melt | Doom (1993) | WARPV |
Firmware instead of chips
The Pico Computer 3 has none of that hardware. Its RP2350 sends a framebuffer to the HDMI screen and that's all: no scroll registers, no Mode 7, no windows. What it has instead is raw processor speed. The SNES's processor ran at 3.58 MHz and the Mega Drive's at 7.67 MHz. The PC3 runs 32-bit Arm cores at 315 MHz, nearly 90 times the SNES's clock, and each clock does far more. That is enough to do in firmware, pixel by pixel, what those machines needed custom silicon for, in a fraction of a frame:
| Command | A whole 320×240 MODE 2 screen |
|---|---|
| WARPH | 0.9 ms |
| WARPV | 1.6 to 4.7 ms |
| AFFINE, turned and zoomed | 4.8 ms |
| AFFINE, a floor in perspective | 3.4 ms |
| SPAN | 0.2 to 0.4 ms |
On a Pico Computer 3 at 315 MHz, from the V8.00.00b3 release notes. A frame at 60 Hz lasts 16.7 ms.
Doing it in firmware also makes the commands more general than the chips were. Any framebuffer can be copied to any other, so effects stack: a WARPH into a WARPV waves a picture both ways. A colour can be made transparent, so warped pictures lie over each other. And the tables are ordinary MMBasic arrays, made and animated with the MATH commands, so the demo has no BASIC loop over pixels anywhere in its frames.
On this page
The four commands
FRAMEBUFFER WARPH from, to, xoff%() [,step] [,first] [,phase] [,edge] [,transparent] [,rows%()]
FRAMEBUFFER WARPV from, to, yoff%() [,step] [,first] [,phase] [,edge] [,transparent]
FRAMEBUFFER AFFINE from, to, u%(), v%(), du%(), dv%() [,step] [,first] [,left] [,width] [,edge] [,transparent]
FRAMEBUFFER SPAN from, to, left%(), right%() [,step] [,first] [,fill] [,transparent] [,outside]
All four work the same way:
fromandtoare framebuffers:N, the screen;Fand2, framebuffers to draw pictures into; andLandT, the two layers shown over the screen. Except for SPAN, they must be different.- The table is a one-dimensional integer array, with an element for each band of
steprows (default 1), starting at rowfirst(default 0). For WARPV it is bands of columns. - WARPH moves each band of rows sideways by its offset, a positive one to the right: the arcades' row scroll.
rows%()can say which row of the source each band shows. - WARPV moves each band of columns up (a positive offset) or down: column scroll.
- AFFINE reads each row from the source along a straight line in any direction, which turns and zooms a picture, lays it down as a floor, or with
leftandwidthturns a sprite. - SPAN copies each row only from its
left%()column to itsright%()column: a window of any shape.fillcolours the rest of the row, andoutside= 1 copies the outside instead. - A picture wraps round unless
edgesays otherwise: -1 leaves the pixels it uncovers as they were, and a colour fills them. Pixels of thetransparentcolour aren't copied. - For WARPH and WARPV, band b uses element (b +
phase) MOD n, so countingphaseup slides a table along without changing it.
They work on the VGA and HDMI builds of the PicoMite, in MODE 2 to 5. Here is what each does to the same picture, the map from the demo:






These pictures were drawn by a model of the commands written from the User Manual's descriptions, not captured from a screen. On the PC3, each is one command.
Setting up the screen
The demo, warp_demo.bas, is eight scenes, one after the other. It starts like this:
OPTION EXPLICIT
OPTION DEFAULT INTEGER
MODE 2
IF MM.HRES <> 320 OR MM.VRES <> 240 THEN
MODE 1
PRINT "This demo needs MODE 2 at 320x240 (OPTION RESOLUTION 640x480)"
END
ENDIF
CONST W = 320, H = 240, FP = 65536 ' FP: AFFINE's places and steps are in 1/65536 of a pixel
DIM cl(15), quit, i
FOR i = 0 TO 15 : cl(i) = MAP(i) : NEXT ' the colour that draws in each of the 16 palette slots
' ...
FRAMEBUFFER CREATE
FRAMEBUFFER CREATE 2
FRAMEBUFFER LAYER
FRAMEBUFFER LAYER TOP
FRAMEBUFFER WRITE N
It runs in MODE 2, 320×240 pixels in 16 colours, which needs OPTION RESOLUTION 640x480, and stops if it hasn't got it. OPTION DEFAULT INTEGER makes every variable and array without a suffix an integer. That suits the tables, which must be integer arrays; the demo's float arrays end in !. cl() records the colour that draws in each of the 16 palette slots, because the scenes change the palette with MAP, and MAP SET applies a change, waiting for the next frame as it does.
Then it makes everything it draws into: two framebuffers, F and 2, and the two layers L and T, which show over the screen wherever they aren't colour 0. Every scene then works the same way. It draws its pictures once. In each frame it works out its tables with MATH, waits for the frame with FRAMEBUFFER WAIT (or MAP SET), and puts each picture on the screen with one command.
WARPH: stars, a lake and a road
240 stars, one command
The demo opens with the starfield of a hundred shoot-'em-ups: every row of the screen has a star, and every row moves at its own speed.
FOR y = 0 TO 119
lay(y) = 1 + INT(RND * 3)
x = INT(RND * (W - 1))
PIXEL x, y, cl(lay(y))
IF lay(y) = 3 THEN PIXEL x + 1, y, cl(3)
NEXT
FOR y = 0 TO H - 1
sr(y) = y MOD 120
spd!(y) = lay(sr(y)) ^ 1.7 * 0.45 + RND * 0.2
NEXT
' ...
DO
fr = fr + 1
MATH SCALE spd!(), -fr, so()
' ...
FRAMEBUFFER WARPH F, N, so(), 1, 0, , , , sr() ' the stars
Only 120 rows of F have stars in them, one star a row, dim, middling or bright. sr(), the rows%() table, shows each of those rows twice down the screen (row y shows star row y MOD 120), and spd!() gives every screen row a speed, faster for the brighter stars, so that they seem nearer. In each frame one MATH SCALE multiplies all the speeds by the frame number, into the integer array so(), and one WARPH moves every row by its own offset. The rows wrap round, so the stars never run out.
Parallax, and a lake that reflects it
The second scene is a lake from day to dusk. drawland draws the land twice, into 2 in its own colours and into F in the colours of the water. In both, rows 0 to 149 hold the sky and the far mountains, and rows 150 to 199 the near hills and trees, with colour 0 wherever there's no hill.
FRAMEBUFFER WRITE 2 : CLS cl(0) : drawland tcn() ' the land in 2 (hills in rows 150-199)
FRAMEBUFFER WRITE F : CLS cl(0) : drawland tcr() ' and in the water's colours in F
FRAMEBUFFER WRITE N
hlr(0) = 150
FOR k = 0 TO 89
rbr(k) = 149 - k ' the reflection is the land upside down
amp! = 0.3 + k * 0.075 ' ripples grow towards the viewer...
ph! = 9 * SQR(k + 1) ' ...and spread out
wsn!(k) = amp! * SIN(ph!) : wcs!(k) = amp! * COS(ph!)
IF k < 50 THEN rhr(k) = 199 - k : hsn!(k) = wsn!(k) : hcs!(k) = wcs!(k)
NEXT
DO
fr = fr + 1
bgo(0) = -fr \ 4 : hlo(0) = -(fr * 7) \ 10
' amp*sin(ph + wt) = amp*sin(ph)*cos(wt) + amp*cos(ph)*sin(wt): two MATH SCALEs and a C_ADD
MATH SCALE wsn!(), COS(fr * 0.12), wa!() : MATH SCALE wcs!(), SIN(fr * 0.12), wb!()
MATH C_ADD wa!(), wb!(), wc!() : MATH ADD wc!(), bgo(0), wa!() : MATH SCALE wa!(), 1, rwo()
MATH SCALE hsn!(), COS(fr * 0.12), ha!() : MATH SCALE hcs!(), SIN(fr * 0.12), hb!()
MATH C_ADD ha!(), hb!(), hc!() : MATH ADD hc!(), hlo(0), ha!() : MATH SCALE ha!(), 1, hwo()
' ...
FRAMEBUFFER WARPH 2, N, bgo(), 150 ' sky and mountains, slowly
FRAMEBUFFER WARPH 2, N, hlo(), 50, 100, , , 0, hlr() ' hills and trees, faster, over them
FRAMEBUFFER WARPH F, N, rwo(), 1, 150, , , , rbr() ' their reflection, rippling
FRAMEBUFFER WARPH F, N, hwo(), 1, 150, , , 0, rhr()
Each frame, four WARPHs paint the picture from the back to the front:
- One element with a step of 150 moves rows 0 to 149 as one band, slowly: the sky and the mountains.
- The hills: 50 rows read from row 150 (
hlr(0)) and shown from row 100 of the screen, moved faster, with colour 0 transparent so that the mountains show between them. That's Moon Patrol's parallax in two commands. - The reflection: rows 150 to 239 of the screen, read upside down from
F(rbr(k) = 149 - k), each moved by its own ripple as well as by the sky's scroll. - The hills' reflection, the same way, over it.
The ripples are a sine wave down the water that grows towards you and moves. Moving a wave would normally take a SIN for every row in every frame, but amp × sin(ph + wt) = amp × sin(ph) × cos(wt) + amp × cos(ph) × sin(wt). So the demo works out amp × sin(ph) and amp × cos(ph) for each row once, and in each frame two MATH SCALEs and a MATH C_ADD (which adds two arrays) make the whole table.
The road
The road of the arcade racers is a straight road, drawn once in perspective, with each line moved sideways. The demo draws it into rows 0 to 119 of F, narrowing towards the horizon, and again into rows 120 to 239 with darker grass and tarmac, white kerbs instead of red, and no centre line. zz(r) records how far away each row of the road is.
FRAMEBUFFER WRITE F
FOR r = 0 TO 119
hw! = 2 + r * 0.95 : kw! = 1 + r * 0.13 : lnw! = r * 0.025
zz(r) = 2400 \ (r + 6) ' how far away road row r is
FOR s = 0 TO 1
yy = r + 120 * s
LINE 0, yy, W - 1, yy, 1, cl(6 + s)
LINE 160 - hw! - kw!, yy, 160 + hw! + kw!, yy, 1, cl(10 + s)
LINE 160 - hw!, yy, 160 + hw!, yy, 1, cl(8 + s)
IF s = 0 THEN LINE 160 - lnw!, yy, 160 + lnw!, yy, 1, cl(12)
NEXT
NEXT
' ...
FOR k = 0 TO 69
cp!(k) = (1 - k / 69) ^ 2 * 110 ' a curve moves the far rows most
sp!(k) = (k + 1) / 70 ' the car's place across the road, the near rows most
NEXT
In each frame three things change:
curve! = 1.3 * SIN(fr * 0.009) + 0.7 * SIN(fr * 0.023 + 1)
camx! = curve! * 12 + 6 * SIN(fr * 0.041)
hy = 120 + 16 * SIN(fr * 0.013) ' the horizon: hills
dist = dist + 5
pan! = pan! - curve! * 1.4
sko(0) = pan! : skr(0) = 140 - hy ' the sky as one band, moved with the horizon
MATH SCALE cp!(), curve!, t1!() : MATH SCALE sp!(), -camx!, t2!()
MATH C_ADD t1!(), t2!(), t3!() : MATH SCALE t3!(), 1, ro()
FOR k = 0 TO 69 ' each 2-row band: its road row (squeezed by the hill) in the light or dark copy
y = 100 + 2 * k
IF y < hy THEN
rm(k) = -1
ELSE
rr = MIN((y - hy) * 120 \ (240 - hy), 118)
rm(k) = rr + 120 * (((zz(rr) + dist) >> 4) AND 1)
ENDIF
NEXT
' ...
FRAMEBUFFER WARPH 2, N, sko(), 140, 0, , , , skr() ' sky and mountains
FRAMEBUFFER WARPH F, N, ro(), 2, 100, , , , rm() ' the road
- Curves.
cp!()moves the far rows most and the near ones hardly at all, so scaling it bycurve!bends the road away to one side.sp!()moves the near rows most, which is the car's own place across the road. TwoMATH SCALEs and aC_ADDmakero(), an offset for each band of two rows. - Hills.
rm(), therows%()table, picks the road row each band shows. Squeezing the road into the rows between the horizonhyand the bottom of the screen moves the horizon up and down, and the road seems to climb and dip. Bands above the horizon get -1, which leaves them alone to show the sky. - Speed. Each road row is shown from the light copy or the dark one, chosen by its distance plus the distance travelled, in steps of 16 (
((zz(rr) + dist) >> 4) AND 1). Asdistgrows the stripes rush towards you: the alternating kerbs and verges that Pole Position and Out Run used to show speed.
The sky is one more WARPH: a single band of 140 rows that pans as the road curves, read from a row that follows the horizon up and down. The FOR loop is the only BASIC loop in the frame, and it runs once for each of the 70 bands, not once for each pixel.
WARPV: waves and the melt
WARPV is WARPH on its side: each band of columns moves up (a positive offset) or down. In the first scene the logo is drawn once, low down in F, and a WARPV lifts it into the layer L with a wave running through it. The scrolling message is a WARPH that scrolls a strip of text into 2, and a WARPV that waves it into the top layer T: the sine scroller of the demo scene.
FOR k = 0 TO 159
sw(k) = -50 + 20 * SIN(2 * PI * k / 80) ' the scroller's wave: 2 periods, so the phase can turn it
lw(k) = 156 + 4 * SIN(2 * PI * k / 80) ' the logo's
NEXT
' ...
spos = spos + 2 : sco(0) = -spos
' ...
FRAMEBUFFER WARPH F, 2, sco(), 32, 100, , , , ssr() ' the strip, scrolled, to rows 100-131 of 2
' ...
FRAMEBUFFER WARPV 2, T, sw(), 2, 0, fr * 2 ' waved into the top layer
FRAMEBUFFER WARPV F, L, lw(), 2, 0, fr, -1 ' the logo waved into the layer
Both wave tables hold exactly two periods of the wave in their 160 elements (bands of two columns), so the wave joins up as phase slides it along: fr and fr * 2 move the waves at two speeds without a new table. The logo's edge of -1 stops it wrapping round from the top of the screen to the bottom.
The credits at the end are a WARPH followed by a WARPV, each with its own phase, which waves the text both ways at once:
FRAMEBUFFER WARPH F, 2, hx(), 1, 0, fr * 2 ' each row sideways...
FRAMEBUFFER WARPV 2, N, vy(), 2, 0, fr * 3 ' ...then each column up and down
The melt
Between scenes the picture melts away column by column, as Doom's screens did:
SUB melt
LOCAL st!(159), tm!(159), dr(159), z(0), k, fr
FRAMEBUFFER COPY N, F
FRAMEBUFFER WARPH L, F, z(), H, , , , 0 ' the layers into the picture, so that they melt with it
FRAMEBUFFER WARPH T, F, z(), H, , , , 0
FRAMEBUFFER WRITE L : CLS : FRAMEBUFFER WRITE T : CLS : FRAMEBUFFER WRITE N
FOR k = 0 TO 159 : st!(k) = RND * 40 : NEXT
FOR fr = 1 TO 80
MATH ADD st!(), -0.35 * fr * fr, tm!()
MATH CLAMP tm!(), -H, 0, dr() ' each column waits a little, then falls faster and faster
FRAMEBUFFER WAIT
CLS cl(0)
FRAMEBUFFER WARPV F, N, dr(), 2, , , -1
IF INKEY$ <> "" THEN quit = 1
IF quit OR MATH(MAX dr()) <= -H THEN EXIT FOR
NEXT
CLS cl(0)
END SUB
First the melt copies the screen into F and flattens the layers onto it, with a WARPH that moves nothing (one element of 0 for all 240 rows) and treats colour 0 as transparent: a handy way to merge a layer into a picture. Each pair of columns gets a random head start of up to 40 pixels. In each frame MATH ADD takes 0.35 × fr² from all of them, and MATH CLAMP turns the results into offsets between 0 and -240, so each column waits a moment and then falls faster and faster. WARPV with an edge of -1 draws them over a cleared screen until every column has gone.
Turning with three shears
Scene four turns a playing card through any angle with nothing but WARPH and WARPV, using Paeth's three shears. To turn a picture by an angle a about the middle of the screen:
- move every row sideways by -tan(a/2) times its distance from the middle row;
- move every column down by sin(a) times its distance from the middle column;
- move the rows again, as the first time.
FOR y = 0 TO H - 1 : ry!(y) = y - 120 : NEXT
FOR x = 0 TO W - 1 : rx!(x) = x - 160 : NEXT
' ...
t! = TAN(RAD(a!) / 2)
MATH SCALE ry!(), -t!, ha() : MATH ADD ha(), dx + t! * dy, hb() ' the rows: first and third shears
MATH SCALE rx!(), -SIN(RAD(a!)), va() : MATH ADD va(), -dy, va() ' the columns: the second
FRAMEBUFFER WARPH F, 2, ha()
FRAMEBUFFER WARPV 2, N, va()
FRAMEBUFFER WAIT
FRAMEBUFFER WARPH N, T, hb()
ry!() and rx!() hold each row's and each column's distance from the middle, made once. In each frame MATH SCALE turns them into the shears, and MATH ADD adds a constant to each, which moves the card about as well as turning it. The three warps carry the picture from F to 2 to N to the top layer T; N is just a workspace here, hidden under the green table drawn in the layer L.
A shear moves pixels but never makes or loses one, so the card stays crisp at every angle. The catch is that a row that wraps round is sheared wrongly, so the picture has to stay well inside the screen: within 113 pixels of the middle, for angles up to 90 degrees either way. A playing card looks the same upside down, so that's every angle it needs.
A mosaic
The card arrives and leaves as a mosaic, which the Super NES did with a register:
FOR k = 0 TO 7 ' the mosaic's row map for each size of block: the rows from 120 on, each shown ms(k) times
FOR y = 0 TO H - 1 : mt(y, k) = 120 + y \ ms(k) : NEXT
NEXT
' ...
n = ms(k)
BLIT RESIZE F, 2, 0, 0, W, H, 0, 0, W \ n, H \ n
BLIT RESIZE 2, 2, 0, 0, W \ n, H \ n, 0, 120, W, H \ n
MATH SLICE mt(), , k, mr()
IF fr <= 25 THEN MAP SET ELSE FRAMEBUFFER WAIT
FRAMEBUFFER WARPH 2, T, z(), 1, 0, , , , mr()
BLIT RESIZE shrinks the card to 1/n of its size, and a second BLIT RESIZE stretches that back to the full width, in the rows from 120 on, so each pixel becomes a block n pixels wide. Then a WARPH whose rows%() table shows each of those rows n times makes the blocks n pixels tall. The tables for the eight block sizes are made once, in the two-dimensional array mt(), and MATH SLICE takes out the one for this frame.
AFFINE: turning, zooming and Mode 7
AFFINE is the Super NES's Mode 7 as one command. It builds each row of the screen by walking through the source picture in a straight line. The first pixel of the row shows the source at (u, v), and each pixel to its right is a step of (du, dv) further on. Within a band of rows, each next row starts one step on at right angles to that. For pixel i of row j of a band:
x = (u - j * dv + i * du) / 65536
y = (v + j * du + i * dv) / 65536
Positions and steps are in 1/65536 of a pixel, so steps can be fractions. du = 65536 and dv = 0 copies the picture as it is, du = 32768 doubles its size, and a step that points diagonally turns it.
Turning and zooming the whole screen
With one element and a step of 240 rows, four numbers turn and zoom the whole picture:
a! = fr * 0.017 : z! = 1.5 + 1.1 * SIN(fr * 0.021) ' the angle, and the size
sx! = 160 + 70 * COS(fr * 0.011) : sy! = 120 + 50 * SIN(fr * 0.014)
du(0) = FP * COS(a!) / z! : dv(0) = -FP * SIN(a!) / z!
u(0) = FP * sx! - 160 * du(0) + 120 * dv(0)
v(0) = FP * sy! - 160 * dv(0) - 120 * du(0)
IF fr <= 25 THEN MAP SET ELSE FRAMEBUFFER WAIT
FRAMEBUFFER AFFINE F, N, u(), v(), du(), dv(), H
For an angle a! and a size z!, the step along a row is (cos a, -sin a) divided by the size. Then u and v are chosen so that the middle of the screen, pixel 160 of row 120, shows the point of the map being looked at, (sx!, sy!): that's what taking 160 steps along and 120 steps down does. As the angle, the size and the point change, the map spins, zooms and drifts.
The map as the ground
Give AFFINE an element for each row and the map lies down as the ground, as F-Zero's track did. Each row below the horizon is a line across the map. The higher up the screen, the further away the line, and the more map it covers. zt!() holds how many map pixels each screen pixel covers on each of the 149 rows below the horizon, 14/(k + 1), and it is made once.
LOCAL hz = 90, n = 149 ' the horizon's row, and the rows below it
' ...
FOR k = 0 TO n - 1 : zt!(k) = 14 / (k + 1) : NEXT
DO
fr = fr + 1
' ...
t! = fr * 0.011 ' round the track
camx! = 160 + 118 * COS(t!) : camy! = 120 + 74 * SIN(t!)
a! = ATAN2(74 * COS(t!), -118 * SIN(t!)) + 0.25 * SIN(fr * 0.05)
ca! = COS(a!) : sa! = SIN(a!)
MATH SCALE zt!(), -FP * sa!, fdu() : MATH SCALE zt!(), FP * ca!, fdv()
MATH SCALE zt!(), FP * (d! * ca! + 160 * sa!), fu() : MATH ADD fu(), FP * camx!, fu()
MATH SCALE zt!(), FP * (d! * sa! - 160 * ca!), fv() : MATH ADD fv(), FP * camy!, fv()
so(0) = -a! * 4 * W / (2 * PI) ' the screen shows a quarter of the way round
' the compass, turned as the camera is: a 69x69 window round the 48x48 picture at (124, 168) of 2
du(0) = FP * COS(a! + PI / 2) : dv(0) = -FP * SIN(a! + PI / 2)
u(0) = FP * 124 - 34 * (du(0) - dv(0)) : v(0) = FP * 168 - 34 * (dv(0) + du(0))
IF fr <= 25 THEN MAP SET ELSE FRAMEBUFFER WAIT
FRAMEBUFFER WARPH 2, N, so(), hz
FRAMEBUFFER AFFINE F, N, fu(), fv(), fdu(), fdv(), 1, hz + 1
FRAMEBUFFER AFFINE 2, N, u(), v(), du(), dv(), 69, 4, 247, 69, -1, 0
In each frame the camera's place (camx!, camy!) and its heading (ca!, sa!) turn zt!() into the four tables with six MATH statements. The step along each row points across the camera's view, scaled by zt!, and each row starts on its line d! × zt! map pixels ahead of the camera (d! is 160).
Above the horizon the sky is a WARPH of one band of 90 rows, which pans as the camera turns: a full turn moves it four screen widths, so the screen shows a quarter of the way round. The compass is a third trick. AFFINE's left and width limit it to a 69×69 window at the top right, turned with the camera, with an edge of -1 and colour 0 transparent so that only the compass is drawn. That's how to turn a sprite, as the Game Boy Advance did.
SPAN: a torch and an iris
SPAN does the job of the Super NES's windows. On each row it copies only the pixels from that row's left column to its right one, and it leaves the rest alone, fills it with a colour, or, with outside = 1, copies the outside instead.
A torch in the dark
FRAMEBUFFER WRITE F : drawmap mlit()
FRAMEBUFFER WRITE 2 : drawmap mdark()
FRAMEBUFFER WRITE N
FOR k = 0 TO 2 * r : hw!(k) = SQR(r * r - (k - r) * (k - r)) : NEXT
' ...
cx = 160 + 150 * SIN(fr * 0.021) : cy = 120 + 130 * SIN(fr * 0.033)
MATH SCALE hw!(), -1, q!() : MATH ADD q!(), cx, lf() : MATH ADD hw!(), cx - 1, rt()
IF fr <= 25 THEN MAP SET ELSE FRAMEBUFFER WAIT
FRAMEBUFFER COPY 2, N
FRAMEBUFFER SPAN F, N, lf(), rt(), 1, cy - r
The map is drawn twice, lit into F and in the dark into 2. In each frame the dark map is copied to the screen, and SPAN copies the lit map over it in a circle. The circle's half-width on each of its 93 rows is worked out once, with SQR. In each frame MATH turns the half-widths into left and right columns about the torch's centre, and SPAN's first, cy - r, puts the circle's top row in place, even when it's off the top of the screen.
An iris
FOR k = 0 TO H - 1 : d2!(k) = (k - 120) * (k - 120) : NEXT
' ...
ri = ABS(fr - 850) * 1.4
MATH SCALE d2!(), -1, wk!() : MATH ADD wk!(), ri * ri, wk!() : MATH CLAMP wk!(), 0, 1000000, wk!()
MATH POWER wk!(), 0.5, ih!()
MATH SCALE ih!(), -1, iq!() : MATH ADD iq!(), 160, il() : MATH ADD ih!(), 159, ir()
FRAMEBUFFER WAIT
FRAMEBUFFER SPAN F, N, il(), ir(), 1, 0, cl(0)
The iris changes size in every frame, so its half-widths are made in every frame, for all 240 rows: ri² - (y - 120)², clamped at 0 for the rows the circle doesn't reach, then MATH POWER 0.5 for the square root. A row whose half-width is 0 has its left column (160) beyond its right one (159), which SPAN takes as an empty span. The fill colour paints everything outside the circle black, so the iris closes to a point and opens again, as at the end of a level in Super Mario World.
Good to know
- The commands are in the VGA and HDMI builds, in MODE 2 to 5. The demo needs an RP2350, such as the PC3, because it uses
F,2and both layers, in MODE 2 at 320×240. - Make the tables once, and change them in each frame with
MATH SCALE,MATH ADD,MATH C_ADD,MATH CLAMPandMATH POWER, or slide them withphase. Work in float arrays, andMATH SCALE a!(), 1, b()copies the result into the integer array a command needs. - A picture that wraps round should join up with itself: the demo's mountains and hills are sums of sine waves that repeat every 320 pixels, so a scrolled row never shows a seam.
- Wait for the frame (
FRAMEBUFFER WAIT) before drawing onto the screen, so that the picture doesn't tear. - In MODE 3, 4 and 5 the PC3's framebuffers are in its PSRAM, and an AFFINE that shrinks a picture or turns it a long way is several times slower than in MODE 2.
Getting it
- PicoMite V8.00.00b3 has all four commands. The Pico Computer 3's firmware is
PicoMitePC3V8.00.00b3.uf2. Read the release notes before updating. Coming from beta 2 or from V7, the PicoMitePC3 resets its options at its first boot (save them first withOPTION DISK SAVEand load them again withOPTION DISK LOAD) and clears its flash slots, but keeps its A: drive. Coming from V7 or beta 1, programs saved by the old firmware must be loaded again from their.basfiles. - The demo is
Bas/warp_demo.basin the PicoMite repository. Run it on a PC3, or any RP2350 VGA or HDMI build, withOPTION RESOLUTION 640x480; any key ends it. - The manuals: the User Manual describes the commands under
FRAMEBUFFER, and the Game Development Guide, in the supplementary manuals, has three sections of worked examples: Raster Effects, Turning, Scaling and Floors and Windows of Any Shape. - The Colour Maximite 2 has had
FRAMEBUFFER WARPHandWARPVsince 8.00.00b3 (also asPAGE WARPHandPAGE WARPV), andAFFINEandSPANwill follow in 8.00.00b5.
These are betas, so reports of anything that doesn't look right are very welcome, on The Back Shed forum.