#ifndef SoundSystem_h__ #define SoundSystem_h__ #include #include "glm/common.hpp" #include "glm/gtx/rotate_vector.hpp" // Calculate Up vector #include "OpenAL/al.h" #include "OpenAL/alc.h" #include "Core/World.h" #include "Core/EventBroker.h" #include "Rendering/RenderQueueFactory.h" // Absolute transform #include "Sound/Sound.h" #include "Sound/EPlaySound.h" #include "Sound/EPlaySoundOnEntity.h" #include "Sound/EPlaySoundOnPosition.h" #include "Sound/EPauseSound.h" #include "Sound/EStopSound.h" #include "Sound/EContinueSound.h" struct Source { Source() { } Sound* SoundResource; ALuint ALsource; }; class SoundSystem { public: SoundSystem() { } SoundSystem(World* world, EventBroker* eventBroker); ~SoundSystem(); void Update(); // Update emitters private: // Help functions for working with OpenaAL void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; glm::vec3 listnerPos() { glm::vec3 pos; alGetListener3f(AL_POSITION, &pos.x, &pos.y, &pos.z); return pos; }; void setListenerVel(glm::vec3 vel) { alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z); }; glm::vec3 listenerVel() { glm::vec3 vel; alGetListener3f(AL_VELOCITY, &vel.x, &vel.y, &vel.z); return vel; }; void setListenerOri(glm::vec3 ori); glm::vec3 listenerOri() { glm::vec3 ori; alGetListener3f(AL_ORIENTATION, &ori.x, &ori.y, &ori.z); return ori; }; void setSourcePos(ALuint source, glm::vec3 pos) { ALfloat spos[3] = { pos.x, pos.y, pos.z }; alSourcefv(source, AL_POSITION, spos); }; void setSourceVel(ALuint source, glm::vec3 vel) { ALfloat svel[3] = { vel.x, vel.y, vel.z }; alSourcefv(source, AL_VELOCITY, svel); }; // Logic void initOpenAL(); void updateEmitters(); void updateListener(); void deleteInactiveEmitters(); void addNewEmitters(); Source* createSource(std::string filePath); void playSound(Source* source); void stopSound(Source* source); void stopEmitter(EntityID emitter); void stopEmitters(); bool isPlaying(ALuint source); void setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance); // OpenAL system variables ALCdevice* m_ALCdevice = nullptr; ALCcontext* m_ALCcontext = nullptr; // Logic World* m_World; EventBroker* m_EventBroker; std::unordered_map m_Sources; // Events EventRelay m_EPlaySound; bool OnPlaySound(const Events::PlaySound &e); EventRelay m_EPlaySoundOnEntity; bool OnPlaySoundOnEntity(const Events::PlaySoundOnEntity &e); EventRelay m_EPlaySoundOnPosition; bool OnPlaySoundOnPosition(const Events::PlaySoundOnPosition &e); EventRelay m_EPauseSound; bool OnPauseSound(const Events::PauseSound &e); EventRelay m_EStopSound; bool OnStopSound(const Events::StopSound &e); EventRelay m_EContinueSound; bool OnContinueSound(const Events::ContinueSound &e); }; #endif