Optimize rendering and add faster fixed-step walking
This commit is contained in:
1 parent
3f9244962b
commit
6f9e6b5ca5
3 files changed
+578
-33
No files matched your search
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user