Skip to content

Commit

Permalink
Fixed wrong sprite placement on y axis
Browse files Browse the repository at this point in the history
  • Loading branch information
velllu committed Dec 8, 2023
1 parent 113a33c commit 4486f00
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/gpu/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ impl GameBoy {
/// The OAM Search phase is where we search for the visible sprites in the current
/// scanline in the OAM region of the RAM
fn oam_search(&mut self) {
// TODO: The timing on this is still to implement, this just checks *all* the
// sprites all at once every OAM Search tick, which is both grossly inaccurate
// and inefficient
self.gpu.sprites.clear();
for i in (0xFE00..0xFE9C).step_by(4) {
self.gpu.sprites.push(self.get_sprite_data(i));
Expand Down Expand Up @@ -92,14 +95,26 @@ impl GameBoy {
// Now that we have the background line to render, we have to get the sprite one
let mut sprite_fifo: Option<Line> = None;
for sprite in &self.gpu.sprites {
if sprite.x == self.gpu.x {
let sprite_y = sprite.y - 16;
let sprite_x = sprite.x - 8;

// We check if there is any sprite that is on the same x axis as our "cursor"
let x_condition = sprite_x == self.gpu.x;

// and we check if we also are on the same y axis, however, a sprite is 8
// pixel long, so we check if we are anywhere between row 0 to 7
let y_condition = ((sprite_y)..(sprite_y + 7)).contains(&self.gpu.y);

if x_condition && y_condition {
sprite_fifo = Some(self.get_line(sprite.tile_number, self.gpu.y as u16 % 8));
}
}

self.draw_line(&background_fifo, self.gpu.x as usize, self.gpu.y as usize);
// TODO: Implement fifo mixing
if let Some(sprite_fifo) = sprite_fifo {
self.draw_line(&sprite_fifo, self.gpu.x as usize, self.gpu.y as usize);
} else {
self.draw_line(&background_fifo, self.gpu.x as usize, self.gpu.y as usize);
}

self.gpu.i += 1;
Expand Down

0 comments on commit 4486f00

Please sign in to comment.