Files
akbasic/docs/06-graphics.md
Andrew Kesterson 5c5bf63356 Draw into the whole window, not its top-left 320x200
The graphics verbs documented a coordinate transform that did not exist. With
SCALE off a coordinate went straight to akgl_draw_* as a pixel address, so an
800x600 window drew a C128 listing into its corner and left the rest unused --
while the chapter said coordinates were 320x200 and stretching to fit was the
host's business.

akbasic_GraphicsBackend gains a size entry point, require_graphics() asks it
before every verb that draws so a resized window is honoured between two
statements, and 320x200 becomes the fallback for a backend that leaves it NULL.
It is the record's one optional member, so a host written against the old header
keeps the behaviour it had.

SCALE now maps onto the device, and RGR(1)/RGR(2) report the drawing surface so
a program can use a window whose size it did not choose. RGR(0) is BASIC 7.0's
own field, the GRAPHIC mode.

SCALE also mapped xmax onto the width rather than onto the last pixel, so
SCALE 1, 319, 199 followed by DRAW 1, 319, 199 drew nothing at all -- one pixel
past the surface. Fixed in the same line, because it is what makes "SCALE gives
a C128 listing the whole window" true rather than nearly true.

The akgl test renders against a 128x128 target, deliberately smaller than the
old constants: a SCALE still dividing by them misses it entirely rather than
landing somewhere plausible.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-01 07:35:20 -04:00

160 lines
4.8 KiB
Markdown

# 6. Graphics
Everything in this chapter needs the SDL build and a graphics device. Without one each
verb refuses by name:
```basic requires=noakgl
10 DRAW 1, 0, 0 TO 100, 100
```
```output
? 10 : RUNTIME ERROR DRAW needs a graphics device and this runtime has none
```
## The coordinate space
**A drawing coordinate is a pixel of the host's window**, with (0, 0) at the top left.
On the standalone interpreter's 800 by 600 window, `DRAW 1, 799, 599` lands on the
bottom-right pixel and everything in between is reachable. A game embedding the
interpreter gets whatever size its own renderer is.
`RGR` is how a program finds out:
```basic requires=akgl
10 PRINT "THE SCREEN IS"
20 PRINT RGR(1)
30 PRINT "BY"
40 PRINT RGR(2)
50 DRAW 1, 0, 0 TO RGR(1) - 1, RGR(2) - 1
```
Subtracting one is not a wart, it is the last pixel: a window `RGR(1)` wide has
columns 0 through `RGR(1) - 1`.
**A C128 listing assumes 320 by 200 and will draw in the top-left corner.** Give it
the whole window by naming the space it was written for:
```basic requires=akgl
10 SCALE 1, 319, 199
20 BOX 1, 0, 0, 319, 199
```
That box is now the border of the window whatever size the window is. When no device
answers the size question at all, 320 by 200 is what the interpreter assumes — the
space a C128 listing was written for is the right thing to fall back to.
## Colour
`COLOR` binds a *source* to a palette index, and the drawing verbs name the source
rather than the colour:
```basic requires=akgl
10 COLOR 1, 3
20 DRAW 1, 10, 20
```
Sources are numbered 0 to 6; palette indices are 1 to 16, as on a C128. That
indirection is BASIC 7.0's, and it is why every drawing verb's first argument is a
small number that is not a colour.
## The verbs
### GRAPHIC
`GRAPHIC mode` chooses a screen mode; `GRAPHIC CLR` clears it. Mode 0 is text and
refuses to draw.
### DRAW
```basic requires=akgl
10 DRAW 1, 10, 20
20 DRAW 1, 0, 0 TO 100, 100 TO 200, 0
```
One coordinate pair plots a point. Two or more, separated by `TO`, draw a polyline. A
bare `DRAW 1` plots wherever `LOCATE` left the pixel cursor.
### BOX
```basic requires=akgl
10 BOX 1, 10, 10, 40, 40
```
Corners, and an optional rotation angle. An unrotated `BOX` outlines rather than fills.
### CIRCLE
```basic requires=akgl
10 CIRCLE 1, 160, 100, 50, 30
```
Source, centre, then the two radii — so it draws ellipses. Further arguments give a
start angle, an end angle, a rotation and the degree increment, which is what makes it
an arc or a polygon.
### PAINT
```basic requires=akgl
10 PAINT 1, 160, 100
```
Flood-fills the region containing a point. If the region is too large for the fill's
own working space it stops and reports rather than leaving a half-painted screen with
no explanation.
### LOCATE
Moves the pixel cursor, which is where a bare `DRAW` plots and where a `BOX` with two
coordinates finishes.
### SCALE
```basic requires=akgl
10 SCALE 1, 1023, 1023
```
Turns on user coordinates and gives their maxima. With it on, your coordinates are
mapped onto the drawing surface: 0 is the first pixel and the maximum you gave is the
*last* one, so `DRAW 1, 1023, 1023` above reaches the bottom-right corner rather than
missing it by a pixel. `SCALE 1` on its own uses 7.0's 1023 by 1023. `SCALE 0` turns
it off and coordinates go back to being window pixels.
### RGR
`RGR(0)` is the current `GRAPHIC` mode. `RGR(1)` and `RGR(2)` are the drawing
surface's width and height in pixels — those two are ours rather than 7.0's, and they
are what a program needs to use a window whose size it did not choose. All three
refuse when there is no graphics device, except `RGR(0)`, which is a mode this
interpreter recorded rather than a screen it has to go and measure.
### WIDTH
`WIDTH 1` or `WIDTH 2` sets how thick a drawn line is. A thick line is drawn as
parallel passes; see Chapter 13.
## Saving and stamping regions
`SSHAPE` copies a rectangle off the screen and `GSHAPE` stamps it back:
```basic requires=akgl
10 BOX 1, 0, 0, 20, 20
20 SSHAPE A$, 0, 0, 20, 20
30 GSHAPE A$, 100, 100
```
**`A$` holds a handle, not the pixels.** On a C128 the string holds the bitmap, so a
program could save it to disk or take its `LEN`. Here a string is a fixed 255 bytes and
the region is a device surface, so what goes in the string is a reference to it —
`SHAPE:0`. You can pass it to `GSHAPE` and to `SPRSAV`, which is everything BASIC ever
does with one, but you cannot store it or measure it.
## What is not here
`FILTER` parses and then refuses: there is no filter stage to configure. See Chapter 7.
The graphics verbs draw straight to the renderer rather than into a display list, so
anything drawn is overwritten by the text layer on the next frame. A program that wants
its drawing to persist has to redraw it. This is recorded as a defect rather than a
design; see Chapter 13.