From 5cfa64030aad7265b51e0b37e6b93a2f798df2f0 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Wed, 30 Aug 2023 16:08:43 +0100 Subject: [PATCH] Generate the tileset directly --- agb-image-converter/src/rust_generator.rs | 9 ++++++++- agb/examples/affine_background.rs | 4 ++-- agb/examples/animated_background.rs | 6 +++--- agb/src/display/example_logo.rs | 6 +++--- agb/src/display/tile_data.rs | 6 +++--- agb/src/display/tiled/vram_manager.rs | 11 ++++++----- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/agb-image-converter/src/rust_generator.rs b/agb-image-converter/src/rust_generator.rs index bd6f243c..4d8b4d5c 100644 --- a/agb-image-converter/src/rust_generator.rs +++ b/agb-image-converter/src/rust_generator.rs @@ -114,6 +114,11 @@ pub(crate) fn generate_code( }); let data = ByteString(&tile_data); + let tile_format = if assignment_offset.is_some() { + quote! { #crate_prefix::display::tiled::TileFormat::FourBpp } + } else { + quote! { #crate_prefix::display::tiled::TileFormat::EightBpp } + }; quote! { #[allow(non_upper_case_globals)] @@ -134,11 +139,13 @@ pub(crate) fn generate_code( &ALIGNED.bytes }; + const TILE_SET: #crate_prefix::display::tiled::TileSet = #crate_prefix::display::tiled::TileSet::new(TILE_DATA, #tile_format); + const TILE_SETTINGS: &[#crate_prefix::display::tiled::TileSetting] = &[ #(#tile_settings),* ]; - #crate_prefix::display::tile_data::TileData::new(TILE_DATA, TILE_SETTINGS) + #crate_prefix::display::tile_data::TileData::new(TILE_SET, TILE_SETTINGS) }; } } diff --git a/agb/examples/affine_background.rs b/agb/examples/affine_background.rs index a0bdd1ab..2f8d37ff 100644 --- a/agb/examples/affine_background.rs +++ b/agb/examples/affine_background.rs @@ -4,7 +4,7 @@ use agb::{ display::{ affine::AffineMatrixBackground, - tiled::{AffineBackgroundSize, TileFormat, TileSet, TiledMap}, + tiled::{AffineBackgroundSize, TiledMap}, Priority, }, fixnum::{num, Num}, @@ -18,7 +18,7 @@ fn main(mut gba: agb::Gba) -> ! { let (gfx, mut vram) = gba.display.video.tiled2(); let vblank = agb::interrupt::VBlank::get(); - let tileset = TileSet::new(affine_tiles::water_tiles.tiles, TileFormat::EightBpp); + let tileset = affine_tiles::water_tiles.tiles; vram.set_background_palettes(affine_tiles::PALETTES); diff --git a/agb/examples/animated_background.rs b/agb/examples/animated_background.rs index 398932b7..c3632390 100644 --- a/agb/examples/animated_background.rs +++ b/agb/examples/animated_background.rs @@ -3,7 +3,7 @@ use agb::{ display::{ - tiled::{RegularBackgroundSize, TileFormat, TileSet, TiledMap}, + tiled::{RegularBackgroundSize, TiledMap}, Priority, }, include_background_gfx, @@ -16,14 +16,14 @@ fn main(mut gba: agb::Gba) -> ! { let (gfx, mut vram) = gba.display.video.tiled0(); let vblank = agb::interrupt::VBlank::get(); - let tileset = TileSet::new(water_tiles::water_tiles.tiles, TileFormat::FourBpp); + let tileset = water_tiles::water_tiles.tiles; vram.set_background_palettes(water_tiles::PALETTES); let mut bg = gfx.background( Priority::P0, RegularBackgroundSize::Background32x32, - TileFormat::FourBpp, + tileset.format(), ); for y in 0..20u16 { diff --git a/agb/src/display/example_logo.rs b/agb/src/display/example_logo.rs index 5c008a29..e7374789 100644 --- a/agb/src/display/example_logo.rs +++ b/agb/src/display/example_logo.rs @@ -1,11 +1,11 @@ -use super::tiled::{RegularMap, TileFormat, TileSet, TiledMap, VRamManager}; +use super::tiled::{RegularMap, TiledMap, VRamManager}; crate::include_background_gfx!(crate, agb_logo, test_logo => deduplicate "gfx/test_logo.png"); pub fn display_logo(map: &mut RegularMap, vram: &mut VRamManager) { vram.set_background_palettes(agb_logo::PALETTES); - let background_tilemap = TileSet::new(agb_logo::test_logo.tiles, TileFormat::FourBpp); + let background_tilemap = agb_logo::test_logo.tiles; for y in 0..20 { for x in 0..30 { @@ -37,7 +37,7 @@ mod tests { let mut map = gfx.background( Priority::P0, RegularBackgroundSize::Background32x32, - TileFormat::FourBpp, + agb_logo::test_logo.tiles.format(), ); display_logo(&mut map, &mut vram); diff --git a/agb/src/display/tile_data.rs b/agb/src/display/tile_data.rs index c1193417..0e01397d 100644 --- a/agb/src/display/tile_data.rs +++ b/agb/src/display/tile_data.rs @@ -1,14 +1,14 @@ -use super::tiled::TileSetting; +use super::tiled::{TileSet, TileSetting}; #[non_exhaustive] pub struct TileData { - pub tiles: &'static [u8], + pub tiles: TileSet<'static>, pub tile_settings: &'static [TileSetting], } impl TileData { #[must_use] - pub const fn new(tiles: &'static [u8], tile_settings: &'static [TileSetting]) -> Self { + pub const fn new(tiles: TileSet<'static>, tile_settings: &'static [TileSetting]) -> Self { TileData { tiles, tile_settings, diff --git a/agb/src/display/tiled/vram_manager.rs b/agb/src/display/tiled/vram_manager.rs index d98c968d..e14b259e 100644 --- a/agb/src/display/tiled/vram_manager.rs +++ b/agb/src/display/tiled/vram_manager.rs @@ -49,17 +49,18 @@ pub struct TileSet<'a> { impl<'a> TileSet<'a> { #[must_use] - pub fn new(tiles: &'a [u8], format: TileFormat) -> Self { + pub const fn new(tiles: &'a [u8], format: TileFormat) -> Self { Self { tiles, format } } + #[must_use] + pub const fn format(&self) -> TileFormat { + self.format + } + fn reference(&self) -> NonNull<[u8]> { self.tiles.into() } - - pub(crate) fn format(&self) -> TileFormat { - self.format - } } #[derive(Debug, Clone, Copy)]