From cf2b8be6e0535b269b1fa5fa0bac8389c68afd52 Mon Sep 17 00:00:00 2001 From: kouta Date: Sun, 7 Jul 2024 00:53:20 -0300 Subject: [PATCH 1/5] Fix affine matrix tile setting --- agb/src/display/tiled/map.rs | 72 +++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/agb/src/display/tiled/map.rs b/agb/src/display/tiled/map.rs index 4a52644f..747f465f 100644 --- a/agb/src/display/tiled/map.rs +++ b/agb/src/display/tiled/map.rs @@ -15,6 +15,7 @@ use super::{ }; use alloc::{vec, vec::Vec}; +use crate::display::tiled::TileFormat::FourBpp; pub trait TiledMapTypes: private::Sealed { type Size: BackgroundSize + Copy; @@ -53,10 +54,7 @@ pub trait TiledMap: TiledMapTypes { fn size(&self) -> Self::Size; } -impl TiledMap for T -where - T: TiledMapPrivate, - T::Size: BackgroundSizePrivate, +impl TiledMap for AffineMap { fn clear(&mut self, vram: &mut VRamManager) { let colours = self.colours(); @@ -88,6 +86,70 @@ where DISPLAY_CONTROL.get() & (1 << (self.background_id() + 0x08)) > 0 } + fn commit(&mut self, vram: &mut VRamManager) { + let screenblock_memory = self.screenblock_memory() as *mut u8; + + if *self.tiles_dirty() { + unsafe { + let tiledata: Vec = self.tiles_mut().iter().map(|a| a.tile_index(FourBpp).raw_index() as u8).collect(); + screenblock_memory.copy_from( + tiledata.as_ptr(), + self.map_size().num_tiles(), + ); + } + } + + let tile_colour_flag: u16 = (self.colours() == TileFormat::EightBpp).into(); + + let new_bg_control_value = (self.priority() as u16) + | ((self.screenblock() as u16) << 8) + | (tile_colour_flag << 7) + | (self.map_size().size_flag() << 14); + + self.bg_control_register().set(new_bg_control_value); + self.update_bg_registers(); + + vram.gc(); + + *self.tiles_dirty() = false; + } + + fn size(&self) -> ::Size { + self.map_size() + } +} +impl TiledMap for RegularMap +{ + 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.tile_index(colours)); + } + + *tile = Default::default(); + } + } + + /// Sets wether the map is visible + /// Use [is_visible](TiledMap::is_visible) to get the value + fn set_visible(&mut self, visible: bool) { + let mode = DISPLAY_CONTROL.get(); + let new_mode = if visible { + mode | (1 << (self.background_id() + 0x08)) as u16 + } else { + mode & !(1 << (self.background_id() + 0x08)) as u16 + }; + DISPLAY_CONTROL.set(new_mode); + } + + /// Checks whether the map is not marked as hidden + /// Use [set_visible](TiledMap::set_visible) to set the value + fn is_visible(&self) -> bool { + DISPLAY_CONTROL.get() & (1 << (self.background_id() + 0x08)) > 0 + } + fn commit(&mut self, vram: &mut VRamManager) { let screenblock_memory = self.screenblock_memory(); @@ -115,7 +177,7 @@ where *self.tiles_dirty() = false; } - fn size(&self) -> T::Size { + fn size(&self) -> ::Size { self.map_size() } } From 8c6e1ae0a91c71d094e2b1ae67a66cfb5f01086f Mon Sep 17 00:00:00 2001 From: kouta-kun <44276987+kouta-kun@users.noreply.github.com> Date: Tue, 9 Jul 2024 16:59:30 -0300 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0d13346..25320141 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- Kludge fix for tilemap gaps in affine graphics modes. + ## [0.20.5] - 2024/06/18 ### Fixed From 1340b992cbd26cc5dd211c813a2a89b8586c8670 Mon Sep 17 00:00:00 2001 From: kouta Date: Wed, 10 Jul 2024 09:29:07 -0300 Subject: [PATCH 3/5] Code review --- CHANGELOG.md | 2 +- agb/src/display/tiled/map.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25320141..90ec1ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed -- Kludge fix for tilemap gaps in affine graphics modes. +- There are no longer gaps between tiles in affine graphics modes. ## [0.20.5] - 2024/06/18 diff --git a/agb/src/display/tiled/map.rs b/agb/src/display/tiled/map.rs index 747f465f..0b1d5bdf 100644 --- a/agb/src/display/tiled/map.rs +++ b/agb/src/display/tiled/map.rs @@ -15,7 +15,6 @@ use super::{ }; use alloc::{vec, vec::Vec}; -use crate::display::tiled::TileFormat::FourBpp; pub trait TiledMapTypes: private::Sealed { type Size: BackgroundSize + Copy; @@ -90,8 +89,8 @@ impl TiledMap for AffineMap let screenblock_memory = self.screenblock_memory() as *mut u8; if *self.tiles_dirty() { + let tiledata: Vec = self.tiles_mut().iter().map(|a| a.tile_index(TileFormat::EightBpp).raw_index() as u8).collect(); unsafe { - let tiledata: Vec = self.tiles_mut().iter().map(|a| a.tile_index(FourBpp).raw_index() as u8).collect(); screenblock_memory.copy_from( tiledata.as_ptr(), self.map_size().num_tiles(), From a9a4a052a9d9fd454b2b19c8facd6c00f8b8dd3a Mon Sep 17 00:00:00 2001 From: kouta Date: Thu, 11 Jul 2024 14:34:12 -0300 Subject: [PATCH 4/5] Remove `priority()` from `TiledMapPrivate` as it can now be accessed straight from the underlying type. --- agb/src/display/tiled/map.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/agb/src/display/tiled/map.rs b/agb/src/display/tiled/map.rs index 0b1d5bdf..e3e46d10 100644 --- a/agb/src/display/tiled/map.rs +++ b/agb/src/display/tiled/map.rs @@ -30,7 +30,6 @@ trait TiledMapPrivate: TiledMapTypes { fn background_id(&self) -> usize; fn screenblock(&self) -> usize; - fn priority(&self) -> Priority; fn map_size(&self) -> Self::Size; fn update_bg_registers(&self); @@ -217,9 +216,6 @@ impl TiledMapPrivate for RegularMap { fn screenblock(&self) -> usize { self.screenblock as usize } - fn priority(&self) -> Priority { - self.priority - } fn map_size(&self) -> Self::Size { self.size } @@ -401,9 +397,6 @@ impl TiledMapPrivate for AffineMap { fn screenblock(&self) -> usize { self.screenblock as usize } - fn priority(&self) -> Priority { - self.priority - } fn map_size(&self) -> Self::Size { self.size } From faa46a4f32b5ddebbcf53f81dbf56f676c212cac Mon Sep 17 00:00:00 2001 From: kouta Date: Thu, 11 Jul 2024 14:56:43 -0300 Subject: [PATCH 5/5] Fix linting issues --- agb/src/display/tiled/map.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/agb/src/display/tiled/map.rs b/agb/src/display/tiled/map.rs index e3e46d10..c5269e49 100644 --- a/agb/src/display/tiled/map.rs +++ b/agb/src/display/tiled/map.rs @@ -52,8 +52,7 @@ pub trait TiledMap: TiledMapTypes { fn size(&self) -> Self::Size; } -impl TiledMap for AffineMap -{ +impl TiledMap for AffineMap { fn clear(&mut self, vram: &mut VRamManager) { let colours = self.colours(); @@ -88,12 +87,13 @@ impl TiledMap for AffineMap let screenblock_memory = self.screenblock_memory() as *mut u8; if *self.tiles_dirty() { - let tiledata: Vec = self.tiles_mut().iter().map(|a| a.tile_index(TileFormat::EightBpp).raw_index() as u8).collect(); + let tiledata: Vec = self + .tiles_mut() + .iter() + .map(|a| a.tile_index(TileFormat::EightBpp).raw_index() as u8) + .collect(); unsafe { - screenblock_memory.copy_from( - tiledata.as_ptr(), - self.map_size().num_tiles(), - ); + screenblock_memory.copy_from(tiledata.as_ptr(), self.map_size().num_tiles()); } } @@ -116,8 +116,7 @@ impl TiledMap for AffineMap self.map_size() } } -impl TiledMap for RegularMap -{ +impl TiledMap for RegularMap { fn clear(&mut self, vram: &mut VRamManager) { let colours = self.colours();