diff options
-rw-r--r-- | src/menu_system.cpp | 7 | ||||
-rw-r--r-- | src/mixer.cpp | 12 | ||||
-rw-r--r-- | src/mixer.h | 5 |
3 files changed, 21 insertions, 3 deletions
diff --git a/src/menu_system.cpp b/src/menu_system.cpp index c099afe..a430009 100644 --- a/src/menu_system.cpp +++ b/src/menu_system.cpp | |||
@@ -65,8 +65,11 @@ void MenuSystem::openPauseMenu() { | |||
65 | game_.getMixer().setMusicVolume(menuItem.value); | 65 | game_.getMixer().setMusicVolume(menuItem.value); |
66 | }), | 66 | }), |
67 | MenuBuilder().Slider("Sound Volume: ") | 67 | MenuBuilder().Slider("Sound Volume: ") |
68 | .InitialValue(10) | 68 | .InitialValue(game_.getMixer().getSoundVolume()) |
69 | .MaxValue(10), | 69 | .MaxValue(10) |
70 | .SelectionChangedFunction([this] (MenuItem& menuItem) { | ||
71 | game_.getMixer().setSoundVolume(menuItem.value); | ||
72 | }), | ||
70 | MenuBuilder().Command("Back") | 73 | MenuBuilder().Command("Back") |
71 | .ActivationFunction([this] (Game& game) { | 74 | .ActivationFunction([this] (Game& game) { |
72 | closePauseMenu(); | 75 | closePauseMenu(); |
diff --git a/src/mixer.cpp b/src/mixer.cpp index 74a870a..6071fca 100644 --- a/src/mixer.cpp +++ b/src/mixer.cpp | |||
@@ -4,9 +4,12 @@ | |||
4 | void Mixer::playSound(std::string_view filename) { | 4 | void Mixer::playSound(std::string_view filename) { |
5 | Mix_Chunk* chunk = getChunkByFilename(std::string(filename)); | 5 | Mix_Chunk* chunk = getChunkByFilename(std::string(filename)); |
6 | 6 | ||
7 | if (Mix_PlayChannel(-1, chunk, 0) == -1) { | 7 | int ret = Mix_PlayChannel(-1, chunk, 0); |
8 | if (ret == -1) { | ||
8 | throw mix_error(); | 9 | throw mix_error(); |
9 | } | 10 | } |
11 | |||
12 | Mix_Volume(ret, soundVolume_); | ||
10 | } | 13 | } |
11 | 14 | ||
12 | int Mixer::loopSound(std::string_view filename) { | 15 | int Mixer::loopSound(std::string_view filename) { |
@@ -17,6 +20,8 @@ int Mixer::loopSound(std::string_view filename) { | |||
17 | throw mix_error(); | 20 | throw mix_error(); |
18 | } | 21 | } |
19 | 22 | ||
23 | Mix_Volume(ret, soundVolume_); | ||
24 | |||
20 | return ret; | 25 | return ret; |
21 | } | 26 | } |
22 | 27 | ||
@@ -32,6 +37,11 @@ void Mixer::unpauseSounds() { | |||
32 | Mix_Resume(-1); | 37 | Mix_Resume(-1); |
33 | } | 38 | } |
34 | 39 | ||
40 | void Mixer::setSoundVolume(int vol) { | ||
41 | soundVolume_ = MIX_MAX_VOLUME / 10.0 * vol; | ||
42 | Mix_Volume(-1, soundVolume_); | ||
43 | } | ||
44 | |||
35 | void Mixer::playMusic(std::string_view name, int ms) { | 45 | void Mixer::playMusic(std::string_view name, int ms) { |
36 | Mix_Music* song = getMusicByName(name); | 46 | Mix_Music* song = getMusicByName(name); |
37 | int ret; | 47 | int ret; |
diff --git a/src/mixer.h b/src/mixer.h index c2a0681..f9c958a 100644 --- a/src/mixer.h +++ b/src/mixer.h | |||
@@ -77,6 +77,10 @@ public: | |||
77 | 77 | ||
78 | void unpauseSounds(); | 78 | void unpauseSounds(); |
79 | 79 | ||
80 | int getSoundVolume() const { return std::round(static_cast<double>(soundVolume_) / MIX_MAX_VOLUME * 10); } | ||
81 | |||
82 | void setSoundVolume(int vol); | ||
83 | |||
80 | // name is the name of the file, not containing the extension | 84 | // name is the name of the file, not containing the extension |
81 | // ms is the time in milliseconds to fade in | 85 | // ms is the time in milliseconds to fade in |
82 | void playMusic(std::string_view name, int ms = 0); | 86 | void playMusic(std::string_view name, int ms = 0); |
@@ -113,6 +117,7 @@ private: | |||
113 | std::string playingTrack_; | 117 | std::string playingTrack_; |
114 | int musicVolume_ = MIX_MAX_VOLUME; | 118 | int musicVolume_ = MIX_MAX_VOLUME; |
115 | bool musicMuted_ = false; | 119 | bool musicMuted_ = false; |
120 | int soundVolume_ = MIX_MAX_VOLUME; | ||
116 | }; | 121 | }; |
117 | 122 | ||
118 | #endif /* end of include guard: MIXER_H_6DF82000 */ | 123 | #endif /* end of include guard: MIXER_H_6DF82000 */ |