From 0f915ae6a6ae7abf4d37935a249a38926c89e998 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sun, 27 Mar 2011 15:55:55 +0000 Subject: [PATCH] - better handling of sound system reset when clicking ok in options menu --- source/glest_game/main/program.cpp | 32 ++++++++++++++++--- source/glest_game/main/program.h | 3 ++ source/glest_game/menu/menu_state_options.cpp | 3 ++ source/glest_game/sound/sound_renderer.cpp | 29 +++++++++++------ source/glest_game/sound/sound_renderer.h | 2 ++ 5 files changed, 55 insertions(+), 14 deletions(-) diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index 3eb8cc87..197efc87 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -41,6 +41,7 @@ namespace Glest{ namespace Game{ const int Program::maxTimes= 10; Program *Program::singleton = NULL; +const int SOUND_THREAD_UPDATE_MILLISECONDS = 25; // ===================================================== // class Program::CrashProgramState @@ -178,9 +179,11 @@ Program::~Program(){ restoreDisplaySettings(); singleton = NULL; - BaseThread::shutdownAndWait(soundThreadManager); - delete soundThreadManager; - soundThreadManager = NULL; + if(soundThreadManager != NULL) { + BaseThread::shutdownAndWait(soundThreadManager); + delete soundThreadManager; + soundThreadManager = NULL; + } } void Program::keyDown(char key){ @@ -556,7 +559,7 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){ if(BaseThread::shutdownAndWait(soundThreadManager) == true) { delete soundThreadManager; } - soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,10); + soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,SOUND_THREAD_UPDATE_MILLISECONDS); soundThreadManager->setUniqueID(__FILE__); soundThreadManager->start(); } @@ -668,6 +671,27 @@ void Program::showMessage(const char *msg) { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } +void Program::stopSoundSystem() { + if(soundThreadManager != NULL) { + BaseThread::shutdownAndWait(soundThreadManager); + delete soundThreadManager; + soundThreadManager = NULL; + } +} + +void Program::startSoundSystem() { + stopSoundSystem(); + if(SoundRenderer::getInstance().runningThreaded() == true) { + soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,SOUND_THREAD_UPDATE_MILLISECONDS); + soundThreadManager->setUniqueID(__FILE__); + soundThreadManager->start(); + } +} + +void Program::resetSoundSystem() { + startSoundSystem(); +} + void Program::reInitGl() { if(window != NULL) { Config &config= Config::getInstance(); diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index aabb5348..e6953e9e 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -168,6 +168,9 @@ public: bool isInSpecialKeyCaptureEvent() { return programState->isInSpecialKeyCaptureEvent(); } void reInitGl(); + void resetSoundSystem(); + void stopSoundSystem(); + void startSoundSystem(); private: diff --git a/source/glest_game/menu/menu_state_options.cpp b/source/glest_game/menu/menu_state_options.cpp index 6e3dca99..c787c49c 100644 --- a/source/glest_game/menu/menu_state_options.cpp +++ b/source/glest_game/menu/menu_state_options.cpp @@ -790,9 +790,12 @@ void MenuStateOptions::saveConfig(){ SoundRenderer &soundRenderer= SoundRenderer::getInstance(); soundRenderer.stopAllSounds(); + program->stopSoundSystem(); bool initOk = soundRenderer.init(program->getWindow()); soundRenderer.loadConfig(); soundRenderer.setMusicVolume(CoreData::getInstance().getMenuMusic()); + program->startSoundSystem(); + soundRenderer.playMusic(CoreData::getInstance().getMenuMusic()); Renderer::getInstance().loadConfig(); diff --git a/source/glest_game/sound/sound_renderer.cpp b/source/glest_game/sound/sound_renderer.cpp index 97615b65..e9fb6838 100644 --- a/source/glest_game/sound/sound_renderer.cpp +++ b/source/glest_game/sound/sound_renderer.cpp @@ -53,6 +53,7 @@ bool SoundRenderer::init(Window *window) { Config &config= Config::getInstance(); si.setFactory(fr.getSoundFactory(config.getString("FactorySound"))); + cleanup(); stopAllSounds(); MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); @@ -74,6 +75,23 @@ bool SoundRenderer::init(Window *window) { return wasInitOk(); } +void SoundRenderer::cleanup() { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + stopAllSounds(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + + MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); + if(runThreadSafe == true) { + safeMutex.setMutex(&mutex); + } + delete soundPlayer; + soundPlayer = NULL; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); +} + bool SoundRenderer::wasInitOk() const { bool result = false; if(soundPlayer != NULL) { @@ -92,16 +110,7 @@ bool SoundRenderer::wasInitOk() const { SoundRenderer::~SoundRenderer() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - stopAllSounds(); - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(&mutex); - } - delete soundPlayer; - soundPlayer = NULL; + cleanup(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/glest_game/sound/sound_renderer.h b/source/glest_game/sound/sound_renderer.h index 701ebea9..c2292314 100644 --- a/source/glest_game/sound/sound_renderer.h +++ b/source/glest_game/sound/sound_renderer.h @@ -52,6 +52,8 @@ private: private: SoundRenderer(); + void cleanup(); + public: //misc ~SoundRenderer();