diff --git a/src/game_data.h b/src/game_data.h index 810b7a9..db2639a 100644 --- a/src/game_data.h +++ b/src/game_data.h @@ -8,5 +8,6 @@ namespace game { public: MeasurementSystem measurementSystem{MeasurementSystem::METRIC}; + int volume{100}; }; } \ No newline at end of file diff --git a/src/resource/audio.cpp b/src/resource/audio.cpp index 66fb2d6..cbec0b6 100644 --- a/src/resource/audio.cpp +++ b/src/resource/audio.cpp @@ -12,6 +12,12 @@ namespace game::resource return mixer; } + void Audio::set_gain(float gain) + { + auto mixer = mixer_get(); + MIX_SetMasterGain(mixer, gain); + } + void Audio::retain() { if (refCount) ++(*refCount); diff --git a/src/resource/audio.h b/src/resource/audio.h index a76e85a..400a4a7 100644 --- a/src/resource/audio.h +++ b/src/resource/audio.h @@ -10,7 +10,7 @@ namespace game::resource MIX_Audio* internal{nullptr}; MIX_Track* track{nullptr}; int* refCount{nullptr}; - MIX_Mixer* mixer_get(); + static MIX_Mixer* mixer_get(); void unload(); void retain(); void release(); @@ -27,5 +27,6 @@ namespace game::resource void play(bool isLoop = false); void stop(); bool is_playing() const; + static void set_gain(float vol); }; } diff --git a/src/resources.cpp b/src/resources.cpp index ba336eb..bb3a930 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -21,4 +21,8 @@ namespace game } void Resources::sound_play(audio::Type type) { audio[type].play(); } + + void Resources::set_audio_gain(float vol) { + Audio::set_gain(vol); + } } \ No newline at end of file diff --git a/src/resources.h b/src/resources.h index e35afcb..8e88c6f 100644 --- a/src/resources.h +++ b/src/resources.h @@ -124,5 +124,6 @@ namespace game Resources(); void sound_play(audio::Type); + void set_audio_gain(float vol); }; } \ No newline at end of file diff --git a/src/window/main_menu.cpp b/src/window/main_menu.cpp index 6627317..ead52e8 100644 --- a/src/window/main_menu.cpp +++ b/src/window/main_menu.cpp @@ -6,6 +6,7 @@ namespace game::window ImVec2 pos) { MeasurementSystem& measurementSystem = gameData.measurementSystem; + int& volume = gameData.volume; ImGui::SetNextWindowSize(size); ImGui::SetNextWindowPos(pos); @@ -48,6 +49,13 @@ namespace game::window ImGui::RadioButton("Metric", (int*)&measurementSystem, MeasurementSystem::METRIC); ImGui::SameLine(); ImGui::RadioButton("Imperial", (int*)&measurementSystem, MeasurementSystem::IMPERIAL); + + ImGui::SeparatorText("Sound"); + if (ImGui::SliderInt("Volume", (int*)&volume, 0, 100)) + { + resources.set_audio_gain((float)volume / 100); + } + ImGui::EndTabItem(); }