From 1e1e753be1d9568bd37028604f3613d84ad37e24 Mon Sep 17 00:00:00 2001 From: Lyoko Agents Date: Thu, 29 Oct 2026 00:47:57 +0000 Subject: [PATCH] Avoid blanking when opening and closing the sidebar --- source/game.h | 1 + source/main.c | 2 +- source/menu.c | 29 +++++++++++++-- tests/test_menu.c | 89 +++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 115 insertions(+), 6 deletions(-) diff --git a/source/game.h b/source/game.h index 8b15403..990ad0a 100644 --- a/source/game.h +++ b/source/game.h @@ -130,6 +130,7 @@ int save_menu_input(unsigned keys, int can_open, const struct Location *location void draw_save_menu(void); void draw_menu_scene(int x, int y); int menu_redraw_is_panel(enum SaveMenuPhase before, enum SaveMenuPhase after); +void draw_menu_panel(enum SaveMenuPhase before); void draw_bag(void); int has_caught(enum Species kind); int near_npc(int x, int y); diff --git a/source/main.c b/source/main.c index 08dcb65..c86acad 100644 --- a/source/main.c +++ b/source/main.c @@ -95,7 +95,7 @@ int main(void) if (menu_changed) { int panel_only = menu_redraw_is_panel(menu_before, save_menu.phase); if (!panel_only) DISPLAY = 0x0080; - if (panel_only) draw_save_menu(); + if (panel_only) draw_menu_panel(menu_before); else draw_menu_scene(x, y); present_frame(); if (!panel_only) DISPLAY = 0x0403; diff --git a/source/menu.c b/source/menu.c index 7dad601..8e136ca 100644 --- a/source/menu.c +++ b/source/menu.c @@ -2,6 +2,26 @@ struct SaveMenuState save_menu; static int team_member, team_parent; +static u16 panel_underlay[SAVE_MENU_H][SAVE_MENU_W]; + +static void copy_panel_underlay(int restoring) +{ + for (int y = 0; y < SAVE_MENU_H; ++y) + for (int x = 0; x < SAVE_MENU_W; ++x) { + int pixel = (SAVE_MENU_Y + y) * WIDTH + SAVE_MENU_X + x; + if (restoring) SCREEN[pixel] = panel_underlay[y][x]; + else panel_underlay[y][x] = SCREEN[pixel]; + } + if (restoring) mark_dirty(SAVE_MENU_X, SAVE_MENU_Y, SAVE_MENU_W, SAVE_MENU_H); +} + +void draw_menu_panel(enum SaveMenuPhase before) +{ + /* World stays paused; fullscreen returns recapture after rebuilding actors/HUD. */ + if (before == SAVE_MENU_CLOSED) copy_panel_underlay(0); + if (save_menu.phase == SAVE_MENU_CLOSED) copy_panel_underlay(1); + else draw_save_menu(); +} int save_menu_input(unsigned keys, int can_open, const struct Location *location) { @@ -70,14 +90,17 @@ void draw_menu_scene(int x, int y) else if (save_menu.phase == SAVE_MENU_BAG) draw_bag(); else { draw_scene(x, y); - if (save_menu.phase != SAVE_MENU_CLOSED) draw_save_menu(); + if (save_menu.phase != SAVE_MENU_CLOSED) { + copy_panel_underlay(0); + draw_save_menu(); + } } } int menu_redraw_is_panel(enum SaveMenuPhase before, enum SaveMenuPhase after) { - return before >= SAVE_MENU_OPTIONS && before <= SAVE_MENU_FAILED && - after >= SAVE_MENU_OPTIONS && after <= SAVE_MENU_FAILED; + return before <= SAVE_MENU_FAILED && after <= SAVE_MENU_FAILED && + (before != SAVE_MENU_CLOSED || after != SAVE_MENU_CLOSED); } void draw_save_menu(void) diff --git a/tests/test_menu.c b/tests/test_menu.c index 4171c61..67ea545 100644 --- a/tests/test_menu.c +++ b/tests/test_menu.c @@ -95,8 +95,8 @@ static void check_highlight_redraw(void) for (int from = SAVE_MENU_CLOSED; from <= SAVE_MENU_BAG; ++from) for (int to = SAVE_MENU_CLOSED; to <= SAVE_MENU_BAG; ++to) assert(menu_redraw_is_panel((enum SaveMenuPhase)from, (enum SaveMenuPhase)to) == - (from >= SAVE_MENU_OPTIONS && from <= SAVE_MENU_FAILED && - to >= SAVE_MENU_OPTIONS && to <= SAVE_MENU_FAILED)); + (from <= SAVE_MENU_FAILED && to <= SAVE_MENU_FAILED && + (from != SAVE_MENU_CLOSED || to != SAVE_MENU_CLOSED))); for (int room = OUTSIDE; room < ROOM_COUNT; ++room) { current_room = (enum Room)room; for (int up = 0; up < 2; ++up) { @@ -136,6 +136,90 @@ static void check_highlight_redraw(void) save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0}; } +static void check_panel_underlay(void) +{ + static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT]; + unsigned char saved[sizeof(test_save)]; + memcpy(saved, test_save, sizeof(saved)); + partner_owned = caught_count = 1; active_member = 0; + caught_species[0] = TIDEFIN; + for (int room = OUTSIDE; room < ROOM_COUNT; ++room) + for (int facing = FACING_DOWN; facing <= FACING_RIGHT; ++facing) { + current_room = (enum Room)room; + player_facing = (enum Facing)facing; + int x = indoors() ? 200 : room == ROUTE ? 207 : 210; + int y = room == BEACH ? 85 : 119; + reset_follower(x - 14, y); + render_scene(x, y); + memcpy(expected, SCREEN, sizeof(expected)); + copy_pending_rows(visible); + int fx = follower_x, fy = follower_y, head = trail_head; + for (int phase = SAVE_MENU_OPTIONS; phase <= SAVE_MENU_FAILED; ++phase) { + save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0}; + if (room == OUTSIDE && facing == FACING_DOWN && phase == SAVE_MENU_OPTIONS) + write_preview("build/panel-before-preview.ppm"); + press(START); + assert(menu_redraw_is_panel(SAVE_MENU_CLOSED, save_menu.phase)); + draw_menu_panel(SAVE_MENU_CLOSED); + assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H); + copy_pending_rows(visible); + assert(!menu_input(START, 1)); + for (int py = 0; py < HEIGHT; ++py) + for (int px = 0; px < WIDTH; ++px) + if (px < SAVE_MENU_X || px >= SAVE_MENU_X + SAVE_MENU_W || + py < SAVE_MENU_Y || py >= SAVE_MENU_Y + SAVE_MENU_H) + assert(SCREEN[py * WIDTH + px] == expected[py * WIDTH + px]); + save_menu.phase = (enum SaveMenuPhase)phase; + draw_menu_panel(SAVE_MENU_OPTIONS); + assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H); + copy_pending_rows(visible); + if (room == OUTSIDE && facing == FACING_DOWN && phase == SAVE_MENU_OPTIONS) + write_preview("build/panel-open-preview.ppm"); + unsigned close = phase == SAVE_MENU_OPTIONS ? B : phase == SAVE_MENU_SAVED ? A : START; + press(close); + assert(save_menu.phase == SAVE_MENU_CLOSED); + assert(menu_redraw_is_panel((enum SaveMenuPhase)phase, save_menu.phase)); + draw_menu_panel((enum SaveMenuPhase)phase); + assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H); + assert(memcmp(expected, SCREEN, sizeof(expected)) == 0); + copy_pending_rows(visible); + assert(memcmp(expected, visible, sizeof(expected)) == 0); + assert(!menu_input(close, 1)); + if (room == OUTSIDE && facing == FACING_DOWN && phase == SAVE_MENU_OPTIONS) + write_preview("build/panel-closed-preview.ppm"); + } + assert(player_facing == (enum Facing)facing && follower_x == fx && follower_y == fy && trail_head == head); + /* A partner change on a fullscreen page must replace, not reuse, old underlay. */ + press(START); draw_menu_panel(SAVE_MENU_CLOSED); + press(A); assert(save_menu.phase == SAVE_MENU_TEAM); draw_menu_scene(x, y); + press(RIGHT); release_keys(); + const struct Location here = {room, x, y, facing}; + assert(save_menu_input(A, 1, &here)); + assert(active_member == 1 && save_menu.phase == SAVE_MENU_OPTIONS); + render_scene(x, y); memcpy(expected, SCREEN, sizeof(expected)); + draw_menu_scene(x, y); copy_pending_rows(visible); + press(B); draw_menu_panel(SAVE_MENU_OPTIONS); + assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H); + assert(memcmp(expected, SCREEN, sizeof(expected)) == 0); + copy_pending_rows(visible); + assert(memcmp(expected, visible, sizeof(expected)) == 0); + press(START); draw_menu_panel(SAVE_MENU_CLOSED); + press(DOWN); press(A); assert(save_menu.phase == SAVE_MENU_BAG); + draw_menu_scene(x, y); + press(B); draw_menu_scene(x, y); copy_pending_rows(visible); + press(B); draw_menu_panel(SAVE_MENU_OPTIONS); + assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H); + assert(memcmp(expected, SCREEN, sizeof(expected)) == 0); + copy_pending_rows(visible); + assert(memcmp(expected, visible, sizeof(expected)) == 0); + active_member = 0; + assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef); + } + assert(memcmp(saved, test_save, sizeof(saved)) == 0); + load_progress(); + save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0}; +} + void check_save_menu(void) { unsigned char saved[sizeof(test_save)]; @@ -236,5 +320,6 @@ void check_save_menu(void) current_room = old_room; save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0}; check_highlight_redraw(); + check_panel_underlay(); check_submenus(); }