diff --git a/Cargo.toml b/Cargo.toml index 2724111..4132153 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ comet_ecs = { path = "./crates/comet_ecs", workspace = true } comet_input = { path = "./crates/comet_input", workspace = true } comet_log = { path = "./crates/comet_log", workspace = true } comet_structs = { path = "./crates/comet_structs", workspace = true } +comet_sound = { path = "./crates/comet_sound", workspace = true } cfg-if = "1" anyhow = "1.0" @@ -62,4 +63,5 @@ comet_resources = { path = "./crates/comet_resources", workspace = true } comet_ecs = { path = "./crates/comet_ecs", workspace = true } comet_input = { path = "./crates/comet_input", workspace = true } comet_log = { path = "./crates/comet_log", workspace = true } -comet_structs = { path = "./crates/comet_structs", workspace = true } \ No newline at end of file +comet_structs = { path = "./crates/comet_structs", workspace = true } +comet_sound = { path = "./crates/comet_sound", workspace = true } diff --git a/crates/comet_app/src/app.rs b/crates/comet_app/src/app.rs index b998b7b..977f8ec 100755 --- a/crates/comet_app/src/app.rs +++ b/crates/comet_app/src/app.rs @@ -111,6 +111,11 @@ impl App { self } + pub fn with_audio(mut self, audio_system: Box) -> Self { + self.audio = audio_system; + self + } + fn load_icon(path: &std::path::Path) -> Option { let image = match image::open(path) { Ok(image) => image, @@ -387,8 +392,7 @@ impl App { match renderer.render() { Ok(_) => {} Err( - wgpu::SurfaceError::Lost - | wgpu::SurfaceError::Outdated, + wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated, ) => { let size = renderer.size(); renderer.resize(size); diff --git a/examples/hello_sound.rs b/examples/hello_sound.rs new file mode 100644 index 0000000..fa4d6a4 --- /dev/null +++ b/examples/hello_sound.rs @@ -0,0 +1,26 @@ +use comet::prelude::*; +use comet_sound::Audio; + +fn setup(app: &mut App, _renderer: &mut Renderer2D) { + app.load_audio("startup", "res/sounds/hit.ogg"); + app.play_audio("startup", true); // second parameter loops the sound + + // here the float indicated the volume percentage + // in the standard backend for audio in comet (kira) 0.0 is equal to -20dB and 1.0 to 0dB + app.set_volume("startup", 1.0); +} + +#[allow(unused_variables)] +fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) { + // in this example, update_audio doesnt do anything because the kira audio system + // doesnt need any update + app.update_audio(dt); +} + +fn main() { + App::new() + .with_title("Comet Audio Example") + // This can be used to add your own sound engine to the engine + .with_audio(Box::new(comet_sound::KiraAudio::new())) // to + .run::(setup, update); +} diff --git a/res/sounds/hit.ogg b/res/sounds/hit.ogg new file mode 100644 index 0000000..a5cd995 Binary files /dev/null and b/res/sounds/hit.ogg differ