From 4fe0a7d23ad7c7a17cb1cc317eae3ced8d19fbdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20NOUVEAU?= Date: Sun, 18 Jan 2026 15:33:55 +0100 Subject: [PATCH] feat: added sound control in the settings --- ECS/include/components/graphics.hpp | 13 ++- client/include/client.hpp | 1 + client/src/create_entities.cpp | 123 +++++++++++++++++++++++++++- client/src/main.cpp | 6 ++ client/src/systems.cpp | 17 ++++ 5 files changed, 158 insertions(+), 2 deletions(-) diff --git a/ECS/include/components/graphics.hpp b/ECS/include/components/graphics.hpp index 079376a..14cfdd4 100644 --- a/ECS/include/components/graphics.hpp +++ b/ECS/include/components/graphics.hpp @@ -176,6 +176,8 @@ namespace comp { }; using sound = sf::Sound; + + struct volume_display {}; } void draw_system(registry const &, @@ -212,4 +214,13 @@ void text_field_system(registry &, sparse_array const &, sparse_array &, sparse_array &, - sparse_array const &); \ No newline at end of file + sparse_array const &); + +void sound_system(registry &, + sparse_array &, + sparse_array const &); + +void volume_display_system(registry const &, + sparse_array &, + sparse_array const &, + sparse_array const &); \ No newline at end of file diff --git a/client/include/client.hpp b/client/include/client.hpp index ee09a5c..fe144ff 100644 --- a/client/include/client.hpp +++ b/client/include/client.hpp @@ -43,6 +43,7 @@ struct KeyBindings { }; extern KeyBindings g_key_bindings; +extern float g_audio_volume; namespace comp { struct player {}; diff --git a/client/src/create_entities.cpp b/client/src/create_entities.cpp index 6c51e44..8d976c0 100644 --- a/client/src/create_entities.cpp +++ b/client/src/create_entities.cpp @@ -29,6 +29,7 @@ static assets _assets; KeyBindings g_key_bindings; +float g_audio_volume = 50.0f; template static auto add(Extra &&...extra, const auto &&f) { @@ -79,7 +80,7 @@ namespace comp { auto &s = reg.get_components()[enti]; s->setPlayingOffset(sf::seconds(.15f)); - s->setVolume(25.0f); + s->setVolume(g_audio_volume); s->play(); } @@ -324,6 +325,14 @@ namespace comp { for (auto &&[button, _]: zipper(buttons, settingsButtons)) button.visible = !button.visible; } + void volume_up(registry &, window &, size_t &) { + g_audio_volume = std::min(100.0f, g_audio_volume + 10.0f); + } + + void volume_down(registry &, window &, size_t &) { + g_audio_volume = std::max(0.0f, g_audio_volume - 10.0f); + } + void set_bg(registry ®, window &MainWindow) { sprite bg(_assets.safe_get("assets/background.png"), {1134, 207}); bg.setScale({5.2f, 5.2f}); @@ -408,6 +417,19 @@ namespace comp { enabled() ); + text upLabel("UP:", _assets.safe_get("assets/arial_bold.ttf"), 20, {0, -30}); + upLabel.setOutlineColor({255, 255, 255}); + upLabel.visible = false; + + entity upLabelEntity(reg.spawn_entity()); + reg.add_components(upLabelEntity, + position(140.f, 120.f), + Mainwindow, + upLabel, + settingsButton(), + enabled() + ); + RectButton bindUpBtn = createDefaultButton("assets/arial_bold.ttf", get_key_name(g_key_bindings.keys[MOVE_UP]), {80.f, 50.f}, {140.f, 120.f}); entity bindUpEntity(reg.spawn_entity()); @@ -419,6 +441,19 @@ namespace comp { enabled() ); + text downLabel("DOWN:", _assets.safe_get("assets/arial_bold.ttf"), 20, {0, -30}); + downLabel.setOutlineColor({255, 255, 255}); + downLabel.visible = false; + + entity downLabelEntity(reg.spawn_entity()); + reg.add_components(downLabelEntity, + position(230.f, 120.f), + Mainwindow, + downLabel, + settingsButton(), + enabled() + ); + RectButton bindDownBtn = createDefaultButton("assets/arial_bold.ttf", get_key_name(g_key_bindings.keys[MOVE_DOWN]), {80.f, 50.f}, {230.f, 120.f}); entity bindDownEntity(reg.spawn_entity()); @@ -430,6 +465,19 @@ namespace comp { enabled() ); + text leftLabel("LEFT:", _assets.safe_get("assets/arial_bold.ttf"), 20, {0, -30}); + leftLabel.setOutlineColor({255, 255, 255}); + leftLabel.visible = false; + + entity leftLabelEntity(reg.spawn_entity()); + reg.add_components(leftLabelEntity, + position(320.f, 120.f), + Mainwindow, + leftLabel, + settingsButton(), + enabled() + ); + RectButton bindLeftBtn = createDefaultButton("assets/arial_bold.ttf", get_key_name(g_key_bindings.keys[MOVE_LEFT]), {80.f, 50.f}, {320.f, 120.f}); entity bindLeftEntity(reg.spawn_entity()); @@ -441,6 +489,19 @@ namespace comp { enabled() ); + text rightLabel("RIGHT:", _assets.safe_get("assets/arial_bold.ttf"), 20, {0, -30}); + rightLabel.setOutlineColor({255, 255, 255}); + rightLabel.visible = false; + + entity rightLabelEntity(reg.spawn_entity()); + reg.add_components(rightLabelEntity, + position(410.f, 120.f), + Mainwindow, + rightLabel, + settingsButton(), + enabled() + ); + RectButton bindRightBtn = createDefaultButton("assets/arial_bold.ttf", get_key_name(g_key_bindings.keys[MOVE_RIGHT]), {80.f, 50.f}, {410.f, 120.f}); entity bindRightEntity(reg.spawn_entity()); @@ -452,6 +513,19 @@ namespace comp { enabled() ); + text shootLabel("SHOOT:", _assets.safe_get("assets/arial_bold.ttf"), 20, {0, -30}); + shootLabel.setOutlineColor({255, 255, 255}); + shootLabel.visible = false; + + entity shootLabelEntity(reg.spawn_entity()); + reg.add_components(shootLabelEntity, + position(500.f, 120.f), + Mainwindow, + shootLabel, + settingsButton(), + enabled() + ); + RectButton bindShootBtn = createDefaultButton("assets/arial_bold.ttf", get_key_name(g_key_bindings.keys[SHOOT]), {80.f, 50.f}, {500.f, 120.f}); entity bindShootEntity(reg.spawn_entity()); @@ -463,6 +537,53 @@ namespace comp { enabled() ); + text volumeLabel("Volume:", _assets.safe_get("assets/arial_bold.ttf"), 24, {0, 0}); + volumeLabel.setOutlineColor({255, 255, 255}); + volumeLabel.visible = false; + + entity volumeLabelEntity(reg.spawn_entity()); + reg.add_components(volumeLabelEntity, + position(60.f, 200.f), + Mainwindow, + volumeLabel, + settingsButton(), + enabled() + ); + + text volumeDisplayText("50", _assets.safe_get("assets/arial_bold.ttf"), 24, {0, 0}); + volumeDisplayText.setOutlineColor({255, 255, 255}); + volumeDisplayText.visible = false; + + entity volumeDisplayEntity(reg.spawn_entity()); + reg.add_components(volumeDisplayEntity, + position(200.f, 200.f), + Mainwindow, + volumeDisplayText, + volume_display(), + settingsButton(), + enabled() + ); + + RectButton volumeDownBtn = createDefaultButton("assets/arial_bold.ttf", "-", {50.f, 50.f}, {250.f, 200.f}); + entity volumeDownEntity(reg.spawn_entity()); + reg.add_components(volumeDownEntity, + position(250.f, 200.f), + Mainwindow, + button(volumeDownBtn, volume_down, false), + settingsButton(), + enabled() + ); + + RectButton volumeUpBtn = createDefaultButton("assets/arial_bold.ttf", "+", {50.f, 50.f}, {310.f, 200.f}); + entity volumeUpEntity(reg.spawn_entity()); + reg.add_components(volumeUpEntity, + position(310.f, 200.f), + Mainwindow, + button(volumeUpBtn, volume_up, false), + settingsButton(), + enabled() + ); + entity rebindState(reg.spawn_entity()); reg.add_components(rebindState, rebinding_state(), diff --git a/client/src/main.cpp b/client/src/main.cpp index 132eb79..7dd52dd 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -337,6 +337,7 @@ namespace comp { reg.register_component(); reg.register_component(); reg.register_component(); + reg.register_component(); reg.safe_add_system(sync_position_system); reg.safe_add_system(clock_system); @@ -351,6 +352,7 @@ namespace comp { reg.safe_add_system(position_system); reg.safe_add_system(refresh_bg); reg.safe_add_system(update_score); + reg.safe_add_system(sound_system); reg.register_component(); new_window(reg, scoreValue, Mainwindow); @@ -378,11 +380,13 @@ namespace comp { reg.register_component(); reg.register_component(); reg.register_component(); + reg.register_component(); reg.safe_add_system(draw_system); reg.safe_add_system(sprite_system); reg.safe_add_system(button_system); reg.safe_add_system(text_system); + reg.safe_add_system(volume_display_system); waiting_setup_entity(reg, MainWindow, nb_player); } @@ -390,11 +394,13 @@ namespace comp { void setup_settings(registry ®, window &MainWindow) { reg.register_component(); reg.register_component(); + reg.register_component(); reg.safe_add_system(draw_system); reg.safe_add_system(sprite_system); reg.safe_add_system(button_system); reg.safe_add_system(text_system); + reg.safe_add_system(volume_display_system); setup_blur(reg, MainWindow); setup_settings_button(reg, MainWindow); diff --git a/client/src/systems.cpp b/client/src/systems.cpp index 8e63812..6dadc86 100644 --- a/client/src/systems.cpp +++ b/client/src/systems.cpp @@ -50,3 +50,20 @@ void refresh_bg(registry const &, bg.draw(pos.x + bg.size.x * scale, pos.y, window); } } + +void sound_system(registry &, + sparse_array &sounds, + sparse_array const &enabled) { + for (auto &&[sound, _]: zipper(sounds, enabled)) { + sound.setVolume(g_audio_volume); + } +} + +void volume_display_system(registry const &, + sparse_array &texts, + sparse_array const &volume_displays, + sparse_array const &enabled) { + for (auto &&[text, _, __]: zipper(texts, volume_displays, enabled)) { + text.setString(std::to_string((int)g_audio_volume)); + } +}