Let a program ask how big the text grid is

`RGR(1)` and `RGR(2)` gave the window in pixels and nothing gave columns, rows
or the cell size -- so anything placing a character *and* a sprite at the same
spot had to hardcode a number measured by hand against whatever font the host
loaded. The Breakout in examples/ does exactly that, `CW# = 16`, and it is the
one thing in that listing that breaks on a different font or window.

**`RWINDOW` is BASIC 7.0's own answer and had never been implemented here.**
`RWINDOW(0)` is the current text window's rows and `RWINDOW(1)` its columns.
`RWINDOW(2)` reports a C128's 40 or 80 column screen mode, and this interpreter
has neither -- refused by name, because answering 0 would be a plausible lie,
which is worse than a refusal that says why.

The cell size in pixels is `RGR(3)` and `RGR(4)`, beside the surface's own
dimensions rather than on `RWINDOW`. Two reasons: a cell size is a fact about
the surface, and `RWINDOW` reports the *window*, so dividing `RGR(1)` by a
column count stops being right the moment a program calls `WINDOW`.

Both read a new optional `grid` entry point on `akbasic_TextSink` -- columns,
rows, cell width, cell height -- implemented by the akgl sink and forwarded by
the tee, in the shape `moveto` and `window` already had. NULL everywhere else,
so both verbs refuse by name against a sink with no grid. `akbasic_sink_init_
stdio()` clears it for the same reason it now clears the other two.

Measured on the standalone build: `RGR(3)` answers 16 and `RWINDOW` answers 50
columns by 37 rows -- the three numbers the Breakout listing had written out as
constants -- and `RWINDOW` follows a `WINDOW` call while `RGR(3)` does not.

tests/console_verbs.c drives the answers through a stand-in sink with a grid,
since the harness sink is stdio and has none; tests/graphics_verbs.c covers the
new `RGR` fields, their refusal, and the moved range bound. The `c excerpt=`
block in docs/10-embedding.md moves with the header, which is `docs_examples`
doing its job.

TODO.md section 6 item 31's second half, struck.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-02 00:05:17 -04:00
parent fd1f0d7c19
commit 694b446ce4
15 changed files with 361 additions and 21 deletions

View File

@@ -150,6 +150,111 @@ static void test_window_needs_a_grid(void)
harness_stop();
}
/*
* A grid a stdio sink does not have.
*
* `RWINDOW` and `RGR(3)`/`RGR(4)` read the sink's grid, and the harness sink is
* stdio, which has none -- so without a stand-in the only thing assertable here
* is the refusal. This is the akgl sink's shape without the SDL: the runtime
* asks through a function pointer and does not care who answers.
*
* Windowing changes the columns and rows and leaves the cell size alone, which
* is the distinction RWINDOW and RGR are split across, so the fake honours it.
*/
static int FAKE_COLUMNS = 50;
static int FAKE_ROWS = 37;
static akerr_ErrorContext AKERR_NOIGNORE *fake_grid(akbasic_TextSink *self, int *columns, int *rows, int *cellw, int *cellh)
{
PREPARE_ERROR(errctx);
(void)self;
FAIL_ZERO_RETURN(errctx, (columns != NULL && rows != NULL && cellw != NULL && cellh != NULL),
AKERR_NULLPOINTER, "NULL destination in fake grid");
*columns = FAKE_COLUMNS;
*rows = FAKE_ROWS;
*cellw = 16;
*cellh = 16;
SUCCEED_RETURN(errctx);
}
static akerr_ErrorContext AKERR_NOIGNORE *fake_window(akbasic_TextSink *self, int left, int top, int right, int bottom)
{
PREPARE_ERROR(errctx);
(void)self;
FAKE_COLUMNS = (right - left) + 1;
FAKE_ROWS = (bottom - top) + 1;
SUCCEED_RETURN(errctx);
}
/**
* @brief `RWINDOW` and the cell-size `RGR` fields report the grid.
*
* A program had no way to ask how big a character is, so anything placing a
* character *and* a sprite at the same spot hardcoded a cell size measured
* against whatever font the host loaded. The Breakout in `examples/` did, and it
* was the one thing in that listing that broke on a different window. TODO.md
* section 6 item 31.
*/
static void test_rwindow(void)
{
/* Without a grid, both refuse by name rather than answering something. */
TEST_REQUIRE_OK(run_program("10 PRINT RWINDOW(0)\n"));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "character grid") != NULL,
"RWINDOW against a stdio sink should refuse by name, got \"%s\"",
HARNESS_OUTPUT);
harness_stop();
FAKE_COLUMNS = 50;
FAKE_ROWS = 37;
TEST_REQUIRE_OK(load_program("10 PRINT RWINDOW(0)\n"
"20 PRINT RWINDOW(1)\n"
"30 PRINT RGR(3)\n"
"40 PRINT RGR(4)\n"));
HARNESS_SINK.grid = fake_grid;
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 0));
/* Rows first: that is RWINDOW's own order on a C128, and it is not obvious. */
TEST_REQUIRE_STR(HARNESS_OUTPUT, "37\n50\n16\n16\n");
harness_stop();
/* RWINDOW follows a WINDOW; the cell size does not, because windowing does
not change how big a character is. */
FAKE_COLUMNS = 50;
FAKE_ROWS = 37;
TEST_REQUIRE_OK(load_program("10 WINDOW 0, 0, 19, 4\n"
"20 PRINT RWINDOW(1)\n"
"30 PRINT RWINDOW(0)\n"
"40 PRINT RGR(3)\n"));
HARNESS_SINK.grid = fake_grid;
HARNESS_SINK.window = fake_window;
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 0));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "20\n5\n16\n");
HARNESS_SINK.window = NULL;
harness_stop();
/*
* Field 2 is refused by name. A C128 answers 0 for 40-column mode and 1 for
* 80-column; this interpreter has neither, and answering 0 would be a
* plausible lie -- which is worse than a refusal that says why.
*/
FAKE_COLUMNS = 50;
FAKE_ROWS = 37;
TEST_REQUIRE_OK(load_program("10 PRINT RWINDOW(2)\n"));
HARNESS_SINK.grid = fake_grid;
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 0));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "40 or 80 column mode") != NULL,
"RWINDOW(2) should be refused by name, got \"%s\"", HARNESS_OUTPUT);
harness_stop();
TEST_REQUIRE_OK(load_program("10 PRINT RWINDOW(7)\n"));
HARNESS_SINK.grid = fake_grid;
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 0));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "outside 0..1") != NULL,
"RWINDOW(7) should be out of range, got \"%s\"", HARNESS_OUTPUT);
harness_stop();
}
/** @brief TI# counts jiffies and TI$ formats them, both from the host's clock. */
static void test_ti(void)
{
@@ -174,6 +279,7 @@ int main(void)
test_wait();
test_key();
test_window_needs_a_grid();
test_rwindow();
test_ti();
return akbasic_test_failures;
}