From 3b96be216217003dc6177d178e634ef1791a3942 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 27 May 2014 00:36:54 +0200 Subject: [PATCH] Fixed playsound event. Added remove sounds OnComponentsRemove only when stopped playing. --- assets | 2 +- src/Events/PlaySound.h | 1 + src/GameWorld.cpp | 46 ++++++---------- src/InputManager.cpp | 7 +-- src/Renderer.cpp | 2 +- src/Systems/SoundSystem.cpp | 105 ++++++++++++++++++++++++------------ src/Systems/SoundSystem.h | 21 ++++---- 7 files changed, 104 insertions(+), 80 deletions(-) diff --git a/assets b/assets index 6cc3858..2991401 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6cc38589ed77dbb3b9c34c1168d52e0f86e1de46 +Subproject commit 2991401c3190f0b9de54d112d8de6fbdbd7abe59 diff --git a/src/Events/PlaySound.h b/src/Events/PlaySound.h index 86e5851..f84db01 100644 --- a/src/Events/PlaySound.h +++ b/src/Events/PlaySound.h @@ -11,6 +11,7 @@ struct PlaySound : Event { EntityID Emitter; std::string Resource; + bool Loop; }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 3edaff5..12edd02 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -54,19 +54,7 @@ void GameWorld::Initialize() auto cameraComp = AddComponent(camera); cameraComp->FarClip = 2000.f; auto freeSteering = AddComponent(camera); - { - auto se1 = CreateEntity(camera); - auto transform = AddComponent(se1); - transform->Position = glm::vec3(0,10,0); - transform->Scale = glm::vec3(3); - auto emitterComponent = AddComponent(se1); - emitterComponent->Path = "Sounds/WUB.mp3"; - emitterComponent->MinDistance = 1.f; - emitterComponent->MaxDistance = 10000.f; - emitterComponent->Gain = 1; -// auto model = AddComponent(se1); -// model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj"; - } + } CommitEntity(camera); @@ -1155,24 +1143,20 @@ void GameWorld::Initialize() } }*/ -// { -// -// } -// -// { -// auto se2 = CreateEntity(); -// auto transform = AddComponent(se2); -// transform->Position = glm::vec3(100,10,0); -// transform->Scale = glm::vec3(3); -// auto emitterComponent = AddComponent(se2); -// emitterComponent->Path = "Sounds/DarkHorse.mp3"; -// emitterComponent->Gain = 1; -// emitterComponent->MinDistance = 1.f; -// emitterComponent->MaxDistance = 50.f; -// auto model = AddComponent(se2); -// model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj"; -// CommitEntity(se2); -// } + + { + auto se1 = CreateEntity(); + auto transform = AddComponent(se1); + transform->Position = glm::vec3(0,-30,0); + transform->Scale = glm::vec3(3); + auto emitterComponent = AddComponent(se1); + emitterComponent->Path = "Sounds/WUB.mp3"; + emitterComponent->MinDistance = 100.f; + emitterComponent->MaxDistance = 1000.f; + emitterComponent->Gain = 1; + auto model = AddComponent(se1); + model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj"; + } /*for (int x = 0; x < 5; x++) diff --git a/src/InputManager.cpp b/src/InputManager.cpp index d2bd687..4efd94f 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -42,11 +42,12 @@ void InputManager::Update(double dt) } } - if(m_CurrentKeyState[GLFW_KEY_BACKSPACE]) + if(m_CurrentKeyState[GLFW_KEY_Z]) { Events::PlaySound e; - e.Resource = "Sounds/WUB.mp3"; - e.Emitter = 2; + e.Resource = "Sounds/BGM/MainMenu.mp3"; + e.Emitter = 66; + e.Loop = true; EventBroker->Publish(e); } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 19c00f0..9f32642 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -17,7 +17,7 @@ Renderer::Renderer() CAtt = 1.0f; LAtt = 0.0f; QAtt = 3.0f; - m_ShadowMapRes = 2048*6; + m_ShadowMapRes = 2048*0 + 2; m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); m_SunProjection = glm::ortho(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index f6cd57f..62836a6 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -5,25 +5,19 @@ void Systems::SoundSystem::Initialize() { EVENT_SUBSCRIBE_MEMBER(m_EComponentCreated, &SoundSystem::OnComponentCreated); - /*FMOD_RESULT result; - result = FMOD::System_Create(&m_FmodSystem); - result = m_FmodSystem->init(32, FMOD_INIT_NORMAL, 0); - - - FMOD::Sound *sound; - FMOD::Channel *channel; - FMOD_VECTOR pos; - pos.x = 0.f; - pos.y = 0.f; - pos.z = 0.f; - m_FmodSystem->createSound("Sounds/WUB.mp3", FMOD_HARDWARE, 0, &sound); - m_FmodSystem->playSound(FMOD_CHANNEL_FREE, sound, false, 0);*/ - + EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::PlayASound); + FMOD_System_Create(&m_System); - FMOD_System_Init(m_System, 1024, FMOD_INIT_NORMAL, nullptr); + FMOD_RESULT result = FMOD_System_Init(m_System, 1024, FMOD_INIT_NORMAL, nullptr); + if(result != FMOD_OK) + { + LOG_ERROR("Did not load FMOD correctly"); + } + else + { + LOG_INFO("FMOD Initialized"); + } FMOD_System_Set3DSettings(m_System, 10.0f, 1.f, 100.f); //dopplerScale, distancefactor, rolloffscale - //FMOD_System_GetMasterChannelGroup(m_System, &m_ChannelGruoup); - m_Playing = false; } @@ -41,6 +35,25 @@ void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm) void Systems::SoundSystem::Update(double dt) { FMOD_System_Update(m_System); + + //Delete sounds. Not until it's done playing + 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) + { + it++; + } + else + { + FMOD_Sound_Release(m_DeleteSounds[it->first]); + m_DeleteSounds.erase(it->first); + it = m_DeleteChannels.erase(it); + } + + } } void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -78,14 +91,8 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par glm::vec3 tVel = eTransform->Velocity; FMOD_VECTOR eVel = {tVel.x, tVel.y, tVel.z}; - + FMOD_Channel_Set3DAttributes(channel, (const FMOD_VECTOR*)&ePos, (const FMOD_VECTOR*)&eVel); - - if(!m_Playing) - { - PlaySound(channel, sound, 1, emitter->Loop); - m_Playing = true; - } } } @@ -109,30 +116,62 @@ bool Systems::SoundSystem::OnComponentCreated(const Events::ComponentCreated &ev float maxDist = emitter->MaxDistance; float minDist = emitter->MinDistance; float pitch = emitter->Pitch; - - if(LoadSound(sound, path, maxDist, minDist, loop, volume, pitch) != FMOD_OK) - LOG_ERROR("FMOD did not load sound file"); - std::cout<Entity, channel)); m_Sounds.insert(std::make_pair(emitter->Entity, sound)); } - return true; } +bool Systems::SoundSystem::PlayASound(const Events::PlaySound &event) +{ + auto emitter = m_World->GetComponent(event.Emitter); + if(!emitter) + { + LOG_ERROR("FMOD: The Entity %i does not have a SoundEmitter-component, but is trying to play a sound", event.Emitter);FMOD_CHANNEL* channel = m_Channels[event.Emitter]; + return false; + } + if(emitter->Path != event.Resource) //Caching sound file + { + LoadSound(m_Sounds[event.Emitter], event.Resource, 1000.f, 1.f); + emitter->Path = event.Resource; + } + PlaySound( m_Channels[event.Emitter], m_Sounds[event.Emitter], 1, event.Loop); + return true; +} -FMOD_RESULT Systems::SoundSystem::LoadSound(FMOD_SOUND* &sound, std::string filePath, float maxDist, float minDist, bool loop, float volume, float pitch) +void Systems::SoundSystem::LoadSound(FMOD_SOUND* &sound, std::string filePath, float maxDist, float minDist) { FMOD_RESULT result = FMOD_System_CreateSound(m_System, filePath.c_str(), FMOD_3D , 0, &sound); - FMOD_Sound_Set3DMinMaxDistance(m_Sound, minDist, maxDist); - - return result; + if (result != FMOD_OK) + { + LOG_ERROR("FMOD did not load file: %s", filePath.c_str()); + } + FMOD_Sound_Set3DMinMaxDistance(sound, minDist, maxDist); } void Systems::SoundSystem::PlaySound(FMOD_CHANNEL* channel, FMOD_SOUND* sound, float volume, bool loop) { FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, sound, false, &channel); FMOD_Channel_SetVolume(channel, volume); + if(loop) + { + FMOD_Channel_SetMode(channel, FMOD_LOOP_NORMAL); + FMOD_Sound_SetLoopCount(sound, -1); + } +} + +void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* component) +{ + auto emitter = dynamic_cast(component); + if(emitter) + { + EntityID ent = component->Entity; + m_DeleteChannels.insert(std::make_pair(ent, m_Channels[ent])); + m_DeleteSounds.insert(std::make_pair(ent, m_Sounds[ent])); + m_Channels.erase(ent); + m_Sounds.erase(ent); + } } \ No newline at end of file diff --git a/src/Systems/SoundSystem.h b/src/Systems/SoundSystem.h index 8d9d25e..ef93583 100755 --- a/src/Systems/SoundSystem.h +++ b/src/Systems/SoundSystem.h @@ -26,26 +26,25 @@ public: void Initialize() override; void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + void OnComponentRemoved(std::string type, Component* component); private: // Events EventRelay m_EComponentCreated; bool OnComponentCreated(const Events::ComponentCreated &event); EventRelay m_EPlaySound; + bool PlayASound(const Events::PlaySound &event); - FMOD_RESULT LoadSound(FMOD_SOUND*&, std::string, float, float, bool, float, float); + void LoadSound(FMOD_SOUND*&, std::string, float, float); void PlaySound(FMOD_CHANNEL*, FMOD_SOUND*, float volume, bool loop); - void RemoveEmitter(EntityID ent); - std::vector m_Listeners; - FMOD_SYSTEM* m_System; - FMOD_CHANNELGROUP* m_ChannelGruoup; - //std::map m_Channels; - std::map m_Channels; - std::map m_Sounds; - FMOD::System *m_FmodSystem; - FMOD_SOUND* m_Sound; //Temp - bool m_Playing; //TEMP + FMOD_SYSTEM* m_System; + std::vector m_Listeners; + std::map m_Channels; + std::map m_DeleteChannels; + std::map m_Sounds; + std::map m_DeleteSounds; + }; }