From d95a154a59b8ae0ff30bd8c48448a1dd66e1c0e4 Mon Sep 17 00:00:00 2001 From: Tommy Date: Mon, 11 Jul 2022 20:48:58 +1200 Subject: [PATCH] Do not block everything when fading music in and out. Fixes #4460. Also fixes the seconds-long UI block at the start of a scenario and when beginning story view at the start of a campaign. And thus, also fixes the new visual fade-in effect which was completely broken by music fade-in blocking everything. Note that this fix does cause fades to happen in a non-blocking manner. Old music will fade out in the background, then the new music will fade in once the old music has finished fading out. --- src/sound.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/sound.cpp b/src/sound.cpp index e037312a590..6531a399636 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -672,6 +672,14 @@ static void play_new_music() fading_time = 0; } + // Halt any existing music. + // If we don't do this SDL_Mixer blocks everything until fade out is complete. + // Do not remove this without ensuring that it does not block. + // If you don't want it to halt the music, ensure that fades are completed + // before attempting to play new music. + Mix_HaltMusic(); + + // Fade in the new music const int res = Mix_FadeInMusic(itor->second.get(), 1, fading_time); if(res < 0) { ERR_AUDIO << "Could not play music: " << Mix_GetError() << " " << filename << " " << std::endl; @@ -767,6 +775,11 @@ void play_music_config(const config& music_node, bool allow_interrupt_current_tr void music_thinker::process(events::pump_info& info) { + if(Mix_FadingMusic() != MIX_NO_FADING) { + // Do not block everything while fading. + return; + } + if(preferences::music_on()) { if(!music_start_time && !current_track_list.empty() && !Mix_PlayingMusic()) { // Pick next track, add ending time to its start time. @@ -784,6 +797,7 @@ void music_thinker::process(events::pump_info& info) if(want_new_music) { if(Mix_PlayingMusic()) { Mix_FadeOutMusic(fadingout_time); + return; } unload_music = false;