Separated PlaySound into PlaySFX and PlayBGM.

This commit is contained in:
Stiffly
2014-05-28 22:06:26 +02:00
parent 1a043ab411
commit 1b2f930afa
13 changed files with 87 additions and 27 deletions
+6
View File
@@ -10,6 +10,11 @@ namespace Components
struct SoundEmitter : Component
{
enum SoundType
{
BGM_SOUND,
SFX_SOUND
};
SoundEmitter() : Gain(1.f), MaxDistance(1.f), MinDistance(1.f), Pitch(1.f), Loop(false) {}
float Gain;
float MaxDistance;
@@ -17,6 +22,7 @@ struct SoundEmitter : Component
float Pitch;
bool Loop;
std::string Path;
SoundType type;
virtual SoundEmitter* Clone() const override { return new SoundEmitter(*this); }
};
+18
View File
@@ -0,0 +1,18 @@
#ifndef Event_PlayBGM_h__
#define Event_PlayBGM_h__
#include "EventBroker.h"
#include "Entity.h"
namespace Events
{
struct PlayBGM : Event
{
std::string Resource;
bool Loop;
};
}
#endif // Event_PlayBGM_h__
@@ -1,5 +1,5 @@
#ifndef Event_PlaySound_h__
#define Event_PlaySound_h__
#ifndef Event_PlaySFX_h__
#define Event_PlaySFX_h__
#include "EventBroker.h"
#include "Entity.h"
@@ -7,7 +7,7 @@
namespace Events
{
struct PlaySound : Event
struct PlaySFX : Event
{
EntityID Emitter;
std::string Resource;
@@ -16,4 +16,4 @@ struct PlaySound : Event
}
#endif // Event_PlaySound_h__
#endif // Event_PlaySFX_h__
+1 -1
View File
@@ -1092,7 +1092,7 @@ void GameWorld::Initialize()
auto se1 = CreateEntity();
std::cout<<se1<<std::endl;
auto transform = AddComponent<Components::Transform>(se1);
transform->Position = glm::vec3(20,2,-15);
transform->Position = glm::vec3(20,2,-100);
transform->Scale = glm::vec3(3);
auto emitterComponent = AddComponent<Components::SoundEmitter>(se1);
emitterComponent->Path = "Sounds/WUB.mp3";
+1 -2
View File
@@ -44,9 +44,8 @@ void InputManager::Update(double dt)
if(m_CurrentKeyState[GLFW_KEY_Z]) //TEMP
{
Events::PlaySound e;
Events::PlayBGM e;
e.Resource = "Sounds/BGM/WUB.mp3";
e.Emitter = 66;
e.Loop = true;
EventBroker->Publish(e);
}
+2 -1
View File
@@ -12,7 +12,8 @@
#include "Events/LockMouse.h"
#include "Events/GamepadAxis.h"
#include "Events/GamepadButton.h"
#include "Events/PlaySound.h" //Temp
#include "Events/PLaySFX.h" //Temp
#include "Events/PLayBGM.h" //Temp
class InputManager
{
+1 -1
View File
@@ -9,5 +9,5 @@ Sound::Sound(std::string path, FMOD_SYSTEM* system)
Sound::~Sound()
{
FMOD_Sound_Release(m_Sound);
}
+2 -2
View File
@@ -19,10 +19,10 @@ bool Systems::DebugSystem::OnKeyDown(const Events::KeyDown &event)
{
if (event.KeyCode == GLFW_KEY_ENTER)
{
Events::PlaySound e;
Events::PlaySFX e;
e.Emitter = 0;
e.Resource = "Sounds/korvring.wav";
EventBroker->Publish<Events::PlaySound>(e);
EventBroker->Publish<Events::PlaySFX>(e);
return true;
}
+1 -1
View File
@@ -4,7 +4,7 @@
#include "System.h"
#include "Components/Transform.h"
#include "Events/KeyDown.h"
#include "Events/PlaySound.h"
#include "Events/PlaySFX.h"
namespace Systems
{
+34 -7
View File
@@ -5,7 +5,8 @@
void Systems::SoundSystem::Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EComponentCreated, &SoundSystem::OnComponentCreated);
EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::PlayASound);
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::PlaySFX);
EVENT_SUBSCRIBE_MEMBER(m_EPlayBGM, &SoundSystem::PlayBGM);
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
FMOD_System_Create(&m_System);
@@ -18,7 +19,7 @@ void Systems::SoundSystem::Initialize()
{
LOG_INFO("FMOD initialized successfully");
}
FMOD_System_Set3DSettings(m_System, 10.f, 1.f, 1.f); //dopplerScale, distancefactor, rolloffscale
FMOD_System_Set3DSettings(m_System, 1.f, 1.f, 1.f); //dopplerScale, distancefactor, rolloffscale
}
@@ -85,7 +86,20 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
FMOD_CHANNEL* channel = m_Channels[entity];
FMOD_SOUND* sound = m_Sounds[entity];
auto eTransform = m_World->GetComponent<Components::Transform>(entity);
if(emitter->type == emitter->BGM_SOUND)
{
EntityID listener = m_Listeners[0]; // "Pretty" ugly hack... Sets the transform Component for emitter to be same as first listener
auto lTransform = m_World->GetComponent<Components::Transform>(listener);
if(lTransform)
{
eTransform->Position = m_TransformSystem->AbsolutePosition(listener);
eTransform->Velocity = lTransform->Velocity;
}
}
glm::vec3 tPos = m_TransformSystem->AbsolutePosition(entity);
FMOD_VECTOR ePos = {tPos.x, tPos.y, tPos.z};
@@ -116,16 +130,15 @@ bool Systems::SoundSystem::OnComponentCreated(const Events::ComponentCreated &ev
float maxDist = emitter->MaxDistance;
float minDist = emitter->MinDistance;
float pitch = emitter->Pitch;
LoadSound(sound, path, maxDist, minDist);
//LoadSound(sound, path, maxDist, minDist);
m_Channels.insert(std::make_pair(emitter->Entity, channel));
m_Sounds.insert(std::make_pair(emitter->Entity, sound));
}
return true;
}
bool Systems::SoundSystem::PlayASound(const Events::PlaySound &event)
bool Systems::SoundSystem::PlaySFX(const Events::PlaySFX &event)
{
auto emitter = m_World->GetComponent<Components::SoundEmitter>(event.Emitter);
if(!emitter)
{
@@ -134,7 +147,6 @@ bool Systems::SoundSystem::PlayASound(const Events::PlaySound &event)
}
Sound* sound = m_World->GetResourceManager()->Load<Sound>("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);
@@ -148,6 +160,21 @@ bool Systems::SoundSystem::PlayASound(const Events::PlaySound &event)
return true;
}
bool Systems::SoundSystem::PlayBGM(const Events::PlayBGM &event)
{
auto emitter = m_World->CreateEntity();
auto eTransform = m_World->AddComponent<Components::Transform>(emitter);
auto eComponent = m_World->AddComponent<Components::SoundEmitter>(emitter);
eComponent->type = eComponent->BGM_SOUND;
Sound* sound = m_World->GetResourceManager()->Load<Sound>("Sound", event.Resource);
m_Sounds[emitter] = *sound;
FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, *sound, false, &m_Channels[emitter]);
return true;
}
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 | FMOD_HARDWARE , 0, &sound);
+6 -3
View File
@@ -10,7 +10,8 @@
#include "Components/SoundEmitter.h"
#include "Components/Listener.h"
#include "Events/ComponentCreated.h"
#include "Events/PlaySound.h"
#include "Events/PlaySFX.h"
#include "Events/PlayBGM.h"
#include "Sound.h"
namespace Systems
@@ -33,8 +34,10 @@ private:
// Events
EventRelay<SoundSystem, Events::ComponentCreated> m_EComponentCreated;
bool OnComponentCreated(const Events::ComponentCreated &event);
EventRelay<SoundSystem, Events::PlaySound> m_EPlaySound;
bool PlayASound(const Events::PlaySound &event);
EventRelay<SoundSystem, Events::PlaySFX> m_EPlaySFX;
bool PlaySFX(const Events::PlaySFX &event);
EventRelay<SoundSystem, Events::PlayBGM> m_EPlayBGM;
bool PlayBGM(const Events::PlayBGM &event);
void LoadSound(FMOD_SOUND*&, std::string, float, float);
void PlaySound(FMOD_CHANNEL**, FMOD_SOUND*, float volume, bool loop);
+2 -1
View File
@@ -174,7 +174,8 @@
<ClInclude Include="..\..\src\Events\MouseMove.h" />
<ClInclude Include="..\..\src\Events\MousePress.h" />
<ClInclude Include="..\..\src\Events\MouseRelease.h" />
<ClInclude Include="..\..\src\Events\PlaySound.h" />
<ClInclude Include="..\..\src\Events\PlayBGM.h" />
<ClInclude Include="..\..\src\Events\PlaySFX.h" />
<ClInclude Include="..\..\src\Events\SetVelocity.h" />
<ClInclude Include="..\..\src\Events\TankSteer.h" />
<ClInclude Include="..\..\src\Factory.h" />
@@ -49,7 +49,6 @@
<Filter>Input\Systems</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ResourceManager.cpp" />
<ClCompile Include="..\..\src\Sound.cpp" />
<ClCompile Include="..\..\src\Systems\ParticleSystem.cpp">
<Filter>Particle System\Systems</Filter>
</ClCompile>
@@ -66,6 +65,9 @@
<ClCompile Include="..\..\src\Systems\HelicopterSteeringSystem.cpp">
<Filter>Gameplay\Vehicles\Helicopter\Systems</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Sound.cpp">
<Filter>Audio</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Util">
@@ -304,9 +306,6 @@
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="..\..\src\EventBroker.h" />
<ClInclude Include="..\..\src\Events\PlaySound.h">
<Filter>Audio\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\KeyDown.h">
<Filter>Input\Events</Filter>
</ClInclude>
@@ -406,6 +405,12 @@
<ClInclude Include="..\..\src\Events\ComponentCreated.h">
<Filter>Base\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\PlayBGM.h">
<Filter>Audio\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\PlaySFX.h">
<Filter>Audio\Events</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\Fragment2.glsl">