Compare commits

...

2 Commits

Author SHA1 Message Date
8dd14c7719 Origin cleanup 2020-02-21 10:19:21 +01:00
30efdc1108 DMG: Create some abstraction 2020-02-21 09:55:28 +01:00

View File

@ -62,6 +62,24 @@ const MONOCHROME_PALETTE: &'static [[u8; 3]; 4] = &[
]; ];
*/ */
#[derive(Copy, Clone, Default)]
struct DmgPalette(u8);
impl DmgPalette {
fn get_color(self, n: usize) -> sdl2::pixels::Color {
const MONOCHROME_PALETTE: &'static [[u8; 3]; 4] = &[
[255, 255, 255],
[200, 200, 200],
[125, 125, 12],
[50, 50, 50],
];
assert!(n < 4);
let c = self.0 >> (2 * n);
let n = c & 3;
let c = MONOCHROME_PALETTE[n as usize];
sdl2::pixels::Color::RGB(c[0], c[1], c[2])
}
}
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
struct CgbPalette([u8; 8]); struct CgbPalette([u8; 8]);
impl CgbPalette { impl CgbPalette {
@ -126,7 +144,7 @@ impl BgMapAttributes {
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
enum PixelOrigin { enum PixelOrigin {
Empty, Empty,
Background, Background(usize),
Window, Window,
Sprite, Sprite,
} }
@ -210,9 +228,9 @@ impl Sprite {
pub struct Display { pub struct Display {
control: u8, control: u8,
status: u8, status: u8,
background_palette: u8, background_palette: DmgPalette,
// Only 0 and 1 for GB // Only 0 and 1 for GB
object_palette: [u8; 2], object_palette: [DmgPalette; 2],
scrollx: u8, scrollx: u8,
scrolly: u8, scrolly: u8,
windowx: u8, windowx: u8,
@ -266,8 +284,8 @@ impl Display {
Display { Display {
control: 0, control: 0,
status: 0, status: 0,
background_palette: 0, background_palette: Default::default(),
object_palette: [0u8; 2], object_palette: Default::default(),
scrollx: 0, scrollx: 0,
scrolly: 0, scrolly: 0,
windowx: 0, windowx: 0,
@ -378,9 +396,9 @@ impl Display {
0xFF44 => self.curline = 0, 0xFF44 => self.curline = 0,
0xFF45 => self.lyc = val, 0xFF45 => self.lyc = val,
// GB classic // GB classic
0xFF47 => self.background_palette = val, 0xFF47 => self.background_palette.0 = val,
0xFF48 => self.object_palette[0] = val, 0xFF48 => self.object_palette[0].0 = val,
0xFF49 => self.object_palette[1] = val, 0xFF49 => self.object_palette[1].0 = val,
// GBC // GBC
0xFF68 => { 0xFF68 => {
self.background_palette_index = val & 0b0111_1111; self.background_palette_index = val & 0b0111_1111;
@ -451,9 +469,9 @@ impl Display {
0xFF43 => self.scrollx, 0xFF43 => self.scrollx,
0xFF44 => self.curline, 0xFF44 => self.curline,
0xFF45 => self.lyc, 0xFF45 => self.lyc,
0xFF47 => self.background_palette, 0xFF47 => self.background_palette.0,
0xFF48 => self.object_palette[0], 0xFF48 => self.object_palette[0].0,
0xFF49 => self.object_palette[1], 0xFF49 => self.object_palette[1].0,
0xFF4A => self.windowy, 0xFF4A => self.windowy,
0xFF4B => self.windowx, 0xFF4B => self.windowx,
0xFF4F => self.vram_bank | 0b1111_1110, 0xFF4F => self.vram_bank | 0b1111_1110,
@ -637,7 +655,8 @@ impl Display {
// Do not draw if the sprite should be drawn in the background // Do not draw if the sprite should be drawn in the background
if !sprite.is_foreground() { if !sprite.is_foreground() {
match pixel_origin { match pixel_origin {
PixelOrigin::Background | PixelOrigin::Window => continue, PixelOrigin::Background(0) => {},
PixelOrigin::Window | PixelOrigin::Background(_) => continue,
_ => {} _ => {}
} }
} }
@ -647,42 +666,17 @@ impl Display {
b2 = (tile_line_2 & 1 << (7 - x_o)) > 0; b2 = (tile_line_2 & 1 << (7 - x_o)) > 0;
// Sprites may be transparent. // Sprites may be transparent.
if !b1 && !b2 { if !b1 && !b2 && sprite.is_foreground() {
continue; continue;
} }
// GB
/*
let c = (b1 as u8) * 2 + b2 as u8;
let lookup: [u8; 4] = match sprite.palette() {
0 => [
self.object_palette[0] & 3,
(self.object_palette[0] >> 2) & 3,
(self.object_palette[0] >> 4) & 3,
(self.object_palette[0] >> 6) & 3,
],
1 => [
self.object_palette[1] & 3,
(self.object_palette[1] >> 2) & 3,
(self.object_palette[1] >> 4) & 3,
(self.object_palette[1] >> 6) & 3,
],
_ => unreachable!(),
};
let entry = MONOCHROME_PALETTE[lookup[c as usize] as usize];
// Draw stuff. We're currently only in monochrome mode
self.set_pixel(
x.wrapping_add(x_o),
render_y,
sdl2::pixels::Color::RGB(entry[0], entry[1], entry[2]),
PixelOrigin::Sprite,
);
*/
let c = ((b2 as u8) * 2 + b1 as u8) as usize; let c = ((b2 as u8) * 2 + b1 as u8) as usize;
if c == 0 { if c == 0 {
continue; continue;
} }
// DMG:
// let c = self.object_palette[sprite.palette() as usize].get_color(c);
// GBC:
let c = self.object_palette_cgb[sprite.palette() as usize].get_color(c); let c = self.object_palette_cgb[sprite.palette() as usize].get_color(c);
self.set_pixel( self.set_pixel(
x.wrapping_add(x_maybe_flipped), x.wrapping_add(x_maybe_flipped),
@ -810,10 +804,7 @@ impl Display {
// Lookup the color // Lookup the color
let c = ((b2 as u8) * 2 + b1 as u8) as usize; let c = ((b2 as u8) * 2 + b1 as u8) as usize;
let origin = match c { let origin = PixelOrigin::Background(c);
0 => PixelOrigin::Empty, // Hack so that objects will be in front of it.
_ => PixelOrigin::Background,
};
let c = self.background_palette_cgb[bg_attribs.palette_number()].get_color(c); let c = self.background_palette_cgb[bg_attribs.palette_number()].get_color(c);
self.set_pixel(render_x, render_y, c, origin); self.set_pixel(render_x, render_y, c, origin);
/* /*
@ -903,10 +894,13 @@ impl Display {
let b2: bool = (tile_line_2 & 1 << (7 - tile_offset_x)) > 0; let b2: bool = (tile_line_2 & 1 << (7 - tile_offset_x)) > 0;
let c = (b2 as u8) * 2 + b1 as u8; let c = (b2 as u8) * 2 + b1 as u8;
/*
let origin = match c { let origin = match c {
0 => PixelOrigin::Empty, // Hack so that objects will be in front of it. 0 => PixelOrigin::Empty, // Hack so that objects will be in front of it.
_ => PixelOrigin::Window, _ => PixelOrigin::Window,
}; };
*/
let origin = PixelOrigin::Window;
let c = self.background_palette_cgb[bg_attribs.palette_number()] let c = self.background_palette_cgb[bg_attribs.palette_number()]
.get_color(c as usize); .get_color(c as usize);
self.set_pixel(render_x, render_y, c, origin); self.set_pixel(render_x, render_y, c, origin);