summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/menu.h6
-rw-r--r--src/menu_system.cpp19
-rw-r--r--src/mixer.cpp7
-rw-r--r--src/mixer.h5
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
25class MenuBuilder { 26class 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
3void Mixer::playSound(std::string_view filename) { 4void 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
70void Mixer::setMusicVolume(int vol) {
71 musicVolume_ = MIX_MAX_VOLUME / 10.0 * vol;
72 musicMuted_ = false;
73 Mix_VolumeMusic(musicVolume_);
74}
75
69void Mixer::pauseMusic() { 76void 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();