From a865240308ba8a6886fc40487746f3fa82d9e983 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Tue, 29 Aug 2023 16:07:50 +0100 Subject: [PATCH] Start fixing 256 colours --- agb/src/display/tiled/map.rs | 33 ++++++++++++++------------- agb/src/display/tiled/mod.rs | 4 ++-- agb/src/display/tiled/vram_manager.rs | 19 +++------------ examples/combo/src/lib.rs | 18 +++++++-------- 4 files changed, 31 insertions(+), 43 deletions(-) diff --git a/agb/src/display/tiled/map.rs b/agb/src/display/tiled/map.rs index ea1cdac4..5023a3e9 100644 --- a/agb/src/display/tiled/map.rs +++ b/agb/src/display/tiled/map.rs @@ -10,7 +10,7 @@ use crate::memory_mapped::MemoryMapped; use super::{ AffineBackgroundSize, BackgroundID, BackgroundSize, BackgroundSizePrivate, - RegularBackgroundSize, Tile, TileFormat, TileIndex, TileSet, TileSetting, VRamManager, + RegularBackgroundSize, Tile, TileFormat, TileSet, TileSetting, VRamManager, }; use alloc::{vec, vec::Vec}; @@ -20,10 +20,9 @@ pub trait TiledMapTypes: private::Sealed { } trait TiledMapPrivate: TiledMapTypes { - type TileType: Into + Copy + Default + Eq + PartialEq; type AffineMatrix; - fn tiles_mut(&mut self) -> &mut [Self::TileType]; + fn tiles_mut(&mut self) -> &mut [Tile]; fn tiles_dirty(&mut self) -> &mut bool; fn colours(&self) -> TileFormat; @@ -59,9 +58,11 @@ where T::Size: BackgroundSizePrivate, { fn clear(&mut self, vram: &mut VRamManager) { + let colours = self.colours(); + for tile in self.tiles_mut() { if *tile != Default::default() { - vram.remove_tile((*tile).into()); + vram.remove_tile(tile.tile_index(colours)); } *tile = Default::default(); @@ -134,10 +135,9 @@ impl TiledMapTypes for RegularMap { } impl TiledMapPrivate for RegularMap { - type TileType = Tile; type AffineMatrix = (); - fn tiles_mut(&mut self) -> &mut [Self::TileType] { + fn tiles_mut(&mut self) -> &mut [Tile] { &mut self.tiles } fn tiles_dirty(&mut self) -> &mut bool { @@ -195,11 +195,12 @@ impl RegularMap { tileset: &TileSet<'_>, tile_setting: TileSetting, ) { - if tileset.format() != self.colours() { + let colours = self.colours(); + if tileset.format() != colours { panic!( "Cannot set a {:?} colour tile on a {:?} colour background", tileset.format(), - self.colours() + colours ); } @@ -207,7 +208,7 @@ impl RegularMap { let old_tile = self.tiles_mut()[pos]; if old_tile != Tile::default() { - vram.remove_tile(old_tile.into()); + vram.remove_tile(old_tile.tile_index(colours)); } let tile_index = tile_setting.index(); @@ -254,7 +255,7 @@ pub struct AffineMap { transform: AffineMatrixBackground, - tiles: Vec, + tiles: Vec, tiles_dirty: bool, } @@ -263,10 +264,9 @@ impl TiledMapTypes for AffineMap { } impl TiledMapPrivate for AffineMap { - type TileType = u8; type AffineMatrix = AffineMatrixBackground; - fn tiles_mut(&mut self) -> &mut [Self::TileType] { + fn tiles_mut(&mut self) -> &mut [Tile] { &mut self.tiles } fn tiles_dirty(&mut self) -> &mut bool { @@ -320,19 +320,20 @@ impl AffineMap { tile_id: u8, ) { let pos = self.map_size().gba_offset(pos); + let colours = self.colours(); let old_tile = self.tiles_mut()[pos]; - if old_tile != 0 { - vram.remove_tile(old_tile.into()); + if old_tile != Tile::default() { + vram.remove_tile(old_tile.tile_index(colours)); } let tile_index = tile_id as u16; let new_tile = if tile_index != TRANSPARENT_TILE_INDEX { let new_tile_idx = vram.add_tile(tileset, tile_index); - new_tile_idx.raw_index() as u8 + Tile::new(new_tile_idx, TileSetting(0)) } else { - 0 + Tile::default() }; if old_tile == new_tile { diff --git a/agb/src/display/tiled/mod.rs b/agb/src/display/tiled/mod.rs index 8120c763..7c289a3f 100644 --- a/agb/src/display/tiled/mod.rs +++ b/agb/src/display/tiled/mod.rs @@ -156,8 +156,8 @@ impl Tile { Self(idx.raw_index() | setting.setting()) } - fn tile_index(self) -> TileIndex { - TileIndex::new(self.0 as usize & ((1 << 10) - 1), TileFormat::FourBpp) + fn tile_index(self, format: TileFormat) -> TileIndex { + TileIndex::new(self.0 as usize & ((1 << 10) - 1), format) } } diff --git a/agb/src/display/tiled/vram_manager.rs b/agb/src/display/tiled/vram_manager.rs index bb5bb200..d98c968d 100644 --- a/agb/src/display/tiled/vram_manager.rs +++ b/agb/src/display/tiled/vram_manager.rs @@ -2,7 +2,6 @@ use core::{alloc::Layout, ptr::NonNull}; use alloc::{slice, vec::Vec}; -use crate::display::tiled::Tile; use crate::{ agb_alloc::{block_allocator::BlockAllocator, bump_allocator::StartEnd}, display::palette16, @@ -66,21 +65,21 @@ impl<'a> TileSet<'a> { #[derive(Debug, Clone, Copy)] pub enum TileIndex { FourBpp(u16), - EightBpp(u8), + EightBpp(u16), } impl TileIndex { pub(crate) const fn new(index: usize, format: TileFormat) -> Self { match format { TileFormat::FourBpp => Self::FourBpp(index as u16), - TileFormat::EightBpp => Self::EightBpp(index as u8), + TileFormat::EightBpp => Self::EightBpp(index as u16), } } pub(crate) const fn raw_index(self) -> u16 { match self { TileIndex::FourBpp(x) => x, - TileIndex::EightBpp(x) => x as u16, + TileIndex::EightBpp(x) => x, } } @@ -99,18 +98,6 @@ impl TileIndex { } } -impl From for TileIndex { - fn from(tile: Tile) -> Self { - tile.tile_index() - } -} - -impl From for TileIndex { - fn from(index: u8) -> TileIndex { - TileIndex::new(usize::from(index), TileFormat::EightBpp) - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] struct TileReference(NonNull); diff --git a/examples/combo/src/lib.rs b/examples/combo/src/lib.rs index faaaa6f2..213f9301 100644 --- a/examples/combo/src/lib.rs +++ b/examples/combo/src/lib.rs @@ -47,10 +47,10 @@ impl Game { include_background_gfx!( games, "121105", - hat => deduplicate "gfx/hat.png", - purple => deduplicate "gfx/purple.png", - hyperspace => deduplicate "gfx/hyperspace.png", - amplitude => deduplicate "gfx/amplitude.png" + hat => 256 deduplicate "gfx/hat.png", + purple => 256 deduplicate "gfx/purple.png", + hyperspace => 256 deduplicate "gfx/hyperspace.png", + amplitude => 256 deduplicate "gfx/amplitude.png" ); fn get_game(gba: &mut agb::Gba) -> Game { @@ -59,10 +59,10 @@ fn get_game(gba: &mut agb::Gba) -> Game { let (tile, mut vram) = gba.display.video.tiled0(); - let hat = TileSet::new(games::hat.tiles, TileFormat::FourBpp); - let purple = TileSet::new(games::purple.tiles, TileFormat::FourBpp); - let hyperspace = TileSet::new(games::hyperspace.tiles, TileFormat::FourBpp); - let amplitude = TileSet::new(games::amplitude.tiles, TileFormat::FourBpp); + let hat = TileSet::new(games::hat.tiles, TileFormat::EightBpp); + let purple = TileSet::new(games::purple.tiles, TileFormat::EightBpp); + let hyperspace = TileSet::new(games::hyperspace.tiles, TileFormat::EightBpp); + let amplitude = TileSet::new(games::amplitude.tiles, TileFormat::EightBpp); let tiles = [hat, purple, hyperspace, amplitude]; @@ -79,7 +79,7 @@ fn get_game(gba: &mut agb::Gba) -> Game { tile.background( Priority::P0, RegularBackgroundSize::Background32x32, - TileFormat::FourBpp, + TileFormat::EightBpp, ), Box::new(|pos| { let y = pos.y.rem_euclid(20);