diff --git a/CHANGELOG.md b/CHANGELOG.md index ab37a025..095a6e51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Added `find_colour_index_16` and `find_colour_index_256` to the `VRamManager` to find where a colour is in a palette. + ## [0.20.2] - 2024/05/25 ### Fixed diff --git a/agb/examples/dma_effect_background_colour.rs b/agb/examples/dma_effect_background_colour.rs index 6efc4ed9..f7386c59 100644 --- a/agb/examples/dma_effect_background_colour.rs +++ b/agb/examples/dma_effect_background_colour.rs @@ -25,24 +25,25 @@ fn main(mut gba: agb::Gba) -> ! { let dma = gba.dma.dma().dma0; - example_logo::display_logo(&mut map, &mut vram); + example_logo::display_logo_basic(&mut map, &mut vram); let vblank = VBlank::get(); let colours: Box<[_]> = (0..160).map(|i| ((i * 0xffff) / 160) as u16).collect(); - let mut frame = 0; + let background_colour = 0x732b; // generated using `https://agbrs.dev/colour` + let background_colour_index = vram + .find_colour_index_16(0, background_colour) + .expect("Should contain colour 0x732b"); loop { - // hardcoding palette index 2 here which you wouldn't want to do in a real example (instead, look for - // the colour you want to replace) - let _background_color_transfer = - unsafe { dma.hblank_transfer(&vram.background_palette_colour_dma(0, 2), &colours) }; + let _background_color_transfer = unsafe { + dma.hblank_transfer( + &vram.background_palette_colour_dma(0, background_colour_index), + &colours, + ) + }; vblank.wait_for_vblank(); - frame += 1; - if frame > 160 { - frame = 0; - } } } diff --git a/agb/gfx/test_logo.aseprite b/agb/gfx/test_logo.aseprite new file mode 100644 index 00000000..46ec959e Binary files /dev/null and b/agb/gfx/test_logo.aseprite differ diff --git a/agb/gfx/test_logo.png b/agb/gfx/test_logo.png index e0c3f5ab..cb10d46e 100644 Binary files a/agb/gfx/test_logo.png and b/agb/gfx/test_logo.png differ diff --git a/agb/gfx/test_logo_basic.png b/agb/gfx/test_logo_basic.png new file mode 100644 index 00000000..97b139de Binary files /dev/null and b/agb/gfx/test_logo_basic.png differ diff --git a/agb/src/display/example_logo.rs b/agb/src/display/example_logo.rs index 66d98442..4b6295f2 100644 --- a/agb/src/display/example_logo.rs +++ b/agb/src/display/example_logo.rs @@ -1,6 +1,7 @@ use super::tiled::{RegularMap, TiledMap, VRamManager}; crate::include_background_gfx!(crate, agb_logo, test_logo => deduplicate "gfx/test_logo.png"); +crate::include_background_gfx!(crate, agb_logo_basic, test_logo => deduplicate "gfx/test_logo_basic.png"); pub fn display_logo(map: &mut RegularMap, vram: &mut VRamManager) { vram.set_background_palettes(agb_logo::PALETTES); @@ -11,6 +12,15 @@ pub fn display_logo(map: &mut RegularMap, vram: &mut VRamManager) { map.set_visible(true); } +pub fn display_logo_basic(map: &mut RegularMap, vram: &mut VRamManager) { + vram.set_background_palettes(agb_logo_basic::PALETTES); + + map.fill_with(vram, &agb_logo_basic::test_logo); + + map.commit(vram); + map.set_visible(true); +} + #[cfg(test)] mod tests { use crate::display::{tiled::RegularBackgroundSize, Priority}; diff --git a/agb/src/display/tiled/vram_manager.rs b/agb/src/display/tiled/vram_manager.rs index 55503332..4339d56b 100644 --- a/agb/src/display/tiled/vram_manager.rs +++ b/agb/src/display/tiled/vram_manager.rs @@ -461,4 +461,18 @@ impl VRamManager { self.set_background_palette(palette_index as u8, entry); } } + + /// Gets the index of the colour for a given background palette, or None if it doesn't exist + #[must_use] + pub fn find_colour_index_16(&self, palette_index: usize, colour: u16) -> Option { + assert!(palette_index < 16); + + (0..16).find(|i| PALETTE_BACKGROUND.get(palette_index * 16 + i) == colour) + } + + /// Gets the index of the colour in the entire background palette, or None if it doesn't exist + #[must_use] + pub fn find_colour_index_256(&self, colour: u16) -> Option { + (0..256).find(|&i| PALETTE_BACKGROUND.get(i) == colour) + } }