From 92a4c1216225f01e2242bf76e918c41e35193a8f Mon Sep 17 00:00:00 2001 From: Zicklag Date: Sun, 15 Oct 2023 14:47:54 +0000 Subject: [PATCH] feat: make audio manager easier to to access and remove extra atomic cell. (#237) We had put the audio manager in an `Arc>` so that it could be `Clone`, but that was unnecessary when it is a shared resource that will not be duplicated in network play. --- demos/features/src/main.rs | 7 ++----- .../bones_framework/src/render/audio.rs | 13 ++++++------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/demos/features/src/main.rs b/demos/features/src/main.rs index 13a7e7c3a4..f51a653b13 100644 --- a/demos/features/src/main.rs +++ b/demos/features/src/main.rs @@ -427,7 +427,7 @@ fn audio_demo_plugin(session: &mut Session) { fn audio_demo_ui( ctx: Res, localization: Localization, - audio: Res, + mut audio: ResMut, meta: Root, assets: Res, ) { @@ -437,10 +437,7 @@ fn audio_demo_ui( ui.vertical_centered(|ui| { ui.add_space(50.0); if ui.button(localization.get("play-sound")).clicked() { - audio - .borrow_mut() - .play(&*assets.get(meta.audio_demo)) - .unwrap(); + audio.play(&*assets.get(meta.audio_demo)).unwrap(); } }) }); diff --git a/framework_crates/bones_framework/src/render/audio.rs b/framework_crates/bones_framework/src/render/audio.rs index ed5ee34e10..eb6a75dc52 100644 --- a/framework_crates/bones_framework/src/render/audio.rs +++ b/framework_crates/bones_framework/src/render/audio.rs @@ -1,6 +1,6 @@ //! Audio components. -use std::{io::Cursor, sync::Arc}; +use std::io::Cursor; use crate::prelude::*; @@ -18,19 +18,18 @@ pub fn game_plugin(game: &mut Game) { } /// The audio manager resource which can be used to play sounds. -#[derive(HasSchema, Clone, Deref, DerefMut)] -pub struct AudioManager(Arc>); +#[derive(HasSchema, Deref, DerefMut)] +#[schema(no_clone)] +pub struct AudioManager(KiraAudioManager); impl Default for AudioManager { fn default() -> Self { - Self(Arc::new(AtomicCell::new( - KiraAudioManager::::new(default()).unwrap(), - ))) + Self(KiraAudioManager::::new(default()).unwrap()) } } /// The audio source asset type, contains no data, but [`Handle`] is still useful /// because it uniquely represents a sound/music that may be played outside of bones. -#[derive(Clone, HasSchema, Debug)] +#[derive(Clone, HasSchema, Debug, Deref, DerefMut)] #[schema(no_default)] #[type_data(asset_loader(["ogg", "mp3", "flac", "wav"], AudioLoader))] pub struct AudioSource(pub StaticSoundData);