diff --git a/examples/web/gb.ts b/examples/web/gb.ts index 0029e7633e39cb64c235a075d80df6f393428e7f..d38d87dbb0c0df4857a70759cf04294ea6a59765 100644 --- a/examples/web/gb.ts +++ b/examples/web/gb.ts @@ -5,6 +5,7 @@ import { PixelFormat, RomInfo } from "./react/app"; +import { PALETTES } from "./palettes"; import { Cartridge, @@ -23,25 +24,6 @@ const IDLE_HZ = 10; const SAMPLE_RATE = 2; -const PALETTES = [ - { - name: "basic", - colors: ["ffffff", "c0c0c0", "606060", "000000"] - }, - { - name: "hogwards", - colors: ["b6a571", "8b7e56", "554d35", "201d13"] - }, - { - name: "pacman", - colors: ["ffff00", "ffb897", "3732ff", "000000"] - }, - { - name: "mariobros", - colors: ["f7cec3", "cc9e22", "923404", "000000"] - } -]; - const KEYS_NAME: Record<string, number> = { ArrowUp: PadKey.Up, ArrowDown: PadKey.Down, @@ -296,8 +278,10 @@ export class GameboyEmulator extends EmulatorBase implements Emulator { break; } - // runs the initial palette set operation - this.changePalette(); + // runs the initial palette set operation, restoring + // the palette of the emulator according to the currently + // selected one + this.setPalette(); // resets the Game Boy engine to restore it into // a valid state ready to be used @@ -474,10 +458,9 @@ export class GameboyEmulator extends EmulatorBase implements Emulator { } changePalette() { - const palette = PALETTES[this.paletteIndex]; - this.gameBoy?.set_palette_colors_ws(palette.colors); this.paletteIndex += 1; this.paletteIndex %= PALETTES.length; + this.setPalette(); } benchmark(count = 50000000) { @@ -501,6 +484,12 @@ export class GameboyEmulator extends EmulatorBase implements Emulator { } } + private setPalette(index?: number) { + index ??= this.paletteIndex; + const palette = PALETTES[index]; + this.gameBoy?.set_palette_colors_ws(palette.colors); + } + private async fetchRom( romPath: string ): Promise<{ name: string; data: Uint8Array }> { diff --git a/examples/web/palettes.tsx b/examples/web/palettes.tsx new file mode 100644 index 0000000000000000000000000000000000000000..1eaab0384e6eb2a32a409c47cd1fbc0a93fc6389 --- /dev/null +++ b/examples/web/palettes.tsx @@ -0,0 +1,30 @@ +export const PALETTES = [ + { + name: "basic", + colors: ["ffffff", "c0c0c0", "606060", "000000"] + }, + { + name: "hogwards", + colors: ["b6a571", "8b7e56", "554d35", "201d13"] + }, + { + name: "christmas", + colors: ["e8e7df", "8bab95", "9e5c5e", "534d57"] + }, + { + name: "goldsilver", + colors: ["c5c66d", "97a1b0", "585e67", "232529"] + }, + { + name: "pacman", + colors: ["ffff00", "ffb897", "3732ff", "000000"] + }, + { + name: "mariobros", + colors: ["f7cec3", "cc9e22", "923404", "000000"] + }, + { + name: "pokemon", + colors: ["f87800", "b86000", "783800", "000000"] + } +];