Skip to content

Commit

Permalink
feat: make audio manager easier to to access and remove extra atomic …
Browse files Browse the repository at this point in the history
…cell. (#237)

We had put the audio manager in an `Arc<AtomicCell<T>>` so that it could
be `Clone`, but that was unnecessary when it is a shared resource that
will not be duplicated in network play.
  • Loading branch information
zicklag authored Oct 15, 2023
1 parent 6575222 commit 92a4c12
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
7 changes: 2 additions & 5 deletions demos/features/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ fn audio_demo_plugin(session: &mut Session) {
fn audio_demo_ui(
ctx: Res<EguiCtx>,
localization: Localization<GameMeta>,
audio: Res<AudioManager>,
mut audio: ResMut<AudioManager>,
meta: Root<GameMeta>,
assets: Res<AssetServer>,
) {
Expand All @@ -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();
}
})
});
Expand Down
13 changes: 6 additions & 7 deletions framework_crates/bones_framework/src/render/audio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Audio components.

use std::{io::Cursor, sync::Arc};
use std::io::Cursor;

use crate::prelude::*;

Expand All @@ -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<AtomicCell<KiraAudioManager>>);
#[derive(HasSchema, Deref, DerefMut)]
#[schema(no_clone)]
pub struct AudioManager(KiraAudioManager);
impl Default for AudioManager {
fn default() -> Self {
Self(Arc::new(AtomicCell::new(
KiraAudioManager::<CpalBackend>::new(default()).unwrap(),
)))
Self(KiraAudioManager::<CpalBackend>::new(default()).unwrap())
}
}

/// The audio source asset type, contains no data, but [`Handle<AudioSource>`] 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);
Expand Down

0 comments on commit 92a4c12

Please sign in to comment.