diff --git a/agb/src/display/tiled/infinite_scrolled_map.rs b/agb/src/display/tiled/infinite_scrolled_map.rs index 48cc66ba..91310d78 100644 --- a/agb/src/display/tiled/infinite_scrolled_map.rs +++ b/agb/src/display/tiled/infinite_scrolled_map.rs @@ -63,8 +63,8 @@ impl<'a> InfiniteScrolledMap<'a> { let offset = self.current_pos - (x_start * 8, y_start * 8).into(); let offset_scroll = ( - offset.x.rem_euclid(32 * 8) as u16, - offset.y.rem_euclid(32 * 8) as u16, + offset.x.rem_euclid(self.map.size().width() as i32 * 8) as u16, + offset.y.rem_euclid(self.map.size().height() as i32 * 8) as u16, ) .into(); @@ -120,6 +120,8 @@ impl<'a> InfiniteScrolledMap<'a> { let difference_tile_x = div_ceil(difference.x, 8); let difference_tile_y = div_ceil(difference.y, 8); + let size = self.map.size(); + let vertical_rect_to_update: Rect = if div_floor(old_pos.x, 8) != new_tile_x { // need to update the x line // calculate which direction we need to update @@ -149,8 +151,8 @@ impl<'a> InfiniteScrolledMap<'a> { // calculate which direction we need to update let direction = difference.y.signum(); - // either need to update 30 or 31 tiles depending on whether the x coordinate is a perfect multiple - let x_tiles_to_update: i32 = 32; + // either need to update width - 2 or width - 1 tiles depending on whether the x coordinate is a perfect multiple + let x_tiles_to_update: i32 = size.width() as i32; let line_to_update = if direction < 0 { // moving up so need to update the top @@ -177,8 +179,8 @@ impl<'a> InfiniteScrolledMap<'a> { self.map.set_tile( vram, ( - (tile_x - self.offset.x).rem_euclid(32) as u16, - (tile_y - self.offset.y).rem_euclid(32) as u16, + (tile_x - self.offset.x).rem_euclid(size.width() as i32) as u16, + (tile_y - self.offset.y).rem_euclid(size.height() as i32) as u16, ) .into(), tileset, @@ -188,8 +190,8 @@ impl<'a> InfiniteScrolledMap<'a> { let current_scroll = self.map.scroll_pos(); let new_scroll = ( - (current_scroll.x as i32 + difference.x).rem_euclid(32 * 8) as u16, - (current_scroll.y as i32 + difference.y).rem_euclid(32 * 8) as u16, + (current_scroll.x as i32 + difference.x).rem_euclid(size.width() as i32 * 8) as u16, + (current_scroll.y as i32 + difference.y).rem_euclid(size.height() as i32 * 8) as u16, ) .into(); diff --git a/agb/src/display/tiled/map.rs b/agb/src/display/tiled/map.rs index dc6910f8..9f5a8b2b 100644 --- a/agb/src/display/tiled/map.rs +++ b/agb/src/display/tiled/map.rs @@ -9,6 +9,8 @@ use crate::memory_mapped::MemoryMapped; use super::{RegularBackgroundSize, Tile, TileSet, TileSetting, VRamManager}; +use alloc::{vec, vec::Vec}; + pub struct RegularMap { background_id: u8, @@ -17,7 +19,7 @@ pub struct RegularMap { y_scroll: u16, priority: Priority, - tiles: [Tile; 32 * 32], + tiles: Vec, tiles_dirty: bool, size: RegularBackgroundSize, @@ -40,7 +42,7 @@ impl RegularMap { y_scroll: 0, priority, - tiles: [Tile::default(); 32 * 32], + tiles: vec![Default::default(); size.num_tiles()], tiles_dirty: true, size, @@ -54,7 +56,7 @@ impl RegularMap { tileset: &TileSet<'_>, tile_setting: TileSetting, ) { - let pos = (pos.x + pos.y * 32) as usize; + let pos = (pos.x + pos.y * self.size.width() as u16) as usize; let old_tile = self.tiles[pos]; if old_tile != Tile::default() { @@ -120,7 +122,7 @@ impl RegularMap { dma_copy16( self.tiles.as_ptr() as *const u16, screenblock_memory, - 32 * 32, + self.size.num_tiles(), ); } @@ -136,6 +138,10 @@ impl RegularMap { (self.x_scroll, self.y_scroll).into() } + pub(crate) fn size(&self) -> RegularBackgroundSize { + self.size + } + const fn bg_control_register(&self) -> MemoryMapped { unsafe { MemoryMapped::new(0x0400_0008 + 2 * self.background_id as usize) } } diff --git a/agb/src/display/tiled/mod.rs b/agb/src/display/tiled/mod.rs index b40a0701..ca10e66f 100644 --- a/agb/src/display/tiled/mod.rs +++ b/agb/src/display/tiled/mod.rs @@ -43,6 +43,10 @@ impl RegularBackgroundSize { RegularBackgroundSize::Background64x64 => 3, } } + + pub(crate) fn num_tiles(&self) -> usize { + (self.width() * self.height()) as usize + } } #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]