From 93d82f309e4024d4e017c48ed66540735a0f6ed6 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Mon, 17 Jan 2022 23:11:14 +0000 Subject: [PATCH] Test logo now displays --- agb/src/display/background.rs | 52 ++++++++++++++++++++++++--------- agb/src/display/example_logo.rs | 34 ++++++++++++--------- agb/src/display/mod.rs | 1 + agb/src/display/video.rs | 8 ++--- 4 files changed, 65 insertions(+), 30 deletions(-) diff --git a/agb/src/display/background.rs b/agb/src/display/background.rs index 6c00ded4..5537793f 100644 --- a/agb/src/display/background.rs +++ b/agb/src/display/background.rs @@ -3,7 +3,10 @@ use hashbrown::HashMap; use crate::memory_mapped::{MemoryMapped, MemoryMapped1DArray}; -use super::{set_graphics_mode, set_graphics_settings, DisplayMode, GraphicsSettings}; +use super::{ + palette16, set_graphics_mode, set_graphics_settings, DisplayMode, GraphicsSettings, Priority, + DISPLAY_CONTROL, +}; const TILE_BACKGROUND: MemoryMapped1DArray = unsafe { MemoryMapped1DArray::new(0x06000000) }; @@ -26,10 +29,16 @@ impl TileFormat { } pub struct TileSet<'a> { - tiles: &'a [u8], + tiles: &'a [u32], format: TileFormat, } +impl<'a> TileSet<'a> { + pub fn new(tiles: &'a [u32], format: TileFormat) -> Self { + Self { tiles, format } + } +} + #[derive(Clone, Copy, PartialEq, Eq)] pub struct TileSetReference { id: u16, @@ -145,20 +154,16 @@ impl<'a> VRamManager<'a> { "Stale tile data requested" ); - let tile_offset = (tile as usize) * data.format.tile_size(); - &data.tiles[tile_offset..(tile_offset + data.format.tile_size())] + let tile_offset = (tile as usize) * data.format.tile_size() / 4; + &data.tiles[tile_offset..(tile_offset + data.format.tile_size() / 4)] } else { panic!("Cannot find tile data at given reference"); }; let tile_size_in_words = TileFormat::FourBpp.tile_size() / 4; - unsafe { - let (_, tile_data, _) = tile_slice.align_to::(); - - for (i, &word) in tile_data.iter().enumerate() { - TILE_BACKGROUND.set(index_to_copy_into * tile_size_in_words + i, word); - } + for (i, &word) in tile_slice.iter().enumerate() { + TILE_BACKGROUND.set(index_to_copy_into * tile_size_in_words + i, word); } TileIndex(index_to_copy_into as u16) @@ -187,6 +192,19 @@ impl<'a> VRamManager<'a> { PALETTE_BACKGROUND.set(index, colour); } } + + 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); + } + } + + /// Copies palettes to the background palettes without any checks. + pub fn set_background_palettes(&mut self, palettes: &[palette16::Palette16]) { + for (palette_index, entry) in palettes.iter().enumerate() { + self.set_background_palette(palette_index as u8, entry) + } + } } #[derive(Clone, Copy, Debug)] @@ -205,7 +223,7 @@ pub struct RegularMap { screenblock: u8, x_scroll: u16, y_scroll: u16, - priority: u8, + priority: Priority, tiles: [Tile; 32 * 32], tiles_dirty: bool, @@ -219,7 +237,7 @@ impl RegularMap { screenblock, x_scroll: 0, y_scroll: 0, - priority: 0, + priority: Priority::P0, tiles: [Tile(0); 32 * 32], tiles_dirty: true, @@ -231,6 +249,12 @@ impl RegularMap { self.tiles_dirty = true; } + pub fn show(&mut self) { + let mode = DISPLAY_CONTROL.get(); + let new_mode = mode | (1 << (self.background_id + 0x08)); + DISPLAY_CONTROL.set(new_mode); + } + pub fn commit(&mut self) { let new_bg_control_value = (self.priority as u16) | ((self.screenblock as u16) << 8); @@ -246,6 +270,8 @@ impl RegularMap { for (i, tile) in self.tiles.iter().enumerate() { screenblock_memory.set(i, tile.0); } + + self.tiles_dirty = false; } const fn bg_control_register(&self) -> MemoryMapped { @@ -261,7 +287,7 @@ impl RegularMap { } const fn screenblock_memory(&self) -> MemoryMapped1DArray { - unsafe { MemoryMapped1DArray::new(0x0600_0000 + 0x1000 * self.screenblock as usize) } + unsafe { MemoryMapped1DArray::new(0x0600_0000 + 0x1000 * self.screenblock as usize / 2) } } } diff --git a/agb/src/display/example_logo.rs b/agb/src/display/example_logo.rs index 47166642..0f65a178 100644 --- a/agb/src/display/example_logo.rs +++ b/agb/src/display/example_logo.rs @@ -1,23 +1,31 @@ -/*use crate::display::background::BackgroundDistributor; +use crate::display::background::Tiled0; + +use super::background::{Tile, TileFormat, TileSet}; crate::include_gfx!("gfx/agb_logo.toml"); -pub fn display_logo(gfx: &mut BackgroundDistributor) { - use super::background::Map; - gfx.set_background_palettes(agb_logo::test_logo.palettes); - gfx.set_background_tilemap(0, agb_logo::test_logo.tiles); +pub fn display_logo(gfx: &mut Tiled0) { + gfx.vram + .set_background_palettes(agb_logo::test_logo.palettes); - let mut back = gfx.get_regular().unwrap(); + let background_tilemap = TileSet::new(agb_logo::test_logo.tiles, TileFormat::FourBpp); + let background_tilemap_reference = gfx.vram.add_tileset(background_tilemap); - let mut entries: [u16; 30 * 20] = [0; 30 * 20]; - for tile_id in 0..(30 * 20) { - let palette_entry = agb_logo::test_logo.palette_assignments[tile_id as usize] as u16; - entries[tile_id as usize] = tile_id | (palette_entry << 12); + let mut back = gfx.background(); + + for y in 0..20 { + for x in 0..30 { + let tile_id = y * 30 + x; + + let palette_entry = agb_logo::test_logo.palette_assignments[tile_id as usize] as u16; + let tile = gfx.vram.add_tile(background_tilemap_reference, tile_id); + + back.set_tile(x, y, Tile::new(tile, false, false, palette_entry)) + } } - back.set_map(Map::new(&entries, (30_u32, 20_u32).into(), 0)); - back.show(); back.commit(); + back.show(); } #[cfg(test)] mod tests { @@ -31,4 +39,4 @@ mod tests { crate::test_runner::assert_image_output("gfx/test_logo.png"); } -}*/ +} diff --git a/agb/src/display/mod.rs b/agb/src/display/mod.rs index e4ce5206..e16ec663 100644 --- a/agb/src/display/mod.rs +++ b/agb/src/display/mod.rs @@ -109,6 +109,7 @@ pub fn busy_wait_for_vblank() { while VCOUNT.get() < 160 {} } +#[derive(Clone, Copy, PartialEq, Eq)] pub enum Priority { P0 = 0, P1 = 1, diff --git a/agb/src/display/video.rs b/agb/src/display/video.rs index a8d16a33..d4f61270 100644 --- a/agb/src/display/video.rs +++ b/agb/src/display/video.rs @@ -1,4 +1,4 @@ -use super::{bitmap3::Bitmap3, bitmap4::Bitmap4}; +use super::{background::Tiled0, bitmap3::Bitmap3, bitmap4::Bitmap4}; #[non_exhaustive] pub struct Video {} @@ -14,7 +14,7 @@ impl Video { unsafe { Bitmap4::new() } } - // pub fn tiled0(&mut self) -> BackgroundDistributor { - // unsafe { BackgroundDistributor::new() } - // } + pub fn tiled0(&mut self) -> Tiled0 { + unsafe { Tiled0::new() } + } }