draw objects behind bg/window

This commit is contained in:
Alex Janka 2023-02-08 11:12:08 +11:00
parent 465b8454f6
commit 8ef947d54a

View file

@ -304,11 +304,11 @@ impl CPU {
} }
fn render_scanline(&mut self, scanline: u8, lcdc: &LCDC) { fn render_scanline(&mut self, scanline: u8, lcdc: &LCDC) {
let bg_palette = byte_to_palette(self.memory.get(0xFF47));
if lcdc.bg_window_enable { if lcdc.bg_window_enable {
let palette = byte_to_palette(self.memory.get(0xFF47)); self.render_scanline_bg(scanline, lcdc, bg_palette);
self.render_scanline_bg(scanline, lcdc, palette);
if lcdc.window_enable { if lcdc.window_enable {
self.render_scanline_window(scanline, lcdc, palette); self.render_scanline_window(scanline, lcdc, bg_palette);
} }
} else { } else {
for x in 0..WIDTH { for x in 0..WIDTH {
@ -317,7 +317,7 @@ impl CPU {
} }
} }
if lcdc.obj_enable { if lcdc.obj_enable {
self.render_scanline_obj(scanline, lcdc); self.render_scanline_obj(scanline, lcdc, bg_palette);
} }
} }
@ -350,10 +350,10 @@ impl CPU {
} }
} }
fn render_scanline_obj(&mut self, scanline: u8, lcdc: &LCDC) { fn render_scanline_obj(&mut self, scanline: u8, lcdc: &LCDC, bg_palette: Palette) {
let objs = self.parse_oam(scanline, &lcdc.obj_size); let objs = self.parse_oam(scanline, &lcdc.obj_size);
for object in objs { for object in objs {
self.render_object(scanline, object, &lcdc.obj_size); self.render_object(scanline, object, &lcdc.obj_size, bg_palette);
} }
} }
@ -386,7 +386,13 @@ impl CPU {
objs objs
} }
fn render_object(&mut self, scanline: u8, object: Object, obj_size: &ObjSize) { fn render_object(
&mut self,
scanline: u8,
object: Object,
obj_size: &ObjSize,
bg_palette: Palette,
) {
let mut object_row = scanline - object.y; let mut object_row = scanline - object.y;
if object.flags.y_flip { if object.flags.y_flip {
object_row = obj_size.get_height() - (object_row + 1); object_row = obj_size.get_height() - (object_row + 1);
@ -408,7 +414,12 @@ impl CPU {
} }
let x_coord = x_coord_uncorrected - 8; let x_coord = x_coord_uncorrected - 8;
if x_coord < WIDTH { if x_coord < WIDTH {
self.gpu.buffer[(scanline as usize * WIDTH) + x_coord] = colour.to_rgb(); let buffer_index = (scanline as usize * WIDTH) + x_coord;
if !object.flags.behind_bg_and_window
|| self.gpu.buffer[buffer_index] == bg_palette.zero.to_rgb()
{
self.gpu.buffer[buffer_index] = colour.to_rgb();
}
} }
} }
} }