Fix broken client if openal cannot be opened (#9804)
authorLoïc Blot <nerzhul@users.noreply.github.com>
Tue, 5 May 2020 06:38:18 +0000 (08:38 +0200)
committerGitHub <noreply@github.com>
Tue, 5 May 2020 06:38:18 +0000 (08:38 +0200)
src/client/clientlauncher.cpp
src/client/game.cpp
src/client/sound_openal.cpp
src/gui/guiEngine.cpp

index 2a9d6097f60760aac7405048644a0698bdc3a81d..f18915a55e2f9b0ee77e8f63fdcd84770cad35e3 100644 (file)
@@ -105,7 +105,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
        }
 
        RenderingEngine::get_instance()->setupTopLevelWindow(PROJECT_NAME_C);
-       
+
        /*
                This changes the minimum allowed number of vertices in a VBO.
                Default is 500.
index d1eb3bba2a5079aa209cede439e84839fc18147b..1577a37db33b911310ec3645c0aafa7b4040953c 100644 (file)
@@ -1249,7 +1249,7 @@ bool Game::init(
 bool Game::initSound()
 {
 #if USE_SOUND
-       if (g_settings->getBool("enable_sound")) {
+       if (g_settings->getBool("enable_sound") && g_sound_manager_singleton.get()) {
                infostream << "Attempting to use OpenAL audio" << std::endl;
                sound = createOpenALSoundManager(g_sound_manager_singleton.get(), &soundfetcher);
                if (!sound)
index d0f935a7afd8914a36720f2a541ffb6b8637c1f8..20a651c1d4d63b024f8ff490715b911f48f44608 100644 (file)
@@ -275,25 +275,38 @@ public:
                m_device(nullptr, delete_alcdevice),
                m_context(nullptr, delete_alccontext)
        {
-               if (!(m_device = unique_ptr_alcdevice(alcOpenDevice(nullptr), delete_alcdevice)))
-                       throw std::runtime_error("Audio: Global Initialization: Device Open");
+       }
+
+       bool init()
+       {
+               if (!(m_device = unique_ptr_alcdevice(alcOpenDevice(nullptr), delete_alcdevice))) {
+                       errorstream << "Audio: Global Initialization: Failed to open device" << std::endl;
+                       return false;
+               }
 
                if (!(m_context = unique_ptr_alccontext(
                                alcCreateContext(m_device.get(), nullptr), delete_alccontext))) {
-                       throw std::runtime_error("Audio: Global Initialization: Context Create");
+                       errorstream << "Audio: Global Initialization: Failed to create context" << std::endl;
+                       return false;
                }
 
-               if (!alcMakeContextCurrent(m_context.get()))
-                       throw std::runtime_error("Audio: Global Initialization: Context Current");
+               if (!alcMakeContextCurrent(m_context.get())) {
+                       errorstream << "Audio: Global Initialization: Failed to make current context" << std::endl;
+                       return false;
+               }
 
                alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
 
-               if (alGetError() != AL_NO_ERROR)
-                       throw std::runtime_error("Audio: Global Initialization: OpenAL Error");
+               if (alGetError() != AL_NO_ERROR) {
+                       errorstream << "Audio: Global Initialization: OpenAL Error " << alGetError() << std::endl;
+                       return false;
+               }
 
                infostream << "Audio: Global Initialized: OpenAL " << alGetString(AL_VERSION)
                        << ", using " << alcGetString(m_device.get(), ALC_DEVICE_SPECIFIER)
                        << std::endl;
+
+               return true;
        }
 
        ~SoundManagerSingleton()
@@ -682,7 +695,11 @@ public:
 
 std::shared_ptr<SoundManagerSingleton> createSoundManagerSingleton()
 {
-       return std::shared_ptr<SoundManagerSingleton>(new SoundManagerSingleton());
+       auto smg = std::make_shared<SoundManagerSingleton>();
+       if (!smg->init()) {
+               smg.reset();
+       }
+       return smg;
 }
 
 ISoundManager *createOpenALSoundManager(SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher)
index 3107d64cd08349a4afe8364280863dd79eca8293..b40707d0165a3bf8d5fcb109d760f2ad5a4733da 100644 (file)
@@ -144,10 +144,10 @@ GUIEngine::GUIEngine(JoystickController *joystick,
        //create soundmanager
        MenuMusicFetcher soundfetcher;
 #if USE_SOUND
-       if (g_settings->getBool("enable_sound"))
+       if (g_settings->getBool("enable_sound") && g_sound_manager_singleton.get())
                m_sound_manager = createOpenALSoundManager(g_sound_manager_singleton.get(), &soundfetcher);
 #endif
-       if(!m_sound_manager)
+       if (!m_sound_manager)
                m_sound_manager = &dummySoundManager;
 
        //create topleft header