`SOLID id, x1, y1, x2, y2` registers static collision geometry; `SOLID id` retires one and a bare `SOLID` retires them all, the way `TRAP`, `COLLISION` and `DCLOSE` all read absence. `COLLISION 2` and `BUMP(2)` stop being refused and mean *sprite met static geometry*. **This is the thing eight sprite slots made impossible.** A wall of bricks wants sixty, so until now a program could only collide with one by doing the arithmetic itself against its own array -- which is exactly what both breakout listings do, at about two hundred lines between them. A rectangle costs no sprite slot. The id is the **program's own number**, 1 to 64, not a minted handle. That is the whole trick for "which brick did I hit": the id comes back out again, so a wall built as `SOLID I#, ...` maps onto `B#(I#)` with no lookup, and retiring a broken brick is `SOLID I#`. `COLLISION 2` was refused with "sprite-to-background collision needs the screen read back every frame", which was true of the question a C128 asks -- a sprite against the bitmap's set pixels. `SOLID` gives this interpreter a background made of rectangles instead, which is the same question in a form it can answer. Same move `SPRSAV` made when it learned to take an image path. `AKBASIC_INTERRUPT_BACKGROUND` has been sitting in the interrupt table commented "COLLISION 2 -- sprite met background; refused" the whole time. Its accumulator is separate, so a sprite hitting a wall never sets a bit in `BUMP(1)`. **There is no `akgl_CollisionWorld` here, and that is deliberate.** libakgl's uniform grid keeps its cell heads, cell size and origin in file-scope statics, so it is one index per process -- and `akgl_collision_world_init()` ends in a `reset()` that memsets those heads *and* calls `akgl_heap_init_collision_cells()`. An interpreter embedded in a game with its own collision world would have destroyed every registration that game had made, on the first `SOLID` a script ran. So the geometry is indexed by an ordinary array here and pairs go straight to `akgl_collision_test()`, which needs no world. At sixty-four rectangles that is the right answer anyway; libakgl's own numbers put a naive sweep at 0.7% of a frame at sixty-four objects. **The scan now short-circuits when nothing has moved**, and that is what makes any of it affordable. Its inputs are the sprites' boxes, which slots are collidable, and the static geometry; if none changed the answer cannot have. A frame runs one full scan and 255 cached ones. Eight sprites against sixty-four rectangles is five hundred and twelve tests -- fine once a frame, ruinous 256 times. The benchmark was rewritten to say which path it is timing, because with the cache in place a loop that only calls the scan measures the short circuit and nothing else. Breakout now costs 590.6 ns for its one full scan plus 255 cached at 40.0, which is 10.8 us against a 1.19 ms frame -- **0.91%, less than the 2.0% it cost before any of this work**, with static geometry and contacts added on top. `NEW` retires the rectangles, where it cannot undefine a sprite pattern: there *is* an entry point for this one, so leaving them would be a choice, and the wrong one -- a rectangle is invisible, so one left behind by a deleted program is an unexplainable collision in the next. `CLR` leaves them alone. `tests/sprite_verbs.c` gains the whole second path against the mock and its `COLLISION 2` case is rewritten: it pinned the refusal, and now pins that type 2 arms its own handler without disturbing type 1's. `tests/akgl_backends.c` gains the end-to-end version, including a full sixty-four-rectangle wall so the proxy budget is exercised at its ceiling and the pool has to come back intact, and the sixty-fifth refused by name. A bare `SOLID` needed `akbasic_parse_optional_arglist` rather than `akbasic_parse_arglist`, which `DCLOSE` already uses for the same shape. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
614 lines
25 KiB
C
614 lines
25 KiB
C
/**
|
|
* @file sprite_verbs.c
|
|
* @brief Tests the group H verbs against the recording mock backend.
|
|
*
|
|
* Same shape as tests/graphics_verbs.c and for the same reason: these verbs emit
|
|
* nothing a golden file can compare, so the assertions are on the call log --
|
|
* what reached the device, in what order, with what geometry. Running a real
|
|
* BASIC line rather than calling the handler directly is deliberate; it
|
|
* exercises the dispatch-table row and the parse handler too, which is where an
|
|
* added verb is most likely to be wrong. MOVSPR in particular has a parse
|
|
* handler all its own.
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <akbasic/error.h>
|
|
#include <akbasic/runtime.h>
|
|
#include <akbasic/sprite.h>
|
|
|
|
#include "harness.h"
|
|
#include "mockdevice.h"
|
|
#include "testutil.h"
|
|
|
|
/** @brief Bring up a runtime with the mock devices attached and a program loaded. */
|
|
static akerr_ErrorContext AKERR_NOIGNORE *run_program(const char *source)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
|
|
PASS(errctx, harness_start(NULL));
|
|
mock_devices_init();
|
|
PASS(errctx, akbasic_runtime_set_devices(&HARNESS_RUNTIME, &MOCK_GRAPHICS,
|
|
&MOCK_AUDIO, &MOCK_INPUT, &MOCK_SPRITES));
|
|
PASS(errctx, akbasic_runtime_load(&HARNESS_RUNTIME, source));
|
|
PASS(errctx, akbasic_runtime_start(&HARNESS_RUNTIME, AKBASIC_MODE_RUN));
|
|
PASS(errctx, akbasic_runtime_run(&HARNESS_RUNTIME, 0));
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
/**
|
|
* @brief SPRITE sets what it is given and leaves alone what it is not.
|
|
*
|
|
* The "leaves alone" half is the one worth asserting: BASIC 7.0 lets every
|
|
* argument after the number be omitted, so a handler that treated a missing
|
|
* argument as zero would silently turn a sprite off on its way to changing the
|
|
* colour.
|
|
*/
|
|
static void test_sprite(void)
|
|
{
|
|
TEST_REQUIRE_OK(run_program("10 SPRITE 1, 1, 3, 0, 1, 0\n"));
|
|
/* Palette index 3 is red: 0x88, 0x39, 0x32 in src/graphics_tables.c. */
|
|
TEST_REQUIRE_STR(MOCK.log,
|
|
"sprcfg 1 136,57,50 x1 y0 b0\n"
|
|
"sprshow 1 1\n");
|
|
TEST_REQUIRE(HARNESS_RUNTIME.sprite_state.sprites[0].enabled, "SPRITE 1,1 should enable it");
|
|
TEST_REQUIRE_INT(HARNESS_RUNTIME.sprite_state.sprites[0].colorindex, 3);
|
|
harness_stop();
|
|
|
|
/* A second SPRITE naming only the number changes nothing at all. */
|
|
TEST_REQUIRE_OK(run_program("10 SPRITE 1, 1, 3, 0, 1, 0\n"
|
|
"20 SPRITE 1\n"));
|
|
TEST_REQUIRE_INT(HARNESS_RUNTIME.sprite_state.sprites[0].colorindex, 3);
|
|
TEST_REQUIRE(HARNESS_RUNTIME.sprite_state.sprites[0].xexpand,
|
|
"an omitted argument should leave x-expand alone");
|
|
TEST_REQUIRE(HARNESS_RUNTIME.sprite_state.sprites[0].enabled,
|
|
"an omitted argument should leave the enable bit alone");
|
|
harness_stop();
|
|
|
|
/* Sprite 0 and sprite 9 do not exist. */
|
|
TEST_REQUIRE_OK(run_program("10 SPRITE 9, 1\n"));
|
|
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "outside 1..8") != NULL,
|
|
"sprite 9 should be refused, got \"%s\"", HARNESS_OUTPUT);
|
|
harness_stop();
|
|
|
|
TEST_REQUIRE_OK(run_program("10 SPRITE 1, 1, 17\n"));
|
|
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "outside 1..16") != NULL,
|
|
"colour 17 should be refused, got \"%s\"", HARNESS_OUTPUT);
|
|
harness_stop();
|
|
}
|
|
|
|
/**
|
|
* @brief All four MOVSPR forms, which is really a test of its parse handler.
|
|
*
|
|
* The separators are the whole difference between them and they exist nowhere
|
|
* else in the language, so this is the only thing that says `;` and `#` are
|
|
* scanned at all.
|
|
*/
|
|
static void test_movspr(void)
|
|
{
|
|
/* Absolute. */
|
|
TEST_REQUIRE_OK(run_program("10 MOVSPR 1, 100, 50\n"));
|
|
TEST_REQUIRE_STR(MOCK.log, "sprmove 1 100.00,50.00\n");
|
|
harness_stop();
|
|
|
|
/* Relative: a signed coordinate is an offset, not a position. */
|
|
TEST_REQUIRE_OK(run_program("10 MOVSPR 1, 100, 50\n"
|
|
"20 MOVSPR 1, +10, -20\n"));
|
|
TEST_REQUIRE_STR(MOCK.log,
|
|
"sprmove 1 100.00,50.00\n"
|
|
"sprmove 1 110.00,30.00\n");
|
|
harness_stop();
|
|
|
|
/*
|
|
* Polar: distance first, then a bearing clockwise from vertical. 90 degrees
|
|
* is due right, so the whole distance lands on x and none of it on y --
|
|
* which is the assertion that catches a sine and cosine swapped over.
|
|
*/
|
|
TEST_REQUIRE_OK(run_program("10 MOVSPR 1, 100, 50\n"
|
|
"20 MOVSPR 1, 10 ; 90\n"));
|
|
TEST_REQUIRE_STR(MOCK.log,
|
|
"sprmove 1 100.00,50.00\n"
|
|
"sprmove 1 110.00,50.00\n");
|
|
harness_stop();
|
|
|
|
/* And 0 degrees is straight up, which is *minus* y on a screen. */
|
|
TEST_REQUIRE_OK(run_program("10 MOVSPR 1, 100, 50\n"
|
|
"20 MOVSPR 1, 10 ; 0\n"));
|
|
TEST_REQUIRE_STR(MOCK.log,
|
|
"sprmove 1 100.00,50.00\n"
|
|
"sprmove 1 100.00,40.00\n");
|
|
harness_stop();
|
|
|
|
/*
|
|
* Continuous. Nothing moves on the statement itself -- the sprite starts
|
|
* moving on the next step, paced off the host's clock -- so what is asserted
|
|
* here is that the speed was recorded and the device was *not* told to move.
|
|
*/
|
|
TEST_REQUIRE_OK(run_program("10 MOVSPR 1, 45 # 8\n"));
|
|
TEST_REQUIRE_STR(MOCK.log, "");
|
|
TEST_REQUIRE_INT(HARNESS_RUNTIME.sprite_state.sprites[0].speed, 8);
|
|
TEST_REQUIRE_FEQ(HARNESS_RUNTIME.sprite_state.sprites[0].angle, 45.0);
|
|
harness_stop();
|
|
|
|
/* Speed 16 does not exist. */
|
|
TEST_REQUIRE_OK(run_program("10 MOVSPR 1, 45 # 16\n"));
|
|
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "outside 0..15") != NULL,
|
|
"speed 16 should be refused, got \"%s\"", HARNESS_OUTPUT);
|
|
harness_stop();
|
|
}
|
|
|
|
/**
|
|
* @brief Continuous motion is paced off the host's clock, not off step count.
|
|
*
|
|
* The service runs on every step, so a program that does nothing still moves --
|
|
* and a host that never sets a clock still gets a still picture rather than a
|
|
* sprite tearing across the screen at step rate.
|
|
*/
|
|
static void test_continuous_motion(void)
|
|
{
|
|
double stopped = 0.0;
|
|
|
|
TEST_REQUIRE_OK(harness_start(NULL));
|
|
mock_devices_init();
|
|
TEST_REQUIRE_OK(akbasic_runtime_set_devices(&HARNESS_RUNTIME, NULL, NULL, NULL,
|
|
&MOCK_SPRITES));
|
|
TEST_REQUIRE_OK(akbasic_runtime_settime(&HARNESS_RUNTIME, 1000));
|
|
TEST_REQUIRE_OK(akbasic_runtime_load(&HARNESS_RUNTIME,
|
|
"1 MOVSPR 1, 90 # 10\n"
|
|
"2 GOTO 2\n"));
|
|
TEST_REQUIRE_OK(akbasic_runtime_start(&HARNESS_RUNTIME, AKBASIC_MODE_RUN));
|
|
|
|
/* Line 0 and line 1, with the clock standing still. */
|
|
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 2));
|
|
TEST_REQUIRE_FEQ(HARNESS_RUNTIME.sprite_state.sprites[0].x, 0.0);
|
|
|
|
/*
|
|
* One second later: speed 10 at AKBASIC_SPRITE_SPEED_PIXELS_PER_SECOND
|
|
* apiece, due right, so 50 pixels of x and none of y.
|
|
*/
|
|
TEST_REQUIRE_OK(akbasic_runtime_settime(&HARNESS_RUNTIME, 2000));
|
|
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 1));
|
|
TEST_REQUIRE_FEQ(HARNESS_RUNTIME.sprite_state.sprites[0].x,
|
|
10.0 * AKBASIC_SPRITE_SPEED_PIXELS_PER_SECOND);
|
|
TEST_REQUIRE_FEQ(HARNESS_RUNTIME.sprite_state.sprites[0].y, 0.0);
|
|
|
|
/*
|
|
* Speed 0 stops it where it is. The step that runs the MOVSPR services the
|
|
* motion *before* it runs the line -- the sprite is still moving when that
|
|
* step begins -- so one more second's worth lands first; where it stopped is
|
|
* whatever it reads after that, and the point is that six more seconds do
|
|
* not move it again.
|
|
*/
|
|
TEST_REQUIRE_OK(akbasic_runtime_load(&HARNESS_RUNTIME, "1 MOVSPR 1, 90 # 0\n"));
|
|
HARNESS_RUNTIME.environment->nextline = 1;
|
|
TEST_REQUIRE_OK(akbasic_runtime_settime(&HARNESS_RUNTIME, 3000));
|
|
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 1));
|
|
stopped = HARNESS_RUNTIME.sprite_state.sprites[0].x;
|
|
TEST_REQUIRE_INT(HARNESS_RUNTIME.sprite_state.sprites[0].speed, 0);
|
|
|
|
TEST_REQUIRE_OK(akbasic_runtime_settime(&HARNESS_RUNTIME, 9000));
|
|
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 1));
|
|
TEST_REQUIRE_FEQ(HARNESS_RUNTIME.sprite_state.sprites[0].x, stopped);
|
|
harness_stop();
|
|
}
|
|
|
|
/** @brief SPRCOLOR sets the two shared registers, and RSPCOLOR reads them back. */
|
|
static void test_sprcolor(void)
|
|
{
|
|
TEST_REQUIRE_OK(run_program("10 SPRCOLOR 5, 7\n |