Move Sprite/BgAttribs to separate file

This commit is contained in:
Kevin Hamacher 2020-02-21 18:24:29 +01:00
parent fbfe751d75
commit 1cc839c911
3 changed files with 92 additions and 100 deletions

View File

@ -2,7 +2,9 @@ extern crate libc;
extern crate sdl2;
mod palette;
mod structs;
use palette::{CgbPalette, DmgPalette};
use structs::*;
use std::io::Write;
@ -49,49 +51,6 @@ const SPRITE_X_FLIP: u8 = 1 << 5;
//const SPRITE_PALETTE_NO: u8 = 1 << 4; // NonCGB only
const SPRITE_TILE_VRAM_BANK: u8 = 1 << 3; // CGB only
// Display color
/*
const MONOCHROME_PALETTE: &'static [[f64; 3]; 4] = &[
[0.605, 0.734, 0.059],
[0.543, 0.672, 0.059],
[0.188, 0.383, 0.188],
[0.059, 0.219, 0.059],
];
const MONOCHROME_PALETTE: &'static [[u8; 3]; 4] = &[
[255, 255, 255],
[200, 200, 200],
[125, 125, 12],
[50, 50, 50],
];
*/
struct BgMapAttributes(u8);
impl BgMapAttributes {
fn palette_number(&self) -> usize {
(self.0 & 0b111) as usize
}
fn vram_bank_number(&self) -> usize {
((self.0 >> 3) & 1) as usize
}
fn horizontal_flip(&self) -> bool {
(self.0 >> 5) != 0
}
fn vertical_flip(&self) -> bool {
(self.0 >> 6) != 0
}
}
#[derive(Debug, Copy, Clone)]
enum PixelOrigin {
Empty,
Background(usize),
Window,
Sprite,
}
#[derive(Copy, Clone)]
struct Pixel {
origin: PixelOrigin,
@ -121,53 +80,6 @@ impl Default for DisplayMode {
}
}
struct Sprite {
x: u8,
y: u8,
tile: u8,
flags: u8,
}
impl Sprite {
fn is_hidden(&self) -> bool {
self.x == 0 || self.y == 0
}
fn is_foreground(&self) -> bool {
(self.flags & SPRITE_OBJ_BG_PRIORITY) == 0
}
fn is_x_flipped(&self) -> bool {
(self.flags & SPRITE_X_FLIP) == SPRITE_X_FLIP
}
fn is_y_flipped(&self) -> bool {
(self.flags & SPRITE_Y_FLIP) == SPRITE_Y_FLIP
}
fn palette(&self) -> u8 {
// GB
/*
if (self.flags & SPRITE_PALETTE_NO) == 0 {
0
} else {
1
}
*/
// GBC
self.flags & 0b111
}
// GBC only
fn vram_bank(&self) -> u8 {
if (self.flags & SPRITE_TILE_VRAM_BANK) == 0 {
0
} else {
1
}
}
}
pub struct Display {
control: u8,
status: u8,
@ -680,12 +592,7 @@ impl Display {
// Order sprites by priority
let mut queue: Vec<Sprite> = Vec::new();
for i in 0..39 {
queue.push(Sprite {
y: self.oam[i * 4 + 0],
x: self.oam[i * 4 + 1],
tile: self.oam[i * 4 + 2],
flags: self.oam[i * 4 + 3],
});
queue.push(Sprite::load(&self.oam[i * 4..(i + 1) * 4]));
}
// This is the non-CGB priority.
@ -747,9 +654,9 @@ impl Display {
// Lookup the color
let c = ((b2 as u8) * 2 + b1 as u8) as usize;
// let c = self.background_palette.get_color(c);
let c = self.background_palette_cgb[bg_attribs.palette_number()].get_color(c);
self.set_pixel(render_x, render_y, c, PixelOrigin::Background(c));
// let color = self.background_palette.get_color(c);
let color = self.background_palette_cgb[bg_attribs.palette_number()].get_color(c);
self.set_pixel(render_x, render_y, color, PixelOrigin::Background(c));
}
}

View File

@ -4,7 +4,7 @@ use super::sdl2;
pub struct DmgPalette(pub u8);
impl DmgPalette {
pub fn get_color(self, n: usize) -> sdl2::pixels::Color {
const MONOCHROME_PALETTE: &'static [[u8; 3]; 4] = &[
const MONOCHROME_PALETTE: &[[u8; 3]; 4] = &[
[255, 255, 255],
[200, 200, 200],
[125, 125, 12],

85
src/display/structs.rs Normal file
View File

@ -0,0 +1,85 @@
use super::*;
// Display color
pub struct BgMapAttributes(pub u8);
impl BgMapAttributes {
pub fn palette_number(&self) -> usize {
(self.0 & 0b111) as usize
}
pub fn vram_bank_number(&self) -> usize {
((self.0 >> 3) & 1) as usize
}
pub fn horizontal_flip(&self) -> bool {
(self.0 >> 5) != 0
}
pub fn vertical_flip(&self) -> bool {
(self.0 >> 6) != 0
}
}
#[derive(Debug, Copy, Clone)]
pub enum PixelOrigin {
Empty,
Background(usize),
Window,
Sprite,
}
pub struct Sprite {
pub x: u8,
pub y: u8,
pub tile: u8,
flags: u8,
}
impl Sprite {
pub fn load(buf: &[u8]) -> Self {
assert!(buf.len() > 4);
Self {
x: buf[0],
y: buf[1],
tile: buf[2],
flags: buf[3],
}
}
pub fn is_hidden(&self) -> bool {
self.x == 0 || self.y == 0
}
pub fn is_foreground(&self) -> bool {
(self.flags & SPRITE_OBJ_BG_PRIORITY) == 0
}
pub fn is_x_flipped(&self) -> bool {
(self.flags & SPRITE_X_FLIP) == SPRITE_X_FLIP
}
pub fn is_y_flipped(&self) -> bool {
(self.flags & SPRITE_Y_FLIP) == SPRITE_Y_FLIP
}
pub fn palette(&self) -> u8 {
// GB
/*
if (self.flags & SPRITE_PALETTE_NO) == 0 {
0
} else {
1
}
*/
// GBC
self.flags & 0b111
}
// GBC only
pub fn vram_bank(&self) -> u8 {
if (self.flags & SPRITE_TILE_VRAM_BANK) == 0 {
0
} else {
1
}
}
}