diff --git a/src/AudioManager.cpp b/src/AudioManager.cpp index d1cb02c17..2d68e5165 100644 --- a/src/AudioManager.cpp +++ b/src/AudioManager.cpp @@ -51,6 +51,34 @@ void AudioManager::mixAudio(void *unused, Uint8 *stream, int len) AudioManager::AudioManager() { + init(); +} + +AudioManager::~AudioManager() +{ + deinit(); +} + +std::shared_ptr & AudioManager::getInstance() +{ + //check if an AudioManager instance is already created, if not create one + if (sInstance == nullptr) { + sInstance = std::shared_ptr(new AudioManager); + } + return sInstance; +} + +void AudioManager::init() +{ + //stop playing all Sounds + for(unsigned int i = 0; i < sSoundVector.size(); i++) + { + if(sSoundVector.at(i)->isPlaying()) + { + sSoundVector[i]->stop(); + } + } + //Set up format and callback. Play 16-bit stereo audio at 44.1Khz sAudioFormat.freq = 44100; sAudioFormat.format = AUDIO_S16; @@ -65,27 +93,35 @@ AudioManager::AudioManager() } } -AudioManager::~AudioManager() +void AudioManager::deinit() { + //stop playing all Sounds + for(unsigned int i = 0; i < sSoundVector.size(); i++) + { + if(sSoundVector.at(i)->isPlaying()) + { + sSoundVector[i]->stop(); + } + } + //pause audio and close SDL connection SDL_PauseAudio(1); SDL_CloseAudio(); } void AudioManager::registerSound(std::shared_ptr & sound) { - //check if an AudioManager instance is already created, if not create one - if (sInstance == nullptr) { - sInstance = std::shared_ptr(new AudioManager); - } + getInstance(); sSoundVector.push_back(sound); } void AudioManager::unregisterSound(std::shared_ptr & sound) { + getInstance(); for(unsigned int i = 0; i < sSoundVector.size(); i++) { if(sSoundVector.at(i) == sound) { + sSoundVector[i]->stop(); sSoundVector.erase(sSoundVector.begin() + i); return; } @@ -95,6 +131,7 @@ void AudioManager::unregisterSound(std::shared_ptr & sound) void AudioManager::play() { + getInstance(); //unpause audio, the mixer will figure out if samples need to be played... SDL_PauseAudio(0); } \ No newline at end of file diff --git a/src/AudioManager.h b/src/AudioManager.h index b20a965b2..21a441cff 100644 --- a/src/AudioManager.h +++ b/src/AudioManager.h @@ -20,6 +20,11 @@ class AudioManager AudioManager(); public: + static std::shared_ptr & getInstance(); + + void init(); + void deinit(); + static void registerSound(std::shared_ptr & sound); static void unregisterSound(std::shared_ptr & sound); diff --git a/src/SystemData.cpp b/src/SystemData.cpp index c4719d5f2..1707a59e1 100644 --- a/src/SystemData.cpp +++ b/src/SystemData.cpp @@ -68,6 +68,7 @@ void SystemData::launchGame(Window* window, GameData* game) { LOG(LogInfo) << "Attempting to launch game..."; + AudioManager::getInstance()->deinit(); window->deinit(); std::string command = mLaunchCommand; @@ -86,6 +87,7 @@ void SystemData::launchGame(Window* window, GameData* game) } window->init(); + AudioManager::getInstance()->init(); } void SystemData::populateFolder(FolderData* folder)