From 12e01d6e01729b42945aaad7be02adf259cf5c94 Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Wed, 28 Apr 2021 03:09:08 +0100 Subject: [PATCH] add good map support --- agb/src/display/tiled0.rs | 256 +++++++++++++++++++++++++++++++------- 1 file changed, 209 insertions(+), 47 deletions(-) diff --git a/agb/src/display/tiled0.rs b/agb/src/display/tiled0.rs index e220eb44..2e8c4378 100644 --- a/agb/src/display/tiled0.rs +++ b/agb/src/display/tiled0.rs @@ -46,25 +46,61 @@ pub enum BackgroundSize { } #[non_exhaustive] -pub struct Background { - layer: usize, +pub struct Background<'a> { + background: u8, + block: u8, + map: Option<&'a [u16]>, + map_dim_x: u32, + map_dim_y: u32, + pos_x: i32, + pos_y: i32, } -impl Background { - pub fn enable(&mut self) { +impl<'a> Background<'a> { + unsafe fn new(layer: u8, block: u8) -> Background<'a> { + let mut background = Background { + background: layer, + block, + map: None, + map_dim_x: 0, + map_dim_y: 0, + pos_x: 0, + pos_y: 0, + }; + background.set_colour_mode(ColourMode::FourBitPerPixel); + background.set_background_size(BackgroundSize::S32x32); + background.set_block(block); + background + } + + pub fn set_map(&mut self, map: &'a [u16], dim_x: u32, dim_y: u32) { + self.map = Some(map); + self.map_dim_x = dim_x; + self.map_dim_y = dim_y; + self.draw_full_map(); + } +} + +impl Background<'_> { + pub fn show(&mut self) { + assert!(self.map.is_some(), "map should be set before showing"); let mode = DISPLAY_CONTROL.get(); - let new_mode = mode | (1 << (self.layer + 0x08)); + let new_mode = mode | (1 << (self.background + 0x08)); DISPLAY_CONTROL.set(new_mode); } - pub fn disable(&mut self) { + pub fn hide(&mut self) { let mode = DISPLAY_CONTROL.get(); - let new_mode = mode | !(1 << (self.layer + 0x08)); + let new_mode = mode | !(1 << (self.background + 0x08)); DISPLAY_CONTROL.set(new_mode); } unsafe fn get_register(&mut self) -> *mut u16 { - (0x0400_0008 + 2 * self.layer) as *mut u16 + (0x0400_0008 + 2 * self.background as usize) as *mut u16 + } + + unsafe fn set_block(&mut self, block: u8) { + self.set_bits(0x08, 5, block as u16) } unsafe fn set_bits(&mut self, start: u16, num_bits: u16, bits: u16) { @@ -79,29 +115,126 @@ impl Background { unsafe { self.set_bits(0, 2, p as u16) } } - pub fn set_colour_mode(&mut self, mode: ColourMode) { + fn set_colour_mode(&mut self, mode: ColourMode) { unsafe { self.set_bits(0x07, 1, mode as u16) } } - pub fn set_background_size(&mut self, size: BackgroundSize) { + fn set_background_size(&mut self, size: BackgroundSize) { unsafe { self.set_bits(0x0E, 2, size as u16) } } - pub fn set_screen_base_block(&mut self, block: u32) { - assert!( - block < 32, - "screen base block must be in range 0 to 31 inclusive" - ); - unsafe { self.set_bits(0x08, 5, block as u16) } + fn map_get(&self, x: i32, y: i32, default: u16) -> u16 { + let map = self.map.unwrap(); + + if x >= self.map_dim_x as i32 || x < 0 { + default + } else if y >= self.map_dim_y as i32 || y < 0 { + default + } else { + map[(self.map_dim_x as i32 * y + x) as usize] + } + } + + fn set_x(&mut self, x: u16) { + unsafe { *((0x0400_0010 + 4 * self.background as usize) as *mut u16) = x } + } + fn set_y(&mut self, y: u16) { + unsafe { *((0x0400_0012 + 4 * self.background as usize) as *mut u16) = y } + } + + pub fn draw_full_map(&mut self) { + let x_map_space = self.pos_x / 8; + let y_map_space = self.pos_y / 8; + + let x_block_space = x_map_space % 32; + let y_block_space = y_map_space % 32; + + for x in -1..31 { + for y in -1..21 { + unsafe { + (&mut (*MAP)[self.block as usize][(y_block_space + y).rem_euclid(32) as usize] + [(x_block_space + x).rem_euclid(32) as usize] + as *mut u16) + .write_volatile(self.map_get(x_map_space + x, y_map_space + y, 0)) + }; + } + } + } + + pub fn set_position(&mut self, x: i32, y: i32) { + let x_map_space = x / 8; + let y_map_space = y / 8; + + let prev_x_map_space = self.pos_x / 8; + let prev_y_map_space = self.pos_y / 8; + + let x_difference = x_map_space - prev_x_map_space; + let y_difference = y_map_space - prev_y_map_space; + + let x_block_space = x_map_space % 32; + let y_block_space = y_map_space % 32; + + self.pos_x = x; + self.pos_y = y; + + // don't fancily handle if we've moved more than one tile, just copy the whole new map + if x_difference.abs() > 1 || y_difference.abs() > 1 { + self.draw_full_map(); + } else { + if x_difference != 0 { + let x_offset = match x_difference { + -1 => -1, + 1 => 30, + _ => unreachable!(), + }; + for y in -1..21 { + unsafe { + (&mut (*MAP)[self.block as usize] + [(y_block_space + y).rem_euclid(32) as usize] + [(x_block_space + x_offset).rem_euclid(32) as usize] + as *mut u16) + .write_volatile(self.map_get( + x_map_space + x_offset, + y_map_space + y, + 0, + )) + }; + } + } + if y_difference != 0 { + let y_offset = match y_difference { + -1 => -1, + 1 => 20, + _ => unreachable!(), + }; + for x in -1..31 { + unsafe { + (&mut (*MAP)[self.block as usize] + [(y_block_space + y_offset).rem_euclid(32) as usize] + [(x_block_space + x).rem_euclid(32) as usize] + as *mut u16) + .write_volatile(self.map_get( + x_map_space + x, + y_map_space + y_offset, + 0, + )) + }; + } + } + } + + let x_remainder = x % (32 * 8); + let y_remainder = y % (32 * 8); + + self.set_x(x_remainder as u16); + self.set_y(y_remainder as u16); } } #[non_exhaustive] pub struct Tiled0 { - pub background_0: Background, - pub background_1: Background, - pub background_2: Background, - pub background_3: Background, + used_blocks: u32, + num_backgrounds: u8, pub object: ObjectControl, } @@ -110,25 +243,20 @@ impl Tiled0 { set_graphics_settings(GraphicsSettings::empty()); set_graphics_mode(DisplayMode::Tiled0); Tiled0 { - background_0: Background { layer: 0 }, - background_1: Background { layer: 1 }, - background_2: Background { layer: 2 }, - background_3: Background { layer: 3 }, + used_blocks: 0, + num_backgrounds: 0, object: ObjectControl::new(), } } - pub fn set_sprite_palette_entry(&mut self, index: u8, colour: u16) { + fn set_sprite_palette_entry(&mut self, index: u8, colour: u16) { PALETTE_SPRITE.set(index as usize, colour) } - pub fn set_background_palette_entry(&mut self, index: u8, colour: u16) { - PALETTE_BACKGROUND.set(index as usize, colour) - } - pub fn set_sprite_tilemap_entry(&mut self, index: u32, data: u32) { + fn set_sprite_tilemap_entry(&mut self, index: u32, data: u32) { TILE_SPRITE.set(index as usize, data); } - pub fn set_background_tilemap_entry(&mut self, index: u32, data: u32) { + fn set_background_tilemap_entry(&mut self, index: u32, data: u32) { TILE_BACKGROUND.set(index as usize, data); } @@ -138,20 +266,15 @@ impl Tiled0 { } } - pub fn set_background_palette(&mut self, colour: &[u16]) { - for (index, &entry) in colour.iter().enumerate() { - self.set_background_palette_entry(index.try_into().unwrap(), entry) + fn set_background_palette(&mut self, pal_index: u8, palette: &palette16::Palette16) { + for (colour_index, &colour) in palette.colours.iter().enumerate() { + PALETTE_BACKGROUND.set(pal_index as usize * 16 + colour_index, colour); } } pub fn set_background_palettes(&mut self, palettes: &[palette16::Palette16]) { for (palette_index, entry) in palettes.iter().enumerate() { - for (colour_index, &colour) in entry.colours.iter().enumerate() { - self.set_background_palette_entry( - (palette_index * 16 + colour_index) as u8, - colour, - ); - } + self.set_background_palette(palette_index as u8, entry) } } @@ -161,17 +284,56 @@ impl Tiled0 { } } - pub fn set_background_tilemap(&mut self, tiles: &[u32]) { - for (index, &tile) in tiles.iter().enumerate() { - self.set_background_tilemap_entry(index as u32, tile) + pub fn get_background(&mut self) -> Result { + if self.num_backgrounds >= 4 { + return Err("too many backgrounds created, maximum is 4"); } + + if !self.used_blocks == 0 { + return Err("all blocks are used"); + } + + let mut availiable_block = u8::MAX; + + for i in 0..32 { + if (1 << i) & self.used_blocks == 0 { + availiable_block = i; + break; + } + } + + assert!( + availiable_block != u8::MAX, + "should be able to find a block" + ); + + self.used_blocks |= 1 << availiable_block; + + let background = self.num_backgrounds; + self.num_backgrounds = background + 1; + Ok(unsafe { Background::new(background, availiable_block) }) } - pub fn copy_to_map(&mut self, map_id: usize, entries: &[u16]) { - let map = - unsafe { &mut ((*(MAP as *mut [[u16; 32 * 32]; 32]))[map_id]) as *mut [u16; 32 * 32] }; - for (index, &entry) in entries.iter().enumerate() { - unsafe { (&mut (*map)[index] as *mut u16).write_volatile(entry) } + pub fn set_background_tilemap(&mut self, start_tile: u32, tiles: &[u32]) { + let u32_per_block = 512; + + let start_block = (start_tile * 8) / u32_per_block; + // round up rather than down + let end_block = (start_tile * 8 + tiles.len() as u32 + u32_per_block - 1) / u32_per_block; + + let blocks_to_use: u32 = (1 << (end_block - start_block)) - 1 << start_block; + + assert!( + self.used_blocks & blocks_to_use == 0, + "blocks {} to {} should be unused for this copy to succeed", + start_block, + end_block + ); + + self.used_blocks |= blocks_to_use; + + for (index, &tile) in tiles.iter().enumerate() { + self.set_background_tilemap_entry(start_tile + index as u32, tile) } } }