diff --git a/src/gpu/states.rs b/src/gpu/states.rs index 9ff2764..cce4593 100644 --- a/src/gpu/states.rs +++ b/src/gpu/states.rs @@ -108,7 +108,7 @@ impl GameBoy { // "https://gbdev.io/pandocs/Scrolling.html#mid-frame-behavior" // And we get the background fifo and the sprite fifo - let background_fifo = match self.bus[LCDC].get_bit(0) { + let mut background_fifo = match self.bus[LCDC].get_bit(0) { false => Line::new_blank(), // When LCDC.0 is 0, the background tile is white true => self.get_line_from_coordinates( self.gpu.x.wrapping_add(self.bus[SCX]), @@ -119,6 +119,16 @@ impl GameBoy { let mut sprite = self.get_sprite_fifo(self.gpu.x, self.gpu.y, &sprite_height); + // Window rendering + // `- 7` because WX is always 7 more then the actual value + let window_x = self.gpu.x as i32 - (self.bus[WX] as i32 - 7); + let window_y = self.gpu.window_ly as i32 - self.bus[WY] as i32; + + // Window doesn't scroll so we don't wrap around like with the background + if window_x >= 0 && window_y >= 0 && self.can_render_window() { + background_fifo = self.get_line_from_coordinates(window_x as u8, window_y as u8, true); + } + if let Some((sprite_fifo, sprite_data)) = &mut sprite { if self.gpu.rendered_sprites_on_line < 10 { self.apply_palette_to_sprite(sprite_fifo, &sprite_data.palette); @@ -133,17 +143,6 @@ impl GameBoy { self.draw_line(&background_fifo, self.gpu.x as usize, self.gpu.y as usize); } - // Window rendering - // `- 7` because WX is always 7 more then the actual value - let window_x = self.gpu.x as i32 - (self.bus[WX] as i32 - 7); - let window_y = self.gpu.window_ly as i32 - self.bus[WY] as i32; - - // Window doesn't scroll so we don't wrap around like with the background - if window_x >= 0 && window_y >= 0 && self.can_render_window() { - let window_fifo = self.get_line_from_coordinates(window_x as u8, window_y as u8, true); - self.draw_line(&window_fifo, self.gpu.x as usize, self.gpu.y as usize); - } - self.gpu.x += 8; if self.gpu.x == (DISPLAY_SIZE_X as u8) {