From 1dd13da37db32972810b942966d5cdd233215f92 Mon Sep 17 00:00:00 2001 From: Desour Date: Mon, 10 Apr 2023 18:59:01 +0200 Subject: [PATCH] Get rid of global dummySoundManager There is no need for this to be globally unique. --- src/client/CMakeLists.txt | 2 +- src/client/game.cpp | 49 +++++++++++++++++++++------------------ src/client/sound.cpp | 23 ------------------ src/client/sound.h | 3 --- 4 files changed, 27 insertions(+), 50 deletions(-) delete mode 100644 src/client/sound.cpp diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 7aa43d198..7b910d027 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -1,4 +1,4 @@ -set(sound_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/sound.cpp) +set(sound_SRCS "") if(USE_SOUND) set(sound_SRCS ${sound_SRCS} diff --git a/src/client/game.cpp b/src/client/game.cpp index 426fbc453..ac87fc9c7 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -936,8 +936,7 @@ private: NodeDefManager *nodedef_manager = nullptr; GameOnDemandSoundFetcher soundfetcher; // useful when testing - ISoundManager *sound = nullptr; - bool sound_is_dummy = false; + std::unique_ptr sound_manager; SoundMaker *soundmaker = nullptr; ChatBackend *chat_backend = nullptr; @@ -1074,8 +1073,7 @@ Game::~Game() { delete client; delete soundmaker; - if (!sound_is_dummy) - delete sound; + sound_manager.reset(); delete server; // deleted first to stop all server threads @@ -1380,20 +1378,19 @@ bool Game::initSound() #if USE_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) + sound_manager.reset(createOpenALSoundManager(g_sound_manager_singleton.get(), &soundfetcher)); + if (!sound_manager) infostream << "Failed to initialize OpenAL audio" << std::endl; } else infostream << "Sound disabled." << std::endl; #endif - if (!sound) { + if (!sound_manager) { infostream << "Using dummy audio." << std::endl; - sound = &dummySoundManager; - sound_is_dummy = true; + sound_manager = std::make_unique(); } - soundmaker = new SoundMaker(sound, nodedef_manager); + soundmaker = new SoundMaker(sound_manager.get(), nodedef_manager); if (!soundmaker) return false; @@ -1609,7 +1606,7 @@ bool Game::connectToServer(const GameStartData &start_data, client = new Client(start_data.name.c_str(), start_data.password, start_data.address, *draw_control, texture_src, shader_src, - itemdef_manager, nodedef_manager, sound, eventmgr, + itemdef_manager, nodedef_manager, sound_manager.get(), eventmgr, m_rendering_engine, connect_address.isIPv6(), m_game_ui.get(), start_data.allow_login_or_register); client->migrateModStorage(); @@ -2211,7 +2208,8 @@ void Game::openInventory() TextDest *txt_dst = new TextDestPlayerInventory(client); auto *&formspec = m_game_ui->updateFormspec(""); GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(), - &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound); + &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), + sound_manager.get()); formspec->setFormSpec(fs_src->getForm(), inventoryloc); } @@ -2816,7 +2814,8 @@ void Game::handleClientEvent_ShowFormSpec(ClientEvent *event, CameraOrientation auto *&formspec = m_game_ui->updateFormspec(*(event->show_formspec.formname)); GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(), - &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound); + &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), + sound_manager.get()); } delete event->show_formspec.formspec; @@ -2829,7 +2828,7 @@ void Game::handleClientEvent_ShowLocalFormSpec(ClientEvent *event, CameraOrienta LocalFormspecHandler *txt_dst = new LocalFormspecHandler(*event->show_formspec.formname, client); GUIFormSpecMenu::create(m_game_ui->getFormspecGUI(), client, m_rendering_engine->get_gui_env(), - &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound); + &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound_manager.get()); delete event->show_formspec.formspec; delete event->show_formspec.formname; @@ -3164,19 +3163,20 @@ void Game::updateSound(f32 dtime) { // Update sound listener v3s16 camera_offset = camera->getOffset(); - sound->updateListener(camera->getCameraNode()->getPosition() + intToFloat(camera_offset, BS), - v3f(0, 0, 0), // velocity - camera->getDirection(), - camera->getCameraNode()->getUpVector()); + sound_manager->updateListener( + camera->getCameraNode()->getPosition() + intToFloat(camera_offset, BS), + v3f(0, 0, 0), // velocity + camera->getDirection(), + camera->getCameraNode()->getUpVector()); bool mute_sound = g_settings->getBool("mute_sound"); if (mute_sound) { - sound->setListenerGain(0.0f); + sound_manager->setListenerGain(0.0f); } else { // Check if volume is in the proper range, else fix it. float old_volume = g_settings->getFloat("sound_volume"); float new_volume = rangelim(old_volume, 0.0f, 1.0f); - sound->setListenerGain(new_volume); + sound_manager->setListenerGain(new_volume); if (old_volume != new_volume) { g_settings->setFloat("sound_volume", new_volume); @@ -3548,7 +3548,8 @@ bool Game::nodePlacement(const ItemDefinition &selected_def, auto *&formspec = m_game_ui->updateFormspec(""); GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(), - &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound); + &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), + sound_manager.get()); formspec->setFormSpec(meta->getString("formspec"), inventoryloc); return false; @@ -4324,7 +4325,8 @@ void Game::showDeathFormspec() auto *&formspec = m_game_ui->getFormspecGUI(); GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(), - &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound); + &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), + sound_manager.get()); formspec->setFocus("btn_respawn"); } @@ -4460,7 +4462,8 @@ void Game::showPauseMenu() auto *&formspec = m_game_ui->getFormspecGUI(); GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(), - &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound); + &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), + sound_manager.get()); formspec->setFocus("btn_continue"); // game will be paused in next step, if in singleplayer (see m_is_paused) formspec->doPause = true; diff --git a/src/client/sound.cpp b/src/client/sound.cpp deleted file mode 100644 index 44a96dd25..000000000 --- a/src/client/sound.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Minetest -Copyright (C) 2013 celeron55, Perttu Ahola - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ - -#include "sound.h" - -// Global DummySoundManager singleton -DummySoundManager dummySoundManager; diff --git a/src/client/sound.h b/src/client/sound.h index 213d20831..4de63ec65 100644 --- a/src/client/sound.h +++ b/src/client/sound.h @@ -88,6 +88,3 @@ public: void step(float dtime) {} void fadeSound(int sound, float step, float gain) {} }; - -// Global DummySoundManager singleton -extern DummySoundManager dummySoundManager;