Persistent game volume settings

This commit is contained in:
Evan Pratten 2022-04-03 11:35:11 -04:00
parent 0cc0401b66
commit 49c8222e26
5 changed files with 31 additions and 7 deletions

View File

@ -33,14 +33,14 @@ extern crate approx; // For the macro `relative_eq!`
extern crate log; // For the `info!`, `warn!`, etc. macros extern crate log; // For the `info!`, `warn!`, etc. macros
pub(crate) mod asset_manager; pub(crate) mod asset_manager;
pub(crate) mod coord_convert;
pub(crate) mod discord; pub(crate) mod discord;
pub(crate) mod global_resource_package; pub(crate) mod global_resource_package;
pub(crate) mod model;
pub(crate) mod persistent; pub(crate) mod persistent;
pub(crate) mod project_constants; pub(crate) mod project_constants;
pub(crate) mod rendering; pub(crate) mod rendering;
pub(crate) mod scenes; pub(crate) mod scenes;
pub(crate) mod model;
pub(crate) mod coord_convert;
/// This is the game logic entrypoint. Despite being async, /// This is the game logic entrypoint. Despite being async,
/// this is expected to block the main thread for rendering and stuff. /// this is expected to block the main thread for rendering and stuff.
@ -84,6 +84,8 @@ pub async fn entrypoint(force_recreate_savefiles: bool) {
project_constants.target_fps, project_constants.target_fps,
&project_constants, &project_constants,
event_loop_discord_tx, event_loop_discord_tx,
&mut settings,
&mut save_state,
) )
.await; .await;

View File

@ -10,12 +10,15 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistentGameSettings { pub struct PersistentGameSettings {
// TODO: Add data here. // TODO: Add data here.
pub volume: Option<f32>,
} }
// Add any default values here. // Add any default values here.
impl Default for PersistentGameSettings { impl Default for PersistentGameSettings {
fn default() -> Self { fn default() -> Self {
Self {} Self {
volume: Some(0.5),
}
} }
} }

View File

@ -10,6 +10,8 @@
use std::cell::RefCell; use std::cell::RefCell;
use crate::discord::DiscordChannel; use crate::discord::DiscordChannel;
use crate::persistent::save_state::GameSaveState;
use crate::persistent::settings::PersistentGameSettings;
use crate::project_constants::ProjectConstants; use crate::project_constants::ProjectConstants;
use crate::rendering::core_renderer_sm::{PreloadState, RenderBackendStates}; use crate::rendering::core_renderer_sm::{PreloadState, RenderBackendStates};
use crate::rendering::screens::sm_failure_screen; use crate::rendering::screens::sm_failure_screen;
@ -25,6 +27,8 @@ pub async fn handle_graphics_blocking<ConfigBuilder>(
target_frames_per_second: u32, target_frames_per_second: u32,
constants: &ProjectConstants, constants: &ProjectConstants,
discord_signaling: DiscordChannel, discord_signaling: DiscordChannel,
game_settings: &mut PersistentGameSettings,
save_state: &mut GameSaveState
) where ) where
ConfigBuilder: FnOnce(&mut RaylibBuilder), ConfigBuilder: FnOnce(&mut RaylibBuilder),
{ {
@ -58,6 +62,8 @@ pub async fn handle_graphics_blocking<ConfigBuilder>(
&raylib_thread, &raylib_thread,
constants, constants,
audio_subsystem, audio_subsystem,
game_settings,
save_state,
); );
// Handle loading the resources and rendering the loading screen // Handle loading the resources and rendering the loading screen
@ -119,6 +125,8 @@ pub async fn handle_graphics_blocking<ConfigBuilder>(
&discord_signaling, &discord_signaling,
&global_resources, &global_resources,
constants, constants,
game_settings,
save_state,
) )
.await; .await;

View File

