From 5695a8c068bd91e58bf9d88d2d8e4ea55d23e268 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 15 Sep 2015 16:08:52 +0200 Subject: [PATCH] Sound in game! Using OpenAL. NOWHERE TO RUN --- include/Core/Engine.h | 4 +- include/Core/Engine.h.orig | 417 ------------------------------------- include/Core/Sound.h | 26 +-- src/game/Core/Sound.cpp | 112 +++++----- 4 files changed, 69 insertions(+), 490 deletions(-) delete mode 100755 include/Core/Engine.h.orig diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 99a6456..2c7eda4 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -75,9 +75,9 @@ public: m_World->AddSystem(); m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); - - m_World->AddSystem(); + m_World->SystemFactory.Register([this]() { return new Systems::Sound(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); m_World->ComponentFactory.Register(); diff --git a/include/Core/Engine.h.orig b/include/Core/Engine.h.orig deleted file mode 100755 index 819b19e..0000000 --- a/include/Core/Engine.h.orig +++ /dev/null @@ -1,417 +0,0 @@ -/* - This file is part of Daydream Engine. - Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung - - Daydream Engine is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Daydream Engine is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with Daydream Engine. If not, see . -*/ - -#include -#include - -#include "ResourceManager.h" -#include "OBJ.h" -#include "Model.h" -#include "Texture.h" -#include "EventBroker.h" -#include "RenderQueue.h" -#include "Renderer.h" -#include "InputManager.h" -//TODO: Remove includes that are only here for the temporary draw solution. -#include "World.h" -#include "CTransform.h" -#include "Core/EventBroker.h" -#include "Rendering/CModel.h" -#include "Rendering/CSprite.h" -#include "CTemplate.h" -#include "Rendering/CPointLight.h" -#include "Transform/TransformSystem.h" -#include "Game/LevelSystem.h" -#include "Game/PadSystem.h" -#include "Game/CBall.h" -#include "Game/CBrick.h" -#include "Game/CPad.h" - -#include "Physics/PhysicsSystem.h" -#include "Physics/CPhysics.h" -#include "Physics/CBoxShape.h" -#include "Physics/ESetImpulse.h" - -namespace dd -{ - -class Engine -{ -public: - Engine(int argc, char* argv[]) { - m_EventBroker = std::make_shared(); - - m_Renderer = std::make_shared(); - m_Renderer->SetFullscreen(false); - m_Renderer->SetResolution(Rectangle(0, 0, 1920, 1080)); - m_Renderer->Initialize(); - - m_InputManager = std::make_shared(m_Renderer->Window(), m_EventBroker); - - 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->ComponentFactory.Register(); -// m_World->ComponentFactory.Register(); - - - m_World->ComponentFactory.Register(); - - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->SystemFactory.Register( - [this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); }); - m_World->AddSystem(); - - m_World->SystemFactory.Register([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); }); - m_World->AddSystem(); - m_World->SystemFactory.Register([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); }); - m_World->AddSystem(); - - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->Initialize(); - - - //TODO: Remove tobias light-test code. - { - auto t_BrickWall = m_World->CreateEntity(); - auto transform = m_World->AddComponent(t_BrickWall); - transform->Position = glm::vec3(0.f, 0.f, -10.f); - auto sprite = m_World->AddComponent(t_BrickWall); - //TODO: Rename SpriteFile to DiffuseTexture or similar. - sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png"; - sprite->NormalTexture = "Textures/Test/Brick_Normal.png"; - sprite->SpecularTexture = "Textures/Test/Brick_Specular.png"; - } - - - { - auto ent = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(ent); -<<<<<<< HEAD - transform->Position = glm::vec3(0.5f, 0.f, -10.f); - transform->Scale = glm::vec3(1.f, 1.f, 1.f); - - std::shared_ptr sprite = m_World->AddComponent(ent); - sprite->SpriteFile = "Textures/Ball.png"; -======= - transform->Position = glm::vec3(0.f, 0.f, -9.5f); - - //std::shared_ptr sprite = m_World->AddComponent(ent); - //sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; ->>>>>>> GLrenderer - - std::shared_ptr circleShape = m_World->AddComponent(ent); - std::shared_ptr ball = m_World->AddComponent(ent); - - std::shared_ptr physics = m_World->AddComponent(ent); - physics->Static = false; - - auto plight = m_World->AddComponent(ent); - plight->Radius = 2.f; - - m_World->CommitEntity(ent); - - Events::SetImpulse e; - e.Entity = ent; - e.Impulse = glm::vec2(0.f, -7.f); - e.Point = glm::vec2(0.5f, 0.f); - m_EventBroker->Publish(e); - } - - { - auto topWall = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(topWall); - transform->Position = glm::vec3(0.f, 5.f, -10.f); - transform->Scale = glm::vec3(15.f, 0.5f, 1.f); - - std::shared_ptr sprite = m_World->AddComponent(topWall); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; - - std::shared_ptr boxShape = m_World->AddComponent(topWall); - - std::shared_ptr physics = m_World->AddComponent(topWall); - physics->Static = true; - - m_World->CommitEntity(topWall); - } - { - auto leftWall = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(leftWall); - transform->Position = glm::vec3(-9.f, 1.f, -10.f); - transform->Scale = glm::vec3(0.5f, 10.f, 1.f); - - std::shared_ptr sprite = m_World->AddComponent(leftWall); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; - - std::shared_ptr boxShape = m_World->AddComponent(leftWall); - - std::shared_ptr physics = m_World->AddComponent(leftWall); - physics->Static = true; - - m_World->CommitEntity(leftWall); - } - { - auto rightWall = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(rightWall); - transform->Position = glm::vec3(9.f, 1.f, -10.f); - transform->Scale = glm::vec3(0.5f, 10.f, 1.f); - - std::shared_ptr sprite = m_World->AddComponent(rightWall); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; - - std::shared_ptr boxShape = m_World->AddComponent(rightWall); - - std::shared_ptr physics = m_World->AddComponent(rightWall); - physics->Static = true; - - m_World->CommitEntity(rightWall); - } - - - /*{ - auto ent = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(0.f, -3.f, -10.f); - transform->Scale = glm::vec3(2.f, 0.5f, 1.f); - transform->Orientation = glm::rotate(transform->Orientation, glm::radians(25.f), glm::vec3(0, 0, -1)); - - std::shared_ptr sprite = m_World->AddComponent(ent); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; - - std::shared_ptr boxShape = m_World->AddComponent(ent); - - std::shared_ptr physics = m_World->AddComponent(ent); - physics->Static = true; - - m_World->CommitEntity(ent); - }*/ - - { - auto ent = m_World->CreateEntity(); - m_World->SetProperty(ent, "Name", "Pad"); - auto ctransform = m_World->AddComponent(ent); - ctransform->Position = glm::vec3(0.f, -5.f, -10.f); - ctransform->Scale = glm::vec3(3.2, 0.8, 0.); - auto rectangle = m_World->AddComponent(ent); - auto physics = m_World->AddComponent(ent); - auto csprite = m_World->AddComponent(ent); - auto pad = m_World->AddComponent(ent); - csprite->SpriteFile = "Textures/Pad.png"; - m_World->CommitEntity(ent); - } - - m_LastTime = glfwGetTime(); - } - - bool Running() const { return !glfwWindowShouldClose(m_Renderer->Window()); } - - void Tick() - { - double currentTime = glfwGetTime(); - double dt = currentTime - m_LastTime; - m_LastTime = currentTime; - - ResourceManager::Update(); - - // Update input - m_InputManager->Update(dt); - - m_World->Update(dt); - - -// -// if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) { -// ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl"); -// } -// - //TODO Fill up the renderQueue with models (Temp fix) -// TEMPAddToRenderQueue(); - - // Render scene - //TODO send renderqueue to draw. -// m_Renderer->Draw(m_RendererQueue); - - if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) { - ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl"); - } - - //TODO Fill up the renderQueue with models (Temp fix) - TEMPAddToRenderQueue(); - - // Render scene - //TODO send renderqueue to draw. - m_Renderer->Draw(m_RendererQueue); - - // Swap event queues - m_EventBroker->Clear(); - - glfwPollEvents(); - } - - std::shared_ptr m_TransformSystem; - std::shared_ptr m_LevelSystem; - - //TODO: Get this out of engine.h - void TEMPAddToRenderQueue() - { - - if (!m_TransformSystem) - m_TransformSystem = m_World->GetSystem(); - - m_RendererQueue.Clear(); - - for (auto &pair : *m_World->GetEntities()) - { - EntityID entity = pair.first; - - auto templateComponent = m_World->GetComponent(entity); - if (templateComponent) - continue; - - auto transform = m_World->GetComponent(entity); - if (!transform) - continue; - - auto modelComponent = m_World->GetComponent(entity); - if (modelComponent) - { - Model* modelAsset = nullptr; - modelAsset = ResourceManager::Load(modelComponent->ModelFile); - - if (modelAsset) - { - Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); - glm::mat4 modelMatrix = glm::translate(glm::mat4(), absoluteTransform.Position) - * glm::toMat4(absoluteTransform.Orientation) - * glm::scale(absoluteTransform.Scale); - EnqueueModel(modelAsset, modelMatrix, modelComponent->Transparent, modelComponent->Color); - } - } - - //TODO: Add LightLoadShit - - auto pointLightComponent = m_World->GetComponent(entity); - if (pointLightComponent) - { - Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); - EnqueuePointLight(absoluteTransform.Position, - pointLightComponent->Diffuse, - pointLightComponent->Specular, - pointLightComponent->Radius); - } - - - auto spriteComponent = m_World->GetComponent(entity); - if (spriteComponent) - { - - std::string normal = spriteComponent->NormalTexture; - std::string spec = spriteComponent->SpecularTexture; - - if (normal.empty()) { - normal = "Textures/Core/NeutralNormalMap.png"; - } - if (spec.empty()) { - spec = "Textures/Core/NeutralSpecularMap.png"; - } - auto texturediff = ResourceManager::Load(spriteComponent->SpriteFile); - auto texturenorm = ResourceManager::Load(normal); - auto texturespec = ResourceManager::Load(spec); - - - Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); - glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(absoluteTransform.Orientation).z, glm::vec3(0, 0, -1)); - glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position) - * glm::toMat4(orientation2D) - * glm::scale(absoluteTransform.Scale); - EnqueueSprite(texturediff, texturenorm, texturespec, modelMatrix, spriteComponent->Color); - } - } - - m_RendererQueue.Sort(); - } - - //TODO: Get this out of engine.h - void EnqueueModel(Model* model, glm::mat4 modelMatrix, float transparent, glm::vec4 color) - { - for (auto texGroup : model->TextureGroups) - { - ModelJob job; - job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; - job.DiffuseTexture = (texGroup.Texture) ? *texGroup.Texture : 0; - job.NormalTexture = (texGroup.NormalMap) ? *texGroup.NormalMap : 0; - job.SpecularTexture = (texGroup.SpecularMap) ? *texGroup.SpecularMap : 0; - job.VAO = model->VAO; - job.ElementBuffer = model->ElementBuffer; - job.StartIndex = texGroup.StartIndex; - job.EndIndex = texGroup.EndIndex; - job.ModelMatrix = modelMatrix; - job.Color = color; - - m_RendererQueue.Deferred.Add(job); - } - } - - // TODO: Get this out of engine.h - void EnqueueSprite(Texture* texture, Texture* normalTexture, Texture* specularTexture, glm::mat4 modelMatrix, glm::vec4 color) - { - SpriteJob job; - job.TextureID = texture->ResourceID; - job.DiffuseTexture = *texture; - job.NormalTexture = *normalTexture; - job.SpecularTexture = *specularTexture; - job.ModelMatrix = modelMatrix; - job.Color = color; - - m_RendererQueue.Deferred.Add(job); - } - - void EnqueuePointLight(glm::vec3 position, glm::vec3 diffuseColor, glm::vec3 specularColor, float radius) - { - PointLightJob job; - job.Position = position; - job.DiffuseColor = diffuseColor; - job.SpecularColor = specularColor; - job.Radius = radius; - - m_RendererQueue.Lights.Add(job); - - } - -private: - std::shared_ptr m_ResourceManager; - std::shared_ptr m_EventBroker; - std::shared_ptr m_Renderer; - RenderQueueCollection m_RendererQueue; - std::shared_ptr m_InputManager; - std::shared_ptr m_World; - - double m_LastTime; -}; - -} diff --git a/include/Core/Sound.h b/include/Core/Sound.h index f6dffff..8c7256d 100644 --- a/include/Core/Sound.h +++ b/include/Core/Sound.h @@ -5,6 +5,8 @@ #ifndef DAYDREAM_SOUND_H #define DAYDREAM_SOUND_H +#include + #include "AL/al.h" #include "AL/alc.h" #include "Core/System.h" @@ -12,7 +14,6 @@ #include "Core/EKeyDown.h" #include "Core/EventBroker.h" - namespace dd { namespace Systems @@ -21,7 +22,8 @@ class Sound : public System { public: Sound(World *world, std::shared_ptr eventBroker) : System(world, eventBroker) { } - + ~Sound(); + void Initialize() override; //void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void Update(double dt) override; @@ -32,30 +34,30 @@ public: //void StopSound(std::shared_ptr emitter); - void Initialize() override; + private: dd::EventRelay m_EKeyDown; + bool OnKeyDown(const dd::Events::KeyDown &event); 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; + 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_Sources; std::map m_BufferCache; - bool OnKeyDown(const dd::Events::KeyDown &event); //Temp - ALuint m_source; + ALuint m_Source; }; } diff --git a/src/game/Core/Sound.cpp b/src/game/Core/Sound.cpp index 150f4e7..fd5d649 100644 --- a/src/game/Core/Sound.cpp +++ b/src/game/Core/Sound.cpp @@ -4,61 +4,49 @@ #include "PrecompiledHeader.h" #include "Core/Sound.h" -void dd::Systems::Sound::Initialize() +dd::Systems::Sound::~Sound() { - LOG_INFO("Hello im an init func ----------------------------"); - /* - LOG_ERROR("Init started"); + alDeleteSources(1, m_Source); +} - //initialize OpenAL +void dd::Systems::Sound::Initialize() +{ //initialize OpenAL ALCdevice* device = alcOpenDevice(NULL); ALCcontext* context; - if(device) - { + + if (device) { context = alcCreateContext(device, NULL); alcMakeContextCurrent(context); } - else - { + 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::Update(double dt) { - /*const ALfloat pos[3] = {0, 0, 0}; + const ALfloat pos[3] = {0, 0, 0}; alListenerfv(AL_POSITION, pos); - alSourcefv(m_source, 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){ - m_source = CreateSource(); + if (event.KeyCode == 83) { + m_Source = CreateSource(); + ALuint buffer = LoadFile("Sounds/screwed.wav"); - ALuint buffer = LoadFile("assets/Sounds/screwed.wav"); - - alSourcei(m_source, AL_BUFFER, buffer); - alSourcePlay(m_source); - - - LOG_INFO("Playing sound (I wish LOL)"); + alSourcei(m_Source, AL_BUFFER, buffer); + alSourcePlay(m_Source); } } @@ -72,74 +60,80 @@ ALuint dd::Systems::Sound::CreateSource() ALuint dd::Systems::Sound::LoadFile(std::string path) { - if (m_BufferCache.find(path) != m_BufferCache.end()) + if (m_BufferCache.find(path) != m_BufferCache.end()) { return m_BufferCache[path]; + } - FILE *fp = NULL; - fp = fopen(path.c_str(), "rb"); + //Open file + FILE *fp = fopen(path.c_str(), "rb"); + if (!fp) { + LOG_ERROR("Failed to open file %s, no such file exists", path.c_str()); + return 0; + } //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') { + 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') { + 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]!=' ') { + 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_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') - { + 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); + fread(&m_DataSize, sizeof(unsigned long), 1, fp); - unsigned char* buf = new unsigned char[m_dataSize]; - fread(buf, sizeof(unsigned char), m_dataSize, 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) + if (m_BitsPerSample == 8) { + if (m_Channels == 1) { format = AL_FORMAT_MONO8; - else if(m_channels == 2) + } + else if (m_Channels == 2) { format = AL_FORMAT_STEREO8; + } } - if(m_bitsPerSample == 16) - { - if (m_channels == 1) + if (m_BitsPerSample == 16) { + if (m_Channels == 1) { format = AL_FORMAT_MONO16; - else if (m_channels == 2) + } + else if (m_Channels == 2) { format = AL_FORMAT_STEREO16; + } } ALuint buffer; alGenBuffers(1, &buffer); - alBufferData(buffer, format, buf, m_dataSize, m_sampleRate); + alBufferData(buffer, format, buf, m_DataSize, m_SampleRate); delete[] buf; m_BufferCache[path] = buffer;