In lack of camera entity I created a Listener cube to test 3D sound. Works fine. Updates listener pos each tick.

This commit is contained in:
stiffly
2016-01-07 16:03:33 +01:00
parent d8c4c6e5ff
commit 2838446072
3 changed files with 25 additions and 9 deletions
+2 -2
View File
@@ -22,9 +22,9 @@ class SoundSystem
{
public:
SoundSystem() { }
SoundSystem(EventBroker* eventBroker);
SoundSystem(World* world, EventBroker* eventBroker);
~SoundSystem();
void Update() { } // Update emitters
void Update(); // Update emitters
private:
// Private setters and getters for working with glm
void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); };
+15 -6
View File
@@ -1,8 +1,9 @@
#include "Sound/SoundSystem.h"
SoundSystem::SoundSystem(EventBroker* eventBroker)
SoundSystem::SoundSystem(World* world, EventBroker* eventBroker)
{
m_EventBroker = eventBroker;
m_World = world;
// Initialize OpenAL
m_ALCdevice = alcOpenDevice(nullptr);
if (m_ALCdevice != nullptr) {
@@ -16,20 +17,29 @@ SoundSystem::SoundSystem(EventBroker* eventBroker)
alDistanceModel(AL_INVERSE_DISTANCE);
EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound);
//m_EPlaySound = decltype(m_EPlaySound)(std::bind(&SoundSystem::OnPlaySound, this, std::placeholders::_1));
//m_EventBroker->Subscribe(m_EPlaySound);
}
SoundSystem::~SoundSystem()
{
{
alcDestroyContext(m_ALCcontext);
alcCloseDevice(m_ALCdevice);
delete m_ALCcontext;
delete m_ALCdevice;
}
void SoundSystem::Update()
{
// Should only be one listener.
auto listenerComponents = m_World->GetComponents("Listener");
for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) {
EntityID listener = (*it).EntityID;
auto transform = m_World->GetComponent(listener, "Transform");
setListenerPos(transform["Position"]);
}
}
ALuint SoundSystem::createSource()
{
{
ALuint source;
alGenSources((ALuint)1, &source);
alSourcei(source, AL_REFERENCE_DISTANCE, 1.0);
@@ -60,7 +70,6 @@ bool SoundSystem::OnPlaySound(const Events::PlaySound & e)
sauce.ALsource = source;
sauce.SoundResource = sound;
playSound(sauce);
LOG_INFO("You are playing an imaginary sound now! :D");
return false;
}
+8 -1
View File
@@ -65,7 +65,14 @@ Game::Game(int argc, char* argv[])
networkFunction();
}
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand);
m_SoundSystem = new SoundSystem(m_EventBroker);
m_SoundSystem = new SoundSystem(m_World, m_EventBroker);
auto soundListenerCube = m_World->CreateEntity();
m_World->AttachComponent(soundListenerCube, "Listener");
m_World->AttachComponent(soundListenerCube, "Transform");
auto model = m_World->AttachComponent(soundListenerCube, "Model");
model["Resource"] = "Models/Core/UnitCube.obj";
model["Color"] = glm::vec4(1, 0, 0, 1);
m_LastTime = glfwGetTime();
}