Display: Splitting file up, staring with palette

This commit is contained in:
Kevin Hamacher 2020-02-21 18:15:47 +01:00
parent 8dd14c7719
commit c880766ae4
2 changed files with 66 additions and 60 deletions

View File

@ -1,6 +1,9 @@
extern crate libc;
extern crate sdl2;
mod palette;
use palette::{DmgPalette, CgbPalette};
use std::io::Write;
// Internal ram size
@ -62,66 +65,6 @@ 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)]
struct CgbPalette([u8; 8]);
impl CgbPalette {
fn get_color(self, n: usize) -> sdl2::pixels::Color {
if n == 0 {
return sdl2::pixels::Color::RGB(255, 255, 255);
}
let v = ((self.0[2 * n + 1] as u16) << 8) | (self.0[2 * n] as u16);
let r = (v & 0b1_1111) as u8;
let g = ((v >> 5) & 0b1_1111) as u8;
let b = ((v >> 10) & 0b1_1111) as u8;
if false {
sdl2::pixels::Color::RGB(r << 3, g << 3, b << 3)
} else {
// According to some code:
// Real colors:
let r = r as u16;
let g = g as u16;
let b = b as u16;
let mapped_r = ((r * 13 + g * 2 + b) >> 1) as u8;
let mapped_g = ((g * 3 + b) << 1) as u8;
let mapped_b = ((r * 3 + g * 2 + b * 11) >> 1) as u8;
sdl2::pixels::Color::RGB(mapped_r, mapped_g, mapped_b)
}
}
}
impl std::fmt::Display for CgbPalette {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Palette: ")?;
for n in 0..4 {
let v = ((self.0[2 * n + 1] as u16) << 8) | (self.0[2 * n] as u16);
let r = (v & 0b1_1111) as u8;
let g = ((v >> 5) & 0b1_1111) as u8;
let b = ((v >> 10) & 0b1_1111) as u8;
write!(f, "{:02X}{:02X}{:02X} ", r, g, b)?;
}
write!(f, "")
}
}
struct BgMapAttributes(u8);
impl BgMapAttributes {
fn palette_number(&self) -> usize {

63
src/display/palette.rs Normal file
View File

@ -0,0 +1,63 @@
use super::sdl2;
#[derive(Copy, Clone, Default)]
pub struct DmgPalette(pub u8);
impl DmgPalette {
pub 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)]
pub struct CgbPalette(pub [u8; 8]);
impl CgbPalette {
pub fn get_color(self, n: usize) -> sdl2::pixels::Color {
if n == 0 {
return sdl2::pixels::Color::RGB(255, 255, 255);
}
let v = ((self.0[2 * n + 1] as u16) << 8) | (self.0[2 * n] as u16);
let r = (v & 0b1_1111) as u8;
let g = ((v >> 5) & 0b1_1111) as u8;
let b = ((v >> 10) & 0b1_1111) as u8;
if false {
sdl2::pixels::Color::RGB(r << 3, g << 3, b << 3)
} else {
// According to some code:
// Real colors:
let r = r as u16;
let g = g as u16;
let b = b as u16;
let mapped_r = ((r * 13 + g * 2 + b) >> 1) as u8;
let mapped_g = ((g * 3 + b) << 1) as u8;
let mapped_b = ((r * 3 + g * 2 + b * 11) >> 1) as u8;
sdl2::pixels::Color::RGB(mapped_r, mapped_g, mapped_b)
}
}
}
impl std::fmt::Display for CgbPalette {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Palette: ")?;
for n in 0..4 {
let v = ((self.0[2 * n + 1] as u16) << 8) | (self.0[2 * n] as u16);
let r = (v & 0b1_1111) as u8;
let g = ((v >> 5) & 0b1_1111) as u8;
let b = ((v >> 10) & 0b1_1111) as u8;
write!(f, "{:02X}{:02X}{:02X} ", r, g, b)?;
}
write!(f, "")
}
}