Skip to content

Commit

Permalink
Implemented sprite & background mixing
Browse files Browse the repository at this point in the history
  • Loading branch information
velllu committed Dec 13, 2023
1 parent 2383e92 commit 24aaf1c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
21 changes: 18 additions & 3 deletions src/gpu/sprite_parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{common::Bit, GameBoy};

use super::tile_parser::Line;
use super::{tile_parser::Line, Color};

#[derive(PartialEq, Eq)]
pub(crate) enum Priority {
AlwaysAbove,
AboveLightColor,
Expand Down Expand Up @@ -36,8 +37,8 @@ impl GameBoy {
x,
tile_number,
priority: match flags.get_bit(7) {
false => Priority::AlwaysAbove,
true => Priority::AboveLightColor,
false => Priority::AboveLightColor,
true => Priority::AlwaysAbove,
},
palette: match flags.get_bit(4) {
false => Palette::OBP0,
Expand Down Expand Up @@ -82,3 +83,17 @@ impl GameBoy {
sprite_fifo
}
}

impl Line {
pub(crate) fn mix_with_background_tile(&mut self, background_tile: &Line, priority: &Priority) {
if *priority == Priority::AlwaysAbove {
return;
}

for (sprite_pixel, bg_pixel) in self.colors.iter_mut().zip(background_tile.colors) {
if *sprite_pixel == Color::Light {
*sprite_pixel = bg_pixel;
}
}
}
}
8 changes: 5 additions & 3 deletions src/gpu/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ impl GameBoy {
let background_fifo = self.get_line(self.bus[tile_map_address], self.gpu.y as u16 % 8);
let mut sprite = self.get_sprite_fifo(self.gpu.x, self.gpu.y);

// TODO: Implement fifo mixing
if let Some((sprite_fifo, sprite_data)) = &mut sprite {
self.apply_palette_to_sprite(sprite_fifo, &sprite_data.palette);

if self.gpu.rendered_sprites_on_line < 10 {
self.apply_palette_to_sprite(sprite_fifo, &sprite_data.palette);
sprite_fifo.mix_with_background_tile(&background_fifo, &sprite_data.priority);

self.draw_line(&sprite_fifo, self.gpu.x as usize, self.gpu.y as usize);
self.gpu.rendered_sprites_on_line += 1;
} else {
self.draw_line(&background_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);
Expand Down

0 comments on commit 24aaf1c

Please sign in to comment.