Fixed playsound event. Added remove sounds OnComponentsRemove only when stopped playing.
This commit is contained in:
@@ -11,6 +11,7 @@ struct PlaySound : Event
|
||||
{
|
||||
EntityID Emitter;
|
||||
std::string Resource;
|
||||
bool Loop;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+15
-31
@@ -54,19 +54,7 @@ void GameWorld::Initialize()
|
||||
auto cameraComp = AddComponent<Components::Camera>(camera);
|
||||
cameraComp->FarClip = 2000.f;
|
||||
auto freeSteering = AddComponent<Components::FreeSteering>(camera);
|
||||
{
|
||||
auto se1 = CreateEntity(camera);
|
||||
auto transform = AddComponent<Components::Transform>(se1);
|
||||
transform->Position = glm::vec3(0,10,0);
|
||||
transform->Scale = glm::vec3(3);
|
||||
auto emitterComponent = AddComponent<Components::SoundEmitter>(se1);
|
||||
emitterComponent->Path = "Sounds/WUB.mp3";
|
||||
emitterComponent->MinDistance = 1.f;
|
||||
emitterComponent->MaxDistance = 10000.f;
|
||||
emitterComponent->Gain = 1;
|
||||
// auto model = AddComponent<Components::Model>(se1);
|
||||
// model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj";
|
||||
}
|
||||
|
||||
}
|
||||
CommitEntity(camera);
|
||||
|
||||
@@ -1155,24 +1143,20 @@ void GameWorld::Initialize()
|
||||
}
|
||||
}*/
|
||||
|
||||
// {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// {
|
||||
// auto se2 = CreateEntity();
|
||||
// auto transform = AddComponent<Components::Transform>(se2);
|
||||
// transform->Position = glm::vec3(100,10,0);
|
||||
// transform->Scale = glm::vec3(3);
|
||||
// auto emitterComponent = AddComponent<Components::SoundEmitter>(se2);
|
||||
// emitterComponent->Path = "Sounds/DarkHorse.mp3";
|
||||
// emitterComponent->Gain = 1;
|
||||
// emitterComponent->MinDistance = 1.f;
|
||||
// emitterComponent->MaxDistance = 50.f;
|
||||
// auto model = AddComponent<Components::Model>(se2);
|
||||
// model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj";
|
||||
// CommitEntity(se2);
|
||||
// }
|
||||
|
||||
{
|
||||
auto se1 = CreateEntity();
|
||||
auto transform = AddComponent<Components::Transform>(se1);
|
||||
transform->Position = glm::vec3(0,-30,0);
|
||||
transform->Scale = glm::vec3(3);
|
||||
auto emitterComponent = AddComponent<Components::SoundEmitter>(se1);
|
||||
emitterComponent->Path = "Sounds/WUB.mp3";
|
||||
emitterComponent->MinDistance = 100.f;
|
||||
emitterComponent->MaxDistance = 1000.f;
|
||||
emitterComponent->Gain = 1;
|
||||
auto model = AddComponent<Components::Model>(se1);
|
||||
model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj";
|
||||
}
|
||||
|
||||
|
||||
/*for (int x = 0; x < 5; x++)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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<float>(10.f, -10.f, 10.f, -10.f, 10.f, -10.f);
|
||||
|
||||
+72
-33
@@ -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<EntityID, FMOD_CHANNEL*>::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<<path<<std::endl;
|
||||
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)
|
||||
{
|
||||
auto emitter = m_World->GetComponent<Components::SoundEmitter>(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<Components::SoundEmitter*>(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);
|
||||
}
|
||||
}
|
||||
+10
-11
@@ -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<SoundSystem, Events::ComponentCreated> m_EComponentCreated;
|
||||
bool OnComponentCreated(const Events::ComponentCreated &event);
|
||||
EventRelay<SoundSystem, Events::PlaySound> 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<EntityID> m_Listeners;
|
||||
FMOD_SYSTEM* m_System;
|
||||
FMOD_CHANNELGROUP* m_ChannelGruoup;
|
||||
//std::map<EntityID, FMOD_CHANNEL*> m_Channels;
|
||||
std::map<EntityID, FMOD_CHANNEL*> m_Channels;
|
||||
std::map<EntityID, FMOD_SOUND*> m_Sounds;
|
||||
FMOD::System *m_FmodSystem;
|
||||
|
||||
FMOD_SOUND* m_Sound; //Temp
|
||||
bool m_Playing; //TEMP
|
||||
FMOD_SYSTEM* m_System;
|
||||
std::vector<EntityID> m_Listeners;
|
||||
std::map<EntityID, FMOD_CHANNEL*> m_Channels;
|
||||
std::map<EntityID, FMOD_CHANNEL*> m_DeleteChannels;
|
||||
std::map<EntityID, FMOD_SOUND*> m_Sounds;
|
||||
std::map<EntityID, FMOD_SOUND*> m_DeleteSounds;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user