use qilin::audio::{AudioManager, Panning};
use qilin::game::context::GameContext;
use qilin::game::game::Game;
use qilin::render::canvas::Canvas;
use qilin::scene::Scene;
use qilin::types::{GameConfig, TimeStamp, FPS60};
use qilin::Key;
use qilin::ScaleMode;
use qilin::WindowOptions;
use std::ops::Not;
use std::time::Duration;
struct AudioScene {
manager: AudioManager,
}
impl Scene for AudioScene {
fn new() -> Self
where
Self: Sized,
{
Self {
manager: AudioManager::new().unwrap(),
}
}
fn enter(&mut self) {
self.manager
.load("examples/assets/glados.wav", 1.0, Panning::Normal, false)
.unwrap();
println!("Press 'p' to play sound.");
println!("Press 'i' to increase volume.");
println!("Press 'd' to decrease volume.");
println!("Press 'r' to pan sound to the right.");
println!("Press 'l' to pan sound to the left.");
println!("Press 'e' to toggle reverse.");
}
fn update(&mut self, _canvas: &mut Canvas, ctx: &mut GameContext) {
if ctx.is_key_released(Key::P) {
self.manager.play(0).unwrap();
}
if ctx.is_key_released(Key::I) {
self.manager
.set_volume(0, self.manager.get_volume(0).unwrap() + 1.0);
}
if ctx.is_key_released(Key::D) {
self.manager
.set_volume(0, self.manager.get_volume(0).unwrap() - 1.0);
}
if ctx.is_key_released(Key::R) {
self.manager.set_panning(0, Panning::HardRight).unwrap();
}
if ctx.is_key_released(Key::L) {
self.manager.set_panning(0, Panning::HardLeft).unwrap();
}
if ctx.is_key_released(Key::E) {
self.manager
.set_reverse(0, self.manager.get_reverse(0).unwrap().not());
}
}
fn fixed_update(&mut self, _canvas: &mut Canvas, _ctx: &mut GameContext) {}
fn exit(&mut self) { println!("Exiting!") }
}
fn main() {
Game::new::<AudioScene>() .with_config(GameConfig {
title: "Audio".to_string(), update_rate_limit: FPS60, width: 800, height: 600, fixed_time_step: TimeStamp(Duration::from_secs_f32(1.0 / 120.0)), window: WindowOptions {
scale_mode: ScaleMode::AspectRatioStretch, resize: true, ..Default::default()
},
})
.play()
.expect("Failed to play game");
}