Add API for setting tile data from just a number

This commit is contained in:
Gwilym Kuiper 2022-01-23 19:43:08 +00:00
parent 1286a58511
commit cf72a9331a
2 changed files with 19 additions and 6 deletions

View file

@ -56,14 +56,14 @@ fn main(mut gba: agb::Gba) -> ! {
let mut background = gfx.background(); let mut background = gfx.background();
for (i, tile) in MAP_MAP.iter().enumerate() { for (i, &tile) in MAP_MAP.iter().enumerate() {
let i = i as u16; let i = i as u16;
background.set_tile( background.set_tile(
&mut gfx.vram, &mut gfx.vram,
(i % 32, i / 32).into(), (i % 32, i / 32).into(),
tileset_ref, tileset_ref,
tile & ((1 << 10) - 1), tile & ((1 << 10) - 1),
TileSetting::default(), TileSetting::from_raw(tile),
); );
} }

View file

@ -220,10 +220,7 @@ struct Tile(u16);
impl Tile { impl Tile {
fn new(tid: TileIndex, setting: TileSetting) -> Self { fn new(tid: TileIndex, setting: TileSetting) -> Self {
let hflip = setting.hflip; Self(tid.0 | setting.as_raw())
let vflip = setting.vflip;
let palette_id = setting.palette_id as u16;
Self(tid.0 | ((hflip as u16) << 10) | ((vflip as u16) << 11) | (palette_id << 12))
} }
fn tile_index(self) -> TileIndex { fn tile_index(self) -> TileIndex {
@ -246,6 +243,22 @@ impl TileSetting {
palette_id, palette_id,
} }
} }
pub fn from_raw(raw: u16) -> Self {
let hflip = raw & (1 << 10) != 0;
let vflip = raw & (1 << 11) != 0;
let palette_id = raw >> 12;
Self {
hflip,
vflip,
palette_id: palette_id as u8,
}
}
fn as_raw(self) -> u16 {
((self.hflip as u16) << 10) | ((self.vflip as u16) << 11) | ((self.palette_id as u16) << 12)
}
} }
pub struct RegularMap { pub struct RegularMap {