Skip to content

Commit

Permalink
Implemented window internal line counter
Browse files Browse the repository at this point in the history
  • Loading branch information
velllu committed Apr 8, 2024
1 parent f7b5476 commit ab07f0e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/gpu/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub struct Gpu {

/// A dot is 1/4 of a CPU cycle
pub dots: u16,

/// This is similar to LY, except it only gets incremented when the window is visible
/// and it's needed for window rendering
window_ly: u8,
}

impl Gpu {
Expand All @@ -54,6 +58,7 @@ impl Gpu {
x: 0,
y: 0,
dots: 0,
window_ly: 0,
}
}
}
Expand All @@ -76,6 +81,7 @@ impl GameBoy {

if self.gpu.dots == 0 {
self.bus[LY] = 0;
self.gpu.window_ly = 0;
}

if self.gpu.dots == 80 {
Expand Down Expand Up @@ -130,7 +136,7 @@ impl GameBoy {
// 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.y as i32 - self.bus[WY] as i32;
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() {
Expand Down Expand Up @@ -160,6 +166,10 @@ impl GameBoy {
/// line we need to render
fn hblank(&mut self) {
if self.gpu.dots == 0 {
if self.is_window_visible() {
self.gpu.window_ly = self.gpu.window_ly.wrapping_add(1);
}

self.gpu.x = 0;
self.gpu.rendered_sprites_on_line = 0;
self.bus[LY] = self.bus[LY].wrapping_add(1);
Expand Down Expand Up @@ -216,6 +226,7 @@ impl GameBoy {
// be stuck at hblank, making the program crash because of addition overflow.
if self.gpu.hybernated {
self.bus[LY] = 0;
self.gpu.window_ly = 0;
self.gpu.state = GPUState::OAMSearch;
}

Expand Down
22 changes: 21 additions & 1 deletion src/gpu/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use crate::{common::Bit, consts::gpu::LCDC, GameBoy};
use crate::{
common::Bit,
consts::{
display::{DISPLAY_SIZE_X, DISPLAY_SIZE_Y},
gpu::{LCDC, WX, WY},
},
GameBoy,
};

impl GameBoy {
pub(crate) fn can_render_window(&self) -> bool {
Expand All @@ -8,4 +15,17 @@ impl GameBoy {

true
}

/// Returns true if the window's WX and WY are inside the screen bounds
pub(crate) fn is_window_visible(&self) -> bool {
if !(0..(DISPLAY_SIZE_X + 7)).contains(&(self.bus[WX] as usize)) {
return false;
}

if !(0..(DISPLAY_SIZE_Y)).contains(&(self.bus[WY] as usize)) {
return false;
}

true
}
}

0 comments on commit ab07f0e

Please sign in to comment.