diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 4ca5cb3..99a6456 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -66,15 +66,18 @@ public: m_World = std::make_shared(m_EventBroker); + + //TODO: Move this out of engine.h m_World->ComponentFactory.Register(); m_World->SystemFactory.Register( [this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); }); m_World->AddSystem(); - m_World->AddSystem(); m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); - m_World->Initialize(); + + m_World->AddSystem(); + m_World->SystemFactory.Register([this]() { return new Systems::Sound(m_World.get(), m_EventBroker); }); m_World->ComponentFactory.Register(); diff --git a/include/Core/Sound.h b/include/Core/Sound.h index 194617c..f6dffff 100644 --- a/include/Core/Sound.h +++ b/include/Core/Sound.h @@ -5,13 +5,12 @@ #ifndef DAYDREAM_SOUND_H #define DAYDREAM_SOUND_H -#include "../../deps/include/AL/al.h" -#include "../../deps/include/AL/alc.h" - -#include "Core/EKeyDown.h" -#include "Core/EventBroker.h" +#include "AL/al.h" +#include "AL/alc.h" #include "Core/System.h" #include "Core/World.h" +#include "Core/EKeyDown.h" +#include "Core/EventBroker.h" namespace dd @@ -21,16 +20,42 @@ namespace Systems class Sound : public System { public: Sound(World *world, std::shared_ptr eventBroker) - : System(world, eventBroker) { EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Sound::OnKeyDown); } + : System(world, eventBroker) { } - void Init(); + //void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + void Update(double dt) override; + //void OnComponentCreated(std::string type, std::shared_ptr component) override; + //void OnComponentRemoved(std::string type, Component* component) override; + //void PlaySound(Components::SoundEmitter* emitter, std::string path); // Use if you want to play a temporary .wav file not from component + //void PlaySound(std::shared_ptr emitter); // Use if you want to play .wav file from component // imon no hate plx T.T + //void StopSound(std::shared_ptr emitter); + + + void Initialize() override; private: dd::EventRelay m_EKeyDown; + ALuint LoadFile(std::string fileName); + ALuint CreateSource(); + + //File-info + char m_type[4]; + unsigned long m_size, m_chunkSize; + short m_formatType, m_channels; + unsigned long m_sampleRate, m_avgBytesPerSec; + short m_bytesPerSample, m_bitsPerSample; + unsigned long m_dataSize; + + + std::map m_Sources; + std::map m_BufferCache; + bool OnKeyDown(const dd::Events::KeyDown &event); - void HelloWorld(); + + //Temp + ALuint m_source; }; } diff --git a/src/game/Core/Sound.cpp b/src/game/Core/Sound.cpp index e89d9db..150f4e7 100644 --- a/src/game/Core/Sound.cpp +++ b/src/game/Core/Sound.cpp @@ -4,32 +4,144 @@ #include "PrecompiledHeader.h" #include "Core/Sound.h" -void dd::Systems::Sound::Init() +void dd::Systems::Sound::Initialize() { + LOG_INFO("Hello im an init func ----------------------------"); + /* + LOG_ERROR("Init started"); + //initialize OpenAL + ALCdevice* device = alcOpenDevice(NULL); + ALCcontext* context; + if(device) + { + context = alcCreateContext(device, NULL); + alcMakeContextCurrent(context); + } + else + { + LOG_ERROR("OMG OPEN AL FAIL"); + } + + alGetError(); + + alSpeedOfSound(340.29f); // Speed of sound m/s + alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); + + //Subscribe to events + EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Sound::OnKeyDown); + + LOG_ERROR("Init complete"); + + */ } -void dd::Systems::Sound::HelloWorld() +void dd::Systems::Sound::Update(double dt) { - LOG_INFO("Hej världen!"); + /*const ALfloat pos[3] = {0, 0, 0}; + alListenerfv(AL_POSITION, pos); + + alSourcefv(m_source, AL_POSITION, pos); +*/ + } bool dd::Systems::Sound::OnKeyDown(const dd::Events::KeyDown &event) { // #define GLFW_KEY_S 83 if(event.KeyCode == 83){ - //HelloWorld(); + m_source = CreateSource(); - //Init openAL------------------ - ALCdevice* device; alcOpenDevice(NULL); - ALCcontext* context; - if(device){ - context = alcCreateContext(device, NULL); - alcMakeContextCurrent(context); - } - else{ - LOG_ERROR("OpenAL failed to initialize"); - } + ALuint buffer = LoadFile("assets/Sounds/screwed.wav"); + alSourcei(m_source, AL_BUFFER, buffer); + alSourcePlay(m_source); + + + LOG_INFO("Playing sound (I wish LOL)"); } } + +ALuint dd::Systems::Sound::CreateSource() +{ + ALuint source; + alGenSources((ALuint)1, &source); + + return source; +} + +ALuint dd::Systems::Sound::LoadFile(std::string path) +{ + if (m_BufferCache.find(path) != m_BufferCache.end()) + return m_BufferCache[path]; + + FILE *fp = NULL; + fp = fopen(path.c_str(), "rb"); + + //CHECK FOR VALID WAVE-FILE + fread(m_type, sizeof(char), 4, fp); + if(m_type[0]!='R' || m_type[1]!='I' || m_type[2]!='F' || m_type[3]!='F') { + LOG_ERROR("ERROR: No RIFF in WAVE-file"); + return 0; + } + + fread(&m_size, sizeof(unsigned long), 1, fp); + fread(m_type, sizeof(char), 4, fp); + if(m_type[0]!='W' || m_type[1]!='A' || m_type[2]!='V' || m_type[3]!='E') { + LOG_ERROR("ERROR: Not WAVE-file"); + return 0; + } + + fread(m_type, sizeof(char), 4, fp); + if(m_type[0]!='f' || m_type[1]!='m' || m_type[2]!='t' || m_type[3]!=' ') { + LOG_ERROR("ERROR: No fmt in WAVE-file"); + return 0; + } + + //READ THE DATA FROM WAVE-FILE + fread(&m_chunkSize, sizeof(unsigned long), 1, fp); + fread(&m_formatType, sizeof(short), 1, fp); + fread(&m_channels, sizeof(short), 1, fp); + fread(&m_sampleRate, sizeof(unsigned long), 1, fp); + fread(&m_avgBytesPerSec, sizeof(unsigned long), 1, fp); + fread(&m_bytesPerSample, sizeof(short), 1, fp); + fread(&m_bitsPerSample, sizeof(short), 1, fp); + + fread(m_type, sizeof(char), 4, fp); + if(m_type[0]!='d' || m_type[1]!='a' || m_type[2]!='t' || m_type[3]!='a') + { + LOG_ERROR("ERROR: WAVE-file Missing data"); + return 0; + } + + fread(&m_dataSize, sizeof(unsigned long), 1, fp); + + unsigned char* buf = new unsigned char[m_dataSize]; + fread(buf, sizeof(unsigned char), m_dataSize, fp); + fclose(fp); + + // Create buffer + ALuint format = 0; + if(m_bitsPerSample == 8) + { + if(m_channels == 1) + format = AL_FORMAT_MONO8; + else if(m_channels == 2) + format = AL_FORMAT_STEREO8; + } + if(m_bitsPerSample == 16) + { + if (m_channels == 1) + format = AL_FORMAT_MONO16; + else if (m_channels == 2) + format = AL_FORMAT_STEREO16; + } + + ALuint buffer; + alGenBuffers(1, &buffer); + alBufferData(buffer, format, buf, m_dataSize, m_sampleRate); + delete[] buf; + + m_BufferCache[path] = buffer; + return buffer; +}