From 6bb4233d5b2a1688e63c947542e92ec5d5a857a6 Mon Sep 17 00:00:00 2001 From: untodesu Date: Fri, 26 Dec 2025 22:32:38 +0500 Subject: move sound code to io --- src/game/client/CMakeLists.txt | 1 - src/game/client/entity/listener.cc | 2 +- src/game/client/entity/player_move.cc | 2 +- src/game/client/entity/sound_emitter.cc | 2 +- src/game/client/game.cc | 3 +- src/game/client/gui/chat.cc | 3 +- src/game/client/io/CMakeLists.txt | 4 +- src/game/client/io/sound.cc | 211 ++++++++++++++++++++++++++++++++ src/game/client/io/sound.hh | 43 +++++++ src/game/client/receive.cc | 2 +- src/game/client/sound/CMakeLists.txt | 3 - src/game/client/sound/sound.cc | 211 -------------------------------- src/game/client/sound/sound.hh | 43 ------- 13 files changed, 263 insertions(+), 267 deletions(-) create mode 100644 src/game/client/io/sound.cc create mode 100644 src/game/client/io/sound.hh delete mode 100644 src/game/client/sound/CMakeLists.txt delete mode 100644 src/game/client/sound/sound.cc delete mode 100644 src/game/client/sound/sound.hh (limited to 'src/game/client') diff --git a/src/game/client/CMakeLists.txt b/src/game/client/CMakeLists.txt index af30497..c4c6453 100644 --- a/src/game/client/CMakeLists.txt +++ b/src/game/client/CMakeLists.txt @@ -30,7 +30,6 @@ add_subdirectory(entity) add_subdirectory(gui) add_subdirectory(io) add_subdirectory(resource) -add_subdirectory(sound) add_subdirectory(world) if(WIN32 AND MSVC) diff --git a/src/game/client/entity/listener.cc b/src/game/client/entity/listener.cc index 991f014..af574ae 100644 --- a/src/game/client/entity/listener.cc +++ b/src/game/client/entity/listener.cc @@ -12,7 +12,7 @@ #include "client/entity/camera.hh" -#include "client/sound/sound.hh" +#include "client/io/sound.hh" #include "client/const.hh" #include "client/globals.hh" diff --git a/src/game/client/entity/player_move.cc b/src/game/client/entity/player_move.cc index edf9150..fe4e757 100644 --- a/src/game/client/entity/player_move.cc +++ b/src/game/client/entity/player_move.cc @@ -27,7 +27,7 @@ #include "client/io/gamepad.hh" -#include "client/sound/sound.hh" +#include "client/io/sound.hh" #include "client/world/voxel_sounds.hh" diff --git a/src/game/client/entity/sound_emitter.cc b/src/game/client/entity/sound_emitter.cc index 79b5ab0..b0dbc6d 100644 --- a/src/game/client/entity/sound_emitter.cc +++ b/src/game/client/entity/sound_emitter.cc @@ -15,7 +15,7 @@ #include "client/entity/camera.hh" -#include "client/sound/sound.hh" +#include "client/io/sound.hh" #include "client/globals.hh" diff --git a/src/game/client/game.cc b/src/game/client/game.cc index 9ade30f..6219ff9 100644 --- a/src/game/client/game.cc +++ b/src/game/client/game.cc @@ -63,11 +63,10 @@ #include "client/io/gamepad.hh" #include "client/io/glfw.hh" +#include "client/io/sound.hh" #include "client/resource/texture_gui.hh" -#include "client/sound/sound.hh" - #include "client/world/chunk_mesher.hh" #include "client/world/chunk_renderer.hh" #include "client/world/chunk_visibility.hh" diff --git a/src/game/client/gui/chat.cc b/src/game/client/gui/chat.cc index 76260af..f962e5d 100644 --- a/src/game/client/gui/chat.cc +++ b/src/game/client/gui/chat.cc @@ -21,11 +21,10 @@ #include "client/gui/settings.hh" #include "client/io/glfw.hh" +#include "client/io/sound.hh" #include "client/resource/sound_effect.hh" -#include "client/sound/sound.hh" - #include "client/game.hh" #include "client/globals.hh" #include "client/session.hh" diff --git a/src/game/client/io/CMakeLists.txt b/src/game/client/io/CMakeLists.txt index 82bc422..a68f137 100644 --- a/src/game/client/io/CMakeLists.txt +++ b/src/game/client/io/CMakeLists.txt @@ -1,4 +1,6 @@ target_sources(vclient PRIVATE "${CMAKE_CURRENT_LIST_DIR}/gamepad.cc" "${CMAKE_CURRENT_LIST_DIR}/gamepad.hh" - "${CMAKE_CURRENT_LIST_DIR}/glfw.hh") + "${CMAKE_CURRENT_LIST_DIR}/glfw.hh" + "${CMAKE_CURRENT_LIST_DIR}/sound.cc" + "${CMAKE_CURRENT_LIST_DIR}/sound.hh") diff --git a/src/game/client/io/sound.cc b/src/game/client/io/sound.cc new file mode 100644 index 0000000..e67c456 --- /dev/null +++ b/src/game/client/io/sound.cc @@ -0,0 +1,211 @@ +#include "client/pch.hh" + +#include "client/io/sound.hh" + +#include "core/config/number.hh" + +#include "core/io/config_map.hh" + +#include "core/math/constexpr.hh" + +#include "core/resource/resource.hh" + +#include "shared/world/dimension.hh" + +#include "shared/coord.hh" +#include "shared/protocol.hh" + +#include "client/entity/camera.hh" +#include "client/entity/sound_emitter.hh" + +#include "client/gui/settings.hh" + +#include "client/resource/sound_effect.hh" + +#include "client/const.hh" +#include "client/globals.hh" +#include "client/session.hh" + +config::Float sound::volume_master(100.0f, 0.0f, 100.0f); +config::Float sound::volume_effects(100.0f, 0.0f, 100.0f); +config::Float sound::volume_music(100.0f, 0.0f, 100.0f); +config::Float sound::volume_ui(100.0f, 0.0f, 100.0f); + +static ALuint generic_source; +static ALuint player_source; +static ALuint ui_source; + +static resource_ptr sfx_generic; +static resource_ptr sfx_player; +static resource_ptr sfx_ui; + +void sound::init_config(void) +{ + globals::client_config.add_value("sound.volume_master", sound::volume_master); + globals::client_config.add_value("sound.volume_effects", sound::volume_effects); + globals::client_config.add_value("sound.volume_music", sound::volume_music); + globals::client_config.add_value("sound.volume_ui", sound::volume_ui); + + settings::add_slider(1, sound::volume_master, settings_location::SOUND, "sound.volume_master", false, "%.0f%%"); + + settings::add_slider(0, sound::volume_effects, settings_location::SOUND_LEVELS, "sound.volume_effects", false, "%.0f%%"); + settings::add_slider(1, sound::volume_music, settings_location::SOUND_LEVELS, "sound.volume_music", false, "%.0f%%"); + settings::add_slider(2, sound::volume_ui, settings_location::SOUND_LEVELS, "sound.volume_ui", false, "%.0f%%"); +} + +void sound::init(void) +{ + alGenSources(1, &generic_source); + alSourcei(generic_source, AL_SOURCE_RELATIVE, AL_TRUE); + alSource3f(generic_source, AL_POSITION, 0.0f, 0.0f, 0.0f); + alSource3f(generic_source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); + + alGenSources(1, &player_source); + alSourcei(player_source, AL_SOURCE_RELATIVE, AL_TRUE); + alSource3f(player_source, AL_POSITION, 0.0f, 0.0f, 0.0f); + alSource3f(player_source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); + + alGenSources(1, &ui_source); + alSourcei(ui_source, AL_SOURCE_RELATIVE, AL_TRUE); + alSource3f(ui_source, AL_POSITION, 0.0f, 0.0f, 0.0f); + alSource3f(ui_source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); + + sfx_generic = nullptr; + sfx_player = nullptr; + sfx_ui = nullptr; +} + +void sound::init_late(void) +{ +} + +void sound::shutdown(void) +{ + sfx_ui = nullptr; + sfx_player = nullptr; + sfx_generic = nullptr; + + alDeleteBuffers(1, &ui_source); + alDeleteSources(1, &generic_source); + alDeleteSources(1, &player_source); +} + +void sound::update(void) +{ + auto effects_gain = glm::clamp(0.01f * sound::volume_effects.get_value(), 0.0f, 1.0f); + alSourcef(generic_source, AL_GAIN, effects_gain); + alSourcef(player_source, AL_GAIN, effects_gain); + + auto ui_gain = glm::clamp(0.01f * sound::volume_ui.get_value(), 0.0f, 1.0f); + alSourcef(ui_source, AL_GAIN, ui_gain); +} + +void sound::play_generic(std::string_view sound, bool looping, float pitch) +{ + if(sound.size()) { + sound::play_generic(resource::load(sound), looping, pitch); + } + else { + sound::play_generic(static_cast>(nullptr), looping, pitch); + } +} + +void sound::play_entity(entt::entity entity, std::string_view sound, bool looping, float pitch) +{ + if(sound.size()) { + sound::play_entity(entity, resource::load(sound), looping, pitch); + } + else { + sound::play_entity(entity, static_cast>(nullptr), looping, pitch); + } +} + +void sound::play_player(std::string_view sound, bool looping, float pitch) +{ + if(sound.size()) { + sound::play_player(resource::load(sound), looping, pitch); + } + else { + sound::play_player(static_cast>(nullptr), looping, pitch); + } +} + +void sound::play_ui(std::string_view sound, bool looping, float pitch) +{ + if(sound.size()) { + sound::play_ui(resource::load(sound), looping, pitch); + } + else { + sound::play_ui(static_cast>(nullptr), looping, pitch); + } +} + +void sound::play_generic(resource_ptr sound, bool looping, float pitch) +{ + alSourceRewind(generic_source); + + sfx_generic = sound; + + if(sfx_generic) { + alSourcei(generic_source, AL_BUFFER, sfx_generic->buffer); + alSourcei(generic_source, AL_LOOPING, looping); + alSourcef(generic_source, AL_PITCH, glm::clamp(pitch, MIN_PITCH, MAX_PITCH)); + alSourcePlay(generic_source); + } +} + +void sound::play_entity(entt::entity entity, resource_ptr sound, bool looping, float pitch) +{ + if(globals::dimension && globals::dimension->entities.valid(entity)) { + if(auto emitter = globals::dimension->entities.try_get(entity)) { + alSourceRewind(emitter->source); + + emitter->sound = sound; + + if(emitter->sound) { + alSourcei(emitter->source, AL_BUFFER, emitter->sound->buffer); + alSourcei(emitter->source, AL_LOOPING, looping); + alSourcef(emitter->source, AL_PITCH, glm::clamp(pitch, MIN_PITCH, MAX_PITCH)); + alSourcePlay(emitter->source); + } + } + } +} + +void sound::play_player(resource_ptr sound, bool looping, float pitch) +{ + if(sound && session::is_ingame()) { + protocol::EntitySound packet; + packet.entity = globals::player; + packet.sound = sound->name; + packet.looping = looping; + packet.pitch = pitch; + + protocol::send(session::peer, protocol::encode(packet)); + } + + alSourceRewind(player_source); + + sfx_player = sound; + + if(sfx_player) { + alSourcei(player_source, AL_BUFFER, sfx_player->buffer); + alSourcei(player_source, AL_LOOPING, looping); + alSourcef(player_source, AL_PITCH, glm::clamp(pitch, MIN_PITCH, MAX_PITCH)); + alSourcePlay(player_source); + } +} + +void sound::play_ui(resource_ptr sound, bool looping, float pitch) +{ + alSourceRewind(ui_source); + + sfx_ui = sound; + + if(sfx_ui) { + alSourcei(ui_source, AL_BUFFER, sfx_ui->buffer); + alSourcei(ui_source, AL_LOOPING, looping); + alSourcef(ui_source, AL_PITCH, glm::clamp(pitch, MIN_PITCH, MAX_PITCH)); + alSourcePlay(ui_source); + } +} diff --git a/src/game/client/io/sound.hh b/src/game/client/io/sound.hh new file mode 100644 index 0000000..d96d0c4 --- /dev/null +++ b/src/game/client/io/sound.hh @@ -0,0 +1,43 @@ +#pragma once + +#include "core/resource/resource.hh" + +namespace config +{ +class Float; +} // namespace config + +struct SoundEffect; + +namespace sound +{ +extern config::Float volume_master; +extern config::Float volume_effects; +extern config::Float volume_music; +extern config::Float volume_ui; +} // namespace sound + +namespace sound +{ +void init_config(void); +void init(void); +void init_late(void); +void shutdown(void); +void update(void); +} // namespace sound + +namespace sound +{ +void play_generic(std::string_view sound, bool looping, float pitch); +void play_entity(entt::entity entity, std::string_view sound, bool looping, float pitch); +void play_player(std::string_view sound, bool looping, float pitch); +void play_ui(std::string_view sound, bool looping, float pitch); +} // namespace sound + +namespace sound +{ +void play_generic(resource_ptr sound, bool looping, float pitch); +void play_entity(entt::entity entity, resource_ptr sound, bool looping, float pitch); +void play_player(resource_ptr sound, bool looping, float pitch); +void play_ui(resource_ptr sound, bool looping, float pitch); +} // namespace sound diff --git a/src/game/client/receive.cc b/src/game/client/receive.cc index a6fdf8e..bc7b92e 100644 --- a/src/game/client/receive.cc +++ b/src/game/client/receive.cc @@ -18,7 +18,7 @@ #include "client/gui/message_box.hh" #include "client/gui/window_title.hh" -#include "client/sound/sound.hh" +#include "client/io/sound.hh" #include "client/globals.hh" #include "client/session.hh" diff --git a/src/game/client/sound/CMakeLists.txt b/src/game/client/sound/CMakeLists.txt deleted file mode 100644 index 4b577d3..0000000 --- a/src/game/client/sound/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -target_sources(vclient PRIVATE - "${CMAKE_CURRENT_LIST_DIR}/sound.cc" - "${CMAKE_CURRENT_LIST_DIR}/sound.hh") diff --git a/src/game/client/sound/sound.cc b/src/game/client/sound/sound.cc deleted file mode 100644 index 11c9286..0000000 --- a/src/game/client/sound/sound.cc +++ /dev/null @@ -1,211 +0,0 @@ -#include "client/pch.hh" - -#include "client/sound/sound.hh" - -#include "core/config/number.hh" - -#include "core/io/config_map.hh" - -#include "core/math/constexpr.hh" - -#include "core/resource/resource.hh" - -#include "shared/world/dimension.hh" - -#include "shared/coord.hh" -#include "shared/protocol.hh" - -#include "client/entity/camera.hh" -#include "client/entity/sound_emitter.hh" - -#include "client/gui/settings.hh" - -#include "client/resource/sound_effect.hh" - -#include "client/const.hh" -#include "client/globals.hh" -#include "client/session.hh" - -config::Float sound::volume_master(100.0f, 0.0f, 100.0f); -config::Float sound::volume_effects(100.0f, 0.0f, 100.0f); -config::Float sound::volume_music(100.0f, 0.0f, 100.0f); -config::Float sound::volume_ui(100.0f, 0.0f, 100.0f); - -static ALuint generic_source; -static ALuint player_source; -static ALuint ui_source; - -static resource_ptr sfx_generic; -static resource_ptr sfx_player; -static resource_ptr sfx_ui; - -void sound::init_config(void) -{ - globals::client_config.add_value("sound.volume_master", sound::volume_master); - globals::client_config.add_value("sound.volume_effects", sound::volume_effects); - globals::client_config.add_value("sound.volume_music", sound::volume_music); - globals::client_config.add_value("sound.volume_ui", sound::volume_ui); - - settings::add_slider(1, sound::volume_master, settings_location::SOUND, "sound.volume_master", false, "%.0f%%"); - - settings::add_slider(0, sound::volume_effects, settings_location::SOUND_LEVELS, "sound.volume_effects", false, "%.0f%%"); - settings::add_slider(1, sound::volume_music, settings_location::SOUND_LEVELS, "sound.volume_music", false, "%.0f%%"); - settings::add_slider(2, sound::volume_ui, settings_location::SOUND_LEVELS, "sound.volume_ui", false, "%.0f%%"); -} - -void sound::init(void) -{ - alGenSources(1, &generic_source); - alSourcei(generic_source, AL_SOURCE_RELATIVE, AL_TRUE); - alSource3f(generic_source, AL_POSITION, 0.0f, 0.0f, 0.0f); - alSource3f(generic_source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); - - alGenSources(1, &player_source); - alSourcei(player_source, AL_SOURCE_RELATIVE, AL_TRUE); - alSource3f(player_source, AL_POSITION, 0.0f, 0.0f, 0.0f); - alSource3f(player_source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); - - alGenSources(1, &ui_source); - alSourcei(ui_source, AL_SOURCE_RELATIVE, AL_TRUE); - alSource3f(ui_source, AL_POSITION, 0.0f, 0.0f, 0.0f); - alSource3f(ui_source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); - - sfx_generic = nullptr; - sfx_player = nullptr; - sfx_ui = nullptr; -} - -void sound::init_late(void) -{ -} - -void sound::shutdown(void) -{ - sfx_ui = nullptr; - sfx_player = nullptr; - sfx_generic = nullptr; - - alDeleteBuffers(1, &ui_source); - alDeleteSources(1, &generic_source); - alDeleteSources(1, &player_source); -} - -void sound::update(void) -{ - auto effects_gain = glm::clamp(0.01f * sound::volume_effects.get_value(), 0.0f, 1.0f); - alSourcef(generic_source, AL_GAIN, effects_gain); - alSourcef(player_source, AL_GAIN, effects_gain); - - auto ui_gain = glm::clamp(0.01f * sound::volume_ui.get_value(), 0.0f, 1.0f); - alSourcef(ui_source, AL_GAIN, ui_gain); -} - -void sound::play_generic(std::string_view sound, bool looping, float pitch) -{ - if(sound.size()) { - sound::play_generic(resource::load(sound), looping, pitch); - } - else { - sound::play_generic(static_cast>(nullptr), looping, pitch); - } -} - -void sound::play_entity(entt::entity entity, std::string_view sound, bool looping, float pitch) -{ - if(sound.size()) { - sound::play_entity(entity, resource::load(sound), looping, pitch); - } - else { - sound::play_entity(entity, static_cast>(nullptr), looping, pitch); - } -} - -void sound::play_player(std::string_view sound, bool looping, float pitch) -{ - if(sound.size()) { - sound::play_player(resource::load(sound), looping, pitch); - } - else { - sound::play_player(static_cast>(nullptr), looping, pitch); - } -} - -void sound::play_ui(std::string_view sound, bool looping, float pitch) -{ - if(sound.size()) { - sound::play_ui(resource::load(sound), looping, pitch); - } - else { - sound::play_ui(static_cast>(nullptr), looping, pitch); - } -} - -void sound::play_generic(resource_ptr sound, bool looping, float pitch) -{ - alSourceRewind(generic_source); - - sfx_generic = sound; - - if(sfx_generic) { - alSourcei(generic_source, AL_BUFFER, sfx_generic->buffer); - alSourcei(generic_source, AL_LOOPING, looping); - alSourcef(generic_source, AL_PITCH, glm::clamp(pitch, MIN_PITCH, MAX_PITCH)); - alSourcePlay(generic_source); - } -} - -void sound::play_entity(entt::entity entity, resource_ptr sound, bool looping, float pitch) -{ - if(globals::dimension && globals::dimension->entities.valid(entity)) { - if(auto emitter = globals::dimension->entities.try_get(entity)) { - alSourceRewind(emitter->source); - - emitter->sound = sound; - - if(emitter->sound) { - alSourcei(emitter->source, AL_BUFFER, emitter->sound->buffer); - alSourcei(emitter->source, AL_LOOPING, looping); - alSourcef(emitter->source, AL_PITCH, glm::clamp(pitch, MIN_PITCH, MAX_PITCH)); - alSourcePlay(emitter->source); - } - } - } -} - -void sound::play_player(resource_ptr sound, bool looping, float pitch) -{ - if(sound && session::is_ingame()) { - protocol::EntitySound packet; - packet.entity = globals::player; - packet.sound = sound->name; - packet.looping = looping; - packet.pitch = pitch; - - protocol::send(session::peer, protocol::encode(packet)); - } - - alSourceRewind(player_source); - - sfx_player = sound; - - if(sfx_player) { - alSourcei(player_source, AL_BUFFER, sfx_player->buffer); - alSourcei(player_source, AL_LOOPING, looping); - alSourcef(player_source, AL_PITCH, glm::clamp(pitch, MIN_PITCH, MAX_PITCH)); - alSourcePlay(player_source); - } -} - -void sound::play_ui(resource_ptr sound, bool looping, float pitch) -{ - alSourceRewind(ui_source); - - sfx_ui = sound; - - if(sfx_ui) { - alSourcei(ui_source, AL_BUFFER, sfx_ui->buffer); - alSourcei(ui_source, AL_LOOPING, looping); - alSourcef(ui_source, AL_PITCH, glm::clamp(pitch, MIN_PITCH, MAX_PITCH)); - alSourcePlay(ui_source); - } -} diff --git a/src/game/client/sound/sound.hh b/src/game/client/sound/sound.hh deleted file mode 100644 index d96d0c4..0000000 --- a/src/game/client/sound/sound.hh +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "core/resource/resource.hh" - -namespace config -{ -class Float; -} // namespace config - -struct SoundEffect; - -namespace sound -{ -extern config::Float volume_master; -extern config::Float volume_effects; -extern config::Float volume_music; -extern config::Float volume_ui; -} // namespace sound - -namespace sound -{ -void init_config(void); -void init(void); -void init_late(void); -void shutdown(void); -void update(void); -} // namespace sound - -namespace sound -{ -void play_generic(std::string_view sound, bool looping, float pitch); -void play_entity(entt::entity entity, std::string_view sound, bool looping, float pitch); -void play_player(std::string_view sound, bool looping, float pitch); -void play_ui(std::string_view sound, bool looping, float pitch); -} // namespace sound - -namespace sound -{ -void play_generic(resource_ptr sound, bool looping, float pitch); -void play_entity(entt::entity entity, resource_ptr sound, bool looping, float pitch); -void play_player(resource_ptr sound, bool looping, float pitch); -void play_ui(resource_ptr sound, bool looping, float pitch); -} // namespace sound -- cgit