Created a primitive SoundSystem. Can play a sound with 'P' button. Using resourcemanager. Not using the created components yet.

This commit is contained in:
stiffly
2016-01-07 15:22:46 +01:00
parent 49884d7e60
commit d8c4c6e5ff
15 changed files with 338 additions and 5 deletions
+9 -2
View File
@@ -9,8 +9,8 @@ find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED)
find_package(Xerces REQUIRED)
# Because FindOpenAL is retarded
#set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${CMAKE_SOURCE_DIR}/deps/include/AL")
#find_package(OpenAL REQUIRED)
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${CMAKE_SOURCE_DIR}/deps/include/OpenAL")
find_package(OpenAL REQUIRED)
if(UNIX)
find_package(X11 REQUIRED)
endif()
@@ -52,6 +52,12 @@ file(GLOB SOURCE_FILES_Network
)
source_group(Network FILES ${SOURCE_FILES_Network})
file(GLOB SOURCE_FILES_Sound
"${INCLUDE_PATH}/Sound/*.h"
"Sound/*.cpp"
)
source_group(Sound FILES ${SOURCE_FILES_Sound})
file(GLOB SOURCE_FILES_Rendering
"${INCLUDE_PATH}/Rendering/*.h"
"Rendering/*.cpp"
@@ -86,6 +92,7 @@ set(SOURCE_FILES
${SOURCE_FILES_Core_Util}
${SOURCE_FILES_Input}
${SOURCE_FILES_Network}
${SOURCE_FILES_Sound}
${SOURCE_FILES_GUI}
${SOURCE_FILES_Rendering}
${SOURCE_FILES_Rendering_Util}
+66
View File
@@ -0,0 +1,66 @@
#include "Sound/SoundSystem.h"
SoundSystem::SoundSystem(EventBroker* eventBroker)
{
m_EventBroker = eventBroker;
// Initialize OpenAL
m_ALCdevice = alcOpenDevice(nullptr);
if (m_ALCdevice != nullptr) {
m_ALCcontext = alcCreateContext(m_ALCdevice, nullptr);
alcMakeContextCurrent(m_ALCcontext);
} else {
LOG_ERROR("OpenAL failed to initialize.");
}
alSpeedOfSound(340.29f); // Speed of sound
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;
}
ALuint SoundSystem::createSource()
{
ALuint source;
alGenSources((ALuint)1, &source);
alSourcei(source, AL_REFERENCE_DISTANCE, 1.0);
alSourcei(source, AL_MAX_DISTANCE, FLT_MAX);
return source;
}
void SoundSystem::playSound(Source source)
{
alSourcei(source.ALsource, AL_BUFFER, source.SoundResource->Buffer());
alSourcePlay(source.ALsource);
}
void SoundSystem::stopSound(Source source)
{ }
void SoundSystem::stopEmitter(EntityID emitter)
{ }
bool SoundSystem::OnPlaySound(const Events::PlaySound & e)
{
Sound *sound = ResourceManager::Load<Sound>(e.FilePath);
if (sound == nullptr) {
return false;
}
ALuint source = createSource();
Source sauce;
sauce.ALsource = source;
sauce.SoundResource = sound;
playSound(sauce);
LOG_INFO("You are playing an imaginary sound now! :D");
return false;
}
+17 -1
View File
@@ -5,6 +5,7 @@
Game::Game(int argc, char* argv[])
{
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
ResourceManager::RegisterType<Sound>("Sound");
ResourceManager::RegisterType<Model>("Model");
ResourceManager::RegisterType<Texture>("Texture");
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
@@ -63,6 +64,9 @@ Game::Game(int argc, char* argv[])
//boost::thread workerThread(&Game::networkFunction, this);
networkFunction();
}
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand);
m_SoundSystem = new SoundSystem(m_EventBroker);
m_LastTime = glfwGetTime();
}
@@ -103,9 +107,11 @@ void Game::Tick()
// Iterate through systems and update world!
m_SystemPipeline->Update(m_World, dt);
debugTick(dt);
m_Renderer->Update(dt);
m_EventBroker->Process<Client>();
m_EventBroker->Process<SoundSystem>();
m_SoundSystem->Update();
m_RenderQueueFactory->Update(m_World);
GLERROR("Game::Tick m_RenderQueueFactory->Update");
m_Renderer->Draw(m_RenderQueueFactory->RenderQueues());
@@ -114,6 +120,16 @@ void Game::Tick()
m_EventBroker->Clear();
}
bool Game::debugOnInputCommand(const Events::InputCommand & e)
{
if (e.Command == "PlaySound" && e.Value > 0) {
Events::PlaySound e;
e.FilePath = "Audio/crosscounter.wav";
m_EventBroker->Publish(e);
}
return true;
}
void Game::debugTick(double dt)
{
m_EventBroker->Process<Game>();