window renders + it all works kind of almost ish

This commit is contained in:
Alex Janka 2023-02-06 12:14:02 +11:00
parent 1d041e2d57
commit bec5a0c3d6

View file

@ -225,9 +225,10 @@ impl CPU {
fn render_scanline(&mut self, scanline: u8, lcdc: &LCDC) { fn render_scanline(&mut self, scanline: u8, lcdc: &LCDC) {
if lcdc.bg_window_enable { if lcdc.bg_window_enable {
self.render_scanline_bg(scanline, lcdc); let palette = byte_to_palette(self.memory.get(0xFF47));
self.render_scanline_bg(scanline, lcdc, palette);
if lcdc.window_enable { if lcdc.window_enable {
self.render_scanline_window(scanline, lcdc); self.render_scanline_window(scanline, lcdc, palette);
} }
} }
if lcdc.obj_enable { if lcdc.obj_enable {
@ -235,10 +236,9 @@ impl CPU {
} }
} }
fn render_scanline_bg(&mut self, scanline: u8, lcdc: &LCDC) { fn render_scanline_bg(&mut self, scanline: u8, lcdc: &LCDC, palette: Palette) {
let scroll_y = self.memory.get(0xFF42); let scroll_y = self.memory.get(0xFF42);
let scroll_x = self.memory.get(0xFF43); let scroll_x = self.memory.get(0xFF43);
let palette = byte_to_palette(self.memory.get(0xFF47));
self.render_tiles( self.render_tiles(
scanline, scanline,
&lcdc.bg_tilemap, &lcdc.bg_tilemap,
@ -249,12 +249,19 @@ impl CPU {
); );
} }
fn render_scanline_window(&mut self, _scanline: u8, _lcdc: &LCDC) { fn render_scanline_window(&mut self, scanline: u8, lcdc: &LCDC, palette: Palette) {
let pos_y = self.memory.get(0xFF4A); let pos_y = self.memory.get(0xFF4A);
// subtracting 7 to get the Real Number... // subtracting 7 to get the Real Number...
let pos_x = self.memory.get(0xFF4B).wrapping_sub(7); let pos_x = self.memory.get(0xFF4B).wrapping_sub(7);
if pos_y < 143 && pos_x < 166 { if pos_y < 143 && pos_x < 166 {
// within range!! render here self.render_tiles(
scanline,
&lcdc.window_tilemap,
&lcdc.tile_area,
palette,
pos_x,
pos_y,
)
} }
} }
@ -266,7 +273,7 @@ impl CPU {
tilemap: &TilemapArea, tilemap: &TilemapArea,
tiledata: &TiledataArea, tiledata: &TiledataArea,
palette: Palette, palette: Palette,
offset_x: u8, _offset_x: u8,
offset_y: u8, offset_y: u8,
) { ) {
let tile_line = (scanline as usize) + (offset_y as usize); let tile_line = (scanline as usize) + (offset_y as usize);
@ -280,8 +287,8 @@ impl CPU {
let lsbs = self.memory.get(tile_addr); let lsbs = self.memory.get(tile_addr);
let msbs = self.memory.get(tile_addr + 1); let msbs = self.memory.get(tile_addr + 1);
for px_x in 0..8 { for px_x in 0..8 {
let lsb = get_bit(lsbs, px_x); let lsb = get_bit(lsbs, 7 - px_x);
let msb = get_bit(msbs, px_x); let msb = get_bit(msbs, 7 - px_x);
let colour = bits_to_mapped_colour(lsb, msb, palette); let colour = bits_to_mapped_colour(lsb, msb, palette);
let x_coord = ((x * 8) + (px_x as u16)) as usize; let x_coord = ((x * 8) + (px_x as u16)) as usize;
if x_coord < WIDTH { if x_coord < WIDTH {