Update the background example to search for the colour

This commit is contained in:
Gwilym Inzani 2024-05-25 11:11:12 +01:00
parent b219e00454
commit bf4a6887a5
2 changed files with 17 additions and 9 deletions

View file

@ -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;
}
}
}

View file

@ -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<usize> {
assert!(palette_index < 16);
(0..16).find(|i| PALETTE_BACKGROUND.get(palette_index * 16 + i) == colour)
}
}