From cf72a9331add8c3537a09d5d67a33584bedab06d Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sun, 23 Jan 2022 19:43:08 +0000 Subject: [PATCH] Add API for setting tile data from just a number --- agb/examples/chicken.rs | 4 ++-- agb/src/display/background.rs | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/agb/examples/chicken.rs b/agb/examples/chicken.rs index 8f3c6d70..6f141f3a 100644 --- a/agb/examples/chicken.rs +++ b/agb/examples/chicken.rs @@ -56,14 +56,14 @@ fn main(mut gba: agb::Gba) -> ! { 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; background.set_tile( &mut gfx.vram, (i % 32, i / 32).into(), tileset_ref, tile & ((1 << 10) - 1), - TileSetting::default(), + TileSetting::from_raw(tile), ); } diff --git a/agb/src/display/background.rs b/agb/src/display/background.rs index 96dcc8a0..a3c764f3 100644 --- a/agb/src/display/background.rs +++ b/agb/src/display/background.rs @@ -220,10 +220,7 @@ struct Tile(u16); impl Tile { fn new(tid: TileIndex, setting: TileSetting) -> Self { - let hflip = setting.hflip; - 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)) + Self(tid.0 | setting.as_raw()) } fn tile_index(self) -> TileIndex { @@ -246,6 +243,22 @@ impl TileSetting { 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 {