Music: Defer probing for music driver until a music set is loaded

This commit is contained in:
Jonathan G Rennison
2023-08-17 20:21:22 +01:00
parent 8681f29155
commit 129691dcc7
4 changed files with 48 additions and 1 deletions

View File

@@ -12,6 +12,14 @@
#include "../driver.h" #include "../driver.h"
#include <memory>
#include <mutex>
#if defined(__MINGW32__)
#include "../3rdparty/mingw-std-threads/mingw.mutex.h"
#endif
extern std::mutex _music_driver_mutex;
struct MusicSongInfo; struct MusicSongInfo;
/** Driver for all music playback. */ /** Driver for all music playback. */
@@ -46,10 +54,20 @@ public:
*/ */
virtual bool IsInFailedState() { return false; } virtual bool IsInFailedState() { return false; }
static std::unique_ptr<MusicDriver> ExtractDriver()
{
Driver **dptr = DriverFactoryBase::GetActiveDriver(Driver::DT_MUSIC);
Driver *driver = *dptr;
*dptr = nullptr;
return std::unique_ptr<MusicDriver>(static_cast<MusicDriver*>(driver));
}
/** /**
* Get the currently active instance of the music driver. * Get the currently active instance of the music driver.
*/ */
static MusicDriver *GetInstance() { static MusicDriver *GetInstance() {
std::unique_lock<std::mutex> lock(_music_driver_mutex);
return static_cast<MusicDriver*>(*DriverFactoryBase::GetActiveDriver(Driver::DT_MUSIC)); return static_cast<MusicDriver*>(*DriverFactoryBase::GetActiveDriver(Driver::DT_MUSIC));
} }
}; };

View File

@@ -174,6 +174,7 @@ void MusicSystem::ChangePlaylist(PlaylistChoices pl)
*/ */
void MusicSystem::ChangeMusicSet(const std::string &set_name) void MusicSystem::ChangeMusicSet(const std::string &set_name)
{ {
if (set_name != "NoMusic") InitMusicDriver(true);
BaseMusic::SetSet(set_name); BaseMusic::SetSet(set_name);
BaseMusic::ini_set = set_name; BaseMusic::ini_set = set_name;

View File

@@ -97,6 +97,7 @@
#include "3rdparty/cpp-btree/btree_set.h" #include "3rdparty/cpp-btree/btree_set.h"
#include <atomic>
#include <mutex> #include <mutex>
#if defined(__MINGW32__) #if defined(__MINGW32__)
#include "3rdparty/mingw-std-threads/mingw.mutex.h" #include "3rdparty/mingw-std-threads/mingw.mutex.h"
@@ -135,6 +136,10 @@ NewGRFScanCallback *_request_newgrf_scan_callback = nullptr;
SimpleChecksum64 _state_checksum; SimpleChecksum64 _state_checksum;
std::mutex _music_driver_mutex;
static std::string _music_driver_params;
static std::atomic<bool> _music_inited;
/** /**
* Error handling for fatal user errors. * Error handling for fatal user errors.
* @param s the string to print. * @param s the string to print.
@@ -1010,7 +1015,13 @@ int openttd_main(int argc, char *argv[])
DriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND); DriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND);
if (musicdriver.empty() && !_ini_musicdriver.empty()) musicdriver = _ini_musicdriver; if (musicdriver.empty() && !_ini_musicdriver.empty()) musicdriver = _ini_musicdriver;
DriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC); _music_driver_params = std::move(musicdriver);
if (_music_driver_params.empty() && BaseMusic::GetUsedSet()->name == "NoMusic") {
DEBUG(driver, 1, "Deferring loading of music driver until a music set is loaded");
DriverFactoryBase::SelectDriver("null", Driver::DT_MUSIC);
} else {
InitMusicDriver(false);
}
GenerateWorld(GWM_EMPTY, 64, 64); // Make the viewport initialization happy GenerateWorld(GWM_EMPTY, 64, 64); // Make the viewport initialization happy
LoadIntroGame(false); LoadIntroGame(false);
@@ -1041,6 +1052,22 @@ int openttd_main(int argc, char *argv[])
return ret; return ret;
} }
void InitMusicDriver(bool init_volume)
{
if (_music_inited.exchange(true)) return;
{
std::unique_lock<std::mutex> lock(_music_driver_mutex);
static std::unique_ptr<MusicDriver> old_driver;
old_driver = std::move(MusicDriver::ExtractDriver());
DriverFactoryBase::SelectDriver(_music_driver_params, Driver::DT_MUSIC);
}
if (init_volume) MusicDriver::GetInstance()->SetVolume(_settings_client.music.music_vol);
}
void HandleExitGameRequest() void HandleExitGameRequest()
{ {
if (_game_mode == GM_MENU || _game_mode == GM_BOOTSTRAP) { // do not ask to quit on the main screen if (_game_mode == GM_MENU || _game_mode == GM_BOOTSTRAP) { // do not ask to quit on the main screen

View File

@@ -91,6 +91,7 @@ void AskExitToGameMenu();
int openttd_main(int argc, char *argv[]); int openttd_main(int argc, char *argv[]);
void StateGameLoop(); void StateGameLoop();
void HandleExitGameRequest(); void HandleExitGameRequest();
void InitMusicDriver(bool init_volume);
void SwitchToMode(SwitchMode new_mode); void SwitchToMode(SwitchMode new_mode);