From bf4a6887a5846685b3520fd19d7622b922cd507b Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Sat, 25 May 2024 11:11:12 +0100 Subject: [PATCH] Update the background example to search for the colour --- agb/examples/dma_effect_background_colour.rs | 19 ++++++++++--------- agb/src/display/tiled/vram_manager.rs | 7 +++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/agb/examples/dma_effect_background_colour.rs b/agb/examples/dma_effect_background_colour.rs index 6efc4ed9..57c969e9 100644 --- a/agb/examples/dma_effect_background_colour.rs +++ b/agb/examples/dma_effect_background_colour.rs @@ -31,18 +31,19 @@ fn main(mut gba: agb::Gba) -> ! { 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/src/display/tiled/vram_manager.rs b/agb/src/display/tiled/vram_manager.rs index 55503332..2a482a69 100644 --- a/agb/src/display/tiled/vram_manager.rs +++ b/agb/src/display/tiled/vram_manager.rs @@ -461,4 +461,11 @@ 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 + pub fn find_colour_index_16(&mut self, palette_index: usize, colour: u16) -> Option { + assert!(palette_index < 16); + + (0..16).find(|i| PALETTE_BACKGROUND.get(palette_index * 16 + i) == colour) + } }