From c9bf56755ac35494c6d2279e4b6b7ddd4bb40025 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Tue, 29 Aug 2023 15:17:50 +0100 Subject: [PATCH] Also deduplicate for the dungeon puzzler --- agb/src/display/tiled/mod.rs | 12 +++++++++++ examples/the-dungeon-puzzlers-lament/build.rs | 13 +++++------- .../src/backgrounds.rs | 20 +++++++++---------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/agb/src/display/tiled/mod.rs b/agb/src/display/tiled/mod.rs index 7248e380..8120c763 100644 --- a/agb/src/display/tiled/mod.rs +++ b/agb/src/display/tiled/mod.rs @@ -165,6 +165,8 @@ impl Tile { pub struct TileSetting(u16); impl TileSetting { + pub const BLANK: Self = TileSetting::new(1023, false, false, 0); + #[must_use] pub const fn new(tile_id: u16, hflip: bool, vflip: bool, palette_id: u8) -> Self { Self( @@ -180,6 +182,16 @@ impl TileSetting { Self(raw) } + #[must_use] + pub const fn hflip(self, should_flip: bool) -> Self { + Self(self.0 ^ ((should_flip as u16) << 10)) + } + + #[must_use] + pub const fn vflip(self, should_flip: bool) -> Self { + Self(self.0 ^ ((should_flip as u16) << 11)) + } + fn index(self) -> u16 { self.0 & ((1 << 10) - 1) } diff --git a/examples/the-dungeon-puzzlers-lament/build.rs b/examples/the-dungeon-puzzlers-lament/build.rs index 7e4a297a..62b68f75 100644 --- a/examples/the-dungeon-puzzlers-lament/build.rs +++ b/examples/the-dungeon-puzzlers-lament/build.rs @@ -331,12 +331,10 @@ fn export_tiles(map: &tiled::Map, background: TokenStream) -> TokenStream { tile_tileset_x * 2 + x_offset + tile_tileset_y * 9 * 4 + y_offset * 9 * 2; let gba_tile_id = gba_tile_id as u16; - let palette_id = - quote! { backgrounds::#background.palette_assignments[#gba_tile_id as usize] }; - quote! { TileSetting::new(#gba_tile_id, #hflip, #vflip, #palette_id) } + quote! { backgrounds::#background.tile_settings[#gba_tile_id as usize].hflip(#hflip).vflip(#vflip) } } None => { - quote! { TileSetting::new(1023, false, false, 0) } + quote! { TileSetting::BLANK } } } }); @@ -361,12 +359,11 @@ fn export_ui_tiles(map: &tiled::Map, background: TokenStream) -> TokenStream { let tile_id = tile.id() as u16; let vflip = tile.flip_v; let hflip = tile.flip_h; - let palette_id = - quote! { backgrounds::#background.palette_assignments[#tile_id as usize] }; - quote! { TileSetting::new(#tile_id, #hflip, #vflip, #palette_id) } + + quote! { backgrounds::#background.tile_settings[#tile_id as usize].hflip(#hflip).vflip(#vflip) } } None => { - quote! { TileSetting::new(1023, false, false, 0) } + quote! { TileSetting::BLANK } } } }); diff --git a/examples/the-dungeon-puzzlers-lament/src/backgrounds.rs b/examples/the-dungeon-puzzlers-lament/src/backgrounds.rs index d2ac283c..9d31a94e 100644 --- a/examples/the-dungeon-puzzlers-lament/src/backgrounds.rs +++ b/examples/the-dungeon-puzzlers-lament/src/backgrounds.rs @@ -1,12 +1,12 @@ use agb::{ - display::tiled::{RegularMap, TileFormat, TileSet, TileSetting, VRamManager}, + display::tiled::{RegularMap, TileFormat, TileSet, VRamManager}, include_background_gfx, }; include_background_gfx!(backgrounds, "1e151b", - ui => "maps/ui_tiles.png", - level => "maps/level.png", - ending => "gfx/ending_page.aseprite", + ui => deduplicate "maps/ui_tiles.png", + level => deduplicate "maps/level.png", + ending => deduplicate "gfx/ending_page.aseprite", ); mod tilemaps { @@ -56,13 +56,13 @@ pub fn load_ending_page(map: &mut RegularMap, vram_manager: &mut VRamManager) { for y in 0..20u16 { for x in 0..30u16 { let tile_pos = y * 30 + x; - let tile_setting = TileSetting::new( - tile_pos, - false, - false, - backgrounds::ending.palette_assignments[tile_pos as usize], + + map.set_tile( + vram_manager, + (x, y).into(), + &ending_tileset, + backgrounds::ending.tile_settings[tile_pos as usize], ); - map.set_tile(vram_manager, (x, y).into(), &ending_tileset, tile_setting); } } }