Skip to content

Commit

Permalink
Better DMG Palettes
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed May 3, 2024
1 parent 378c890 commit c487a7d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
61 changes: 38 additions & 23 deletions src/components/ppu/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ impl PPU {

fn grey_to_l(palette: Palette, v: u8, i: usize) -> Color {
match v >> (2 * i) & 0x03 {
0x00 => palette.dark,
0x01 => palette.dark_gray,
0x02 => palette.light_gray,
_ => palette.light,
0x00 => palette.light,
0x01 => palette.light_gray,
0x02 => palette.dark_gray,
_ => palette.dark,
}
}

Expand All @@ -296,21 +296,7 @@ impl PPU {
CCMode::SGB => self.cc.sgb_color_lut[color as usize],
};

self.set_rgb(x, color[0], color[1], color[2]);
}

fn set_rgb(&mut self, x: usize, r: u8, g: u8, b: u8) {
let bytes_per_pixel = 4;
let bytes_per_row = bytes_per_pixel * SCREEN_W;
let vertical_offset = self.ly as usize * bytes_per_row;
let horizontal_offset = x * bytes_per_pixel;
let total_offset = vertical_offset + horizontal_offset;

let mut framebuffer = self.framebuffer.write().unwrap();
framebuffer[total_offset + 0] = r;
framebuffer[total_offset + 1] = g;
framebuffer[total_offset + 2] = b;
framebuffer[total_offset + 3] = 0xFF;
self.write_pixel(color[0], color[1], color[2], x, self.ly);
}

fn draw_bg(&mut self) {
Expand Down Expand Up @@ -417,7 +403,7 @@ impl PPU {
Self::grey_to_l(self.ppu_config.palette, self.bgp, color)
};

self.set_rgb(x, color.r, color.g, color.b);
self.write_pixel(color.r(), color.g(), color.b(), x, self.ly);
}
}
}
Expand Down Expand Up @@ -526,7 +512,7 @@ impl PPU {
Self::grey_to_l(self.ppu_config.palette, self.obp0, color)
};

self.set_rgb(px.wrapping_add(x) as usize, color.r, color.g, color.b);
self.write_pixel(color.r(), color.g(), color.b(), px.wrapping_add(x) as usize, self.ly);
}
}
}
Expand All @@ -539,6 +525,21 @@ impl PPU {
fn write_vram(&mut self, a: u16, v: u8, bank: usize) {
self.vram[(bank * 0x2000) + a as usize - 0x8000] = v;
}

fn write_pixel(&mut self, r: u8, g: u8, b: u8, x: usize, y: u8) {
pub const BYTES_PER_PIXEL: usize = 4;
pub const BYTES_PER_ROW: usize = BYTES_PER_PIXEL * SCREEN_W;

let vertical_offset = y as usize * BYTES_PER_ROW;
let horizontal_offset = x as usize * BYTES_PER_PIXEL;
let total_offset = vertical_offset + horizontal_offset;

let mut framebuffer = self.framebuffer.write().unwrap();
framebuffer[total_offset + 0] = r;
framebuffer[total_offset + 1] = g;
framebuffer[total_offset + 2] = b;
framebuffer[total_offset + 3] = 0xFF;
}
}

impl Memory for PPU {
Expand Down Expand Up @@ -613,8 +614,22 @@ impl Memory for PPU {
self.reset_ly();
self.ppu_mode = PPUMode::HBlank;

let mut framebuffer = self.framebuffer.write().unwrap();
*framebuffer = [0xFF; FRAMEBUFFER_SIZE];
match self.mode {
GBMode::DMG => {
let color = self.ppu_config.palette.off;
let (r, g, b) = (color.r(), color.g(), color.b());

for y in 0..SCREEN_H {
for x in 0..SCREEN_W {
self.write_pixel(r, g, b, x, y as u8);
}
}
},
GBMode::CGB => {
let mut framebuffer = self.framebuffer.write().unwrap();
*framebuffer = [0xFF; FRAMEBUFFER_SIZE];
}
}
}
}
0xFF41 => {
Expand Down
30 changes: 21 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,41 @@ pub struct Palette {
pub dark_gray: Color,
pub light_gray: Color,
pub light: Color,
pub off: Color
}

impl Palette {
pub fn new() -> Self {
Self {
dark: Color::new(175, 203, 70),
dark_gray: Color::new(121, 170, 109),
light_gray: Color::new(34, 111, 95),
light: Color::new(8, 41, 95)
dark: Color::new(0x081810),
dark_gray: Color::new(0x396139),
light_gray: Color::new(0x84A563),
light: Color::new(0xC6DE8C),
off: Color::new(0xD2E6A6)
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub hex: u32,
}

impl Color {
pub fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
pub fn new(hex: u32) -> Self {
Self { hex }
}

pub fn r(&self) -> u8 {
((self.hex & 0xFF0000) >> 16) as u8
}

pub fn g(&self) -> u8 {
((self.hex & 0x00FF00) >> 8) as u8
}

pub fn b(&self) -> u8 {
((self.hex & 0x0000FF) >> 0) as u8
}
}

Expand Down

0 comments on commit c487a7d

Please sign in to comment.