From 43b8c2c793ff2469f63f975289259b39144f0d3f Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Sat, 12 Dec 2020 10:26:37 -0600 Subject: [PATCH] Add support for global effects. --- examples/game.rs | 17 +++++++++++++++-- src/lib.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/examples/game.rs b/examples/game.rs index 1b8f5fb..380f2a7 100644 --- a/examples/game.rs +++ b/examples/game.rs @@ -1,5 +1,5 @@ use bevy::{asset::LoadState, prelude::*}; -use bevy_openal::{Listener, OpenAlPlugin, Sound, Sounds}; +use bevy_openal::{efx, Context, GlobalEffects, Listener, OpenAlPlugin, Sound, Sounds}; #[derive(Default)] struct AssetHandles { @@ -7,8 +7,20 @@ struct AssetHandles { loaded: bool, } -fn setup(asset_server: Res, mut handles: ResMut) { +fn setup( + asset_server: Res, + mut handles: ResMut, + context: ResMut, + mut global_effects: ResMut, +) { handles.sounds = asset_server.load_folder(".").expect("Failed to load sfx"); + if let Ok(mut slot) = context.new_aux_effect_slot() { + if let Ok(mut reverb) = context.new_effect::() { + reverb.set_preset(&efx::REVERB_PRESET_GENERIC).unwrap(); + slot.set_effect(&reverb).unwrap(); + global_effects.push(slot); + } + } } fn load_and_create_system( @@ -32,6 +44,7 @@ fn load_and_create_system( Sound { buffer, autoplay: true, + gain: 0.4, looping: true, ..Default::default() }, diff --git a/src/lib.rs b/src/lib.rs index cf0c1e3..8c9ac96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,14 @@ use std::collections::HashMap; use std::io::Cursor; +use std::ops::{Deref, DerefMut}; use std::sync::Arc; +pub use alto::efx; +use alto::efx::AuxEffectSlot; +pub use alto::Context; +pub use alto::Device; pub use alto::Source; -use alto::{Alto, Context, Mono, StaticSource, Stereo}; +use alto::{Alto, Mono, StaticSource, Stereo}; use bevy::{ asset::{AssetLoader, HandleId, LoadContext, LoadedAsset}, prelude::*, @@ -140,9 +145,27 @@ impl Default for Sound { } pub type Sounds = HashMap; +#[derive(Default)] +pub struct GlobalEffects(Vec); + +impl Deref for GlobalEffects { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for GlobalEffects { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + fn source_system( context: Res, buffers: Res, + mut global_effects: ResMut, mut query: Query<(&mut Sounds, Option<&Transform>)>, ) { for (mut sounds, transform) in query.iter_mut() { @@ -175,6 +198,9 @@ fn source_system( source.set_relative(true); source.set_position([0., 0., 0.]).unwrap(); } + for (send, effect) in global_effects.iter_mut().enumerate() { + source.set_aux_send(send as i32, effect).unwrap(); + } } } } @@ -218,8 +244,10 @@ impl Plugin for OpenAlPlugin { let context = device.new_context(None).expect("Could not create context"); app.add_asset::() .init_asset_loader::() + .add_thread_local_resource(device) .add_resource(context) .add_resource(Buffers::default()) + .add_resource(GlobalEffects::default()) .register_type::() .add_system(buffer_creation_system) .add_system(source_system)