Skip to content

Commit

Permalink
Fix flipped sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
joajfreitas committed Jul 28, 2024
1 parent 1adfc17 commit afbe94a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion fpt/src/bw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn test_bit16<const INDEX: u8>(word: u16) -> bool {
word & mask == mask
}

#[allow(unused)]
//#[allow(unused)]
pub fn get_bit8<const INDEX: u8>(word: u8) -> u8 {
(word >> INDEX) & 0x1
}
Expand Down
2 changes: 1 addition & 1 deletion fpt/src/memory/mbc_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::mbc_none::NoMbcCartridge;

pub fn create_mbc(cartridge_data: &[u8]) -> Option<Box<RefCell<dyn Cartridge>>> {
// https://gbdev.io/pandocs/The_Cartridge_Header.html#0147--cartridge-type
match dbg!(get_cartridge_type(cartridge_data)) {
match get_cartridge_type(cartridge_data) {
0x00 => Some(Box::new(RefCell::new(NoMbcCartridge::new(cartridge_data)))), // rom only
0x0F..=0x13 => Some(Box::new(RefCell::new(Mbc3Cartridge::new(cartridge_data)))),
_ => None,
Expand Down
4 changes: 1 addition & 3 deletions fpt/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,9 @@ impl Bus {
|| map::WRAM.contains(&address)
|| map::NOT_USABLE2.contains(&address)
|| address == map::IE
|| map::OAM.contains(&address)
{
self.memory_mut().mem[address as Address] = value;
} else if map::OAM.contains(&address) {
println!("{}:{}", address, value);
self.memory_mut().mem[address as Address] = value;
} else if map::NOT_USABLE1.contains(&address) {
self.memory_mut().mem[address - 0x2000 as Address] = value;
} else {
Expand Down
17 changes: 9 additions & 8 deletions fpt/src/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ impl Ppu {
let mut pixel = tile.get_pixel(yy % 8, xx % 8);

for sprite in self.sprites.iter() {
if (x as u8 > (sprite.x - 8) && (x as u8) < sprite.x)
&& (y as u8 > sprite.y - 8 && (y as u8) < sprite.y)
let sprite_x: i32 = sprite.x as i32 - 8;
let sprite_y: i32 = sprite.y as i32 - 16;
if (x as i32 >= (sprite_x) && (x as i32) < sprite_x + 8)
&& (y as i32 >= sprite_y && (y as i32) < sprite_y + 8)
{
println!("y flip: {}", sprite.flags.y_flip);
println!("x flip: {}", sprite.flags.x_flip);
let tile = self.tilemap.get_tile(sprite.tile_index as usize, true);
let tile_x: usize = dbg!(sprite.x as usize - x);
let tile_y: usize = dbg!(sprite.y as usize - y);
let bit_index = tile_y * 8 + tile_x;
pixel = dbg!((tile.bytes[2 * tile_y] >> tile_x) & 1);
//pixel = tile.bytes[(dbg!(8*(dbg!(sprite.y) as i16 - dbg!(y) as i16) + dbg!(sprite.x) as i16 - dbg!(x) as i16)) as usize] ;
println!("found sprite");
let tile_x = (x as i32 - sprite_x) as usize;
let tile_y = (y as i32 - sprite_y) as usize;
pixel = tile.get_pixel(tile_y, tile_x);
}
}
self.frame[WIDTH * y + x] = pixel;
Expand Down
28 changes: 14 additions & 14 deletions fpt/src/ppu/sprite.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use crate::bw::test_bit8;

#[derive(Debug)]
struct Flags {
priority: bool,
y_flip: bool,
x_flip: bool,
dmg_palette: bool,
bank: bool,
cgb_palette: bool,
pub struct Flags {
//pub priority: bool,
pub y_flip: bool,
pub x_flip: bool,
//pub dmg_palette: u8,
//pub bank: bool,
//pub cgb_palette: u8,
}

impl Flags {
pub fn from(memory: u8) -> Flags {
Flags {
priority: test_bit8::<0>(memory),
x_flip: test_bit8::<1>(memory),
y_flip: test_bit8::<2>(memory),
dmg_palette: test_bit8::<3>(memory),
bank: test_bit8::<4>(memory),
cgb_palette: test_bit8::<5>(memory),
//priority: test_bit8::<7>(memory),
x_flip: test_bit8::<6>(memory),
y_flip: test_bit8::<5>(memory),
//dmg_palette: (memory >> 4) & 0b11,
//bank: test_bit8::<3>(memory),
//cgb_palette: memory & 0b111,
}
}
}
Expand All @@ -28,7 +28,7 @@ pub struct Sprite {
pub y: u8,
pub x: u8,
pub tile_index: u8,
flags: Flags,
pub flags: Flags,
}

impl Sprite {
Expand Down

0 comments on commit afbe94a

Please sign in to comment.