diff --git a/src/Components/SoundEmitter.h b/src/Components/SoundEmitter.h index 05c5f15..6bd80d8 100755 --- a/src/Components/SoundEmitter.h +++ b/src/Components/SoundEmitter.h @@ -10,10 +10,10 @@ namespace Components struct SoundEmitter : Component { - SoundEmitter() : Gain(1.f), MaxDistance(1.f), ReferenceDistance(1.f), Pitch(1.f), Loop(false) {} + SoundEmitter() : Gain(1.f), MaxDistance(1.f), MinDistance(1.f), Pitch(1.f), Loop(false) {} float Gain; float MaxDistance; - float ReferenceDistance; + float MinDistance; float Pitch; bool Loop; std::string Path; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 4135c4e..1e9974d 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -218,7 +218,7 @@ void GameWorld::Initialize() { auto tank = CreateEntity(); - auto listener = AddComponent(tank); + auto transform = AddComponent(tank); transform->Position = glm::vec3(0, 5, 0); //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); @@ -309,6 +309,7 @@ void GameWorld::Initialize() auto cameraComp = AddComponent(camera); cameraComp->FarClip = 2000.f; AddComponent(camera); + AddComponent(tank); //auto freeSteering = AddComponent(camera); CommitEntity(camera); } @@ -688,13 +689,35 @@ void GameWorld::Initialize() } } - auto soundEmitter = CreateEntity(); - auto transform = AddComponent(soundEmitter); - transform->Position = glm::vec3(0,10,0); - transform->Scale = glm::vec3(3); - AddComponent(soundEmitter); - auto model = AddComponent(soundEmitter); - model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj"; + { + auto se1 = CreateEntity(); + 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 = 50.f; + emitterComponent->Gain = 1; + auto model = AddComponent(se1); + model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj"; + CommitEntity(se1); + } + + { + 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); + } /*for (int x = 0; x < 5; x++) diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index dbb9ba6..b8ea62c 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -159,7 +159,7 @@ bool Systems::InputSystem::OnBindKey(const Events::BindKey &event) else { m_KeyBindings[event.KeyCode] = std::make_tuple(event.Command, event.Value); - LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str()); + LOG_DEBUG("Input: Bound key %i:%c to %s", event.KeyCode, (char)event.KeyCode, event.Command.c_str()); } return true; diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index ef2c9fe..2a5aca7 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -19,9 +19,11 @@ void Systems::SoundSystem::Initialize() m_FmodSystem->playSound(FMOD_CHANNEL_FREE, sound, false, 0);*/ FMOD_System_Create(&m_System); - FMOD_System_Init(m_System, 32, FMOD_INIT_NORMAL, nullptr); - FMOD_System_Set3DSettings(m_System, 10.0f, 10.f, 1.0f); //dopplerScale, distancefactor, rolloffscale - FMOD_System_GetMasterChannelGroup(m_System, &m_ChannelGruoup); + FMOD_System_Init(m_System, 1024, FMOD_INIT_NORMAL, nullptr); + FMOD_System_Set3DSettings(m_System, 10.0f, 1.f, 100.f); //dopplerScale, distancefactor, rolloffscale + //FMOD_System_GetMasterChannelGroup(m_System, &m_ChannelGruoup); + m_Playing = false; + } void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf) @@ -60,13 +62,14 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par glm::vec3 tForward = tOri[2]; FMOD_VECTOR lForward = {tForward.x, tForward.y, tForward.z}; - FMOD_System_Set3DListenerAttributes(m_System, 0, (const FMOD_VECTOR*)&lPos, (const FMOD_VECTOR*)&lVel, (const FMOD_VECTOR*)&lForward, (const FMOD_VECTOR*)&lUp); + FMOD_System_Set3DListenerAttributes(m_System, lID, (const FMOD_VECTOR*)&lPos, (const FMOD_VECTOR*)&lVel, (const FMOD_VECTOR*)&lForward, (const FMOD_VECTOR*)&lUp); } auto emitter = m_World->GetComponent(entity); if(emitter) { FMOD_CHANNEL* channel = m_Channels[entity]; + FMOD_SOUND* sound = m_Sounds[entity]; auto eTransform = m_World->GetComponent(entity); glm::vec3 tPos = eTransform->Position; @@ -75,53 +78,74 @@ 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_SetMode(channel, FMOD_3D_LINEARROLLOFF); FMOD_Channel_Set3DAttributes(channel, (const FMOD_VECTOR*)&ePos, (const FMOD_VECTOR*)&eVel); + if(!m_Playing) { - PlaySound(m_Channels[entity], 1, false); + PlaySound(channel, sound, 1, emitter->Loop); + m_Playing = true; } } - } void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr component) { - if(type == typeid(Components::SoundEmitter).name()) + +// if(type == typeid(Components::Listener).name()) +// { +// m_Listeners.push_back(component->Entity); +// } +// +// if (type == typeid(Components::SoundEmitter).name()) +// { +// //std::shared_ptr eComponent = std::dynamic_pointer_cast(component); +// +// FMOD_CHANNEL* channel; +// m_Channels.insert(std::pair(component->Entity, channel)); +// +// LoadSound("Sounds/DarkHorse.mp3", 100, 30, false, 1, 0); +// } +} + +void Systems::SoundSystem::OnEntityCommit(EntityID entity) +{ + //TEMP ska slängas in i OnComponentCreated + auto lComponent = m_World->GetComponent(entity); + if(lComponent) { - m_Listeners.push_back(component->Entity); + m_Listeners.push_back(entity); } - if (type == typeid(Components::SoundEmitter).name()) + auto eCompoonent = m_World->GetComponent(entity); + if(eCompoonent) { FMOD_CHANNEL* channel; - m_Channels.insert(std::pair(component->Entity, channel)); - LoadSound("Sounds/WUB.mp3"); + FMOD_SOUND* sound; + + std::string path = eCompoonent->Path; + float volume = eCompoonent->Gain; + bool loop = eCompoonent->Loop; + float maxDist = eCompoonent->MaxDistance; + float minDist = eCompoonent->MinDistance; + float pitch = eCompoonent->Pitch; + + if(LoadSound(sound, path, maxDist, minDist, loop, volume, pitch) != FMOD_OK) + LOG_ERROR("FMOD did not load sound file"); + m_Channels.insert(std::make_pair(entity, channel)); + m_Sounds.insert(std::make_pair(entity, sound)); } } -bool Systems::SoundSystem::LoadSound(std::string filePath) +FMOD_RESULT Systems::SoundSystem::LoadSound(FMOD_SOUND* &sound, std::string filePath, float maxDist, float minDist, bool loop, float volume, float pitch) { - bool loadedFile; - FMOD_RESULT result = FMOD_System_CreateSound(m_System, filePath.c_str(), FMOD_3D_LINEARROLLOFF, NULL, &m_Sound); - result = FMOD_Sound_Set3DMinMaxDistance(m_Sound, 1.f, 100.f); + FMOD_RESULT result = FMOD_System_CreateSound(m_System, filePath.c_str(), FMOD_3D , 0, &sound); + FMOD_Sound_Set3DMinMaxDistance(m_Sound, minDist, maxDist); - if(result != FMOD_OK) - { - loadedFile = false; - LOG_ERROR("FMOD ERROR: Could not load sound file: %s \n", filePath.c_str()); - } - else - { - loadedFile = true; - } - return loadedFile; + return result; } -void Systems::SoundSystem::PlaySound(FMOD_CHANNEL* channel, float volume, bool loop) +void Systems::SoundSystem::PlaySound(FMOD_CHANNEL* channel, FMOD_SOUND* sound, float volume, bool loop) { - m_Playing = true; - FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, m_Sound, false, &channel); + FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, sound, false, &channel); FMOD_Channel_SetVolume(channel, volume); } \ No newline at end of file diff --git a/src/Systems/SoundSystem.h b/src/Systems/SoundSystem.h index c3894d9..b1230a5 100755 --- a/src/Systems/SoundSystem.h +++ b/src/Systems/SoundSystem.h @@ -26,20 +26,25 @@ public: void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void OnComponentCreated(std::string type, std::shared_ptr component) override; + void OnEntityCommit(EntityID entity) override; - FMOD::System *m_FmodSystem; + private: // Events EventRelay m_EPlaySound; - bool LoadSound(std::string filePath); - void PlaySound(FMOD_CHANNEL*, float volume, bool loop); + FMOD_RESULT LoadSound(FMOD_SOUND*&, std::string, float, float, bool, 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 };