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) + } }