From 20d46f63c87ed4db85f3f08578d08bd5057cffdd Mon Sep 17 00:00:00 2001 From: stiffly Date: Sun, 13 Mar 2016 00:16:55 +0100 Subject: [PATCH] The main menu music would not start again when you returned to the main menu. --- include/Engine/Sound/SoundManager.h | 3 +++ src/Engine/Sound/SoundManager.cpp | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index ff9f440a..87a762fc 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -30,6 +30,7 @@ #include "../Engine/Core/EPause.h" #include "../Engine/Core/EComponentAttached.h" #include "../Core/EPlayerSpawned.h" +#include "../Engine/Network/EPlayerDisconnected.h" typedef std::pair> QueuedBuffers; @@ -139,6 +140,8 @@ private: bool OnPlayQueueOnEntity(const Events::PlayQueueOnEntity &e); EventRelay m_EChangeBGM; bool OnChangeBGM(const Events::ChangeBGM &e); + EventRelay m_EplayerDisconnected; + bool OnPlayerDisconnected(const Events::PlayerDisconnected& e); }; diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index 2495fdd6..520a9c1d 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -30,6 +30,7 @@ SoundManager::SoundManager(World* world, EventBroker* eventBroker) EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundManager::OnPlayerSpawned); EVENT_SUBSCRIBE_MEMBER(m_EPlayQueueOnEntity, &SoundManager::OnPlayQueueOnEntity); EVENT_SUBSCRIBE_MEMBER(m_EChangeBGM, &SoundManager::OnChangeBGM); + EVENT_SUBSCRIBE_MEMBER(m_EplayerDisconnected, &SoundManager::OnPlayerDisconnected); Events::ChangeBGM e; e.FilePath = "Audio/bgm/MenuMusic.wav"; @@ -394,7 +395,9 @@ bool SoundManager::OnPlayQueueOnEntity(const Events::PlayQueueOnEntity &e) bool SoundManager::OnChangeBGM(const Events::ChangeBGM &e) { if (m_CurrentBGM != nullptr) { - stopSound(m_CurrentBGM); + if (getSourceState(m_CurrentBGM->ALsource) == AL_PLAYING) { + stopSound(m_CurrentBGM); + } } m_CurrentBGM = createSource(e.FilePath); m_CurrentBGM->Type = SoundType::BGM; @@ -403,6 +406,22 @@ bool SoundManager::OnChangeBGM(const Events::ChangeBGM &e) return true; } + +bool SoundManager::OnPlayerDisconnected(const Events::PlayerDisconnected& e) +{ + // A player returned to the main menu. Easiest solution when we don't have states in the game. Not "PC". + if (m_CurrentBGMCombo != nullptr) { + if (getSourceState(m_CurrentBGMCombo->ALsource) == AL_PLAYING) { + stopSound(m_CurrentBGMCombo); + } + } + stopSound(m_CurrentBGMCombo); + Events::ChangeBGM changeBGM; + changeBGM.FilePath = "Audio/BGM/MenuMusic.wav"; + m_EventBroker->Publish(changeBGM); + return true; +} + ALenum SoundManager::getSourceState(ALuint source) { ALenum state;