Skip to content

Commit

Permalink
Implemented basic window rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
velllu committed Apr 7, 2024
1 parent c671d68 commit f7b5476
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub mod gpu {
pub const LYC: u16 = 0xFF45;
pub const OBP0: u16 = 0xFF48;
pub const OBP1: u16 = 0xFF49;
pub const WY: u16 = 0xFF4A;
pub const WX: u16 = 0xFF4B;
pub const IE: u16 = 0xFFFF;
}

Expand Down
1 change: 1 addition & 0 deletions src/gpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub(crate) mod palette;
pub(crate) mod sprite_parser;
pub mod states;
pub mod tile_parser;
pub(crate) mod window;

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Color {
Expand Down
21 changes: 18 additions & 3 deletions src/gpu/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
common::Bit,
consts::{
display::{DISPLAY_SIZE_X, DISPLAY_SIZE_Y},
gpu::{LCDC, LY, SCX, SCY},
gpu::{LCDC, LY, SCX, SCY, WX, WY},
},
GameBoy,
};
Expand Down Expand Up @@ -36,8 +36,8 @@ pub struct Gpu {
rendered_sprites_on_line: u8,

// These represent the current position of the "cursor"
x: u8,
y: u8,
pub(crate) x: u8,
pub(crate) y: u8,

/// A dot is 1/4 of a CPU cycle
pub dots: u16,
Expand Down Expand Up @@ -98,12 +98,16 @@ impl GameBoy {
self.gpu.dots += 12;
}

// TODO: Scrolling is actually handled a bit differently, read this
// "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) {
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]),
self.gpu.y.wrapping_add(self.bus[SCY]),
false,
),
};

Expand All @@ -123,6 +127,17 @@ 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.y 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
9 changes: 7 additions & 2 deletions src/gpu/tile_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ impl GameBoy {

/// Returns the line that should be at the given coordinates of the screen (excluding
/// horizontal and vertical scroll)
pub(crate) fn get_line_from_coordinates(&self, x: u8, y: u8) -> Line {
let tile_map_address: u16 = match self.bus[0xFF40].get_bit(3) {
pub(crate) fn get_line_from_coordinates(&self, x: u8, y: u8, window: bool) -> Line {
let lcdc_bit = match window {
false => 3,
true => 6,
};

let tile_map_address: u16 = match self.bus[LCDC].get_bit(lcdc_bit) {
false => 0x9800,
true => 0x9C00,
};
Expand Down
11 changes: 11 additions & 0 deletions src/gpu/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::{common::Bit, consts::gpu::LCDC, GameBoy};

impl GameBoy {
pub(crate) fn can_render_window(&self) -> bool {
if !self.bus[LCDC].get_bit(0) || !self.bus[LCDC].get_bit(5) {
return false;
}

true
}
}

0 comments on commit f7b5476

Please sign in to comment.