From 38e57489a3c896b0e5fe266bf17bdba8b03f57ba Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sun, 23 Jan 2022 20:03:09 +0000 Subject: [PATCH] Rework the settings a little to allow for raw tile ids --- agb/examples/chicken.rs | 3 +-- agb/src/display/background.rs | 45 ++++++++++++++------------------- agb/src/display/example_logo.rs | 3 +-- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/agb/examples/chicken.rs b/agb/examples/chicken.rs index 6f141f3a..af2a7294 100644 --- a/agb/examples/chicken.rs +++ b/agb/examples/chicken.rs @@ -3,7 +3,7 @@ use agb::{ display::{ - background::{RegularMap, TileFormat, TileSet, TileSetting}, + background::{TileFormat, TileSet, TileSetting}, object::ObjectStandard, HEIGHT, WIDTH, }, @@ -62,7 +62,6 @@ fn main(mut gba: agb::Gba) -> ! { &mut gfx.vram, (i % 32, i / 32).into(), tileset_ref, - tile & ((1 << 10) - 1), TileSetting::from_raw(tile), ); } diff --git a/agb/src/display/background.rs b/agb/src/display/background.rs index a3c764f3..a47876bb 100644 --- a/agb/src/display/background.rs +++ b/agb/src/display/background.rs @@ -219,8 +219,8 @@ impl<'a> VRamManager<'a> { struct Tile(u16); impl Tile { - fn new(tid: TileIndex, setting: TileSetting) -> Self { - Self(tid.0 | setting.as_raw()) + fn new(idx: TileIndex, setting: TileSetting) -> Self { + Self(idx.0 | setting.setting()) } fn tile_index(self) -> TileIndex { @@ -229,35 +229,28 @@ impl Tile { } #[derive(Clone, Copy, Debug, Default)] -pub struct TileSetting { - hflip: bool, - vflip: bool, - palette_id: u8, -} +pub struct TileSetting(u16); impl TileSetting { - pub fn new(hflip: bool, vflip: bool, palette_id: u8) -> Self { - Self { - hflip, - vflip, - palette_id, - } + pub const fn new(tile_id: u16, hflip: bool, vflip: bool, palette_id: u8) -> Self { + Self( + (tile_id & ((1 << 10) - 1)) + | ((hflip as u16) << 10) + | ((vflip as u16) << 11) + | ((palette_id as u16) << 12), + ) } - 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, - } + pub const fn from_raw(raw: u16) -> Self { + Self(raw) } - fn as_raw(self) -> u16 { - ((self.hflip as u16) << 10) | ((self.vflip as u16) << 11) | ((self.palette_id as u16) << 12) + fn index(self) -> u16 { + self.0 & ((1 << 10) - 1) + } + + fn setting(self) -> u16 { + self.0 & !((1 << 10) - 1) } } @@ -293,7 +286,6 @@ impl RegularMap { vram: &mut VRamManager, pos: Vector2D, tileset_ref: TileSetReference, - tile_index: u16, tile_setting: TileSetting, ) { let pos = (pos.x + pos.y * 32) as usize; @@ -303,6 +295,7 @@ impl RegularMap { vram.remove_tile(old_tile.tile_index()); } + let tile_index = tile_setting.index(); let new_tile_idx = vram.add_tile(tileset_ref, tile_index); let new_tile = Tile::new(new_tile_idx, tile_setting); diff --git a/agb/src/display/example_logo.rs b/agb/src/display/example_logo.rs index 93f571bc..a4c30119 100644 --- a/agb/src/display/example_logo.rs +++ b/agb/src/display/example_logo.rs @@ -13,13 +13,12 @@ pub fn display_logo(map: &mut RegularMap, vram: &mut VRamManager) { let tile_id = y * 30 + x; let palette_entry = agb_logo::test_logo.palette_assignments[tile_id as usize]; - let tile_setting = TileSetting::new(false, false, palette_entry); + let tile_setting = TileSetting::new(tile_id, false, false, palette_entry); map.set_tile( vram, (x, y).into(), background_tilemap_reference, - tile_id, tile_setting, ); }