Added a system that will handle BGM. Created specific logic for sounds to loop like Petter wants.
This commit is contained in:
@@ -9,25 +9,23 @@
|
||||
#include "OpenAL/al.h"
|
||||
#include "OpenAL/alc.h"
|
||||
|
||||
#include "imgui/imgui.h"
|
||||
|
||||
#include "Core/World.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "../Engine/Core/World.h"
|
||||
#include "../Engine/Core/EventBroker.h"
|
||||
#include "../Engine/Core/ResourceManager.h"
|
||||
#include "../Engine/Core/ConfigFile.h"
|
||||
#include "Core/Transform.h" // Absolute transform
|
||||
#include "Sound/Sound.h"
|
||||
#include "../Engine/Core/Transform.h"
|
||||
#include "../Engine/Sound/Sound.h"
|
||||
#include "../Engine/Sound/EPlayQueueOnEntity.h"
|
||||
#include "Sound/EPlaySoundOnEntity.h"
|
||||
#include "Sound/EPlaySoundOnPosition.h"
|
||||
#include "Sound/EPlayBackgroundMusic.h"
|
||||
#include "Sound/EPauseSound.h"
|
||||
#include "Sound/EContinueSound.h"
|
||||
#include "Sound/EStopSound.h"
|
||||
#include "Sound/ESetBGMGain.h"
|
||||
#include "Sound/ESetSFXGain.h"
|
||||
#include "Core/EPause.h"
|
||||
#include "Core/EComponentAttached.h"
|
||||
#include "../Engine/Sound/EPlaySoundOnEntity.h"
|
||||
#include "../Engine/Sound/EPlaySoundOnPosition.h"
|
||||
#include "../Engine/Sound/EPlayBackgroundMusic.h"
|
||||
#include "../Engine/Sound/EPauseSound.h"
|
||||
#include "../Engine/Sound/EContinueSound.h"
|
||||
#include "../Engine/Sound/EStopSound.h"
|
||||
#include "../Engine/Sound/ESetBGMGain.h"
|
||||
#include "../Engine/Sound/ESetSFXGain.h"
|
||||
#include "../Engine/Core/EPause.h"
|
||||
#include "../Engine/Core/EComponentAttached.h"
|
||||
#include "../Core/EPlayerSpawned.h"
|
||||
|
||||
typedef std::pair<ALuint, std::vector<ALuint>> QueuedBuffers;
|
||||
@@ -43,6 +41,7 @@ struct Source
|
||||
Sound* SoundResource = nullptr;
|
||||
ALuint ALsource;
|
||||
SoundType Type;
|
||||
float Duration;
|
||||
};
|
||||
|
||||
class SoundManager
|
||||
@@ -74,14 +73,18 @@ private:
|
||||
ALenum getSourceState(ALuint source);
|
||||
void setGain(Source* source, float gain);
|
||||
void setSoundProperties(Source* source, ComponentWrapper* soundComponent);
|
||||
float getDurationSeconds(Source* source);
|
||||
float getTimeOffsetSeconds(Source* source);
|
||||
|
||||
// Specific logic
|
||||
void playSound(Source* source);
|
||||
// Need to be the same format (sample rate etc)
|
||||
// Needs to be the same format (sample rate etc)
|
||||
void playQueue(QueuedBuffers qb);
|
||||
void stopSound(Source* source);
|
||||
Source* createSource(std::string filePath);
|
||||
std::unordered_map<EntityID, Source*> m_Sources;
|
||||
void mainMenuLoop();
|
||||
Source* m_CurrentBGM;
|
||||
|
||||
// Logic
|
||||
World* m_World = nullptr;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef Systems_BackgroundMusicSystem_h__
|
||||
#define Systems_BackgroundMusicSystem_h__
|
||||
|
||||
#include "../Engine/Core/System.h"
|
||||
#include "../Engine/Core/EventBroker.h"
|
||||
|
||||
// Handles transitions between BGM's depending on the current state of the game.
|
||||
class BackgroundMusicSystem : public PureSystem
|
||||
{
|
||||
public:
|
||||
BackgroundMusicSystem(SystemParams params);
|
||||
~BackgroundMusicSystem();
|
||||
void UpdateComponent(EntityWrapper& entity, ComponentWrapper& cEmitter, double dt) override;
|
||||
|
||||
private:
|
||||
void mainMenuLoop();
|
||||
|
||||
const std::string menu = "Audio/crosscounter.wav";
|
||||
};
|
||||
|
||||
#endif // ifndef
|
||||
@@ -56,13 +56,10 @@ void SoundManager::stopEmitters()
|
||||
void SoundManager::Update(double dt)
|
||||
{
|
||||
m_EventBroker->Process<SoundManager>();
|
||||
deleteInactiveEmitters(); // can be optimized with "EEntityDeleted"
|
||||
deleteInactiveEmitters();
|
||||
updateEmitters(dt);
|
||||
updateListener(dt);
|
||||
|
||||
// Editor debug info
|
||||
ImGui::SliderFloat("BGM", &m_BGMVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f);
|
||||
ImGui::SliderFloat("SFX", &m_SFXVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f);
|
||||
mainMenuLoop();
|
||||
}
|
||||
|
||||
void SoundManager::deleteInactiveEmitters()
|
||||
@@ -166,9 +163,20 @@ Source* SoundManager::createSource(std::string filePath)
|
||||
Source* source = new Source();
|
||||
source->ALsource = alSource;
|
||||
source->SoundResource = ResourceManager::Load<Sound>(filePath);
|
||||
source->Duration = getDurationSeconds(source);
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
void SoundManager::mainMenuLoop()
|
||||
{
|
||||
float time = getTimeOffsetSeconds(m_CurrentBGM);
|
||||
if (m_CurrentBGM->Duration - time <= 5) { // 5 is hard coded value that Petter recommended
|
||||
m_CurrentBGM = createSource("Audio/crosscounter.wav");
|
||||
playSound(m_CurrentBGM);
|
||||
}
|
||||
}
|
||||
|
||||
void SoundManager::playSound(Source* source)
|
||||
{
|
||||
alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer());
|
||||
@@ -338,12 +346,32 @@ void SoundManager::setSoundProperties(Source* source, ComponentWrapper* soundCom
|
||||
float gain = (source->Type == SoundType::SFX) ? m_SFXVolumeChannel : m_BGMVolumeChannel;
|
||||
alSourcef(source->ALsource, AL_GAIN, (float)(double)(*soundComponent)["Gain"] * gain);
|
||||
alSourcef(source->ALsource, AL_PITCH, (float)(double)(*soundComponent)["Pitch"]);
|
||||
alSourcei(source->ALsource, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]); // YOLO
|
||||
alSourcei(source->ALsource, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]);
|
||||
alSourcef(source->ALsource, AL_MAX_DISTANCE, (float)(double)(*soundComponent)["MaxDistance"]);
|
||||
alSourcef(source->ALsource, AL_ROLLOFF_FACTOR, (float)(double)(*soundComponent)["RollOffFactor"]);
|
||||
alSourcef(source->ALsource, AL_REFERENCE_DISTANCE, (float)(double)(*soundComponent)["ReferenceDistance"]);
|
||||
}
|
||||
|
||||
float SoundManager::getDurationSeconds(Source* source)
|
||||
{
|
||||
ALuint buffer = source->SoundResource->Buffer();
|
||||
ALint sizeBytes, channels, bits, frequenzy;
|
||||
alGetBufferi(buffer, AL_SIZE, &sizeBytes);
|
||||
alGetBufferi(buffer, AL_CHANNELS, &channels);
|
||||
alGetBufferi(buffer, AL_BITS, &bits);
|
||||
alGetBufferi(buffer, AL_FREQUENCY, &frequenzy);
|
||||
float sampleLength = (float)sizeBytes * 8 / (channels * bits);
|
||||
return (sampleLength / frequenzy);
|
||||
}
|
||||
|
||||
|
||||
float SoundManager::getTimeOffsetSeconds(Source* source)
|
||||
{
|
||||
float time;
|
||||
alGetSourcef(source->ALsource, AL_SEC_OFFSET, &time);
|
||||
return time;
|
||||
}
|
||||
|
||||
void SoundManager::initOpenAL()
|
||||
{
|
||||
// Initialize OpenAL
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "../Game/Systems/BackgroundMusicSystem.h"
|
||||
|
||||
BackgroundMusicSystem::BackgroundMusicSystem(SystemParams params)
|
||||
: System(params)
|
||||
, PureSystem("SoundEmitter")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
BackgroundMusicSystem::~BackgroundMusicSystem()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void BackgroundMusicSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cEmitter, double dt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void BackgroundMusicSystem::mainMenuLoop()
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user