Skip to content

Commit e2f9a1a

Browse files
committed
Implement sound system
1 parent 02827c5 commit e2f9a1a

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

assets/beep.ogg

6.34 KB
Binary file not shown.

src/config/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::path::PathBuf;
1111
pub struct Configuration {
1212
pub vm: VmOptions,
1313
pub debug: DebugOptions,
14+
pub sound: SoundOptions,
1415
}
1516

1617
#[derive(Clone, Deserialize, Serialize)]
@@ -23,11 +24,17 @@ pub struct DebugOptions {
2324
pub enable_debug_menu: bool,
2425
}
2526

27+
#[derive(Clone, Deserialize, Serialize)]
28+
pub struct SoundOptions {
29+
pub volume: f32,
30+
}
31+
2632
impl Default for Configuration {
2733
fn default() -> Self {
2834
Self {
2935
vm: VmOptions { cycles_per_frame: 10 },
3036
debug: DebugOptions { enable_debug_menu: false },
37+
sound: SoundOptions { volume: 1.0 },
3138
}
3239
}
3340
}

src/ui/options.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::ui::state::State;
22
use notan::egui;
3-
use notan::egui::Window;
3+
use notan::egui::{Slider, Window};
44

55
pub fn option_dialog(state: &mut State, ctx: &egui::Context) {
66
Window::new("Options")
@@ -24,6 +24,14 @@ pub fn option_dialog(state: &mut State, ctx: &egui::Context) {
2424
}
2525
});
2626

27+
ui.horizontal(|ui| {
28+
ui.label("Sound volume");
29+
ui.add(Slider::new(
30+
&mut state.configuration.sound.volume,
31+
0.0..=1.0,
32+
));
33+
});
34+
2735
ui.horizontal(|ui| {
2836
ui.checkbox(
2937
&mut state.configuration.debug.enable_debug_menu,

src/ui/state.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pub struct State {
2424
pub timer: f32,
2525
pub show_configuration_window: bool,
2626
pub cycles_per_frame: u32,
27+
beep_audio_source: AudioSource,
28+
sound: Option<Sound>,
29+
is_beeping: bool,
2730
}
2831

2932
#[derive(Parser)]
@@ -32,7 +35,7 @@ struct Args {
3235
file: OsString,
3336
}
3437

35-
pub fn setup(gfx: &mut Graphics) -> State {
38+
pub fn setup(app: &mut App, gfx: &mut Graphics) -> State {
3639
let args = Args::parse();
3740

3841
let file_path = if args.file.is_empty() {
@@ -78,6 +81,11 @@ pub fn setup(gfx: &mut Graphics) -> State {
7881

7982
let config = Configuration::load().unwrap();
8083

84+
let audio_source = app
85+
.audio
86+
.create_source(include_bytes!("../../assets/beep.ogg"))
87+
.expect("Failed to create audio source");
88+
8189
State {
8290
last_dir,
8391
configuration: config.clone(),
@@ -92,6 +100,9 @@ pub fn setup(gfx: &mut Graphics) -> State {
92100
timer: 0.0,
93101
show_configuration_window: false,
94102
cycles_per_frame: config.vm.cycles_per_frame,
103+
beep_audio_source: audio_source,
104+
sound: None,
105+
is_beeping: false,
95106
}
96107
}
97108

@@ -123,9 +134,21 @@ pub fn update(app: &mut App, state: &mut State) {
123134
if state.vm.delay_timer != 0 {
124135
state.vm.delay_timer -= 1;
125136
}
126-
if state.vm.sound_timer != 0 {
127-
// TODO: play sound here
137+
138+
if state.vm.sound_timer > 0 {
139+
if !state.is_beeping {
140+
state.sound = Option::from(app.audio.play_sound(
141+
&state.beep_audio_source,
142+
state.configuration.sound.volume,
143+
true,
144+
));
145+
state.is_beeping = true;
146+
}
147+
128148
state.vm.sound_timer -= 1;
149+
} else if state.is_beeping {
150+
app.audio.stop(&state.sound.clone().unwrap());
151+
state.is_beeping = false;
129152
}
130153
}
131154

0 commit comments

Comments
 (0)