@ -10,7 +10,7 @@ use raylib::{
use crate::{ use crate::{
discord::{DiscordChannel, DiscordRpcSignal}, discord::{DiscordChannel, DiscordRpcSignal},
global_resource_package::GlobalResources, global_resource_package::GlobalResources,
project_constants::ProjectConstants, project_constants::ProjectConstants, persistent::settings::PersistentGameSettings,
}; };
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -35,10 +35,11 @@ impl MainMenu {
raylib_handle: &mut RaylibHandle, raylib_handle: &mut RaylibHandle,
thread: &RaylibThread, thread: &RaylibThread,
constants: &ProjectConstants, constants: &ProjectConstants,
game_settings: &mut PersistentGameSettings
) -> Self { ) -> Self {
Self { Self {
has_updated_discord_rpc: false, has_updated_discord_rpc: false,
volume_percentage: 0.5, volume_percentage: game_settings.volume.unwrap_or(0.5),
} }
} }
@ -50,6 +51,7 @@ impl MainMenu {
global_resources: &GlobalResources, global_resources: &GlobalResources,
constants: &ProjectConstants, constants: &ProjectConstants,
audio_subsystem: &mut RaylibAudio, audio_subsystem: &mut RaylibAudio,
game_settings: &mut PersistentGameSettings
) -> MenuStateSignal { ) -> MenuStateSignal {
// Handle updating discord RPC // Handle updating discord RPC
if !self.has_updated_discord_rpc { if !self.has_updated_discord_rpc {
@ -296,6 +298,8 @@ impl MainMenu {
} else if self.volume_percentage <= 0.0 { } else if self.volume_percentage <= 0.0 {
self.volume_percentage = 0.0; self.volume_percentage = 0.0;
} }
audio_subsystem.set_master_volume(self.volume_percentage);
game_settings.volume = Some(self.volume_percentage);
} }
} }
@ -330,6 +334,8 @@ impl MainMenu {
} else if self.volume_percentage <= 0.0 { } else if self.volume_percentage <= 0.0 {
self.volume_percentage = 0.0; self.volume_percentage = 0.0;
} }
audio_subsystem.set_master_volume(self.volume_percentage);
game_settings.volume = Some(self.volume_percentage);
} }
} }

View File

@ -7,7 +7,7 @@ use raylib::prelude::*;
use crate::{ use crate::{
discord::DiscordChannel, global_resource_package::GlobalResources, discord::DiscordChannel, global_resource_package::GlobalResources,
project_constants::ProjectConstants, project_constants::ProjectConstants, persistent::{save_state::GameSaveState, settings::PersistentGameSettings},
}; };
use self::{ use self::{
@ -38,11 +38,13 @@ impl SceneRenderDelegate {
rl_thread: &RaylibThread, rl_thread: &RaylibThread,
constants: &ProjectConstants, constants: &ProjectConstants,
audio_subsystem: RaylibAudio, audio_subsystem: RaylibAudio,
game_settings: &mut PersistentGameSettings,
save_state: &mut GameSaveState
) -> Self { ) -> Self {
// Init some scenes // Init some scenes
let scene_test_fox = TestFoxScene::new(raylib, rl_thread); let scene_test_fox = TestFoxScene::new(raylib, rl_thread);
let scene_playable = PlayableScene::new(raylib, rl_thread, constants); let scene_playable = PlayableScene::new(raylib, rl_thread, constants);
let scene_main_menu = MainMenu::new(raylib, rl_thread, constants); let scene_main_menu = MainMenu::new(raylib, rl_thread, constants, game_settings);
Self { Self {
menu_control_signal: MenuStateSignal::DoMainMenu, menu_control_signal: MenuStateSignal::DoMainMenu,
@ -64,6 +66,8 @@ impl SceneRenderDelegate {
discord: &DiscordChannel, discord: &DiscordChannel,
global_resources: &GlobalResources, global_resources: &GlobalResources,
constants: &ProjectConstants, constants: &ProjectConstants,
game_settings: &mut PersistentGameSettings,
save_state: &mut GameSaveState
) { ) {
// Render the main menu if in it, otherwise, render the game // Render the main menu if in it, otherwise, render the game
match self.menu_control_signal { match self.menu_control_signal {
@ -96,6 +100,7 @@ impl SceneRenderDelegate {
global_resources, global_resources,
constants, constants,
&mut self.audio_subsystem, &mut self.audio_subsystem,
game_settings
) )
.await; .await;