From d5a5b454a4670a72826882c6ce4c87d1f597767c Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Sat, 7 Dec 2024 17:25:37 -0500 Subject: Native --- src/audio.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'src/audio.rs') diff --git a/src/audio.rs b/src/audio.rs index 2b190de..5cbce90 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -1,9 +1,11 @@ use std::{cell::RefCell, collections::HashMap}; +#[cfg(target_arch = "wasm32")] pub struct Context { pub audio: web_sys::AudioContext, } +#[cfg(target_arch = "wasm32")] impl Context { pub fn new() -> Self { let audio = web_sys::AudioContext::new() @@ -14,11 +16,13 @@ impl Context { } } +#[cfg(target_arch = "wasm32")] pub struct Audio { pub buffer: &'static RefCell>, //pub source: &'static web_sys::AudioBufferSourceNode, } +#[cfg(target_arch = "wasm32")] impl Audio { pub fn new(ctx: &Context, bytes: &[u8]) -> Self { let sbuffer: &_ = Box::leak(Box::new(RefCell::new(None))); @@ -52,6 +56,7 @@ impl Audio { } } +#[cfg(target_arch = "wasm32")] pub struct Assets { pub ctx: Context, @@ -60,6 +65,7 @@ pub struct Assets { pub music_node: Option, } +#[cfg(target_arch = "wasm32")] impl Assets { pub fn new(f : F) -> Self where F: Fn(&Context) -> HashMap { let ctx = Context::new(); @@ -94,3 +100,63 @@ impl Assets { } } } + +#[cfg(not(target_arch = "wasm32"))] +pub struct Context { +} + +#[cfg(not(target_arch = "wasm32"))] +impl Context { + pub fn new() -> Self { + Self { + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +pub struct Audio { +} + +#[cfg(not(target_arch = "wasm32"))] +impl Audio { + pub fn new(ctx: &Context, _bytes: &[u8]) -> Self { + Self { + } + } + + pub fn play(&self, ctx: &Context, looping: Option<(Option, Option)>) { + } +} + +#[cfg(not(target_arch = "wasm32"))] +pub struct Assets { + pub ctx: Context, + pub audio: HashMap, +} + +#[cfg(not(target_arch = "wasm32"))] +impl Assets { + pub fn new(f : F) -> Self where F: Fn(&Context) -> HashMap { + let ctx = Context::new(); + + let audio = f(&ctx); + + Self { + ctx, + audio, + } + } + + pub fn play_sfx(&mut self, name: &str) { + if let Some(a) = self.audio.get(name) { + a.play(&self.ctx, None); + } + } + + pub fn is_music_playing(&self) -> bool { + false + } + + pub fn play_music(&mut self, name: &str, start: Option, end: Option) { + } +} -- cgit v1.2.3