agb/tracker/agb-tracker/examples/basic.rs

46 lines
1.3 KiB
Rust
Raw Normal View History

2023-07-13 00:38:09 +10:00
#![no_std]
#![no_main]
use agb::sound::mixer::Frequency;
use agb::Gba;
2023-07-13 02:36:41 +10:00
use agb_tracker::{import_xm, Track, Tracker};
2023-07-13 00:38:09 +10:00
2023-07-13 08:41:30 +10:00
const DB_TOFFE: Track = import_xm!("examples/db_toffe.xm");
2023-07-13 00:38:09 +10:00
#[agb::entry]
fn main(mut gba: Gba) -> ! {
let vblank_provider = agb::interrupt::VBlank::get();
let timer_controller = gba.timers.timers();
let mut timer = timer_controller.timer2;
let mut timer2 = timer_controller.timer3;
timer.set_enabled(true);
timer2.set_cascade(true).set_enabled(true);
2023-07-13 00:38:09 +10:00
let mut mixer = gba.mixer.mixer(Frequency::Hz18157);
mixer.enable();
2023-07-13 08:41:30 +10:00
let mut tracker = Tracker::new(&DB_TOFFE);
2023-07-13 02:36:41 +10:00
2023-07-13 00:38:09 +10:00
loop {
let before_mixing_cycles_high = timer2.value();
let before_mixing_cycles_low = timer.value();
2023-07-13 02:36:41 +10:00
tracker.step(&mut mixer);
2023-07-13 00:38:09 +10:00
mixer.frame();
let after_mixing_cycles_low = timer.value();
let after_mixing_cycles_high = timer2.value();
2023-07-13 00:38:09 +10:00
vblank_provider.wait_for_vblank();
let before_mixing_cycles =
((before_mixing_cycles_high as u32) << 16) + before_mixing_cycles_low as u32;
let after_mixing_cycles =
((after_mixing_cycles_high as u32) << 16) + after_mixing_cycles_low as u32;
let total_cycles = after_mixing_cycles.wrapping_sub(before_mixing_cycles);
agb::println!("{total_cycles} cycles");
2023-07-13 00:38:09 +10:00
}
}