From 47af5904e5abb9dd896fb8a405408d7446cb0e2d Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Tue, 8 Sep 2026 18:17:54 -0400 Subject: [PATCH] refactor(pico8): tighten the frame loop and reuse existing helpers Render through a packed-RGBA palette lookup and a 32-bit pixel view, so a paint is two stores per framebuffer byte instead of eight clamped byte writes, and rebind the heap views only when wasm memory grows. Separate `advance` from `render` so a catch-up tick emulates several frames but paints once, rather than discarding up to two full frames of pixel work exactly when the machine is already behind. Load the runtime and fetch the cart in parallel, reuse one input struct instead of allocating per step, and skip disconnected gamepads, whose stale analog values press buttons on their own in Firefox (#3851). Replace the hand-rolled liveness flag with useIsAlive, take the canvas geometry and the canvas background token from their existing homes, and move the pad, axis and mouse-button mappings next to the keyboard map. Co-Authored-By: Claude Opus 5 (1M context) --- .../v2/composables/useCanPlay/index.test.ts | 25 +-- .../composables/usePlatformPlayable/index.ts | 10 +- .../v2/composables/useViewTransition/index.ts | 24 +-- frontend/src/v2/views/Player/Pico8.vue | 168 ++++++++++-------- .../src/v2/views/Player/pico8Runtime.test.ts | 30 +++- frontend/src/v2/views/Player/pico8Runtime.ts | 84 +++++---- frontend/src/v2/views/Player/scriptLoader.ts | 5 +- 7 files changed, 203 insertions(+), 143 deletions(-) diff --git a/frontend/src/v2/composables/useCanPlay/index.test.ts b/frontend/src/v2/composables/useCanPlay/index.test.ts index c15945637..cca04cdac 100644 --- a/frontend/src/v2/composables/useCanPlay/index.test.ts +++ b/frontend/src/v2/composables/useCanPlay/index.test.ts @@ -68,18 +68,21 @@ beforeEach(() => { describe("useCanPlay", () => { it.each([ - ["EJS", "ejs", "canPlayEJS"], - ["js-dos", "jsDos", "canPlayJsDos"], - ["PICO-8", "pico8", "canPlayPico8"], - ["Ruffle", "ruffle", "canPlayRuffle"], - ] as const)("reports %s support on its own flag", (_label, stub, flag) => { - support[stub].mockReturnValue(true); - if (stub === "pico8") support.pico8Rom.mockReturnValue(true); - const result = useCanPlay(() => makeRom()); + ["EJS", "ejs", "canPlayEJS", []], + ["js-dos", "jsDos", "canPlayJsDos", []], + ["PICO-8", "pico8", "canPlayPico8", ["pico8Rom"]], + ["Ruffle", "ruffle", "canPlayRuffle", []], + ] as const)( + "reports %s support on its own flag", + (_label, stub, flag, alsoRequires) => { + support[stub].mockReturnValue(true); + for (const extra of alsoRequires) support[extra].mockReturnValue(true); + const result = useCanPlay(() => makeRom()); - expect(result[flag].value).toBe(true); - expect(result.canPlay.value).toBe(true); - }); + expect(result[flag].value).toBe(true); + expect(result.canPlay.value).toBe(true); + }, + ); // A physical game, or one whose file vanished from the library, has nothing // to hand the emulator: every route boots from the download endpoint. diff --git a/frontend/src/v2/composables/usePlatformPlayable/index.ts b/frontend/src/v2/composables/usePlatformPlayable/index.ts index 2ca20e042..fe414ad06 100644 --- a/frontend/src/v2/composables/usePlatformPlayable/index.ts +++ b/frontend/src/v2/composables/usePlatformPlayable/index.ts @@ -9,12 +9,10 @@ // slugs at once (sort comparators, group-by buckets in PlatformsIndex). // // `emulator` resolves to the in-browser engine that actually drives the -// platform: "ruffle" for Flash, "jsdos" for Windows 3.x/9x, "pico8" for -// PICO-8, "dosbox" -// when the EJS catalogue picks the dosbox_pure core (DOS is wrapped by -// EJS but distinctive enough to surface by name in the UI), -// "emulatorjs" for everything else playable, and `null` when nothing on -// the server can run it. +// platform: "ruffle" for Flash, "jsdos" for Windows 3.x/9x, "dosbox" when +// the EJS catalogue picks the dosbox_pure core (DOS is wrapped by EJS but +// distinctive enough to surface by name in the UI), "emulatorjs" for +// everything else playable, and `null` when nothing on the server can run it. // // `mode` folds the in-browser answer together with streaming: a platform // served by a configured streaming container is playable too, just not in diff --git a/frontend/src/v2/composables/useViewTransition/index.ts b/frontend/src/v2/composables/useViewTransition/index.ts index 6cc234348..786cbaa58 100644 --- a/frontend/src/v2/composables/useViewTransition/index.ts +++ b/frontend/src/v2/composables/useViewTransition/index.ts @@ -24,8 +24,18 @@ // * Browser without `document.startViewTransition` → just navigate. import { nextTick, ref } from "vue"; import type { RouteLocationNormalized, Router } from "vue-router"; +import { ROUTES } from "@/plugins/router"; import { useReducedMotion } from "@/v2/composables/useReducedMotion"; +// Routes that paint a `rom-cover-` hero, so any pair of them can morph. +const MORPH_ROM_ROUTES: ReadonlySet = new Set([ + ROUTES.ROM, + ROUTES.EMULATORJS, + ROUTES.JSDOS, + ROUTES.PICO8, + ROUTES.RUFFLE, +]); + export interface MorphSource { el: HTMLElement; name: string; @@ -97,17 +107,9 @@ export function useViewTransition() { function morphNameForRoute(route: RouteLocationNormalized): string | null { const name = route.name; const params = route.params as Record; - // `rom` (detail) and the players all own a - // `rom-cover-` hero, so morph between any of them and the gallery / - // each other. - if ( - (name === "rom" || - name === "emulatorjs" || - name === "jsdos" || - name === "pico8" || - name === "ruffle") && - params.rom - ) { + // `rom` (detail) and the players all own a `rom-cover-` hero, so morph + // between any of them and the gallery, or each other. + if (MORPH_ROM_ROUTES.has(String(name)) && params.rom) { return `rom-cover-${params.rom}`; } if (name === "platform" && params.platform) { diff --git a/frontend/src/v2/views/Player/Pico8.vue b/frontend/src/v2/views/Player/Pico8.vue index 1df20b9af..d9104921e 100644 --- a/frontend/src/v2/views/Player/Pico8.vue +++ b/frontend/src/v2/views/Player/Pico8.vue @@ -1,4 +1,6 @@