Fixed OpenAL Clean-up device. Added playBGM-event.

This commit is contained in:
Stiffly
2015-09-22 16:56:53 +02:00
parent 3f4ede4b7d
commit 5b6556e9fb
5 changed files with 77 additions and 13 deletions
Binary file not shown.
Binary file not shown.
+25
View File
@@ -0,0 +1,25 @@
#ifndef EVENTS_EPLAYBGM_H__
#define EVENTS_EPLAYBGM_H__
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
struct PlayBGM : public Event
{
std::string path;
float volume = 1;
//TODO: Rethink this
bool SupportBGM;
};
}
}
#endif
+8 -3
View File
@@ -8,6 +8,7 @@
#include "Core/World.h"
#include "Sound.h"
#include "Sound/EPlaySFX.h"
#include "Sound/EPlayBGM.h"
#include "Physics/EContact.h"
#include "Core/EventBroker.h"
#include "Game/CBall.h"
@@ -36,15 +37,19 @@ private:
//Events
dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX;
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<SoundSystem, dd::Events::PlayBGM> m_EPlayBGM;
bool OnPlaySFX(const dd::Events::PlaySFX &event);
bool OnContact(const dd::Events::Contact &event);
bool OnPlayBGM(const dd::Events::PlayBGM &event);
ALuint CreateSource();
std::map<Component*, ALuint> m_Sources;
std::map<ALuint, ALuint> m_SourcesToBuffers;
ALCdevice* m_Device;
//std::list<ALuint>
//Temp
ALuint m_Source;
ALuint m_Source, m_BGMSource, m_BGMSupportSource;
};
}
+44 -10
View File
@@ -5,15 +5,16 @@ dd::Systems::SoundSystem::~SoundSystem()
{
alSourcei(m_Source, AL_BUFFER, 0);
alDeleteSources(1, &m_Source);
alcCloseDevice(m_Device);
}
void dd::Systems::SoundSystem::Initialize()
{ //initialize OpenAL
ALCdevice* device = alcOpenDevice(NULL);
m_Device = alcOpenDevice(NULL);
ALCcontext* context;
if (device) {
context = alcCreateContext(device, NULL);
if (m_Device) {
context = alcCreateContext(m_Device, NULL);
alcMakeContextCurrent(context);
}
else {
@@ -22,8 +23,10 @@ void dd::Systems::SoundSystem::Initialize()
alGetError();
//Create source
//Create sources
m_Source = CreateSource();
m_BGMSource = CreateSource();
m_BGMSupportSource = CreateSource();
alSpeedOfSound(340.29f); // Speed of sound m/s
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
@@ -35,6 +38,19 @@ void dd::Systems::SoundSystem::Initialize()
//Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX);
EVENT_SUBSCRIBE_MEMBER(m_EPlayBGM, &SoundSystem::OnPlayBGM);
//Todo: Move this
dd::Events::PlayBGM e;
e.path = "Sounds/BGM/soft-guitar.wav";
e.SupportBGM = false;
EventBroker->Publish(e);
dd::Events::PlayBGM e2;
e2.path = "Sounds/BGM/water-flowing.wav";
e2.SupportBGM = true;
e2.volume = 0.1;
EventBroker->Publish(e2);
}
void dd::Systems::SoundSystem::Update(double dt)
@@ -42,22 +58,40 @@ void dd::Systems::SoundSystem::Update(double dt)
}
bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event)
bool dd::Systems::SoundSystem::OnPlayBGM(const dd::Events::PlayBGM &event)
{
//m_Source = CreateSource();
Sound *sound = ResourceManager::Load<Sound>(event.path);
ALuint buffer = sound->Buffer();
alSourcei(m_Source, AL_BUFFER, buffer);
alSourcePlay(m_Source);
ALuint source = CreateSource();
alSourcei(source, AL_BUFFER, buffer);
alSourcei(source, AL_LOOPING, AL_TRUE);
alSourcef(source, AL_GAIN, event.volume);
alSourcePlay(source);
m_SourcesToBuffers[source] = buffer;
}
bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event)
{
Sound *sound = ResourceManager::Load<Sound>(event.path);
ALuint buffer = sound->Buffer();
ALuint source = CreateSource();
alSourcei(source, AL_BUFFER, buffer);
alSourcei(source, AL_LOOPING, AL_FALSE);
alSourcePlay(source);
m_SourcesToBuffers[source] = buffer;
}
bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event)
{
//Check which entity has the collisionSound component.
auto collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity1);
if (collisionSound == NULL) {
if (collisionSound == nullptr) {
collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity2);
if (collisionSound == NULL) {
if (collisionSound == nullptr) {
//None of the entities had a collisionSound component.
return false;
}