diff --git a/src/ppu.rs b/src/ppu.rs index e066341135c4655618816f21a6c5ba89e754bdc5..eb08d2d13cb1ab1fca65c81bb495f93f1fcc197a 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -99,9 +99,13 @@ impl PaletteInfo { /// Represents a tile within the Game Boy context, /// should contain the pixel buffer of the tile. +/// The tiles are always 8x8 pixels in size. #[cfg_attr(feature = "wasm", wasm_bindgen)] #[derive(Clone, Copy, PartialEq, Eq)] pub struct Tile { + /// The buffer for the tile, should contain a byte + /// per each pixel of the tile with values ranging + /// from 0 to 3 (4 colors). buffer: [u8; 64], } @@ -111,6 +115,12 @@ impl Tile { self.buffer[y * TILE_WIDTH + x] } + pub fn get_flipped(&self, x: usize, y: usize, xflip: bool, yflip: bool) -> u8 { + let x: usize = if xflip { 7 - x } else { x }; + let y = if yflip { 7 - y } else { y }; + self.buffer[y * TILE_WIDTH + x] + } + pub fn set(&mut self, x: usize, y: usize, value: u8) { self.buffer[y * TILE_WIDTH + x] = value; } @@ -1051,6 +1061,11 @@ impl Ppu { &self.palette_bg }; + // obtains the values of both X and Y flips for the current tile + // they will be applied by the get tile pixel method + let mut xflip = tile_attr.xflip; + let mut yflip = tile_attr.yflip; + // increments the tile index value by the required offset for the VRAM // bank in which the tile is stored, this is only required for CGB mode tile_index += tile_attr.vram_bank as usize * TILE_COUNT_DMG; @@ -1075,7 +1090,7 @@ impl Ppu { if index as i16 >= wx as i16 - 7 { // obtains the current pixel data from the tile and // re-maps it according to the current palette - let pixel = self.tiles[tile_index].get(x, y); + let pixel = self.tiles[tile_index].get_flipped(x, y, xflip, yflip); let color = palette[pixel as usize]; // updates the pixel in the color buffer, which stores @@ -1120,6 +1135,11 @@ impl Ppu { &self.palette_bg }; + // obtains the values of both X and Y flips for the current tile + // they will be applied by the get tile pixel method + xflip = tile_attr.xflip; + yflip = tile_attr.yflip; + // increments the tile index value by the required offset for the VRAM // bank in which the tile is stored, this is only required for CGB mode tile_index += tile_attr.vram_bank as usize * TILE_COUNT_DMG;