diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2021-03-13 13:34:12 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2021-03-13 15:34:50 -0500 |
commit | 9107e9e6c337fbd9790eeb7e89e473d5da129f36 (patch) | |
tree | 4dc08eaebcbff0e284713965b743c6189ba9aaad | |
parent | 8a7d87a312b3dc42877577e99533c96d48714368 (diff) | |
download | tanetane-9107e9e6c337fbd9790eeb7e89e473d5da129f36.tar.gz tanetane-9107e9e6c337fbd9790eeb7e89e473d5da129f36.tar.bz2 tanetane-9107e9e6c337fbd9790eeb7e89e473d5da129f36.zip |
Music volume is controllable from settings menu
#7
-rw-r--r-- | src/menu.h | 6 | ||||
-rw-r--r-- | src/menu_system.cpp | 19 | ||||
-rw-r--r-- | src/mixer.cpp | 7 | ||||
-rw-r--r-- | src/mixer.h | 5 |
4 files changed, 35 insertions, 2 deletions
diff --git a/src/menu.h b/src/menu.h index 9ef94be..7483d89 100644 --- a/src/menu.h +++ b/src/menu.h | |||
@@ -20,6 +20,7 @@ struct MenuItem { | |||
20 | bool playSfx = true; | 20 | bool playSfx = true; |
21 | int value = 0; | 21 | int value = 0; |
22 | int maxValue = 0; | 22 | int maxValue = 0; |
23 | std::function<void(MenuItem&)> selectionChanged; | ||
23 | }; | 24 | }; |
24 | 25 | ||
25 | class MenuBuilder { | 26 | class MenuBuilder { |
@@ -57,6 +58,11 @@ public: | |||
57 | return *this; | 58 | return *this; |
58 | } | 59 | } |
59 | 60 | ||
61 | MenuBuilder& SelectionChangedFunction(std::function<void(MenuItem&)> val) { | ||
62 | result_.selectionChanged = std::move(val); | ||
63 | return *this; | ||
64 | } | ||
65 | |||
60 | MenuItem Build() const { | 66 | MenuItem Build() const { |
61 | return result_; | 67 | return result_; |
62 | } | 68 | } |
diff --git a/src/menu_system.cpp b/src/menu_system.cpp index e1aca8d..c099afe 100644 --- a/src/menu_system.cpp +++ b/src/menu_system.cpp | |||
@@ -55,8 +55,15 @@ void MenuSystem::openPauseMenu() { | |||
55 | .ActivationFunction([this] (Game&) { | 55 | .ActivationFunction([this] (Game&) { |
56 | openSubmenu(Menu({ | 56 | openSubmenu(Menu({ |
57 | MenuBuilder().Slider("Music Volume: ") | 57 | MenuBuilder().Slider("Music Volume: ") |
58 | .InitialValue(10) | 58 | .InitialValue(game_.getMixer().isMusicMuted() ? 0 : game_.getMixer().getMusicVolume()) |
59 | .MaxValue(10), | 59 | .MaxValue(10) |
60 | .SelectionChangedFunction([this] (MenuItem& menuItem) { | ||
61 | if (game_.getMixer().isMusicMuted()) { | ||
62 | menuItem.value = game_.getMixer().getMusicVolume(); | ||
63 | } | ||
64 | |||
65 | game_.getMixer().setMusicVolume(menuItem.value); | ||
66 | }), | ||
60 | MenuBuilder().Slider("Sound Volume: ") | 67 | MenuBuilder().Slider("Sound Volume: ") |
61 | .InitialValue(10) | 68 | .InitialValue(10) |
62 | .MaxValue(10), | 69 | .MaxValue(10), |
@@ -135,6 +142,10 @@ void MenuSystem::pressedLeft() { | |||
135 | if (menuItem.type == MenuType::Slider && menuItem.value > 0) { | 142 | if (menuItem.type == MenuType::Slider && menuItem.value > 0) { |
136 | menuItem.value--; | 143 | menuItem.value--; |
137 | 144 | ||
145 | if (menuItem.selectionChanged) { | ||
146 | menuItem.selectionChanged(menuItem); | ||
147 | } | ||
148 | |||
138 | game_.getMixer().playSound("../res/sfx/horizontal_menu.wav"); | 149 | game_.getMixer().playSound("../res/sfx/horizontal_menu.wav"); |
139 | } | 150 | } |
140 | } | 151 | } |
@@ -146,6 +157,10 @@ void MenuSystem::pressedRight() { | |||
146 | if (menuItem.type == MenuType::Slider && menuItem.value < menuItem.maxValue) { | 157 | if (menuItem.type == MenuType::Slider && menuItem.value < menuItem.maxValue) { |
147 | menuItem.value++; | 158 | menuItem.value++; |
148 | 159 | ||
160 | if (menuItem.selectionChanged) { | ||
161 | menuItem.selectionChanged(menuItem); | ||
162 | } | ||
163 | |||
149 | game_.getMixer().playSound("../res/sfx/horizontal_menu.wav"); | 164 | game_.getMixer().playSound("../res/sfx/horizontal_menu.wav"); |
150 | } | 165 | } |
151 | } | 166 | } |
diff --git a/src/mixer.cpp b/src/mixer.cpp index 4e8f307..74a870a 100644 --- a/src/mixer.cpp +++ b/src/mixer.cpp | |||
@@ -1,4 +1,5 @@ | |||
1 | #include "mixer.h" | 1 | #include "mixer.h" |
2 | #include <iostream> | ||
2 | 3 | ||
3 | void Mixer::playSound(std::string_view filename) { | 4 | void Mixer::playSound(std::string_view filename) { |
4 | Mix_Chunk* chunk = getChunkByFilename(std::string(filename)); | 5 | Mix_Chunk* chunk = getChunkByFilename(std::string(filename)); |
@@ -66,6 +67,12 @@ void Mixer::unmuteMusic() { | |||
66 | musicMuted_ = false; | 67 | musicMuted_ = false; |
67 | } | 68 | } |
68 | 69 | ||
70 | void Mixer::setMusicVolume(int vol) { | ||
71 | musicVolume_ = MIX_MAX_VOLUME / 10.0 * vol; | ||
72 | musicMuted_ = false; | ||
73 | Mix_VolumeMusic(musicVolume_); | ||
74 | } | ||
75 | |||
69 | void Mixer::pauseMusic() { | 76 | void Mixer::pauseMusic() { |
70 | Mix_PauseMusic(); | 77 | Mix_PauseMusic(); |
71 | } | 78 | } |
diff --git a/src/mixer.h b/src/mixer.h index 854c67d..c2a0681 100644 --- a/src/mixer.h +++ b/src/mixer.h | |||
@@ -2,6 +2,7 @@ | |||
2 | #define MIXER_H_6DF82000 | 2 | #define MIXER_H_6DF82000 |
3 | 3 | ||
4 | #include <SDL_mixer.h> | 4 | #include <SDL_mixer.h> |
5 | #include <cmath> | ||
5 | #include <memory> | 6 | #include <memory> |
6 | #include <stdexcept> | 7 | #include <stdexcept> |
7 | #include <map> | 8 | #include <map> |
@@ -92,6 +93,10 @@ public: | |||
92 | 93 | ||
93 | bool isMusicMuted() const { return musicMuted_; } | 94 | bool isMusicMuted() const { return musicMuted_; } |
94 | 95 | ||
96 | int getMusicVolume() const { return std::round(static_cast<double>(musicVolume_) / MIX_MAX_VOLUME * 10); } | ||
97 | |||
98 | void setMusicVolume(int vol); | ||
99 | |||
95 | void pauseMusic(); | 100 | void pauseMusic(); |
96 | 101 | ||
97 | void unpauseMusic(); | 102 | void unpauseMusic(); |