From 1a043ab411d75ab66181a3d69077a900ff66bf34 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Wed, 28 May 2014 19:56:28 +0200 Subject: [PATCH] Using ResourceManager for cached laoding of sounds. --- src/InputManager.cpp | 16 ++++++++-------- src/InputManager.h | 2 +- src/Sound.cpp | 10 ++++++++-- src/Sound.h | 10 +++++++++- src/Systems/SoundSystem.cpp | 11 ++++------- src/Systems/SoundSystem.h | 2 +- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/InputManager.cpp b/src/InputManager.cpp index cb56824..1a88e95 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -42,14 +42,14 @@ void InputManager::Update(double dt) } } -// if(m_CurrentKeyState[GLFW_KEY_Z]) //TEMP -// { -// Events::PlaySound e; -// e.Resource = "Sounds/BGM/WUB.mp3"; -// e.Emitter = 66; -// e.Loop = true; -// EventBroker->Publish(e); -// } + if(m_CurrentKeyState[GLFW_KEY_Z]) //TEMP + { + Events::PlaySound e; + e.Resource = "Sounds/BGM/WUB.mp3"; + e.Emitter = 66; + e.Loop = true; + EventBroker->Publish(e); + } // Mouse buttons for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) diff --git a/src/InputManager.h b/src/InputManager.h index e6cc259..3326070 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -12,7 +12,7 @@ #include "Events/LockMouse.h" #include "Events/GamepadAxis.h" #include "Events/GamepadButton.h" -//#include "Events/PlaySound.h" //Temp +#include "Events/PlaySound.h" //Temp class InputManager { diff --git a/src/Sound.cpp b/src/Sound.cpp index 853af6c..ddd5311 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -1,7 +1,13 @@ #include "PrecompiledHeader.h" #include "Sound.h" -Sound::Sound(std::string path) -{ +Sound::Sound(std::string path, FMOD_SYSTEM* system) +{ + FMOD_System_CreateSound(system, path.c_str(), FMOD_3D, NULL, &m_Sound); } + +Sound::~Sound() +{ + +} \ No newline at end of file diff --git a/src/Sound.h b/src/Sound.h index d36ab20..f077fc4 100644 --- a/src/Sound.h +++ b/src/Sound.h @@ -2,11 +2,19 @@ #define Sound_h__ #include "ResourceManager.h" +#include +#include class Sound : public Resource { public: - Sound(std::string path); + Sound(std::string path, FMOD_SYSTEM* system); + ~Sound(); + + operator FMOD_SOUND*() const { return m_Sound; } + +private: + FMOD_SOUND* m_Sound; }; #endif // Sound_h__ diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index 3339546..b022366 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -30,7 +30,7 @@ void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf) void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm) { - rm->RegisterType("Sound", [](std::string resourceName) { return new Sound(resourceName); }); + rm->RegisterType("Sound", [this](std::string resourceName) { return new Sound(resourceName, this->m_System); }); } void Systems::SoundSystem::Update(double dt) @@ -132,12 +132,9 @@ bool Systems::SoundSystem::PlayASound(const Events::PlaySound &event) 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; -// } + Sound* sound = m_World->GetResourceManager()->Load("Sound", event.Resource); + m_Sounds[event.Emitter] = *sound; + //LoadSound(m_Sounds[event.Emitter], event.Resource, 1000.f, 1.f); PlaySound(&m_Channels[event.Emitter], m_Sounds[event.Emitter], 1.0, event.Loop); FMOD_System_Update(m_System); diff --git a/src/Systems/SoundSystem.h b/src/Systems/SoundSystem.h index edd322e..e1a5406 100755 --- a/src/Systems/SoundSystem.h +++ b/src/Systems/SoundSystem.h @@ -28,7 +28,7 @@ public: void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void OnComponentRemoved(std::string type, Component* component); - + FMOD_SYSTEM* GetFMODSystem() const { return m_System; } private: // Events EventRelay m_EComponentCreated;