fix error colour being dmg zero in cgb mode

This commit is contained in:
Alex Janka 2023-05-21 10:54:25 +10:00
parent c63cff149d
commit cd7a686e0d
3 changed files with 32 additions and 28 deletions

View file

@ -3,8 +3,8 @@ use self::{
cgb::CgbData, cgb::CgbData,
tile_window::TileWindow, tile_window::TileWindow,
types::{ types::{
BgAttributes, ColourInner, GpuInterrupts, Lcdc, Oam, ObjPalette, ObjSize, Object, rgb_from_bytes, BgAttributes, ColourInner, GpuInterrupts, Lcdc, Oam, ObjPalette, ObjSize,
ObjectFlags, Palette, Stat, TiledataArea, TilemapArea, Vram, VramBank, Object, ObjectFlags, Palette, Stat, TiledataArea, TilemapArea, Vram, VramBank,
}, },
}; };
use crate::{ use crate::{
@ -61,7 +61,14 @@ where
pub fn new(cgb: bool, window: R, tile_window_renderer: Option<R>) -> Self { pub fn new(cgb: bool, window: R, tile_window_renderer: Option<R>) -> Self {
let tile_window = tile_window_renderer let tile_window = tile_window_renderer
.map(|tile_window_renderer| TileWindow::new(tile_window_renderer, cgb)); .map(|tile_window_renderer| TileWindow::new(tile_window_renderer, cgb));
let buffer = vec![ColourInner::Error.rgb_bytes(None, false).into(); WIDTH * HEIGHT]; let buffer = vec![
if cgb {
rgb_from_bytes(0xFFFF).into()
} else {
ColourInner::Error.rgb_bytes(None).into()
};
WIDTH * HEIGHT
];
Self { Self {
buffer, buffer,
@ -217,7 +224,7 @@ where
} else { } else {
for x in 0..WIDTH { for x in 0..WIDTH {
self.buffer[(scanline as usize * WIDTH) + x] = ColourInner::Error self.buffer[(scanline as usize * WIDTH) + x] = ColourInner::Error
.rgb_bytes(None, self.cgb_data.is_some()) .rgb_bytes(self.cgb_data.as_ref().map(|d| (&d.palettes.bg, 0_u8)))
.into(); .into();
} }
} }
@ -362,7 +369,7 @@ where
{ {
let cgb_data = self.cgb_data.as_ref().map(|v| { let cgb_data = self.cgb_data.as_ref().map(|v| {
( (
v.palettes.obj, &v.palettes.obj,
if self.is_cgb_mode() { if self.is_cgb_mode() {
object.flags.cgb_palette object.flags.cgb_palette
} else { } else {
@ -371,8 +378,7 @@ where
) )
}); });
self.buffer[buffer_index] = self.buffer[buffer_index] = colour.rgb_bytes(cgb_data).into();
colour.rgb_bytes(cgb_data, self.cgb_data.is_some()).into();
} }
} }
} }
@ -451,10 +457,9 @@ where
let cgb_data = self let cgb_data = self
.cgb_data .cgb_data
.as_ref() .as_ref()
.map(|v| (v.palettes.bg, attributes.palette)); .map(|v| (&v.palettes.bg, attributes.palette));
self.buffer[(scanline as usize * WIDTH) + x] = self.buffer[(scanline as usize * WIDTH) + x] = colour.rgb_bytes(cgb_data).into();
colour.rgb_bytes(cgb_data, self.cgb_data.is_some()).into();
} }
} }

View file

@ -33,7 +33,7 @@ where
window.prepare(current_width, TILE_WINDOW_HEIGHT); window.prepare(current_width, TILE_WINDOW_HEIGHT);
Self { Self {
sprite_buffer: vec![ sprite_buffer: vec![
ColourInner::Error.rgb_bytes(None, false).into(); ColourInner::Error.rgb_bytes(None).into();
current_width * TILE_WINDOW_HEIGHT current_width * TILE_WINDOW_HEIGHT
], ],
sprite_renderer: window, sprite_renderer: window,
@ -56,10 +56,8 @@ where
}; };
self.sprite_renderer self.sprite_renderer
.resize(current_width, TILE_WINDOW_HEIGHT); .resize(current_width, TILE_WINDOW_HEIGHT);
self.sprite_buffer = vec![ self.sprite_buffer =
ColourInner::Error.rgb_bytes(None, false).into(); vec![ColourInner::Error.rgb_bytes(None).into(); current_width * TILE_WINDOW_HEIGHT];
current_width * TILE_WINDOW_HEIGHT
];
} }
for (tile_y, data_begin) in (0x8000..0xA000).step_by(0x10 * 16).enumerate() { for (tile_y, data_begin) in (0x8000..0xA000).step_by(0x10 * 16).enumerate() {
self.draw_row(tile_y, data_begin, palette, memory, is_cgb_mode); self.draw_row(tile_y, data_begin, palette, memory, is_cgb_mode);
@ -150,7 +148,7 @@ where
let addr = offset + real_px_x + (real_px_y * line_width); let addr = offset + real_px_x + (real_px_y * line_width);
if addr < self.sprite_buffer.len() { if addr < self.sprite_buffer.len() {
self.sprite_buffer[addr] = colour.rgb_bytes(None, is_cgb_mode).into(); self.sprite_buffer[addr] = colour.rgb_bytes(None).into();
} }
} }
} }

View file

@ -117,7 +117,7 @@ impl From<Colour> for [u8; 4] {
} }
} }
fn rgb_from_bytes(bytes: u16) -> Colour { pub(super) fn rgb_from_bytes(bytes: u16) -> Colour {
let b = (bytes & (0b11111 << 10)) >> 10; let b = (bytes & (0b11111 << 10)) >> 10;
let g = (bytes & (0b11111 << 5)) >> 5; let g = (bytes & (0b11111 << 5)) >> 5;
let r = bytes & 0b11111; let r = bytes & 0b11111;
@ -139,16 +139,19 @@ fn rgb_from_bytes(bytes: u16) -> Colour {
} }
impl ColourInner { impl ColourInner {
pub(super) fn rgb_bytes( pub(super) fn rgb_bytes(&self, cgb_data: Option<(&CgbPalette, u8)>) -> Colour {
&self,
cgb_data: Option<(CgbPalette, u8)>,
is_cgb_mode: bool,
) -> Colour {
if let Some((cgb_palette, pallete_num)) = cgb_data { if let Some((cgb_palette, pallete_num)) = cgb_data {
if *self == ColourInner::Error { let offset: usize = (pallete_num as usize * 2 * 4)
+ (if *self == ColourInner::Error {
if cfg!(debug_assertions) {
return ERROR_COLOUR; return ERROR_COLOUR;
} else {
ColourInner::Zero
} }
let offset: usize = (pallete_num as usize * 2 * 4) + (*self as usize * 2); } else {
*self
} as usize
* 2);
rgb_from_bytes(*from_bytes(&cgb_palette.data[offset..=offset + 1])) rgb_from_bytes(*from_bytes(&cgb_palette.data[offset..=offset + 1]))
} else { } else {
@ -160,8 +163,6 @@ impl ColourInner {
ColourInner::Error => { ColourInner::Error => {
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
ERROR_COLOUR ERROR_COLOUR
} else if is_cgb_mode {
rgb_from_bytes(0xFFFF)
} else { } else {
dmg_colours::ZERO dmg_colours::ZERO
} }