Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/beep.ogg
Binary file not shown.
7 changes: 7 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::path::PathBuf;
pub struct Configuration {
pub vm: VmOptions,
pub debug: DebugOptions,
pub sound: SoundOptions,
}

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

#[derive(Clone, Deserialize, Serialize)]
pub struct SoundOptions {
pub volume: f32,
}

impl Default for Configuration {
fn default() -> Self {
Self {
vm: VmOptions { cycles_per_frame: 10 },
debug: DebugOptions { enable_debug_menu: false },
sound: SoundOptions { volume: 0.85 },
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/ui/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ui::state::State;
use notan::egui;
use notan::egui::Window;
use notan::egui::{Slider, Window};

pub fn option_dialog(state: &mut State, ctx: &egui::Context) {
Window::new("Options")
Expand All @@ -24,6 +24,14 @@ pub fn option_dialog(state: &mut State, ctx: &egui::Context) {
}
});

ui.horizontal(|ui| {
ui.label("Sound volume");
ui.add(Slider::new(
&mut state.configuration.sound.volume,
0.0..=1.0,
));
});

ui.horizontal(|ui| {
ui.checkbox(
&mut state.configuration.debug.enable_debug_menu,
Expand Down
32 changes: 29 additions & 3 deletions src/ui/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct State {
pub timer: f32,
pub show_configuration_window: bool,
pub cycles_per_frame: u32,
sound: Option<Sound>,
beep_audio_source: AudioSource,
is_beeping: bool,
}

#[derive(Parser)]
Expand All @@ -32,7 +35,7 @@ struct Args {
file: OsString,
}

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

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

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

let audio_source = app
.audio
.create_source(include_bytes!("../../assets/beep.ogg"))
.expect("Failed to create audio source");

State {
last_dir,
configuration: config.clone(),
Expand All @@ -92,6 +100,9 @@ pub fn setup(gfx: &mut Graphics) -> State {
timer: 0.0,
show_configuration_window: false,
cycles_per_frame: config.vm.cycles_per_frame,
sound: None,
beep_audio_source: audio_source,
is_beeping: false,
}
}

Expand Down Expand Up @@ -123,9 +134,24 @@ pub fn update(app: &mut App, state: &mut State) {
if state.vm.delay_timer != 0 {
state.vm.delay_timer -= 1;
}
if state.vm.sound_timer != 0 {
// TODO: play sound here

if state.vm.sound_timer > 0 {
if !state.is_beeping {
state.sound = Some(app.audio.play_sound(
&state.beep_audio_source,
state.configuration.sound.volume,
true,
));

state.is_beeping = true;
}

state.vm.sound_timer -= 1;
} else if state.is_beeping {
if let Some(sound) = &state.sound {
app.audio.stop(sound);
state.is_beeping = false;
}
}
}

Expand Down