DMG: Create some abstraction

This commit is contained in:
Kevin Hamacher 2020-02-21 09:55:28 +01:00
parent b7c1d9b8a0
commit 30efdc1108

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 {
@ -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,