From 2ef5d49f7e4554e1b247a1c8f2c965a5dea1cc6b Mon Sep 17 00:00:00 2001 From: Stiffly Date: Wed, 4 Jun 2014 15:21:05 +0200 Subject: [PATCH] Added Destructor. (improved StopSound and PlayBGM events) --- assets | 2 +- src/GameWorld.cpp | 7 ++++ src/InputManager.cpp | 22 ++++++++++ src/InputManager.h | 3 ++ src/Systems/SoundSystem.cpp | 42 ++++++++++++------- src/Systems/SoundSystem.h | 5 +-- .../Returngeance/Returngeance.vcxproj.filters | 12 ++---- 7 files changed, 64 insertions(+), 29 deletions(-) diff --git a/assets b/assets index e09ebf5..cfda101 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit e09ebf5a29f007cb8ee884f48dc600926f0665bc +Subproject commit cfda101f676fde45f9fd362b99fcec4f99e79cf1 diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index acf8db1..1dcb922 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -8,6 +8,13 @@ void GameWorld::Initialize() ResourceManager->Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj"); ResourceManager->Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj"); + //Background Music + { + Events::PlayBGM e; + e.Resource = "Sounds/BGM/RideoftheValkyries.mp3"; + EventBroker->Publish(e); + } + // Interface BindKey(GLFW_KEY_A, "interface_horizontal", -1.f); BindKey(GLFW_KEY_D, "interface_horizontal", 1.f); diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 01d61ba..97494c8 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -85,6 +85,28 @@ void InputManager::Update(double dt) EventBroker->Publish(e); } + + if(m_CurrentKeyState[GLFW_KEY_P]) + { + Events::StopSound e; + e.Emitter = 1; + EventBroker->Publish(e); + } + if(m_CurrentKeyState[GLFW_KEY_L]) + { + Events::PlayBGM e; + e.Resource = "Sounds/BGM/WilliamTellOverture.mp3"; + e.Loop = true; + EventBroker->Publish(e); + } + if(m_CurrentKeyState[GLFW_KEY_O]) + { + Events::PlayBGM e; + e.Resource = "Sounds/BGM/DiesIrae.mp3"; + e.Loop = true; + EventBroker->Publish(e); + } + // // Lock mouse while holding LMB // if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT]) diff --git a/src/InputManager.h b/src/InputManager.h index 69827ba..bb30918 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -13,6 +13,9 @@ #include "Events/GamepadAxis.h" #include "Events/GamepadButton.h" +#include "Events/StopSound.h" +#include "Events/PlayBGM.h" + class InputManager { public: diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index 1617078..cfb104e 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -2,13 +2,18 @@ #include "SoundSystem.h" #include "World.h" +Systems::SoundSystem::~SoundSystem() +{ + FMOD_System_Release(m_System); +} + void Systems::SoundSystem::Initialize() { EVENT_SUBSCRIBE_MEMBER(m_EComponentCreated, &SoundSystem::OnComponentCreated); EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::PlaySFX); EVENT_SUBSCRIBE_MEMBER(m_EPlayBGM, &SoundSystem::PlayBGM); EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::StopSound); - + m_isPlaying = false; m_TransformSystem = m_World->GetSystem(); FMOD_System_Create(&m_System); FMOD_RESULT result = FMOD_System_Init(m_System, 32, FMOD_INIT_3D_RIGHTHANDED | FMOD_INIT_ENABLE_PROFILE, 0); @@ -44,9 +49,8 @@ void Systems::SoundSystem::Update(double dt) std::map::iterator it; for(it = m_DeleteChannels.begin(); it != m_DeleteChannels.end();) { - FMOD_BOOL isPlaying = false; - FMOD_Channel_IsPlaying(it->second, &isPlaying); - if(isPlaying) + FMOD_Channel_IsPlaying(it->second, &m_isPlaying); + if(m_isPlaying) { it++; } @@ -58,13 +62,12 @@ void Systems::SoundSystem::Update(double dt) } } - //LOG_INFO("Antal emitters i listan: %i", m_Channels.size()); + LOG_INFO("Antal emitters i listan: %i", m_Channels.size()); for(it = m_Channels.begin(); it != m_Channels.end();) { - FMOD_BOOL isPlaying = false; - FMOD_Channel_IsPlaying(it->second, &isPlaying); - if(!isPlaying) + FMOD_Channel_IsPlaying(it->second, &m_isPlaying); + if(!m_isPlaying) { it = m_Channels.erase(it); } @@ -163,21 +166,28 @@ bool Systems::SoundSystem::PlaySFX(const Events::PlaySFX &event) bool Systems::SoundSystem::PlayBGM(const Events::PlayBGM &event) { - auto emitter = m_World->CreateEntity(); - auto eTransform = m_World->AddComponent(emitter); - auto eComponent = m_World->AddComponent(emitter); - m_Channels[emitter] = m_BGMChannel; - eComponent->type = Components::SoundEmitter::SoundType::SOUND_3D; + + FMOD_Channel_Stop(m_BGMChannel); Sound* sound = ResourceManager->Load("Sound2D", event.Resource); - //m_Sounds[emitter] = *sound; - FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, *sound, false, &m_Channels[emitter]); + FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, *sound, false, &m_BGMChannel); + FMOD_Channel_SetMode(m_BGMChannel, FMOD_LOOP_NORMAL); + FMOD_Sound_SetLoopCount(*sound, -1); + return true; } bool Systems::SoundSystem::StopSound(const Events::StopSound &event) { - FMOD_Channel_Stop(m_Channels[event.Emitter]); + auto eComp = m_World->GetComponent(event.Emitter); + if(eComp) + { + FMOD_Channel_Stop(m_Channels[event.Emitter]); + } + else + { + FMOD_Channel_Stop(m_BGMChannel); + } return true; } diff --git a/src/Systems/SoundSystem.h b/src/Systems/SoundSystem.h index 66ef6bc..6d3b48d 100755 --- a/src/Systems/SoundSystem.h +++ b/src/Systems/SoundSystem.h @@ -24,13 +24,12 @@ public: SoundSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) : System(world, eventBroker, resourceManager) { } - + ~SoundSystem(); void RegisterComponents(ComponentFactory* cf) override; void RegisterResourceTypes(std::shared_ptr<::ResourceManager> rm) override; void Initialize() override; void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; - //void OnComponentRemoved(std::string type, Component* component); void OnComponentRemoved(EntityID entity, std::string type, Component* component) override; FMOD_SYSTEM* GetFMODSystem() const { return m_System; } private: @@ -48,11 +47,11 @@ private: void PlaySound(FMOD_CHANNEL**, FMOD_SOUND*, float volume, bool loop); FMOD_SYSTEM* m_System; + FMOD_BOOL m_isPlaying; FMOD_CHANNEL* m_BGMChannel; std::vector m_Listeners; std::map m_Channels; std::map m_DeleteChannels; - //std::map m_Sounds; std::map m_DeleteSounds; std::shared_ptr m_TransformSystem; diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index fed4ecf..1c02897 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -78,15 +78,13 @@ Game\Systems - - Gameplay\Systems - Game\Systems Audio + @@ -526,9 +524,6 @@ Rendering\Components - - Gameplay\Systems - Physics\Events @@ -565,9 +560,6 @@ GUI - - Gameplay\Events - Game\Events @@ -577,6 +569,8 @@ Game\Components + +