diff --git a/Makefile b/Makefile index e58036a..202450b 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ # EXAMPLE=animated_shapes # EXAMPLE=animated_text # EXAMPLE=sprite -EXAMPLE=text +# EXAMPLE=text # EXAMPLE=particles # EXAMPLE=post_processing # EXAMPLE=shapes +EXAMPLE=sound # EXAMPLE=ecs_sprite # EXAMPLE=ecs_topdown_game # EXAMPLE=particle_systems diff --git a/README.md b/README.md index e622fac..0574cbe 100644 --- a/README.md +++ b/README.md @@ -338,7 +338,7 @@ comfy is free and open source and dual licensed under MIT and Apache 2.0 license - [x] generate wasm examples - [x] generate webms for website - [x] simple sprite -- [ ] simple text +- [x] simple text - [x] simple shapes - [x] simple particles - [ ] lighting example @@ -349,7 +349,7 @@ comfy is free and open source and dual licensed under MIT and Apache 2.0 license - [ ] blood canvas - [ ] raytracing - [ ] physics -- [ ] SFX +- [x] SFX - [ ] looped music + filter + egui - [ ] postprocessing - [x] top-down minigame diff --git a/assets/bell-sfx.ogg b/assets/bell-sfx.ogg new file mode 100644 index 0000000..fd95b79 Binary files /dev/null and b/assets/bell-sfx.ogg differ diff --git a/comfy-core/src/assets.rs b/comfy-core/src/assets.rs index eb589d8..4f74d15 100644 --- a/comfy-core/src/assets.rs +++ b/comfy-core/src/assets.rs @@ -172,6 +172,19 @@ impl Assets { } } + pub fn load_sound_from_bytes(&mut self, name: &str, bytes: &[u8]) { + let handle = Sound::from_path(name); + + let data = StaticSoundData::from_cursor( + std::io::Cursor::new(bytes.to_vec()), + Default::default(), + ) + .unwrap(); + + self.sound_ids.insert(name.to_string(), handle); + self.sounds.lock().insert(handle, data); + } + pub fn process_load_queue(&mut self) { let _span = span!("process_load_queue"); { diff --git a/comfy/examples/sound.rs b/comfy/examples/sound.rs new file mode 100644 index 0000000..ad933ac --- /dev/null +++ b/comfy/examples/sound.rs @@ -0,0 +1,24 @@ +use comfy::*; + +simple_game!("Sound Example", setup, update); + +fn setup(c: &mut EngineContext) { + c.load_sound_from_bytes( + // Every sound gets a string name later used to reference it. + "comfy-bell", + include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../assets/bell-sfx.ogg" + )), + ); +} + +fn update(_c: &mut EngineContext) { + let color = if is_key_down(KeyCode::Space) { RED } else { WHITE }; + + draw_text("Press SPACE to play SFX", Vec2::ZERO, color, TextAlign::Center); + + if is_key_pressed(KeyCode::Space) { + play_sound("comfy-bell"); + } +} diff --git a/comfy/src/context.rs b/comfy/src/context.rs index d23dd34..35edda2 100644 --- a/comfy/src/context.rs +++ b/comfy/src/context.rs @@ -93,6 +93,10 @@ impl<'a> EngineContext<'a> { ); } + pub fn load_sound_from_bytes(&self, name: &str, bytes: &[u8]) { + ASSETS.borrow_mut().load_sound_from_bytes(name, bytes); + } + pub fn commands(&self) -> core::cell::RefMut { self.commands.borrow_mut() } diff --git a/comfy/src/engine.rs b/comfy/src/engine.rs index 6e70f5d..527e94d 100644 --- a/comfy/src/engine.rs +++ b/comfy/src/engine.rs @@ -393,21 +393,10 @@ impl EngineState { srand(thread_rng().next_u64()); set_main_camera_zoom(30.0); - { - let mut assets = ASSETS.borrow_mut(); - - let handle = Sound::from_path("handle"); - let bytes = include_bytes!("../../assets/error.ogg"); - - let data = StaticSoundData::from_cursor( - std::io::Cursor::new(bytes), - Default::default(), - ) - .unwrap(); - - assets.sound_ids.insert("error".to_string(), handle); - assets.sounds.lock().insert(handle, data); - } + ASSETS.borrow_mut().load_sound_from_bytes( + "error", + include_bytes!("../../assets/error.ogg"), + ); Self { cached_loader: RefCell::new(CachedImageLoader::new()), diff --git a/comfy/src/particles.rs b/comfy/src/particles.rs index 2390892..6911d32 100644 --- a/comfy/src/particles.rs +++ b/comfy/src/particles.rs @@ -247,7 +247,6 @@ pub struct Particle { impl Particle { pub fn initialize(&mut self, position: Vec2, size: Option) { - // self.position = random_box(position, size.unwrap_or(Vec2::ZERO)); if let Some(size) = size { self.position = random_box(position, size); } else { diff --git a/games/bitmob/src/main.rs b/games/bitmob/src/main.rs index c6ac9e0..bc4695c 100644 --- a/games/bitmob/src/main.rs +++ b/games/bitmob/src/main.rs @@ -2,6 +2,6 @@ use comfy::*; simple_game!("BITMOB", update); -fn update(c: &EngineContext) { +fn update(_c: &EngineContext) { draw_circle(Vec2::ZERO, 2.0, PINK, 0); }