diff --git a/Escape-the-Dawn/Components/SoundEmitter.h b/Escape-the-Dawn/Components/SoundEmitter.h index 441afe0..d211c05 100644 --- a/Escape-the-Dawn/Components/SoundEmitter.h +++ b/Escape-the-Dawn/Components/SoundEmitter.h @@ -10,8 +10,11 @@ namespace Components struct SoundEmitter : Component { - float Volume; - float MaxRange; + float Gain; + float MaxDistance; + float ReferenceDistance; + float Pitch; + bool Loop; }; } diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index 59c7bf2..c67b346 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -5,11 +5,14 @@ #include "AL/alc.h" #include "System.h" +#include "Components/Transform.h" #include "Components/SoundEmitter.h" #include #include #include #include +#include +#include namespace Systems { @@ -24,20 +27,22 @@ public: void OnComponentCreated(std::string type, std::shared_ptr component) override; void PlaySound(std::shared_ptr emitter, std::string fileName); void LoadFile(const char* fileName); - void CreateSource(int ID, float pos[3], ALboolean looping); + ALuint CreateSource(); + private: ALCcontext* context; + + //File-info char type[4]; DWORD size, chunkSize; short formatType, channels; DWORD sampleRate, avgBytesPerSec; short bytesPerSample, bitsPerSample; DWORD dataSize; - unsigned char* buf; - std::vector source; - + std::map source; + std::map buffer; // string = fileName }; } diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index cb69823..e9cc2ef 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -25,16 +25,59 @@ void Systems::SoundSystem::Update(double dt) void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); - if(soundEmitter == nullptr) + auto transformComponent = m_World->GetComponent(entity, "Transform"); + if (transformComponent == nullptr) return; - + auto entityName = m_World->GetProperty(entity, "Name"); + if (entityName == "Player") + { + glm::vec3 playerPos = transformComponent->Position; + ALfloat listenerPos[3] = { playerPos.x, playerPos.y, playerPos.z }; + + glm::vec3 playerVel = transformComponent->Velocity; + ALfloat listenerVel[3] = { playerVel.x, playerVel.y, playerVel.z }; + + glm::fquat playerQuatOri = transformComponent->Orientation; + glm::vec3 playerOriFW = glm::rotate(playerQuatOri, glm::vec3(0, 0, -1)); + glm::vec3 playerOriUP = glm::rotate(playerQuatOri, glm::vec3(0, 1, 0)); + ALfloat listenerOri[6] = { playerOriFW.x, playerOriFW.y, playerOriFW.z, playerOriUP.x, playerOriUP.y, playerOriUP.z }; + + //Listener + alListenerfv(AL_POSITION, listenerPos); + alListenerfv(AL_VELOCITY, listenerVel); + alListenerfv(AL_ORIENTATION, listenerOri); + } + + auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); + if(soundEmitter != nullptr) + { + alSourcef(source[soundEmitter.get()], AL_GAIN, soundEmitter->Gain); //Volume + alSourcei(source[soundEmitter.get()], AL_MAX_DISTANCE, soundEmitter->MaxDistance); + alSourcei(source[soundEmitter.get()], AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); + alSourcef(source[soundEmitter.get()], AL_PITCH, soundEmitter->Pitch); + alSourcei(source[soundEmitter.get()], AL_LOOPING, soundEmitter->Loop); + + glm::vec3 emitterPos = transformComponent->Position; + ALfloat sourcePos[3] = { emitterPos.x, emitterPos.y, emitterPos.z }; + + glm::vec3 emitterVel= transformComponent->Position; + ALfloat sourceVel[3] = { emitterVel.x, emitterVel.y, emitterVel.z }; + + alSourcefv(source[soundEmitter.get()], AL_POSITION, sourcePos); + alSourcefv(source[soundEmitter.get()], AL_VELOCITY, sourceVel); + } } void Systems::SoundSystem::PlaySound(std::shared_ptr emitter, std::string fileName) { - //alSourcePlay(source); + alSourcePlay(source[emitter.get()]); +} + +void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr component) +{ + if(type == "SoundEmitter") + source[component.get()] = CreateSource(); } void Systems::SoundSystem::LoadFile(const char* fileName) @@ -77,7 +120,7 @@ void Systems::SoundSystem::LoadFile(const char* fileName) fclose(fp); } -void Systems::SoundSystem::CreateSource(int ID, float pos[3], ALboolean looping) +ALuint Systems::SoundSystem::CreateSource() { ALuint source; ALuint buffer; @@ -104,23 +147,6 @@ void Systems::SoundSystem::CreateSource(int ID, float pos[3], ALboolean looping) } alBufferData(buffer, format, buf, dataSize, frequency); - - ALfloat SourcePos[] = { pos[0], pos[1], pos[2] }; - ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; - ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; // Playerpos? Camerapos? - ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; - ALfloat ListenerOri[] = { 0.0, 0.0, -1.0 , 0.0, 1.0, 0.0 }; //two vectors, one defining where the listener points, and one "up" vector - - //Listener - alListenerfv(AL_POSITION, ListenerPos); - alListenerfv(AL_VELOCITY, ListenerVel); - alListenerfv(AL_ORIENTATION, ListenerOri); - //Source - alSourcei(source, AL_BUFFER, buffer); - alSourcef(source, AL_PITCH, 1.0f); - alSourcef(source, AL_GAIN, 1.0f); //Volume - alSourcefv(source, AL_POSITION, SourcePos); - alSourcefv(source, AL_VELOCITY, SourceVel); - alSourcei(source, AL_LOOPING, looping); + return source; } \ No newline at end of file