From f4f87f29c5dcf8a1e2e801f8c956aa086bc24f37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sun, 13 Nov 2022 22:57:40 +0000
Subject: [PATCH] refactor: moved palettes out

---
 examples/web/gb.ts        | 35 ++++++++++++-----------------------
 examples/web/palettes.tsx | 30 ++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 23 deletions(-)
 create mode 100644 examples/web/palettes.tsx

diff --git a/examples/web/gb.ts b/examples/web/gb.ts
index 0029e763..d38d87db 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 00000000..1eaab038
--- /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"]
+    }
+];
-- 
GitLab