Optimize rendering and add faster fixed-step walking

This commit is contained in:
Rory Rogers committed 2026-10-28 14:50:28 +00:00
1 parent 3f9244962b
commit 6f9e6b5ca5
3 files changed
+578 -33

No files matched your search

+6
View File
@@ -55,6 +55,12 @@ Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)`
Graphics are drawn into a RAM buffer first. Only the changed spans of each row are copied to the GBA screen during VBlank, so walking does not expose a half-erased character. Full map changes are copied while the display is temporarily blanked.
The active map's scenery is cached in a separate 76,800-byte RAM buffer. Walking and dialogue cleanup copy background rows from that cache instead of recalculating every pixel. The cache rebuilds when maps change; opening or closing an exterior door updates only its 13×18-pixel patch. Actors and UI are never stored in the scenery cache.
Sprite artwork is decoded once into cached palette indices. Character and creature bodies are drawn directly in clipped rows, with dirty-region tracking once per source row instead of a rectangle operation per opaque pixel. Mirroring, transparent pixels, dynamic palettes, and scaled battle sprites use the same renderer.
Movement has a separate 60 Hz fixed-step clock using cascaded GBA timers 2 and 3, independent of rendering. Walking is two pixels per tick (120 pixels/second), with normalized diagonal speed. Every pixel is collision-checked and recorded for Sprout and grass encounters, so faster movement does not skip walls or map exits or double the following distance. Missed updates can catch up by at most five ticks; older backlog is dropped after a long stall. Menus, conversations, battles, and map-loading time do not build up movement backlog. The existing footstep artwork is unchanged, with its phase now clocked by simulation ticks rather than rendered frames.
## Saving your partner and items
Receiving Sprout or Finn's Poké Balls automatically writes a small cartridge SRAM save, stored by mGBA alongside the ROM as `game.sav`. Keep that file to retain your partner and item count after reopening the game. Older Sprout-only saves remain compatible. Partner ownership, the ball count, and whether Finn's gift was claimed are saved, but your map and position are not. The golden-creature battle is a repeatable practice battle with two moves, health bars, victory, defeat, and running away. Sprout recovers afterward. Experience and catching creatures are not implemented yet.
+249 -33
View File
@@ -38,6 +38,29 @@ static void mark_dirty(int x, int y, int w, int h)
}
enum { A = 1, B = 2, SELECT = 4, START = 8, RIGHT = 16, LEFT = 32, UP = 64, DOWN = 128 };
enum { MOVEMENT_HZ = 60, MOVEMENT_CLOCK_HZ = 16384, WALK_SPEED = 2, MAX_MOVEMENT_TICKS = 5 };
static struct { unsigned last, fraction; } movement_clock;
static unsigned diagonal_fraction;
static void pause_movement_at(unsigned now)
{
movement_clock.last = now;
movement_clock.fraction = 0;
diagonal_fraction = 0;
}
/* Fixed 60 Hz simulation, independent of how often a frame is drawn. */
static int movement_ticks_at(unsigned now)
{
unsigned elapsed = now - movement_clock.last;
movement_clock.last = now;
if (elapsed > MOVEMENT_CLOCK_HZ / 4) elapsed = MOVEMENT_CLOCK_HZ / 4;
unsigned total = movement_clock.fraction + elapsed * MOVEMENT_HZ;
unsigned ticks = total / MOVEMENT_CLOCK_HZ;
movement_clock.fraction = total % MOVEMENT_CLOCK_HZ;
/* Drop excess backlog after a long stall rather than teleporting. */
return ticks > MAX_MOVEMENT_TICKS ? MAX_MOVEMENT_TICKS : (int)ticks;
}
/* These are good first things to change! Colors use numbers from 0 to 31. */
static const u16 grass = RGB(15, 26, 12);
@@ -47,6 +70,14 @@ enum { NPC_X = 146, NPC_Y = 106, PROF_X = 113, PROF_Y = 92, RANGER_X = 83, RANGE
enum Room { OUTSIDE, COTTAGE, ROUTE, MELLON_GARDEN, MELLON_HOUSE };
static enum Room current_room = OUTSIDE;
static int door_open = 0;
/* One scenery-only cache; the frame buffer still holds actors and UI. */
static u16 background_cache[WIDTH * HEIGHT];
static enum Room background_room;
static int background_valid, background_door_open;
#ifdef GAME_TEST
static unsigned scenery_samples;
static unsigned rectangle_calls, sprite_decodes, sprite_row_marks;
#endif
enum Facing { FACING_DOWN, FACING_UP, FACING_LEFT, FACING_RIGHT };
static enum Facing player_facing = FACING_DOWN;
enum { DIALOGUE_X = 8, DIALOGUE_Y = 96, DIALOGUE_W = 224, DIALOGUE_H = 44 };
@@ -286,6 +317,7 @@ static int use_map_exit(int *x, int *y, unsigned keys)
}
door_open = 0;
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(*x, *y);
return 1;
}
@@ -319,6 +351,7 @@ static int use_door(int *x, int *y)
door_open = 0;
player_facing = FACING_DOWN;
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(*x, *y);
return 1;
}
@@ -329,6 +362,7 @@ static void reset_position(int *x, int *y)
*y = indoors() ? 119 : current_room == ROUTE ? 60 : current_room == MELLON_GARDEN ? 113 : 103;
player_facing = FACING_DOWN;
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(*x, *y);
}
@@ -410,6 +444,9 @@ static int advance_conversation(int page)
static void rectangle(int x, int y, int w, int h, u16 color)
{
#ifdef GAME_TEST
++rectangle_calls;
#endif
mark_dirty(x, y, w, h);
for (int row = y; row < y + h; ++row) {
for (int col = x; col < x + w; ++col) {
@@ -818,6 +855,9 @@ static u16 mellon_house_scenery(int x, int y)
static u16 scenery(int x, int y)
{
#ifdef GAME_TEST
++scenery_samples;
#endif
if (current_room == ROUTE) return route_scenery(x, y);
if (current_room == COTTAGE) return cottage_scenery(x, y);
if (current_room == MELLON_GARDEN) return mellon_garden_scenery(x, y);
@@ -825,26 +865,139 @@ static u16 scenery(int x, int y)
return village_scenery(x, y);
}
static void restore(int x, int y, int w, int h)
static void ensure_background(void)
{
mark_dirty(x, y, w, h);
for (int row = y; row < y + h; ++row)
for (int col = x; col < x + w; ++col)
if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT)
SCREEN[row * WIDTH + col] = scenery(col, row);
if (!background_valid || background_room != current_room) {
for (int row = 0; row < HEIGHT; ++row)
for (int col = 0; col < WIDTH; ++col)
background_cache[row * WIDTH + col] = scenery(col, row);
background_room = current_room;
background_door_open = door_open;
background_valid = 1;
} else if (background_door_open != door_open) {
/* Only these door pixels change; keep the rest of the map cached. */
if (current_room == OUTSIDE || current_room == MELLON_GARDEN)
for (int row = 54; row < 72; ++row)
for (int col = 171; col < 184; ++col)
background_cache[row * WIDTH + col] = scenery(col, row);
background_door_open = door_open;
}
}
/* Transparent pixel art. Each character selects a color in the palette. */
static void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
int mirrored)
static void restore(int x, int y, int w, int h)
{
for (int row = 0; row < rows; ++row)
for (int col = 0; art[row][col]; ++col)
int left = x < 0 ? 0 : x;
int right = x + w > WIDTH ? WIDTH : x + w;
int top = y < 0 ? 0 : y;
int bottom = y + h > HEIGHT ? HEIGHT : y + h;
if (left >= right || top >= bottom) return;
ensure_background();
mark_dirty(left, top, right - left, bottom - top);
for (int row = top; row < bottom; ++row) {
const u16 *source = &background_cache[row * WIDTH + left];
u16 *destination = &SCREEN[row * WIDTH + left];
for (int col = 0; col < right - left; ++col) destination[col] = source[col];
}
}
enum { SPRITE_SIZE = 24, SPRITE_CACHE_SIZE = 7 };
struct DecodedSprite {
const char *const *art;
unsigned char width, rows;
unsigned char first[SPRITE_SIZE], end[SPRITE_SIZE];
unsigned char pixels[SPRITE_SIZE * SPRITE_SIZE];
};
static struct DecodedSprite sprite_cache[SPRITE_CACHE_SIZE];
static unsigned sprite_cache_next;
static const struct DecodedSprite *decode_sprite(const char *const *art, int width, int rows)
{
for (int i = 0; i < SPRITE_CACHE_SIZE; ++i)
if (sprite_cache[i].art == art && sprite_cache[i].width == width &&
sprite_cache[i].rows == rows) return &sprite_cache[i];
struct DecodedSprite *decoded = &sprite_cache[sprite_cache_next];
sprite_cache_next = (sprite_cache_next + 1) % SPRITE_CACHE_SIZE;
decoded->art = art;
decoded->width = (unsigned char)width;
decoded->rows = (unsigned char)rows;
for (int row = 0; row < rows; ++row) {
decoded->first[row] = (unsigned char)width;
decoded->end[row] = 0;
for (int col = 0; col < width; ++col) {
unsigned char encoded = 0;
if (art[row][col] != '.') {
char pixel = art[row][col];
int index = pixel <= '9' ? pixel - '0' : pixel - 'A' + 10;
rectangle(x + (mirrored ? 15 - col : col), y + row, 1, 1, palette[index]);
/* Zero means transparent; palette entry zero is still usable. */
encoded = (unsigned char)(index + 1);
if (decoded->first[row] == width) decoded->first[row] = (unsigned char)col;
decoded->end[row] = (unsigned char)(col + 1);
}
decoded->pixels[row * SPRITE_SIZE + col] = encoded;
}
}
#ifdef GAME_TEST
++sprite_decodes;
#endif
return decoded;
}
/* Decode once, then write clipped rows directly into the RAM frame buffer. */
static void blit_sprite(int x, int y, const char *const *art, int width, int rows,
const u16 *palette, int scale, int mirrored)
{
if (width <= 0 || width > SPRITE_SIZE || rows <= 0 || rows > SPRITE_SIZE || scale <= 0)
return;
if (x >= WIDTH || y >= HEIGHT || x + width * scale <= 0 || y + rows * scale <= 0) return;
const struct DecodedSprite *decoded = decode_sprite(art, width, rows);
for (int row = 0; row < rows; ++row) {
int first = decoded->first[row], end = decoded->end[row];
if (first >= end) continue;
int top = y + row * scale, bottom = top + scale;
if (bottom <= 0 || top >= HEIGHT) continue;
int left = x + (mirrored ? width - end : first) * scale;
int right = x + (mirrored ? width - first : end) * scale;
if (left < 0) left = 0;
if (right > WIDTH) right = WIDTH;
if (top < 0) top = 0;
if (bottom > HEIGHT) bottom = HEIGHT;
if (left >= right) continue;
mark_dirty(left, top, right - left, bottom - top);
#ifdef GAME_TEST
++sprite_row_marks;
#endif
const unsigned char *source = &decoded->pixels[row * SPRITE_SIZE];
if (scale == 1) {
u16 *destination = &SCREEN[top * WIDTH + left];
int col = mirrored ? width - 1 - (left - x) : left - x;
int advance = mirrored ? -1 : 1;
for (int offset = 0; offset < right - left; ++offset, col += advance) {
unsigned char pixel = source[col];
if (pixel) destination[offset] = palette[pixel - 1];
}
} else {
for (int col = first; col < end; ++col) {
unsigned char pixel = source[col];
if (!pixel) continue;
u16 color = palette[pixel - 1];
int start = x + (mirrored ? width - 1 - col : col) * scale;
int finish = start + scale;
if (start < left) start = left;
if (finish > right) finish = right;
for (int screen_y = top; screen_y < bottom; ++screen_y) {
u16 *destination = &SCREEN[screen_y * WIDTH];
for (int screen_x = start; screen_x < finish; ++screen_x)
destination[screen_x] = color;
}
}
}
}
}
static void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
int mirrored)
{
blit_sprite(x, y, art, 16, rows, palette, 1, mirrored);
}
static const char *const creature_art[] = {
@@ -880,11 +1033,7 @@ static void scaled_creature(int x, int y, int scale, const u16 *palette)
{
rectangle(x - 7 * scale, y, 14 * scale, 2 * scale,
indoors() ? RGB(15, 11, 8) : RGB(8, 16, 7));
for (int row = 0; row < 16; ++row)
for (int col = 0; col < 16; ++col)
if (creature_art[row][col] != '.')
rectangle(x + (col - 8) * scale, y + (row - 16) * scale, scale, scale,
palette[creature_art[row][col] - '0']);
blit_sprite(x - 8 * scale, y - 16 * scale, creature_art, 16, 16, palette, scale, 0);
}
#include "wild_creatures.h"
@@ -893,11 +1042,7 @@ static void draw_wild_creature(enum Species enemy, int x, int y)
{
const struct WildArt *art = &wild_art[enemy - FERNFOX];
rectangle(x - 20, y, 40, 3, RGB(8, 16, 7));
for (int row = 0; row < 24; ++row)
for (int col = 0; col < 24; ++col)
if (art->rows[row][col] != '.')
rectangle(x + (col - 12) * 2, y + (row - 24) * 2, 2, 2,
art->palette[art->rows[row][col] - '0']);
blit_sprite(x - 24, y - 48, art->rows, 24, 24, art->palette, 2, 0);
}
static void draw_sprout(int x, int y, int scale)
@@ -1052,6 +1197,38 @@ static int can_walk(int x, int y)
return 1;
}
enum WalkResult { WALK_NORMAL, WALK_MAP_CHANGED, WALK_ENCOUNTER };
static enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks)
{
player_facing = facing_for_keys(keys, player_facing);
int dx = !!(keys & RIGHT) - !!(keys & LEFT);
int dy = !!(keys & DOWN) - !!(keys & UP);
if (!dx && !dy) { diagonal_fraction = 0; return WALK_NORMAL; }
if (ticks > MAX_MOVEMENT_TICKS) ticks = MAX_MOVEMENT_TICKS;
for (int tick = 0; tick < ticks; ++tick) {
int pixels = WALK_SPEED;
if (dx && dy) {
/* 181/256 approximates 1/sqrt(2): no diagonal speed boost. */
diagonal_fraction += WALK_SPEED * 181;
pixels = (int)(diagonal_fraction / 256);
diagonal_fraction %= 256;
} else {
diagonal_fraction = 0;
}
for (int pixel = 0; pixel < pixels; ++pixel) {
int old_x = *x, old_y = *y;
if (can_walk(*x + dx, *y)) *x += dx;
if (can_walk(*x, *y + dy)) *y += dy;
/* Record every substep, preserving Sprout's existing path gap. */
move_follower(old_x, old_y, *x, *y);
if (use_map_exit(x, y, keys)) return WALK_MAP_CHANGED;
if (check_grass_encounter(old_x, old_y, *x, *y)) return WALK_ENCOUNTER;
}
}
return WALK_NORMAL;
}
static void status(const char *message)
{
rectangle(0, 0, WIDTH, 18, RGB(29, 30, 25));
@@ -1204,6 +1381,30 @@ static void wait_for_frame(void)
while (VCOUNT < 160) { }
}
/* Timers 2+3 form a cascaded 32-bit clock at 16,777,216 / 1024 Hz. */
static unsigned movement_time(void)
{
volatile u16 *low = (volatile u16 *)0x04000108;
volatile u16 *high = (volatile u16 *)0x0400010c;
u16 upper, lower;
do {
upper = *high;
lower = *low;
} while (upper != *high);
return ((unsigned)upper << 16) | lower;
}
static void start_movement_timer(void)
{
volatile u16 *timer2 = (volatile u16 *)0x04000108;
volatile u16 *timer3 = (volatile u16 *)0x0400010c;
timer2[1] = timer3[1] = 0;
timer2[0] = timer3[0] = 0;
timer3[1] = 0x0084; /* Enable cascade from timer 2. */
timer2[1] = 0x0083; /* Enable, prescaler 1024; no IRQ required. */
pause_movement_at(movement_time());
}
struct DmaChannel {
const void *source;
volatile void *destination;
@@ -1235,17 +1436,25 @@ int main(void)
int dialogue_page = -1; /* -1 means we are not talking. */
int team_open = 0;
unsigned previous = 0;
start_movement_timer();
load_progress();
reset_follower(x, y);
reset_dirty_rows();
draw_scene(x, y);
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
for (;;) {
unsigned keys = (~KEYS) & 0x03ff;
unsigned pressed = keys & ~previous;
previous = keys;
unsigned now = movement_time();
int movement_ticks = 0;
if (team_open || battle.phase != BATTLE_OFF || dialogue_page >= 0)
pause_movement_at(now);
else
movement_ticks = movement_ticks_at(now);
random_state += 0x9e3779b9u; /* Mix in how long the player waits. */
if (team_open) {
if (pressed & (SELECT | A | B)) {
@@ -1254,6 +1463,7 @@ int main(void)
draw_scene(x, y);
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
} else {
present_frame();
}
@@ -1268,6 +1478,7 @@ int main(void)
status("SPROUT RESTED AND IS READY AGAIN");
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
} else {
draw_battle_hud();
present_frame();
@@ -1305,38 +1516,39 @@ int main(void)
draw_team();
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
continue;
}
int old_x = x, old_y = y;
int old_follower_x = follower_x, old_follower_y = follower_y;
enum Facing old_facing = player_facing;
player_facing = facing_for_keys(keys, player_facing);
int next_x = x + !!(keys & RIGHT) - !!(keys & LEFT);
int next_y = y + !!(keys & DOWN) - !!(keys & UP);
if (can_walk(next_x, y)) x = next_x;
if (can_walk(x, next_y)) y = next_y;
enum WalkResult walk = WALK_NORMAL;
if (pressed & START) {
reset_position(&x, &y);
greeting = 0;
location_status(x, y);
pause_movement_at(now);
movement_ticks = 0;
} else {
move_follower(old_x, old_y, x, y);
walk = walk_movement(keys, &x, &y, movement_ticks);
}
if (use_map_exit(&x, &y, keys)) {
if (walk == WALK_MAP_CHANGED) {
greeting = 0;
dialogue_page = -1;
DISPLAY = 0x0080;
draw_scene(x, y);
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
continue;
}
if (check_grass_encounter(old_x, old_y, x, y)) {
if (walk == WALK_ENCOUNTER) {
greeting = 0;
DISPLAY = 0x0080;
draw_battle();
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
continue;
}
if (pressed & A) {
@@ -1357,6 +1569,7 @@ int main(void)
draw_scene(x, y);
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
continue;
} else if (near_npc(x, y)) {
begin_conversation(x, y);
@@ -1380,6 +1593,7 @@ int main(void)
draw_battle();
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
continue;
} else if (near_golden(x, y)) {
status("HELLO NEW FRIEND");
@@ -1389,7 +1603,10 @@ int main(void)
greeting = 180;
}
}
if (greeting && --greeting == 0) location_status(x, y);
if (greeting) {
greeting = greeting > movement_ticks ? greeting - movement_ticks : 0;
if (!greeting) location_status(x, y);
}
if (x != old_x || y != old_y || player_facing != old_facing ||
(partner_owned && (old_follower_x != follower_x || old_follower_y != follower_y))) {
restore(old_x - 8, old_y - 20, 17, 24);
@@ -1405,7 +1622,6 @@ int main(void)
}
if (dialogue_page >= 0) dialogue_box(dialogue_page);
present_frame();
++frame;
if (frame == 1024) frame = 0;
frame = (frame + movement_ticks) & 1023;
}
}
+323
View File
@@ -7,6 +7,7 @@ static unsigned short test_pixels[240 * 160 + 2];
#define SCREEN (test_pixels + 1)
static unsigned char test_save[32];
#define SAVE_MEMORY test_save
#define GAME_TEST
#define main gba_main
#include "../source/main.c"
@@ -724,6 +725,325 @@ static void check_ball_gift(void)
partner_owned = balls_given = ball_count = follower_ready = 0;
}
static void check_background_cache(void)
{
static u16 reference[WIDTH * HEIGHT];
background_valid = 0;
scenery_samples = 0;
reset_dirty_rows();
restore(-10, -10, 2, 2);
restore(240, 160, 4, 4);
assert(!background_valid && scenery_samples == 0 && pending_pixels() == 0);
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) {
current_room = (enum Room)room;
door_open = 0;
unsigned before = scenery_samples;
restore(0, 0, WIDTH, HEIGHT);
assert(scenery_samples - before == WIDTH * HEIGHT);
assert(background_valid && background_room == current_room);
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x)
reference[y * WIDTH + x] = scenery(x, y);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
/* Reference cost: every 17x24 restore previously evaluated 408 pixels. */
before = scenery_samples;
for (int y = 63; y < 87; ++y)
for (int x = 100; x < 117; ++x) (void)scenery(x, y);
assert(scenery_samples - before == 408);
before = scenery_samples;
for (int frame = 0; frame < 500; ++frame) {
restore(100, 63, 17, 24);
restore(120, 63, 17, 24); /* Same restoration work with Sprout. */
}
assert(scenery_samples == before); /* Zero procedural work while walking. */
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
/* Actors and menu graphics must never leak into the scenery cache. */
int x, y;
reset_position(&x, &y);
actors(x, y, 0);
draw_team();
restore(0, 0, WIDTH, HEIGHT);
assert(scenery_samples == before);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
}
const enum Room houses[] = {OUTSIDE, MELLON_GARDEN};
for (unsigned i = 0; i < sizeof(houses) / sizeof(houses[0]); ++i) {
current_room = houses[i];
door_open = 0;
restore(0, 0, WIDTH, HEIGHT);
for (int open = 1; open >= 0; --open) {
door_open = open;
unsigned before = scenery_samples;
restore(170, 54, 15, 24);
assert(scenery_samples - before == 13 * 18); /* Patch only the door. */
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x)
reference[y * WIDTH + x] = scenery(x, y);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
before = scenery_samples;
restore(170, 54, 15, 24);
assert(scenery_samples == before);
}
}
/* Clip once before copying, without altering pixels outside the request. */
current_room = OUTSIDE;
restore(0, 0, WIDTH, HEIGHT);
const u16 marker = RGB(1, 2, 3);
rectangle(0, 0, WIDTH, HEIGHT, marker);
reset_dirty_rows();
unsigned before = scenery_samples;
restore(-3, -2, 5, 4);
restore(239, 159, 5, 4);
restore(0, 0, 0, 10);
restore(0, 0, 10, 0);
assert(scenery_samples == before && pending_pixels() == 5);
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x) {
u16 expected = ((x < 2 && y < 2) || (x == 239 && y == 159)) ?
background_cache[y * WIDTH + x] : marker;
assert(SCREEN[y * WIDTH + x] == expected);
}
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
puts("Background cache: 5 maps match reference; warm restores use 0 scenery evaluations (was 408 each)");
}
/* Reference implementation of the previous per-pixel rectangle renderer. */
static void reference_sprite(int x, int y, const char *const *art, int width, int rows,
const u16 *palette, int scale, int mirrored)
{
for (int row = 0; row < rows; ++row)
for (int col = 0; col < width; ++col) {
char pixel = art[row][col];
if (pixel == '.') continue;
int index = pixel <= '9' ? pixel - '0' : pixel - 'A' + 10;
rectangle(x + (mirrored ? width - 1 - col : col) * scale, y + row * scale,
scale, scale, palette[index]);
}
}
static void sprite_test_canvas(void)
{
for (int i = 0; i < WIDTH * HEIGHT; ++i) SCREEN[i] = (u16)((i * 3 ^ 0x5a5a) & 0x7fff);
reset_dirty_rows();
}
static void check_sprite_rows(void)
{
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
static const char *const zero_art[] = {"0A..B1..", "..1.....", "........"};
const struct { const char *const *art; int width, rows; } images[] = {
{person_art, 16, 20}, {person_back_art, 16, 20}, {person_side_art, 16, 20},
{creature_art, 16, 16}, {fernfox_art, 24, 24}, {emberwing_art, 24, 24},
{pebblit_art, 24, 24}, {zero_art, 8, 3}
};
const int positions[][2] = {
{70, 60}, {-8, 40}, {232, 40}, {80, -12}, {80, 155},
{-5, -7}, {236, 156}, {-100, -100}, {240, 30}, {50, 160}
};
u16 palette[13], alternate[13];
for (int i = 0; i < 13; ++i) {
palette[i] = RGB(i, i * 2, 31 - i);
alternate[i] = RGB(31 - i, i, i * 2);
}
palette[0] = 0; /* Palette entry zero is opaque black, not transparency. */
for (unsigned image = 0; image < sizeof(images) / sizeof(images[0]); ++image)
for (int scale = 1; scale <= 3; ++scale)
for (int mirror = 0; mirror < 2; ++mirror)
for (unsigned p = 0; p < sizeof(positions) / sizeof(positions[0]); ++p) {
int x = positions[p][0], y = positions[p][1];
const u16 *colors = p & 1 ? alternate : palette;
sprite_test_canvas();
reference_sprite(x, y, images[image].art, images[image].width,
images[image].rows, colors, scale, mirror);
memcpy(expected, SCREEN, sizeof(expected));
sprite_test_canvas();
memcpy(visible, SCREEN, sizeof(visible));
unsigned before_rectangles = rectangle_calls;
unsigned before_rows = sprite_row_marks;
blit_sprite(x, y, images[image].art, images[image].width,
images[image].rows, colors, scale, mirror);
assert(rectangle_calls == before_rectangles);
assert(sprite_row_marks - before_rows <= (unsigned)images[image].rows);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
assert(memcmp(expected, visible, sizeof(expected)) == 0);
}
/* Palette changes reuse decoded indices, rather than cached colors. */
sprite_test_canvas();
blit_sprite(20, 20, zero_art, 8, 3, palette, 1, 0);
assert(SCREEN[20 * WIDTH + 20] == palette[0]);
unsigned before_decodes = sprite_decodes;
blit_sprite(20, 20, zero_art, 8, 3, alternate, 1, 0);
assert(sprite_decodes == before_decodes);
assert(SCREEN[20 * WIDTH + 20] == alternate[0]);
sprite_test_canvas();
unsigned before_rectangles = rectangle_calls;
reference_sprite(80, 60, person_art, 16, 20, palette, 1, 0);
unsigned old_rectangles = rectangle_calls - before_rectangles;
assert(old_rectangles > 100);
sprite_test_canvas();
before_rectangles = rectangle_calls;
unsigned before_rows = sprite_row_marks;
blit_sprite(80, 60, person_art, 16, 20, palette, 1, 0);
assert(rectangle_calls == before_rectangles);
assert(sprite_row_marks - before_rows == 20);
before_decodes = sprite_decodes;
for (int frame = 0; frame < 1000; ++frame)
blit_sprite(80, 60, person_art, 16, 20, palette, 1, frame & 1);
assert(sprite_decodes == before_decodes && rectangle_calls == before_rectangles);
sprite_test_canvas();
blit_sprite(0, 0, zero_art, 0, 3, palette, 1, 0);
blit_sprite(0, 0, zero_art, 8, 25, palette, 1, 0);
blit_sprite(0, 0, zero_art, 8, 3, palette, 0, 0);
assert(pending_pixels() == 0);
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
printf("Sprite rows: character body uses 0 rectangle calls (was %u), 20 row marks, no repeat decoding\n",
old_rectangles);
}
static void check_movement_clock_and_speed(void)
{
const int render_rates[] = {15, 20, 30, 60, 120};
for (int following = 0; following < 2; ++following)
for (unsigned rate = 0; rate < sizeof(render_rates) / sizeof(render_rates[0]); ++rate) {
current_room = OUTSIDE;
partner_owned = following;
int x = 40, y = 125, total_ticks = 0;
reset_follower(x, y);
pause_movement_at(0);
for (int frame = 1; frame <= render_rates[rate]; ++frame) {
unsigned now = (unsigned)(frame * MOVEMENT_CLOCK_HZ / render_rates[rate]);
int ticks = movement_ticks_at(now);
total_ticks += ticks;
assert(walk_movement(RIGHT, &x, &y, ticks) == WALK_NORMAL);
}
assert(total_ticks == MOVEMENT_HZ && x == 160 && y == 125);
if (following) assert(follower_x == x - FOLLOW_STEPS && follower_y == y);
}
/* Timer rollover, idle reads, bounded catch-up, and paused menus. */
pause_movement_at(0xffffff00u);
assert(movement_ticks_at(0x00000100u) == 1);
assert(movement_ticks_at(0x00000100u) == 0);
pause_movement_at(0);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == MAX_MOVEMENT_TICKS);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == 0); /* No hidden backlog. */
pause_movement_at(123456u);
assert(movement_ticks_at(123456u) == 0);
pause_movement_at(999999u); /* Time spent in a menu is discarded. */
assert(movement_ticks_at(999999u) == 0);
assert(movement_ticks_at(999999u + MOVEMENT_CLOCK_HZ / 60 + 1) == 1);
partner_owned = 0;
current_room = OUTSIDE;
int x = 108, y = 103;
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 110 && y == 103 && player_facing == FACING_RIGHT);
assert(walk_movement(UP, &x, &y, 0) == WALK_NORMAL);
assert(x == 110 && y == 103 && player_facing == FACING_UP);
x = 132; y = 106;
assert(walk_movement(RIGHT, &x, &y, MAX_MOVEMENT_TICKS) == WALK_NORMAL);
assert(x == 133 && can_walk(x, y)); /* Never tunnel through Mila. */
x = 84; y = 70;
assert(walk_movement(LEFT, &x, &y, 2) == WALK_NORMAL);
assert(x == 82 && can_walk(x, y)); /* Stop at the pond, pixel by pixel. */
x = 177; y = 80;
assert(walk_movement(UP, &x, &y, 2) == WALK_NORMAL);
assert(y == 78 && near_door(x, y)); /* Door remains usable. */
pause_movement_at(0);
x = 160; y = 90;
for (int tick = 0; tick < 20; ++tick)
assert(walk_movement(RIGHT | DOWN, &x, &y, 1) == WALK_NORMAL);
assert(x == 188 && y == 118); /* Approximately 40 pixels, not 56 diagonally. */
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 190 && diagonal_fraction == 0);
assert(walk_movement(RIGHT | LEFT, &x, &y, 5) == WALK_NORMAL);
assert(x == 190 && y == 118);
/* Crossing an exit stops remaining substeps instead of skipping its trigger. */
partner_owned = 1;
x = 108; y = 129;
reset_follower(x, y);
assert(walk_movement(DOWN, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == ROUTE && x == 108 && y == 60);
assert(follower_x == x && follower_y == y);
y = 48;
assert(walk_movement(UP, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == OUTSIDE && x == 108 && y == 125);
x = 221; y = 113;
assert(walk_movement(RIGHT, &x, &y, 5) == WALK_MAP_CHANGED);
assert(current_room == MELLON_GARDEN && x == 31 && y == 113);
x = 18;
assert(walk_movement(LEFT, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == OUTSIDE && x == 210 && y == 113);
current_room = COTTAGE;
x = 120; y = 125;
assert(walk_movement(DOWN, &x, &y, 5) == WALK_NORMAL);
assert(current_room == COTTAGE && y == 131); /* Indoor exits still require A. */
/* Two-pixel ticks still count every traveled substep for grass and following. */
current_room = ROUTE;
x = 40; y = 70;
reset_follower(x, y);
grass_steps = 0;
random_state = 1; /* First roll does not trigger an encounter. */
for (int tick = 0; tick < 12; ++tick)
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 64 && grass_steps == 24 && follower_x == 50 && follower_y == 70);
grass_steps = 63; /* Next traveled pixel guarantees an encounter. */
assert(walk_movement(RIGHT, &x, &y, 5) == WALK_ENCOUNTER);
assert(x == 65 && y == 70 && battle.phase == BATTLE_TURN);
assert(battle_input(B) && battle_input(A) && battle.phase == BATTLE_OFF);
/* Faster and catch-up frames must erase both old sprites completely. */
static u16 snapshot[WIDTH * HEIGHT];
const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
const int tick_counts[] = {1, 2, 1, 3, 0};
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) {
current_room = (enum Room)room;
reset_position(&x, &y);
if (current_room == OUTSIDE) { x = 40; y = 125; }
if (current_room == ROUTE) y = 94;
reset_follower(x, y);
render_scene(x, y);
reset_dirty_rows();
int phase = 0;
for (int frame = 0; frame < 40; ++frame) {
int old_x = x, old_y = y, old_fx = follower_x, old_fy = follower_y;
int ticks = tick_counts[frame % 5];
assert(walk_movement(directions[frame / 10], &x, &y, ticks) == WALK_NORMAL);
restore(old_x - 8, old_y - 20, 17, 24);
restore(old_fx - 8, old_fy - 20, 17, 24);
actors(x, y, (phase >> 3) & 1);
location_status(x, y);
assert(pending_pixels() < 16500);
memcpy(snapshot, SCREEN, sizeof(snapshot));
render_scene(x, y);
restore(x - 8, y - 20, 17, 24);
actors(x, y, (phase >> 3) & 1);
assert(memcmp(snapshot, SCREEN, sizeof(snapshot)) == 0);
reset_dirty_rows();
phase += ticks;
}
}
current_room = OUTSIDE;
partner_owned = follower_ready = 0;
grass_steps = 0;
player_facing = FACING_DOWN;
pause_movement_at(0);
puts("Movement: 120 pixels/second at 15/20/30/60/120 render updates, with or without Sprout");
}
int main(void)
{
int x = 108, y = 103;
@@ -905,6 +1225,9 @@ int main(void)
check_following_and_battle();
check_wild_encounters();
check_ball_gift();
check_background_cache();
check_sprite_rows();
check_movement_clock_and_speed();
assert(test_pixels[0] == 0xbeef);
assert(test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
puts("Ball gift/inventory/save migration, wild encounters, battles, following, and map checks passed");