From bd3ffa8a07fa5c7cb7c21701eb07d0cb90cf96a9 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Wed, 18 Jun 2008 20:16:07 +0100 Subject: [PATCH] Enable/disable sound effects via options menu --- src/Main.cpp | 3 +++ src/Options.cpp | 15 ++++++++++++++- src/SoundEffect.cpp | 4 +++- src/SoundEffect.hpp | 4 ++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index e3b9171..7246dba 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -20,6 +20,7 @@ #include "Game.hpp" #include "Options.hpp" #include "ConfigFile.hpp" +#include "SoundEffect.hpp" #include @@ -76,12 +77,14 @@ int main(int argc, char **argv) const int DEFAULT_HRES = 800; const int DEFAULT_VRES = 600; const int DEFAULT_FSCREEN = false; + const int DEFAULT_SOUND = true; { ConfigFile cfile; width = cfile.get_int("hres", DEFAULT_HRES); height = cfile.get_int("vres", DEFAULT_VRES); fullscreen = cfile.get_bool("fullscreen", DEFAULT_FSCREEN); + SoundEffect::SetEnabled(cfile.get_bool("sound", DEFAULT_SOUND)); } #ifdef WIN32 diff --git a/src/Options.cpp b/src/Options.cpp index 6893147..bbda0e4 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -19,6 +19,7 @@ #include "Menu.hpp" #include "OpenGL.hpp" #include "Input.hpp" +#include "SoundEffect.hpp" const double Options::FADE_SPEED = 0.1; @@ -53,8 +54,14 @@ Options::Options() } } + Item sound = { "Sound Effects" }; + sound.values.push_back("On"); + sound.values.push_back("Off"); + sound.active = (cfile.get_bool("sound") ? 0 : 1); + items.push_back(fullscreen); items.push_back(resolution); + items.push_back(sound); } void Options::Load() @@ -117,12 +124,18 @@ void Options::Apply() cfile.put("fullscreen", fullscreen); } - if ((*it).name == "Resolution") { + else if ((*it).name == "Resolution") { ParseResolutionString((*it).values[(*it).active], &hres, &vres); cfile.put("hres", hres); cfile.put("vres", vres); } + else if ((*it).name == "Sound Effects") { + bool sound = (*it).active == 0; + SoundEffect::SetEnabled(sound); + + cfile.put("sound", sound); + } } assert(hres > 0 && vres > 0); diff --git a/src/SoundEffect.cpp b/src/SoundEffect.cpp index 111e1fb..c7d8a37 100644 --- a/src/SoundEffect.cpp +++ b/src/SoundEffect.cpp @@ -22,6 +22,7 @@ int SoundEffect::audioRate(22050); int SoundEffect::audioChannels(2); int SoundEffect::audioBuffers(4096); Uint16 SoundEffect::audioFormat(AUDIO_S16); +bool SoundEffect::enabled(true); SoundEffect::SoundEffect(const char *filename) : channel(-1) @@ -60,5 +61,6 @@ SoundEffect::~SoundEffect() void SoundEffect::Play() { - channel = Mix_PlayChannel(-1, sound, 0); + if (enabled) + channel = Mix_PlayChannel(-1, sound, 0); } diff --git a/src/SoundEffect.hpp b/src/SoundEffect.hpp index 9fb8479..bcf6b7c 100644 --- a/src/SoundEffect.hpp +++ b/src/SoundEffect.hpp @@ -27,6 +27,8 @@ public: SoundEffect(const char *filename); ~SoundEffect(); + static void SetEnabled(bool state) { enabled = state; } + void Play(); private: Mix_Chunk *sound; @@ -35,6 +37,8 @@ private: static int loadCount; static int audioChannels, audioBuffers, audioRate; static Uint16 audioFormat; + + static bool enabled; }; #endif -- 2.39.2