From da0392239228515b6613aafd0019a6391441d83a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Zyba=C5=82a?= Date: Sat, 20 Jan 2024 20:44:45 +0100 Subject: [PATCH] Add sound mute button --- .github/workflows/linux.yml | 2 +- src/action.cpp | 1 + src/game.cpp | 33 +++++++++++++++++++++++++++++---- src/game.hpp | 8 ++++---- src/main.cpp | 12 ++++++------ 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 752c66c..0f7369a 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -42,7 +42,7 @@ jobs: - name: Build Product run: | cd ${{ env.PROJECT_NAME }} - cmake -B build + cmake -B build -DCMAKE_BUILD_TYPE=Release cmake --build build shell: bash diff --git a/src/action.cpp b/src/action.cpp index de7896c..36b8df6 100644 --- a/src/action.cpp +++ b/src/action.cpp @@ -632,6 +632,7 @@ void Game::set_room(const Room::Type &room_type) noexcept { current_music = station_music[GetRandomValue(0, station_music.size() - 1)]; PlayMusicStream(current_music); + SetMusicVolume(current_music, music_volume); } room = Room::get(room_type); diff --git a/src/game.cpp b/src/game.cpp index c21d855..3737928 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -260,6 +260,30 @@ void Game::update() gui->messages.pop_front(); } + if (IsKeyPressed(KEY_M)) + { + static bool music_enabled = true; + static float previous_music_volume = music_volume; + static float previous_sound_volume = SoundManager::volume; + + music_enabled = !music_enabled; + if (music_enabled) + { + music_volume = previous_music_volume; + SoundManager::volume = previous_sound_volume; + } + else + { + previous_music_volume = music_volume; + music_volume = 0.0f; + + previous_sound_volume = SoundManager::volume; + SoundManager::volume = 0.0f; + } + + SetMusicVolume(current_music, music_volume); + } + frame++; } @@ -341,7 +365,7 @@ void Game::update_game() { if (IsKeyPressed(KEY_F1) && asteroids) { - for (size_t i = 0; i < NUMBER_OF_ASTEROIDS; i++) + for (size_t i = 0; i < 10; i++) { const Vector2 position = { static_cast(GetRandomValue(0, width)), static_cast(GetRandomValue(0, height)) }; @@ -508,10 +532,10 @@ void Game::set_state(GameState new_state) noexcept { state = new_state; - bullets = std::make_unique>(); - asteroids = std::make_unique>(); + bullets = std::make_unique>(); + asteroids = std::make_unique>(); particles = std::make_unique>(); - pickables = std::make_unique>(); + pickables = std::make_unique>(); survive_time = 0.0f; switch (state) @@ -520,6 +544,7 @@ void Game::set_state(GameState new_state) noexcept { current_music = asteroid_music[GetRandomValue(0, asteroid_music.size() - 1)]; PlayMusicStream(current_music); + SetMusicVolume(current_music, music_volume); assert(!missions.empty()); assert(current_mission < missions.size()); diff --git a/src/game.hpp b/src/game.hpp index 2dcd072..e1676ea 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -134,10 +134,10 @@ class Game std::unique_ptr player; std::shared_ptr room; std::unique_ptr tileset_sprite; - std::unique_ptr> bullets; - std::unique_ptr> asteroids; + std::unique_ptr> bullets; + std::unique_ptr> asteroids; std::unique_ptr> particles; - std::unique_ptr> pickables; + std::unique_ptr> pickables; std::vector station_music; std::vector asteroid_music; @@ -145,8 +145,8 @@ class Game static constexpr int width = 480; static constexpr int height = 270; - static constexpr int NUMBER_OF_ASTEROIDS = 6; static Config config; + float music_volume { 0.7f }; static uint64_t frame; void init(); diff --git a/src/main.cpp b/src/main.cpp index c0e91ee..0a2d3ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,12 +16,13 @@ #include "render_pass.hpp" #include "utils.hpp" -const int AUDIO_BUFFER_SIZE = (4096 * 12); +const constexpr int AUDIO_BUFFER_SIZE = (4096 * 12); +const constexpr size_t MAX_UPDATE_STEPS = 5; -const int window_width = Game::width * 2; -const int window_height = Game::height * 2; +const constexpr int window_width = Game::width * 2; +const constexpr int window_height = Game::height * 2; -const bool integer_scaling = false; +const constexpr bool integer_scaling = false; static std::unique_ptr game_render_pass; static std::unique_ptr ui_render_pass; @@ -44,13 +45,12 @@ void update_draw_frame() render_destination.height = Game::height * scale; const float interval = DELTA_TIME; - size_t steps = 6; const float dt = GetFrameTime(); static float accumulator = 0.0f; accumulator += dt; - while (accumulator >= interval) + for (size_t steps = 0; accumulator >= interval && steps < MAX_UPDATE_STEPS; ++steps) { accumulator -= interval;