Fixed OpenAL Clean-up device. Added playBGM-event.
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -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,6 +8,7 @@
|
|||||||
#include "Core/World.h"
|
#include "Core/World.h"
|
||||||
#include "Sound.h"
|
#include "Sound.h"
|
||||||
#include "Sound/EPlaySFX.h"
|
#include "Sound/EPlaySFX.h"
|
||||||
|
#include "Sound/EPlayBGM.h"
|
||||||
#include "Physics/EContact.h"
|
#include "Physics/EContact.h"
|
||||||
#include "Core/EventBroker.h"
|
#include "Core/EventBroker.h"
|
||||||
#include "Game/CBall.h"
|
#include "Game/CBall.h"
|
||||||
@@ -36,15 +37,19 @@ private:
|
|||||||
//Events
|
//Events
|
||||||
dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX;
|
dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX;
|
||||||
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
|
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
|
||||||
|
dd::EventRelay<SoundSystem, dd::Events::PlayBGM> m_EPlayBGM;
|
||||||
bool OnPlaySFX(const dd::Events::PlaySFX &event);
|
bool OnPlaySFX(const dd::Events::PlaySFX &event);
|
||||||
bool OnContact(const dd::Events::Contact &event);
|
bool OnContact(const dd::Events::Contact &event);
|
||||||
|
bool OnPlayBGM(const dd::Events::PlayBGM &event);
|
||||||
|
|
||||||
ALuint CreateSource();
|
ALuint CreateSource();
|
||||||
|
|
||||||
std::map<Component*, ALuint> m_Sources;
|
std::map<ALuint, ALuint> m_SourcesToBuffers;
|
||||||
|
|
||||||
|
ALCdevice* m_Device;
|
||||||
|
|
||||||
//std::list<ALuint>
|
|
||||||
//Temp
|
//Temp
|
||||||
ALuint m_Source;
|
ALuint m_Source, m_BGMSource, m_BGMSupportSource;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,15 +5,16 @@ dd::Systems::SoundSystem::~SoundSystem()
|
|||||||
{
|
{
|
||||||
alSourcei(m_Source, AL_BUFFER, 0);
|
alSourcei(m_Source, AL_BUFFER, 0);
|
||||||
alDeleteSources(1, &m_Source);
|
alDeleteSources(1, &m_Source);
|
||||||
|
alcCloseDevice(m_Device);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dd::Systems::SoundSystem::Initialize()
|
void dd::Systems::SoundSystem::Initialize()
|
||||||
{ //initialize OpenAL
|
{ //initialize OpenAL
|
||||||
ALCdevice* device = alcOpenDevice(NULL);
|
m_Device = alcOpenDevice(NULL);
|
||||||
ALCcontext* context;
|
ALCcontext* context;
|
||||||
|
|
||||||
if (device) {
|
if (m_Device) {
|
||||||
context = alcCreateContext(device, NULL);
|
context = alcCreateContext(m_Device, NULL);
|
||||||
alcMakeContextCurrent(context);
|
alcMakeContextCurrent(context);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -22,8 +23,10 @@ void dd::Systems::SoundSystem::Initialize()
|
|||||||
|
|
||||||
alGetError();
|
alGetError();
|
||||||
|
|
||||||
//Create source
|
//Create sources
|
||||||
m_Source = CreateSource();
|
m_Source = CreateSource();
|
||||||
|
m_BGMSource = CreateSource();
|
||||||
|
m_BGMSupportSource = CreateSource();
|
||||||
|
|
||||||
alSpeedOfSound(340.29f); // Speed of sound m/s
|
alSpeedOfSound(340.29f); // Speed of sound m/s
|
||||||
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
|
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
|
||||||
@@ -35,6 +38,19 @@ void dd::Systems::SoundSystem::Initialize()
|
|||||||
//Subscribe to events
|
//Subscribe to events
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
|
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX);
|
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)
|
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);
|
Sound *sound = ResourceManager::Load<Sound>(event.path);
|
||||||
ALuint buffer = sound->Buffer();
|
ALuint buffer = sound->Buffer();
|
||||||
alSourcei(m_Source, AL_BUFFER, buffer);
|
ALuint source = CreateSource();
|
||||||
alSourcePlay(m_Source);
|
|
||||||
|
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)
|
bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event)
|
||||||
{
|
{
|
||||||
//Check which entity has the collisionSound component.
|
//Check which entity has the collisionSound component.
|
||||||
auto collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity1);
|
auto collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity1);
|
||||||
if (collisionSound == NULL) {
|
if (collisionSound == nullptr) {
|
||||||
collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity2);
|
collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity2);
|
||||||
if (collisionSound == NULL) {
|
if (collisionSound == nullptr) {
|
||||||
//None of the entities had a collisionSound component.
|
//None of the entities had a collisionSound component.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user