use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
pub struct TileID(pub u8);
#[derive(Debug, Clone, Copy, Default)]
pub struct TileEntry {
pub w: u8,
pub h: u8,
pub cluster_index: u16,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct TileFlags(pub u8);
impl From<PaletteID> for TileFlags {
fn from(palette_id: PaletteID) -> TileFlags {
TileFlags::new(false, false, palette_id)
}
}
impl TileFlags {
pub const fn new(flip_h: bool, flip_v: bool, palette: PaletteID) -> Self {
assert!(
palette.0 < 16,
err!("Tile Palette must be in the 0 to 15 range")
);
let mut data: u8 = 0b_0000_0000;
if flip_h {
data |= 0b_1000_0000;
}
if flip_v {
data |= 0b_0100_0000;
}
let masked_palette = palette.0 & 0b_0000_1111;
data |= masked_palette;
Self(data)
}
pub const fn with_flip_x(self) -> Self {
Self(self.0 | 0b_1000_0000)
}
pub const fn with_flip_y(self) -> Self {
Self(self.0 | 0b_0100_0000)
}
pub const fn with_rotation(self) -> Self {
Self(self.0 | 0b_0010_0000)
}
pub const fn with_fg(self) -> Self {
Self(self.0 | 0b_0001_0000)
}
pub const fn with_palette(self, palette: PaletteID) -> Self {
let data = self.0 & 0b_1111_0000;
Self(data | palette.0)
}
pub const fn set_flip_x(&mut self, state: bool) {
if state {
self.0 |= 0b_1000_0000
} else {
self.0 &= 0b_0111_1111
}
}
pub const fn set_flip_y(&mut self, state: bool) {
if state {
self.0 |= 0b_0100_0000
} else {
self.0 &= 0b_1011_1111
}
}
pub const fn set_rotation(&mut self, state: bool) {
if state {
self.0 |= 0b_0010_0000
} else {
self.0 &= 0b_1101_1111
}
}
pub const fn set_fg(&mut self, state: bool) {
if state {
self.0 |= 0b_0001_0000
} else {
self.0 &= 0b_1110_1111
}
}
pub const fn rotate_up(&mut self) {
self.0 &= 0b_0001_1111; self.0 |= 0b_0000_0000; }
pub const fn rotate_left(&mut self) {
self.0 &= 0b_0001_1111; self.0 |= 0b_0010_0000; }
pub const fn rotate_down(&mut self) {
self.0 &= 0b_0001_1111; self.0 |= 0b_0100_0000; }
pub const fn rotate_right(&mut self) {
self.0 &= 0b_0001_1111; self.0 |= 0b_1010_0000; }
pub const fn is_flipped_x(&self) -> bool {
self.0 & 0b_1000_0000 != 0
}
pub const fn is_flipped_y(&self) -> bool {
self.0 & 0b_0100_0000 != 0
}
pub const fn is_rotated(&self) -> bool {
self.0 & 0b_0010_0000 != 0
}
pub const fn is_fg(&self) -> bool {
self.0 & 0b_0001_0000 != 0
}
pub const fn palette(&self) -> PaletteID {
PaletteID(self.0 & 0b_0000_1111)
}
}