Skip to content

Commit

Permalink
Made window render below sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
velllu committed Apr 18, 2024
1 parent ab07f0e commit 654fca6
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/gpu/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand All @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 654fca6

Please sign in to comment.