From 60cb5a829b54732176936e21511aa5372ae46fab Mon Sep 17 00:00:00 2001 From: untodesu Date: Thu, 27 Mar 2025 13:28:09 +0500 Subject: Allow game to not die when sound is not available --- game/client/factory.cc | 5 +++- game/client/game.cc | 22 +++++++++++------ game/client/gamepad.cc | 4 +-- game/client/main.cc | 60 ++++++++++++++++++++++++++++----------------- game/client/sound_effect.cc | 7 ++++++ 5 files changed, 65 insertions(+), 33 deletions(-) diff --git a/game/client/factory.cc b/game/client/factory.cc index 636cf35..5eca00c 100644 --- a/game/client/factory.cc +++ b/game/client/factory.cc @@ -6,6 +6,7 @@ #include "shared/head.hh" #include "shared/transform.hh" +#include "client/globals.hh" #include "client/sound_emitter.hh" void client_factory::create_player(Dimension *dimension, entt::entity entity) @@ -20,5 +21,7 @@ void client_factory::create_player(Dimension *dimension, entt::entity entity) dimension->entities.emplace_or_replace(entity, transform); dimension->entities.emplace_or_replace(entity, transform); - dimension->entities.emplace_or_replace(entity); + if(globals::sound_ctx) { + dimension->entities.emplace_or_replace(entity); + } } diff --git a/game/client/game.cc b/game/client/game.cc index af6fba6..c1d3fa8 100644 --- a/game/client/game.cc +++ b/game/client/game.cc @@ -342,7 +342,9 @@ void client_game::init(void) globals::gui_scale = 0U; globals::gui_screen = GUI_MAIN_MENU; - sound::init(); + if(globals::sound_ctx) { + sound::init(); + } client_receive::init(); @@ -356,7 +358,9 @@ void client_game::init_late(void) { toggles::init_late(); - sound::init_late(); + if(globals::sound_ctx) { + sound::init_late(); + } language::init_late(); @@ -418,7 +422,9 @@ void client_game::deinit(void) session::deinit(); - sound::deinit(); + if(globals::sound_ctx) { + sound::deinit(); + } hotbar::deinit(); main_menu::deinit(); @@ -506,9 +512,13 @@ void client_game::update(void) else globals::dimension->entities.emplace_or_replace(globals::player); } - sound::update(); + if(globals::sound_ctx) { + sound::update(); - listener::update(); + listener::update(); + + SoundEmitterComponent::update(); + } interpolation::update(); @@ -516,8 +526,6 @@ void client_game::update(void) camera::update(); - SoundEmitterComponent::update(); - voxel_anims::update(); chunk_mesher::update(); diff --git a/game/client/gamepad.cc b/game/client/gamepad.cc index 7c03d29..9321d08 100644 --- a/game/client/gamepad.cc +++ b/game/client/gamepad.cc @@ -49,7 +49,7 @@ static void on_glfw_joystick_event(const GlfwJoystickEvent &event) for(int i = 0; i < NUM_AXES; gamepad::last_state.axes[i++] = 0.0f); for(int i = 0; i < NUM_BUTTONS; gamepad::last_state.buttons[i++] = GLFW_RELEASE); - spdlog::warn("gamepad: detected gamepad: {}", glfwGetGamepadName(event.joystick_id)); + spdlog::info("gamepad: detected gamepad: {}", glfwGetGamepadName(event.joystick_id)); return; } @@ -100,7 +100,7 @@ void gamepad::init(void) for(int i = 0; i < NUM_AXES; gamepad::last_state.axes[i++] = 0.0f); for(int i = 0; i < NUM_BUTTONS; gamepad::last_state.buttons[i++] = GLFW_RELEASE); - spdlog::warn("gamepad: detected gamepad: {}", glfwGetGamepadName(joystick)); + spdlog::info("gamepad: detected gamepad: {}", glfwGetGamepadName(joystick)); break; } diff --git a/game/client/main.cc b/game/client/main.cc index 86e9783..e7fdc3b 100644 --- a/game/client/main.cc +++ b/game/client/main.cc @@ -252,29 +252,41 @@ int main(int argc, char **argv) glfwSetWindowIcon(globals::window, 1, &icon_image); } - if(!saladLoadALdefault()) { - spdlog::critical("salad: failed to load function pointers"); - std::terminate(); - } - - globals::sound_dev = alcOpenDevice(nullptr); - - if(globals::sound_dev == nullptr) { - spdlog::critical("openal: alcOpenDevice failed"); - std::terminate(); + if(cmdline::contains("nosound")) { + spdlog::warn("client: sound disabled [per command line]"); + globals::sound_dev = nullptr; + globals::sound_ctx = nullptr; } - - spdlog::info("sound: {}", reinterpret_cast(alcGetString(globals::sound_dev, ALC_DEVICE_SPECIFIER))); - - globals::sound_ctx = alcCreateContext(globals::sound_dev, nullptr); - - if(globals::sound_ctx == nullptr) { - spdlog::critical("openal: alcCreateContext failed"); - std::terminate(); + else { + if(!saladLoadALdefault()) { + spdlog::warn("client: sound disabled [openal loading failed]"); + globals::sound_dev = nullptr; + globals::sound_ctx = nullptr; + } + else { + globals::sound_dev = alcOpenDevice(nullptr); + + if(globals::sound_dev == nullptr) { + spdlog::warn("client: sound disabled [no device]"); + globals::sound_ctx = nullptr; + } + else { + spdlog::info("sound: {}", reinterpret_cast(alcGetString(globals::sound_dev, ALC_DEVICE_SPECIFIER))); + + globals::sound_ctx = alcCreateContext(globals::sound_dev, nullptr); + + if(globals::sound_ctx == nullptr) { + spdlog::warn("client: sound disabled [context creation failed]"); + alcCloseDevice(globals::sound_dev); + globals::sound_dev = nullptr; + } + else { + alcMakeContextCurrent(globals::sound_ctx); + } + } + } } - alcMakeContextCurrent(globals::sound_ctx); - splash::init_client(); window_title::update(); @@ -417,9 +429,11 @@ int main(int argc, char **argv) ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); - alcMakeContextCurrent(nullptr); - alcDestroyContext(globals::sound_ctx); - alcCloseDevice(globals::sound_dev); + if(globals::sound_ctx){ + alcMakeContextCurrent(nullptr); + alcDestroyContext(globals::sound_ctx); + alcCloseDevice(globals::sound_dev); + } glfwDestroyWindow(globals::window); glfwTerminate(); diff --git a/game/client/sound_effect.cc b/game/client/sound_effect.cc index c6cee72..4e01e8c 100644 --- a/game/client/sound_effect.cc +++ b/game/client/sound_effect.cc @@ -3,6 +3,8 @@ #include "core/resource.hh" +#include "client/globals.hh" + static emhash8::HashMap> resource_map; static std::size_t drwav_read_physfs(void *file, void *output, std::size_t count) @@ -27,6 +29,11 @@ resource_ptr resource::load(const char *name, unsigned return it->second; } + if(globals::sound_ctx == nullptr) { + // Sound is disabled + return nullptr; + } + auto file = PHYSFS_openRead(name); if(file == nullptr) { -- cgit