Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ambient sound not updating in game on setting options change #2030

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
2 changes: 1 addition & 1 deletion src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3331,7 +3331,7 @@ bool options_manager::save()
{
const auto savefile = PATH_INFO::options();
cache_to_globals();
update_music_volume();
update_volumes();

return write_to_file( savefile, [&]( std::ostream & fout ) {
JsonOut jout( fout, true );
Expand Down
40 changes: 39 additions & 1 deletion src/sdlsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ static bool sound_init_success = false;
static std::map<std::string, music_playlist> playlists;
static std::string current_soundpack_path;

/** The ambient sound we're currently playing **/
struct ambient_parameters {
std::string id;
std::string variant;
int volume;
sfx::channel channel;
int fade_in_duration;
double pitch;
int loops;
};
static ambient_parameters current_ambient;

static std::unordered_map<std::string, int> unique_paths;
static sfx_resources_t sfx_resources;
static std::vector<id_and_variant> sfx_preload;
Expand Down Expand Up @@ -270,7 +282,7 @@ void stop_music()
absolute_playlist_at = 0;
}

void update_music_volume()
void update_volumes()
{
if( test_mode ) {
return;
Expand All @@ -281,14 +293,33 @@ void update_music_volume()
return;
}

// Change volume of current music
Mix_VolumeMusic( current_music_track_volume * get_option<int>( "MUSIC_VOLUME" ) / 100 );


// Start playing music, if we aren't already doing so (if
// SOUND_ENABLED was toggled.)

// needs to be changed to something other than a static string when
// #28018 is resolved, as this function may be called from places
// other than the main menu.
play_music( "title" );


// Stop channels playing (different ambient sounds)
// Then start back the last saved ambient sound with the new volume (fetched in the function)
if( !current_ambient.id.empty() ) {
// Stop currently playing channels
for( int i = 0; i < static_cast<int>( sfx::channel::MAX_CHANNEL ); i++ ) {
if( sfx::is_channel_playing( static_cast<sfx::channel>( i ) ) ) {
Mix_HaltChannel( i );
}
}
// Start the last playing channel with updated volume
sfx::play_ambient_variant_sound( current_ambient.id, current_ambient.variant,
current_ambient.volume, current_ambient.channel, current_ambient.fade_in_duration,
current_ambient.pitch, current_ambient.loops );
}
}

// Allocate new Mix_Chunk as a null-chunk. Results in a valid, but empty chunk
Expand Down Expand Up @@ -605,6 +636,13 @@ void sfx::play_ambient_variant_sound( const std::string &id, const std::string &
cleanup_when_channel_finished( ch, effect_to_play );
}
}
current_ambient.id = id;
current_ambient.variant = variant;
current_ambient.volume = volume;
current_ambient.channel = channel;
current_ambient.fade_in_duration = fade_in_duration;
current_ambient.pitch = pitch;
current_ambient.loops = loops;
}

void load_soundset()
Expand Down
4 changes: 2 additions & 2 deletions src/sdlsound.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bool init_sound();
void shutdown_sound();
void play_music( const std::string &playlist );
void stop_music();
void update_music_volume();
void update_volumes();
void load_soundset();

#else
Expand All @@ -25,7 +25,7 @@ inline void shutdown_sound() { }
inline void play_music( const std::string &/*playlist*/ )
{
}
inline void update_music_volume() { }
inline void update_volumes() { }
inline void load_soundset() { }

#endif
Expand Down