agb/agb/examples/wave.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

#![no_std]
#![no_main]
2022-01-17 21:44:25 +00:00
use core::cell::RefCell;
use agb::{
display::{
example_logo,
tiled::{RegularBackgroundSize, TileFormat},
},
fixnum::FixedNum,
2022-01-17 21:44:25 +00:00
interrupt::{free, Interrupt},
};
2022-01-17 21:44:25 +00:00
use bare_metal::{CriticalSection, Mutex};
struct BackCosines {
cosines: [u16; 32],
row: usize,
}
2023-04-06 21:16:13 +01:00
static BACK: Mutex<RefCell<BackCosines>> = Mutex::new(RefCell::new(BackCosines {
cosines: [0; 32],
row: 0,
}));
#[agb::entry]
2022-01-16 21:38:30 +00:00
fn main(mut gba: agb::Gba) -> ! {
let (gfx, mut vram) = gba.display.video.tiled0();
2022-05-03 22:29:51 +01:00
let mut background = gfx.background(
agb::display::Priority::P0,
RegularBackgroundSize::Background32x32,
TileFormat::FourBpp,
2022-05-03 22:29:51 +01:00
);
2022-01-23 20:03:59 +00:00
example_logo::display_logo(&mut background, &mut vram);
2023-04-06 21:16:13 +01:00
let _a = unsafe {
agb::interrupt::add_interrupt_handler(Interrupt::HBlank, |key: CriticalSection| {
let mut back = BACK.borrow(key).borrow_mut();
let deflection = back.cosines[back.row % 32];
((0x0400_0010) as *mut u16).write_volatile(deflection);
back.row += 1;
})
};
2021-07-03 18:57:17 +01:00
let vblank = agb::interrupt::VBlank::get();
2023-04-06 21:16:13 +01:00
let mut time = 0;
loop {
vblank.wait_for_vblank();
2022-01-17 21:44:25 +00:00
free(|key| {
2023-04-06 21:16:13 +01:00
let mut back = BACK.borrow(key).borrow_mut();
2022-11-17 21:00:51 +00:00
back.row = 0;
2022-01-17 21:44:25 +00:00
time += 1;
2022-11-17 21:00:51 +00:00
for (r, a) in back.cosines.iter_mut().enumerate() {
2022-01-17 21:44:25 +00:00
let n: FixedNum<8> = (FixedNum::new(r as i32) / 32 + FixedNum::new(time) / 128)
.cos()
* (256 * 4 - 1)
/ 256;
*a = (n.trunc() % (32 * 8)) as u16;
}
})
}
}