Skip to content
Snippets Groups Projects
Verified Commit e2389ecf authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

feat: WASM palette colors

parent d8bc788e
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Support for true fullscreen at a browser level * Support for true fullscreen at a browser level
* Support for more flexible palette colors * Support for more flexible palette colors
* Support for setting palette colors using WASM
### Changed ### Changed
......
...@@ -286,6 +286,14 @@ class GameboyEmulator extends EmulatorBase implements Emulator { ...@@ -286,6 +286,14 @@ class GameboyEmulator extends EmulatorBase implements Emulator {
break; break;
} }
// @todo replace this with somethign more flexible
this.gameBoy.set_palette_colors_ws([
"b6a571",
"8b7e56",
"554d35",
"201d13"
]);
// resets the Game Boy engine to restore it into // resets the Game Boy engine to restore it into
// a valid state ready to be used // a valid state ready to be used
this.gameBoy.reset(); this.gameBoy.reset();
......
...@@ -10,6 +10,8 @@ use crate::{ ...@@ -10,6 +10,8 @@ use crate::{
}; };
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
use crate::ppu::Palette;
use std::convert::TryInto;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
...@@ -244,9 +246,34 @@ impl GameBoy { ...@@ -244,9 +246,34 @@ impl GameBoy {
prev(info); prev(info);
})); }));
} }
pub fn load_rom_ws(&mut self, data: &[u8]) -> Cartridge { pub fn load_rom_ws(&mut self, data: &[u8]) -> Cartridge {
self.load_rom(data).clone() self.load_rom(data).clone()
} }
pub fn set_palette_colors_ws(&mut self, value: Vec<JsValue>) {
let palette: Palette = value
.into_iter()
.map(|v| self.convert_value(&v))
.collect::<Vec<[u8; 3]>>()
.try_into()
.unwrap();
self.ppu().set_palette_colors(&palette);
}
fn convert_value(&self, value: &JsValue) -> [u8; 3] {
value
.as_string()
.unwrap()
.chars()
.collect::<Vec<char>>()
.chunks(2)
.map(|s| s.iter().collect::<String>())
.map(|s| u8::from_str_radix(&s, 16).unwrap())
.collect::<Vec<u8>>()
.try_into()
.unwrap()
}
} }
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
......
...@@ -178,8 +178,8 @@ pub struct Ppu { ...@@ -178,8 +178,8 @@ pub struct Ppu {
/// The base colors that are going to be used in the registration /// The base colors that are going to be used in the registration
/// of the concrete palettes, this value basically controls the /// of the concrete palettes, this value basically controls the
/// colors that are going to be shown for each of the four base /// colors that are going to be shown for each of the four base
/// values - 0x00, 0x01, 0x02, and 0x03 /// values - 0x00, 0x01, 0x02, and 0x03.
pallette_colors: Palette, palette_colors: Palette,
/// The palette of colors that is currently loaded in Game Boy /// The palette of colors that is currently loaded in Game Boy
/// and used for background (tiles). /// and used for background (tiles).
...@@ -308,7 +308,7 @@ impl Ppu { ...@@ -308,7 +308,7 @@ impl Ppu {
priority: false, priority: false,
index: 0, index: 0,
}; OBJ_COUNT], }; OBJ_COUNT],
pallette_colors: PALETTE_COLORS, palette_colors: PALETTE_COLORS,
palette: [[0u8; RGB_SIZE]; PALETTE_SIZE], palette: [[0u8; RGB_SIZE]; PALETTE_SIZE],
palette_obj_0: [[0u8; RGB_SIZE]; PALETTE_SIZE], palette_obj_0: [[0u8; RGB_SIZE]; PALETTE_SIZE],
palette_obj_1: [[0u8; RGB_SIZE]; PALETTE_SIZE], palette_obj_1: [[0u8; RGB_SIZE]; PALETTE_SIZE],
...@@ -518,7 +518,7 @@ impl Ppu { ...@@ -518,7 +518,7 @@ impl Ppu {
for index in 0..PALETTE_SIZE { for index in 0..PALETTE_SIZE {
let color_index: usize = (value as usize >> (index * 2)) & 3; let color_index: usize = (value as usize >> (index * 2)) & 3;
match color_index { match color_index {
0..=3 => self.palette[index] = self.pallette_colors[color_index], 0..=3 => self.palette[index] = self.palette_colors[color_index],
color_index => panic!("Invalid palette color index {:04x}", color_index), color_index => panic!("Invalid palette color index {:04x}", color_index),
} }
} }
...@@ -528,7 +528,7 @@ impl Ppu { ...@@ -528,7 +528,7 @@ impl Ppu {
for index in 0..PALETTE_SIZE { for index in 0..PALETTE_SIZE {
let color_index: usize = (value as usize >> (index * 2)) & 3; let color_index: usize = (value as usize >> (index * 2)) & 3;
match color_index { match color_index {
0..=3 => self.palette_obj_0[index] = self.pallette_colors[color_index], 0..=3 => self.palette_obj_0[index] = self.palette_colors[color_index],
color_index => panic!("Invalid palette color index {:04x}", color_index), color_index => panic!("Invalid palette color index {:04x}", color_index),
} }
} }
...@@ -538,7 +538,7 @@ impl Ppu { ...@@ -538,7 +538,7 @@ impl Ppu {
for index in 0..PALETTE_SIZE { for index in 0..PALETTE_SIZE {
let color_index: usize = (value as usize >> (index * 2)) & 3; let color_index: usize = (value as usize >> (index * 2)) & 3;
match color_index { match color_index {
0..=3 => self.palette_obj_1[index] = self.pallette_colors[color_index], 0..=3 => self.palette_obj_1[index] = self.palette_colors[color_index],
color_index => panic!("Invalid palette color index {:04x}", color_index), color_index => panic!("Invalid palette color index {:04x}", color_index),
} }
} }
...@@ -565,6 +565,10 @@ impl Ppu { ...@@ -565,6 +565,10 @@ impl Ppu {
&self.tiles &self.tiles
} }
pub fn set_palette_colors(&mut self, value: &Palette) {
self.palette_colors = *value;
}
pub fn palette(&self) -> Palette { pub fn palette(&self) -> Palette {
self.palette self.palette
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment