Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Working sfx example + sample loading
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Sep 22, 2023
1 parent 153bf2f commit 6b8fe57
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Binary file added assets/bell-sfx.ogg
Binary file not shown.
13 changes: 13 additions & 0 deletions comfy-core/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
{
Expand Down
24 changes: 24 additions & 0 deletions comfy/examples/sound.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
4 changes: 4 additions & 0 deletions comfy/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandBuffer> {
self.commands.borrow_mut()
}
Expand Down
19 changes: 4 additions & 15 deletions comfy/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
1 change: 0 additions & 1 deletion comfy/src/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ pub struct Particle {

impl Particle {
pub fn initialize(&mut self, position: Vec2, size: Option<Vec2>) {
// self.position = random_box(position, size.unwrap_or(Vec2::ZERO));
if let Some(size) = size {
self.position = random_box(position, size);
} else {
Expand Down
2 changes: 1 addition & 1 deletion games/bitmob/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 6b8fe57

Please sign in to comment.