nothing works
This commit is contained in:
@@ -66,15 +66,18 @@ public:
|
|||||||
|
|
||||||
m_World = std::make_shared<World>(m_EventBroker);
|
m_World = std::make_shared<World>(m_EventBroker);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//TODO: Move this out of engine.h
|
//TODO: Move this out of engine.h
|
||||||
m_World->ComponentFactory.Register<Components::Transform>();
|
m_World->ComponentFactory.Register<Components::Transform>();
|
||||||
m_World->SystemFactory.Register<Systems::TransformSystem>(
|
m_World->SystemFactory.Register<Systems::TransformSystem>(
|
||||||
[this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
|
[this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
|
||||||
m_World->AddSystem<Systems::TransformSystem>();
|
m_World->AddSystem<Systems::TransformSystem>();
|
||||||
m_World->AddSystem<Systems::Sound>();
|
|
||||||
m_World->ComponentFactory.Register<Components::Model>();
|
m_World->ComponentFactory.Register<Components::Model>();
|
||||||
m_World->ComponentFactory.Register<Components::Template>();
|
m_World->ComponentFactory.Register<Components::Template>();
|
||||||
m_World->Initialize();
|
|
||||||
|
m_World->AddSystem<Systems::Sound>();
|
||||||
|
m_World->SystemFactory.Register<Systems::Sound>([this]() { return new Systems::Sound(m_World.get(), m_EventBroker); });
|
||||||
|
|
||||||
|
|
||||||
m_World->ComponentFactory.Register<Components::Sprite>();
|
m_World->ComponentFactory.Register<Components::Sprite>();
|
||||||
|
|||||||
+33
-8
@@ -5,13 +5,12 @@
|
|||||||
#ifndef DAYDREAM_SOUND_H
|
#ifndef DAYDREAM_SOUND_H
|
||||||
#define DAYDREAM_SOUND_H
|
#define DAYDREAM_SOUND_H
|
||||||
|
|
||||||
#include "../../deps/include/AL/al.h"
|
#include "AL/al.h"
|
||||||
#include "../../deps/include/AL/alc.h"
|
#include "AL/alc.h"
|
||||||
|
|
||||||
#include "Core/EKeyDown.h"
|
|
||||||
#include "Core/EventBroker.h"
|
|
||||||
#include "Core/System.h"
|
#include "Core/System.h"
|
||||||
#include "Core/World.h"
|
#include "Core/World.h"
|
||||||
|
#include "Core/EKeyDown.h"
|
||||||
|
#include "Core/EventBroker.h"
|
||||||
|
|
||||||
|
|
||||||
namespace dd
|
namespace dd
|
||||||
@@ -21,16 +20,42 @@ namespace Systems
|
|||||||
class Sound : public System {
|
class Sound : public System {
|
||||||
public:
|
public:
|
||||||
Sound(World *world, std::shared_ptr<dd::EventBroker> eventBroker)
|
Sound(World *world, std::shared_ptr<dd::EventBroker> 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> 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<Components::SoundEmitter> emitter); // Use if you want to play .wav file from component // imon no hate plx T.T
|
||||||
|
//void StopSound(std::shared_ptr<Components::SoundEmitter> emitter);
|
||||||
|
|
||||||
|
|
||||||
|
void Initialize() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
dd::EventRelay<Sound, dd::Events::KeyDown> m_EKeyDown;
|
dd::EventRelay<Sound, dd::Events::KeyDown> 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<Component*, ALuint> m_Sources;
|
||||||
|
std::map<std::string, ALuint> m_BufferCache;
|
||||||
|
|
||||||
bool OnKeyDown(const dd::Events::KeyDown &event);
|
bool OnKeyDown(const dd::Events::KeyDown &event);
|
||||||
void HelloWorld();
|
|
||||||
|
//Temp
|
||||||
|
ALuint m_source;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+126
-14
@@ -4,32 +4,144 @@
|
|||||||
#include "PrecompiledHeader.h"
|
#include "PrecompiledHeader.h"
|
||||||
#include "Core/Sound.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)
|
bool dd::Systems::Sound::OnKeyDown(const dd::Events::KeyDown &event)
|
||||||
{
|
{
|
||||||
// #define GLFW_KEY_S 83
|
// #define GLFW_KEY_S 83
|
||||||
if(event.KeyCode == 83){
|
if(event.KeyCode == 83){
|
||||||
//HelloWorld();
|
m_source = CreateSource();
|
||||||
|
|
||||||
//Init openAL------------------
|
ALuint buffer = LoadFile("assets/Sounds/screwed.wav");
|
||||||
ALCdevice* device; alcOpenDevice(NULL);
|
|
||||||
ALCcontext* context;
|
|
||||||
if(device){
|
|
||||||
context = alcCreateContext(device, NULL);
|
|
||||||
alcMakeContextCurrent(context);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
LOG_ERROR("OpenAL failed to initialize");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user