Skip to content
Snippets Groups Projects
Verified Commit be6739a2 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: support for X and Y flip

parent 41bde16a
No related branches found
No related tags found
1 merge request!16Support for Game Boy Color (CGB) 😎🖍️
Pipeline #2634 passed
...@@ -99,9 +99,13 @@ impl PaletteInfo { ...@@ -99,9 +99,13 @@ impl PaletteInfo {
/// Represents a tile within the Game Boy context, /// Represents a tile within the Game Boy context,
/// should contain the pixel buffer of the tile. /// should contain the pixel buffer of the tile.
/// The tiles are always 8x8 pixels in size.
#[cfg_attr(feature = "wasm", wasm_bindgen)] #[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
pub struct Tile { 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], buffer: [u8; 64],
} }
...@@ -111,6 +115,12 @@ impl Tile { ...@@ -111,6 +115,12 @@ impl Tile {
self.buffer[y * TILE_WIDTH + x] 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) { pub fn set(&mut self, x: usize, y: usize, value: u8) {
self.buffer[y * TILE_WIDTH + x] = value; self.buffer[y * TILE_WIDTH + x] = value;
} }
...@@ -1051,6 +1061,11 @@ impl Ppu { ...@@ -1051,6 +1061,11 @@ impl Ppu {
&self.palette_bg &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 // 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 // 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; tile_index += tile_attr.vram_bank as usize * TILE_COUNT_DMG;
...@@ -1075,7 +1090,7 @@ impl Ppu { ...@@ -1075,7 +1090,7 @@ impl Ppu {
if index as i16 >= wx as i16 - 7 { if index as i16 >= wx as i16 - 7 {
// obtains the current pixel data from the tile and // obtains the current pixel data from the tile and
// re-maps it according to the current palette // 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]; let color = palette[pixel as usize];
// updates the pixel in the color buffer, which stores // updates the pixel in the color buffer, which stores
...@@ -1120,6 +1135,11 @@ impl Ppu { ...@@ -1120,6 +1135,11 @@ impl Ppu {
&self.palette_bg &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 // 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 // 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; tile_index += tile_attr.vram_bank as usize * TILE_COUNT_DMG;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment