From d8c4c6e5ff9138285aed5b10429f5ea5ab64e151 Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 7 Jan 2016 15:22:46 +0100 Subject: [PATCH 01/48] Created a primitive SoundSystem. Can play a sound with 'P' button. Using resourcemanager. Not using the created components yet. --- assets | 2 +- deps | 2 +- include/Engine/Sound/EPlaySound.h | 18 +++ include/Engine/Sound/Sound.h | 113 +++++++++++++++++++ include/Engine/Sound/SoundSystem.h | 62 ++++++++++ include/Game/Game.h | 7 ++ resources/Schema/Components.xsd | 2 + resources/Schema/Components/Listener.xml | 3 + resources/Schema/Components/Listener.xsd | 8 ++ resources/Schema/Components/SoundEmitter.xml | 7 ++ resources/Schema/Components/SoundEmitter.xsd | 22 ++++ resources/Schema/Types/Entity.xsd | 2 + src/Engine/CMakeLists.txt | 11 +- src/Engine/Sound/SoundSystem.cpp | 66 +++++++++++ src/Game/Game.cpp | 18 ++- 15 files changed, 338 insertions(+), 5 deletions(-) create mode 100644 include/Engine/Sound/EPlaySound.h create mode 100644 include/Engine/Sound/Sound.h create mode 100644 include/Engine/Sound/SoundSystem.h create mode 100644 resources/Schema/Components/Listener.xml create mode 100644 resources/Schema/Components/Listener.xsd create mode 100644 resources/Schema/Components/SoundEmitter.xml create mode 100644 resources/Schema/Components/SoundEmitter.xsd create mode 100644 src/Engine/Sound/SoundSystem.cpp diff --git a/assets b/assets index 673d4a4e..6cbf2365 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 673d4a4e4c5a3f5bc9fedf82234e8f8751f63a44 +Subproject commit 6cbf2365d49e6280750ea3bcd0f9c271779e6f15 diff --git a/deps b/deps index 1ae6ba5b..293516d6 160000 --- a/deps +++ b/deps @@ -1 +1 @@ -Subproject commit 1ae6ba5b1297ed71b560aee211b9f0007ba52547 +Subproject commit 293516d671b97de26594fe979b7898b7e271e24c diff --git a/include/Engine/Sound/EPlaySound.h b/include/Engine/Sound/EPlaySound.h new file mode 100644 index 00000000..83fc4629 --- /dev/null +++ b/include/Engine/Sound/EPlaySound.h @@ -0,0 +1,18 @@ +#ifndef Events_PlaySound_h__ +#define Events_PlaySound_h__ + +#include "Core/EventBroker.h" +#include "Core/Entity.h" + +namespace Events +{ + + struct PlaySound : Event +{ + std::string FilePath; + EntityID emitter; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/Sound.h b/include/Engine/Sound/Sound.h new file mode 100644 index 00000000..ffb9b30e --- /dev/null +++ b/include/Engine/Sound/Sound.h @@ -0,0 +1,113 @@ +#ifndef Sound_h__ +#define Sound_h__ + +#include "Core/ResourceManager.h" + +class Sound : public Resource +{ + friend class ResourceManager; +public: + Sound(std::string path) { m_Buffer = LoadFile(path); } + ~Sound() { ClearBuffer(); } + ALuint Buffer() { return m_Buffer; } + std::string Path() { return m_Path; } + float Gain() { return m_Gain; } + void SetGain(float value) { m_Gain = value; } + void ClearBuffer() { alDeleteBuffers(1, &m_Buffer); m_BufferCache.clear(); }; + +private: + float m_Gain = 1; + ALuint m_Buffer; + std::string m_Path; + + // 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 int m_DataSize; + std::map m_BufferCache; + + ALuint LoadFile(std::string path) + { + if (m_BufferCache.find(path) != m_BufferCache.end()) { + return m_BufferCache[path]; + } + + //// Open file + FILE *fp = fopen(path.c_str(), "rb"); + if (!fp) { + printf("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') { + printf("ERROR: No RIFF in WAVE-file"); + return 0; + } + + fread(&m_Size, 4 * sizeof(char), 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') { + printf("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] != ' ') { + printf("ERROR: No fmt in WAVE-file"); + return 0; + } + + //// READ THE DATA FROM WAVE-FILE + fread(&m_ChunkSize, 4 * sizeof(char), 1, fp); + fread(&m_FormatType, 2 * sizeof(char), 1, fp); + fread(&m_Channels, 2 * sizeof(char), 1, fp); + fread(&m_SampleRate, 4 * sizeof(char), 1, fp); + fread(&m_AvgBytesPerSec, 4 * sizeof(char), 1, fp); + fread(&m_BytesPerSample, 2 * sizeof(char), 1, fp); + fread(&m_BitsPerSample, 2 * sizeof(char), 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') { + printf("ERROR: WAVE-file Missing data"); + return 0; + } + + fread(&m_DataSize, 4 * sizeof(char), 1, fp); + + unsigned char* buf = new unsigned char[m_DataSize]; + fread(buf, sizeof(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; + } +}; + +#endif diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h new file mode 100644 index 00000000..0035a3bb --- /dev/null +++ b/include/Engine/Sound/SoundSystem.h @@ -0,0 +1,62 @@ +#ifndef SoundSystem_h__ +#define SoundSystem_h__ + +#include + +#include "glm/common.hpp" +#include "OpenAL/al.h" +#include "OpenAL/alc.h" + +#include "Core/World.h" +#include "Core/EventBroker.h" +#include "Sound/Sound.h" +#include "Sound/EPlaySound.h" + +struct Source +{ + Sound* SoundResource; + ALuint ALsource; +}; + +class SoundSystem +{ +public: + SoundSystem() { } + SoundSystem(EventBroker* eventBroker); + ~SoundSystem(); + void Update() { } // Update emitters +private: + // Private setters and getters for working with glm + void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; + glm::vec3 listnerPos() { glm::vec3 pos; alGetListener3f(AL_POSITION, &pos.x, &pos.y, &pos.z); return pos; }; + void setListenerVel(glm::vec3 vel) { alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z); }; + glm::vec3 listenerVel() { glm::vec3 vel; alGetListener3f(AL_VELOCITY, &vel.x, &vel.y, &vel.z); return vel; }; + void setListenerOri(glm::vec3 ori) { alListener3f(AL_ORIENTATION, ori.x, ori.y, ori.z); }; + glm::vec3 listenerOri() { glm::vec3 ori; alGetListener3f(AL_ORIENTATION, &ori.x, &ori.y, &ori.z); return ori; }; + void setSourcePos(ALuint source, glm::vec3 pos) { ALfloat spos[3] = { pos.x, pos.y, pos.z }; alSourcefv(source, AL_POSITION, spos); }; + void setSourceVel(ALuint source, glm::vec3 vel) { ALfloat svel[3] = { vel.x, vel.y, vel.z }; alSourcefv(source, AL_VELOCITY, svel); }; + void setSourceOri(ALuint source, glm::vec3 ori) { ALfloat sori[3] = { ori.x, ori.y, ori.z }; alSourcefv(source, AL_ORIENTATION, sori); }; + + // Logic + World* m_World; + EventBroker* m_EventBroker; + + ALuint createSource(); + void playSound(Source source); + void stopSound(Source source); + void stopEmitter(EntityID emitter); + + // OpenAL system variables + ALCdevice* m_ALCdevice = nullptr; + ALCcontext* m_ALCcontext = nullptr; + + // Logic + std::unordered_map m_Sources; + + // Events + EventRelay m_EPlaySound; + bool OnPlaySound(const Events::PlaySound &e); + +}; + +#endif \ No newline at end of file diff --git a/include/Game/Game.h b/include/Game/Game.h index 33dc88a6..6b284c74 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -25,6 +25,10 @@ #include "Network/Server.h" #include "Network/Client.h" +// Sound +#include "Sound/SoundSystem.h" +#include "Sound/EPlaySound.h" + class Game { @@ -54,6 +58,9 @@ private: Network* m_ClientOrServer; bool m_IsClientOrServer = false; + // Sound + SoundSystem* m_SoundSystem; + EventRelay m_EInputCommand; bool debugOnInputCommand(const Events::InputCommand& e); diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index fd04fd39..a287431c 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -8,4 +8,6 @@ + + \ No newline at end of file diff --git a/resources/Schema/Components/Listener.xml b/resources/Schema/Components/Listener.xml new file mode 100644 index 00000000..7d1fac13 --- /dev/null +++ b/resources/Schema/Components/Listener.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/resources/Schema/Components/Listener.xsd b/resources/Schema/Components/Listener.xsd new file mode 100644 index 00000000..5f71f11a --- /dev/null +++ b/resources/Schema/Components/Listener.xsd @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Components/SoundEmitter.xml b/resources/Schema/Components/SoundEmitter.xml new file mode 100644 index 00000000..a032dfbb --- /dev/null +++ b/resources/Schema/Components/SoundEmitter.xml @@ -0,0 +1,7 @@ + + 1.0 + 1.0 + 20.0 + 1.0 + 1.0 + \ No newline at end of file diff --git a/resources/Schema/Components/SoundEmitter.xsd b/resources/Schema/Components/SoundEmitter.xsd new file mode 100644 index 00000000..6bebcac5 --- /dev/null +++ b/resources/Schema/Components/SoundEmitter.xsd @@ -0,0 +1,22 @@ + + + + + + + + + + The "volume" of the emitter. A value betweeen 0-1 + + The pitch of the emitter. A value betweeen 0-1 + + The distance where there will no longer be any attenuation. + + The rolloff rate of the source. + + The distance that the source will be the loudest. + + + + \ No newline at end of file diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 92f7dc31..67612935 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -15,6 +15,8 @@ + + diff --git a/src/Engine/CMakeLists.txt b/src/Engine/CMakeLists.txt index 5d43db8b..40bc9cb0 100644 --- a/src/Engine/CMakeLists.txt +++ b/src/Engine/CMakeLists.txt @@ -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} diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp new file mode 100644 index 00000000..965f97a4 --- /dev/null +++ b/src/Engine/Sound/SoundSystem.cpp @@ -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(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; +} + diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 033db642..66ab6fae 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -5,6 +5,7 @@ Game::Game(int argc, char* argv[]) { ResourceManager::RegisterType("ConfigFile"); + ResourceManager::RegisterType("Sound"); ResourceManager::RegisterType("Model"); ResourceManager::RegisterType("Texture"); ResourceManager::RegisterType("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(); - + m_EventBroker->Process(); + 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(); From 2838446072fd3464718a342e0768c75ac7050416 Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 7 Jan 2016 16:03:33 +0100 Subject: [PATCH 02/48] In lack of camera entity I created a Listener cube to test 3D sound. Works fine. Updates listener pos each tick. --- include/Engine/Sound/SoundSystem.h | 4 ++-- src/Engine/Sound/SoundSystem.cpp | 21 +++++++++++++++------ src/Game/Game.cpp | 9 ++++++++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 0035a3bb..0555feb4 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -22,9 +22,9 @@ class SoundSystem { public: SoundSystem() { } - SoundSystem(EventBroker* eventBroker); + SoundSystem(World* world, EventBroker* eventBroker); ~SoundSystem(); - void Update() { } // Update emitters + void Update(); // Update emitters private: // Private setters and getters for working with glm void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 965f97a4..b9736096 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -1,8 +1,9 @@ #include "Sound/SoundSystem.h" -SoundSystem::SoundSystem(EventBroker* eventBroker) +SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) { m_EventBroker = eventBroker; + m_World = world; // Initialize OpenAL m_ALCdevice = alcOpenDevice(nullptr); if (m_ALCdevice != nullptr) { @@ -16,20 +17,29 @@ SoundSystem::SoundSystem(EventBroker* eventBroker) 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; } +void SoundSystem::Update() +{ + // Should only be one listener. + auto listenerComponents = m_World->GetComponents("Listener"); + for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { + EntityID listener = (*it).EntityID; + auto transform = m_World->GetComponent(listener, "Transform"); + setListenerPos(transform["Position"]); + } +} + ALuint SoundSystem::createSource() -{ +{ ALuint source; alGenSources((ALuint)1, &source); alSourcei(source, AL_REFERENCE_DISTANCE, 1.0); @@ -60,7 +70,6 @@ bool SoundSystem::OnPlaySound(const Events::PlaySound & e) sauce.ALsource = source; sauce.SoundResource = sound; playSound(sauce); - LOG_INFO("You are playing an imaginary sound now! :D"); return false; } diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 66ab6fae..1863fa38 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -65,7 +65,14 @@ Game::Game(int argc, char* argv[]) networkFunction(); } EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand); - m_SoundSystem = new SoundSystem(m_EventBroker); + m_SoundSystem = new SoundSystem(m_World, m_EventBroker); + + auto soundListenerCube = m_World->CreateEntity(); + m_World->AttachComponent(soundListenerCube, "Listener"); + m_World->AttachComponent(soundListenerCube, "Transform"); + auto model = m_World->AttachComponent(soundListenerCube, "Model"); + model["Resource"] = "Models/Core/UnitCube.obj"; + model["Color"] = glm::vec4(1, 0, 0, 1); m_LastTime = glfwGetTime(); } From da6e50dc7eb17671664033b871e58fc57c657ba6 Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 7 Jan 2016 17:04:43 +0100 Subject: [PATCH 03/48] Correct orientation. Need to clean this up. --- include/Engine/Sound/SoundSystem.h | 17 ++++++++++++++++- src/Engine/Sound/SoundSystem.cpp | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 0555feb4..de31e216 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -4,6 +4,7 @@ #include #include "glm/common.hpp" +#include "glm/gtx/rotate_vector.hpp" #include "OpenAL/al.h" #include "OpenAL/alc.h" @@ -31,7 +32,21 @@ private: glm::vec3 listnerPos() { glm::vec3 pos; alGetListener3f(AL_POSITION, &pos.x, &pos.y, &pos.z); return pos; }; void setListenerVel(glm::vec3 vel) { alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z); }; glm::vec3 listenerVel() { glm::vec3 vel; alGetListener3f(AL_VELOCITY, &vel.x, &vel.y, &vel.z); return vel; }; - void setListenerOri(glm::vec3 ori) { alListener3f(AL_ORIENTATION, ori.x, ori.y, ori.z); }; + void setListenerOri(glm::vec3 ori) + { + glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); + forward = glm::rotateX(forward, ori.x); + forward = glm::rotateY(forward, ori.y); + forward = glm::rotateZ(forward, ori.z); + glm::normalize(forward); + glm::vec3 up = glm::vec3(0.0, 1.0, 0.0); + up = glm::rotateX(up, ori.x); + up = glm::rotateY(up, ori.y); + up = glm::rotateZ(up, ori.z); + glm::normalize(up); + ALfloat lori[6] = { forward.x, forward.y, forward.z , up.x, up.y, up.z }; + alListenerfv(AL_ORIENTATION, lori); + }; glm::vec3 listenerOri() { glm::vec3 ori; alGetListener3f(AL_ORIENTATION, &ori.x, &ori.y, &ori.z); return ori; }; void setSourcePos(ALuint source, glm::vec3 pos) { ALfloat spos[3] = { pos.x, pos.y, pos.z }; alSourcefv(source, AL_POSITION, spos); }; void setSourceVel(ALuint source, glm::vec3 vel) { ALfloat svel[3] = { vel.x, vel.y, vel.z }; alSourcefv(source, AL_VELOCITY, svel); }; diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index b9736096..64312fa8 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -35,6 +35,7 @@ void SoundSystem::Update() EntityID listener = (*it).EntityID; auto transform = m_World->GetComponent(listener, "Transform"); setListenerPos(transform["Position"]); + setListenerOri(transform["Orientation"]); } } From 3cce9f80751d5bc2da692d9570e511e0f92f5967 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 8 Jan 2016 15:29:20 +0100 Subject: [PATCH 04/48] Using absolute transform --- include/Engine/Sound/SoundSystem.h | 28 ++--- resources/Schema/Components/SoundEmitter.xml | 1 + resources/Schema/Components/SoundEmitter.xsd | 1 + src/Engine/Sound/SoundSystem.cpp | 116 ++++++++++++++++--- 4 files changed, 111 insertions(+), 35 deletions(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index de31e216..5a8225ef 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -10,6 +10,7 @@ #include "Core/World.h" #include "Core/EventBroker.h" +#include "Rendering/RenderQueueFactory.h" // Absolute transform #include "Sound/Sound.h" #include "Sound/EPlaySound.h" @@ -27,35 +28,27 @@ public: ~SoundSystem(); void Update(); // Update emitters private: - // Private setters and getters for working with glm + // Help functions for working with OpenaAL void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; glm::vec3 listnerPos() { glm::vec3 pos; alGetListener3f(AL_POSITION, &pos.x, &pos.y, &pos.z); return pos; }; void setListenerVel(glm::vec3 vel) { alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z); }; glm::vec3 listenerVel() { glm::vec3 vel; alGetListener3f(AL_VELOCITY, &vel.x, &vel.y, &vel.z); return vel; }; - void setListenerOri(glm::vec3 ori) - { - glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); - forward = glm::rotateX(forward, ori.x); - forward = glm::rotateY(forward, ori.y); - forward = glm::rotateZ(forward, ori.z); - glm::normalize(forward); - glm::vec3 up = glm::vec3(0.0, 1.0, 0.0); - up = glm::rotateX(up, ori.x); - up = glm::rotateY(up, ori.y); - up = glm::rotateZ(up, ori.z); - glm::normalize(up); - ALfloat lori[6] = { forward.x, forward.y, forward.z , up.x, up.y, up.z }; - alListenerfv(AL_ORIENTATION, lori); - }; + void setListenerOri(glm::vec3 ori); glm::vec3 listenerOri() { glm::vec3 ori; alGetListener3f(AL_ORIENTATION, &ori.x, &ori.y, &ori.z); return ori; }; void setSourcePos(ALuint source, glm::vec3 pos) { ALfloat spos[3] = { pos.x, pos.y, pos.z }; alSourcefv(source, AL_POSITION, spos); }; void setSourceVel(ALuint source, glm::vec3 vel) { ALfloat svel[3] = { vel.x, vel.y, vel.z }; alSourcefv(source, AL_VELOCITY, svel); }; - void setSourceOri(ALuint source, glm::vec3 ori) { ALfloat sori[3] = { ori.x, ori.y, ori.z }; alSourcefv(source, AL_ORIENTATION, sori); }; + + bool isPlaying(ALuint source); // Logic World* m_World; EventBroker* m_EventBroker; + void initOpenAL(); + void updateEmitters(); + void updateListener(); + void deleteInactiveEmitters(); + void addNewEmitters(); ALuint createSource(); void playSound(Source source); void stopSound(Source source); @@ -71,7 +64,6 @@ private: // Events EventRelay m_EPlaySound; bool OnPlaySound(const Events::PlaySound &e); - }; #endif \ No newline at end of file diff --git a/resources/Schema/Components/SoundEmitter.xml b/resources/Schema/Components/SoundEmitter.xml index a032dfbb..cbcfc440 100644 --- a/resources/Schema/Components/SoundEmitter.xml +++ b/resources/Schema/Components/SoundEmitter.xml @@ -1,4 +1,5 @@ + Audio/crosscounter.wav 1.0 1.0 20.0 diff --git a/resources/Schema/Components/SoundEmitter.xsd b/resources/Schema/Components/SoundEmitter.xsd index 6bebcac5..31c18ce9 100644 --- a/resources/Schema/Components/SoundEmitter.xsd +++ b/resources/Schema/Components/SoundEmitter.xsd @@ -6,6 +6,7 @@ + The "volume" of the emitter. A value betweeen 0-1 diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 64312fa8..fd379d50 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -4,6 +4,17 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) { m_EventBroker = eventBroker; m_World = world; + + initOpenAL(); + + alSpeedOfSound(340.29f); // Speed of sound + alDistanceModel(AL_INVERSE_DISTANCE); + + EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound); +} + +void SoundSystem::initOpenAL() +{ // Initialize OpenAL m_ALCdevice = alcOpenDevice(nullptr); if (m_ALCdevice != nullptr) { @@ -12,11 +23,6 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) } else { LOG_ERROR("OpenAL failed to initialize."); } - - alSpeedOfSound(340.29f); // Speed of sound - alDistanceModel(AL_INVERSE_DISTANCE); - - EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound); } SoundSystem::~SoundSystem() @@ -28,14 +34,67 @@ SoundSystem::~SoundSystem() } void SoundSystem::Update() +{ + //deleteInactiveEmitters(); // Not tested + addNewEmitters(); + updateEmitters(); + updateListener(); +} + +void SoundSystem::deleteInactiveEmitters() +{ + auto emitterComponents = m_World->GetComponents("SoundEmitter"); + for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { + EntityID emitter = (*it).EntityID; + if (isPlaying(m_Sources[emitter].ALsource)) { // The sound is still playing, do not remove + continue; + } + alDeleteBuffers(1, &m_Sources[emitter].ALsource); + delete m_Sources[emitter].SoundResource; + m_Sources.erase(emitter); + } +} + +void SoundSystem::addNewEmitters() +{ + auto emitterComponents = m_World->GetComponents("SoundEmitter"); + for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { + EntityID emitter = (*it).EntityID; + std::unordered_map::iterator i; + i = m_Sources.find(emitter); + if (i == m_Sources.end()) { // Did not exist, add it + Source source; + source.ALsource = createSource(); + source.SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); + m_Sources[emitter] = source; + } + } +} + +void SoundSystem::updateEmitters() +{ + auto emitterComponents = m_World->GetComponents("SoundEmitter"); + for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { + EntityID emitter = (*it).EntityID; + std::unordered_map::iterator i; + i = m_Sources.find(emitter); + if (i != m_Sources.end()) { + setSourcePos(m_Sources[emitter].ALsource, RenderQueueFactory::AbsolutePosition(m_World, emitter)); + } + // No orientation, emitts in all directions + // Velocity for doppler effect + } +} + +void SoundSystem::updateListener() { // Should only be one listener. auto listenerComponents = m_World->GetComponents("Listener"); for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { EntityID listener = (*it).EntityID; - auto transform = m_World->GetComponent(listener, "Transform"); - setListenerPos(transform["Position"]); - setListenerOri(transform["Orientation"]); + setListenerPos(RenderQueueFactory::AbsolutePosition(m_World, listener)); + setListenerOri(glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, listener))); + // Velocity for doppler effect } } @@ -62,15 +121,38 @@ void SoundSystem::stopEmitter(EntityID emitter) bool SoundSystem::OnPlaySound(const Events::PlaySound & e) { - Sound *sound = ResourceManager::Load(e.FilePath); - if (sound == nullptr) { - return false; - } - ALuint source = createSource(); - Source sauce; - sauce.ALsource = source; - sauce.SoundResource = sound; - playSound(sauce); + //Sound *sound = ResourceManager::Load(e.FilePath); + //if (sound == nullptr) { + // return false; + //} + //ALuint source = createSource(); + //Source sauce; + //sauce.ALsource = source; + //sauce.SoundResource = sound; + //playSound(sauce); return false; } +void SoundSystem::setListenerOri(glm::vec3 ori) +{ + glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); + forward = glm::rotateX(forward, ori.x); + forward = glm::rotateY(forward, ori.y); + forward = glm::rotateZ(forward, ori.z); + glm::normalize(forward); + glm::vec3 up = glm::vec3(0.0, 1.0, 0.0); + up = glm::rotateX(up, ori.x); + up = glm::rotateY(up, ori.y); + up = glm::rotateZ(up, ori.z); + glm::normalize(up); + ALfloat lOri[6] = { forward.x, forward.y, forward.z, up.x, up.y, up.z }; + alListenerfv(AL_ORIENTATION, lOri); +} + +bool SoundSystem::isPlaying(ALuint source) +{ + ALenum state; + alGetSourcei(source, AL_SOURCE_STATE, &state); + return (state == AL_PLAYING); +} + From f80ad1377b88b9d3f88a76f1c78b2ac99b04ab7b Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 11 Jan 2016 17:52:27 +0100 Subject: [PATCH 05/48] Making use of Doppler effect. Added a bunch of events: Pause, stop, continue, variations of play. --- README.md | 3 +- include/Engine/Sound/EContinueSound.h | 17 ++ include/Engine/Sound/EPauseSound.h | 17 ++ include/Engine/Sound/EPlaySound.h | 2 +- include/Engine/Sound/EPlaySoundOnEntity.h | 21 +++ include/Engine/Sound/EPlaySoundOnPosition.h | 26 +++ include/Engine/Sound/EStopSound.h | 17 ++ include/Engine/Sound/SoundSystem.h | 37 ++-- include/Game/Game.h | 4 +- resources/Schema/Components/SoundEmitter.xml | 1 + resources/Schema/Components/SoundEmitter.xsd | 12 +- src/Engine/Sound/SoundSystem.cpp | 179 +++++++++++++++---- src/Game/Game.cpp | 14 +- 13 files changed, 296 insertions(+), 54 deletions(-) create mode 100644 include/Engine/Sound/EContinueSound.h create mode 100644 include/Engine/Sound/EPauseSound.h create mode 100644 include/Engine/Sound/EPlaySoundOnEntity.h create mode 100644 include/Engine/Sound/EPlaySoundOnPosition.h create mode 100644 include/Engine/Sound/EStopSound.h diff --git a/README.md b/README.md index 7c5f2268..7a488299 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ Libraries bundled along with binaries for Windows (MSVC14), available as a submo | **[zlib](http://www.zlib.net)** | 1.28 | [zlib License](http://www.zlib.net/zlib_license.html) | | **[libpng](http://www.libpng.org/pub/png/libpng.html)** | 1.6.19 | [libpng License](http://www.libpng.org/pub/png/src/libpng-LICENSE.txt) | | **[Xerces-C++](https://xerces.apache.org/xerces-c)** | 3.1.2 | [Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) | -| **[ImGui](https://github.com/ocornut/imgui)** | 2015-12-12 | [MIT License](https://github.com/ocornut/imgui/blob/de3a154f3801de22c8e0bd2aeabf663a70c05972/LICENSE) | +| **[ImGui](https://github.com/ocornut/imgui)** | 2015-12-12 | [MIT License](https://github.com/ocornut/imgui/blob/de3a154f3801de22c8e0bd2aeabf663a70c05972/LICENSE) +| **[OpenAL](https://www.openal.org/)** | 1.1 | [OpenAL License](http://www.libpng.org/pub/png/src/libpng-LICENSE.txt) | #### External libraries Libraries that are too big to be bundled with the project. diff --git a/include/Engine/Sound/EContinueSound.h b/include/Engine/Sound/EContinueSound.h new file mode 100644 index 00000000..2c624b0b --- /dev/null +++ b/include/Engine/Sound/EContinueSound.h @@ -0,0 +1,17 @@ +#ifndef Events_ContinueSound_h__ +#define Events_ContinueSound_h__ + +#include "Core/EventBroker.h" +#include "Core/Entity.h" + +namespace Events +{ + +struct ContinueSound : Event +{ + EntityID EmitterID; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/EPauseSound.h b/include/Engine/Sound/EPauseSound.h new file mode 100644 index 00000000..f9e2e6f0 --- /dev/null +++ b/include/Engine/Sound/EPauseSound.h @@ -0,0 +1,17 @@ +#ifndef Events_PauseSound_h__ +#define Events_PauseSound_h__ + +#include "Core/EventBroker.h" +#include "Core/Entity.h" + +namespace Events +{ + +struct PauseSound : Event +{ + EntityID EmitterID; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/EPlaySound.h b/include/Engine/Sound/EPlaySound.h index 83fc4629..9c9eab0a 100644 --- a/include/Engine/Sound/EPlaySound.h +++ b/include/Engine/Sound/EPlaySound.h @@ -10,7 +10,7 @@ namespace Events struct PlaySound : Event { std::string FilePath; - EntityID emitter; + EntityID EmitterID; }; } diff --git a/include/Engine/Sound/EPlaySoundOnEntity.h b/include/Engine/Sound/EPlaySoundOnEntity.h new file mode 100644 index 00000000..39dea432 --- /dev/null +++ b/include/Engine/Sound/EPlaySoundOnEntity.h @@ -0,0 +1,21 @@ +#ifndef Events_PlaySoundOnEntity_h__ +#define Events_PlaySoundOnEntity_h__ + +#include +#include "Core/Entity.h" +#include "Core/Event.h" + +namespace Events +{ + +// Plays a sound on an entity with a SoundEmitter component attached. +// Sound behavior is thereby specified in the SoundEmitter component. +struct PlaySoundOnEntity : public Event +{ + EntityID EmitterID = 0; + std::string FilePath = ""; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/EPlaySoundOnPosition.h b/include/Engine/Sound/EPlaySoundOnPosition.h new file mode 100644 index 00000000..6a6b4d13 --- /dev/null +++ b/include/Engine/Sound/EPlaySoundOnPosition.h @@ -0,0 +1,26 @@ +#ifndef Events_PlaySoundOnPosition_h__ +#define Events_PlaySoundOnPosition_h__ + +#include +#include +#include "Core/Event.h" + +namespace Events +{ + +// Plays a sound on a given position +struct PlaySoundOnPosition : public Event +{ + glm::vec3 Position = glm::vec3(0); + std::string FilePath = ""; + float Gain = 1; + float Pitch = 1; + bool Loop = false; + float MaxDistance = 20; + float RollOffFactor = 1; + float ReferenceDistance = 1; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/EStopSound.h b/include/Engine/Sound/EStopSound.h new file mode 100644 index 00000000..b6cd37b8 --- /dev/null +++ b/include/Engine/Sound/EStopSound.h @@ -0,0 +1,17 @@ +#ifndef Events_StopSound_h__ +#define Events_StopSound_h__ + +#include "Core/EventBroker.h" +#include "Core/Entity.h" + +namespace Events +{ + +struct StopSound : Event +{ + EntityID EmitterID; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 5a8225ef..5100a54b 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -4,7 +4,7 @@ #include #include "glm/common.hpp" -#include "glm/gtx/rotate_vector.hpp" +#include "glm/gtx/rotate_vector.hpp" // Calculate Up vector #include "OpenAL/al.h" #include "OpenAL/alc.h" @@ -13,9 +13,16 @@ #include "Rendering/RenderQueueFactory.h" // Absolute transform #include "Sound/Sound.h" #include "Sound/EPlaySound.h" +#include "Sound/EPlaySoundOnEntity.h" +#include "Sound/EPlaySoundOnPosition.h" +#include "Sound/EPauseSound.h" +#include "Sound/EStopSound.h" +#include "Sound/EContinueSound.h" + struct Source { + Source() { } Sound* SoundResource; ALuint ALsource; }; @@ -38,32 +45,42 @@ private: void setSourcePos(ALuint source, glm::vec3 pos) { ALfloat spos[3] = { pos.x, pos.y, pos.z }; alSourcefv(source, AL_POSITION, spos); }; void setSourceVel(ALuint source, glm::vec3 vel) { ALfloat svel[3] = { vel.x, vel.y, vel.z }; alSourcefv(source, AL_VELOCITY, svel); }; - bool isPlaying(ALuint source); - // Logic - World* m_World; - EventBroker* m_EventBroker; - void initOpenAL(); void updateEmitters(); void updateListener(); void deleteInactiveEmitters(); void addNewEmitters(); - ALuint createSource(); - void playSound(Source source); - void stopSound(Source source); + Source* createSource(std::string filePath); + void playSound(Source* source); + void stopSound(Source* source); void stopEmitter(EntityID emitter); + void stopEmitters(); + bool isPlaying(ALuint source); + void setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance); // OpenAL system variables ALCdevice* m_ALCdevice = nullptr; ALCcontext* m_ALCcontext = nullptr; // Logic - std::unordered_map m_Sources; + World* m_World; + EventBroker* m_EventBroker; + std::unordered_map m_Sources; // Events EventRelay m_EPlaySound; bool OnPlaySound(const Events::PlaySound &e); + EventRelay m_EPlaySoundOnEntity; + bool OnPlaySoundOnEntity(const Events::PlaySoundOnEntity &e); + EventRelay m_EPlaySoundOnPosition; + bool OnPlaySoundOnPosition(const Events::PlaySoundOnPosition &e); + EventRelay m_EPauseSound; + bool OnPauseSound(const Events::PauseSound &e); + EventRelay m_EStopSound; + bool OnStopSound(const Events::StopSound &e); + EventRelay m_EContinueSound; + bool OnContinueSound(const Events::ContinueSound &e); }; #endif \ No newline at end of file diff --git a/include/Game/Game.h b/include/Game/Game.h index 6b284c74..a4264840 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -27,7 +27,9 @@ // Sound #include "Sound/SoundSystem.h" -#include "Sound/EPlaySound.h" +//#include "Sound/EPlaySound.h" +//#include "Sound/EPlaySoundOnEntity.h" +//#include "Sound/EPlaySoundOnPosition.h" class Game diff --git a/resources/Schema/Components/SoundEmitter.xml b/resources/Schema/Components/SoundEmitter.xml index cbcfc440..871919f1 100644 --- a/resources/Schema/Components/SoundEmitter.xml +++ b/resources/Schema/Components/SoundEmitter.xml @@ -2,6 +2,7 @@ Audio/crosscounter.wav 1.0 1.0 + false 20.0 1.0 1.0 diff --git a/resources/Schema/Components/SoundEmitter.xsd b/resources/Schema/Components/SoundEmitter.xsd index 31c18ce9..c734e6ba 100644 --- a/resources/Schema/Components/SoundEmitter.xsd +++ b/resources/Schema/Components/SoundEmitter.xsd @@ -7,15 +7,17 @@ - + The "volume" of the emitter. A value betweeen 0-1 - + The pitch of the emitter. A value betweeen 0-1 - + + If the sound should loop or not. + The distance where there will no longer be any attenuation. - + The rolloff rate of the source. - + The distance that the source will be the loudest. diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index fd379d50..582ca2db 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -8,9 +8,15 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) initOpenAL(); alSpeedOfSound(340.29f); // Speed of sound - alDistanceModel(AL_INVERSE_DISTANCE); + alDistanceModel(AL_LINEAR_DISTANCE); + alDopplerFactor(1); EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound); + EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnEntity, &SoundSystem::OnPlaySoundOnEntity); + EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnPosition, &SoundSystem::OnPlaySoundOnPosition); + EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound); + EVENT_SUBSCRIBE_MEMBER(m_EPauseSound, &SoundSystem::OnPauseSound); + EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundSystem::OnContinueSound); } void SoundSystem::initOpenAL() @@ -27,12 +33,31 @@ void SoundSystem::initOpenAL() SoundSystem::~SoundSystem() { + stopEmitters(); // Stopps emitters + deleteInactiveEmitters(); // Deletes stopped emitters + + std::unordered_map::iterator it; + for (it = m_Sources.begin(); it != m_Sources.end(); it++) { + m_World->DeleteEntity((*it).first); + } + m_Sources.clear(); + alcDestroyContext(m_ALCcontext); alcCloseDevice(m_ALCdevice); delete m_ALCcontext; delete m_ALCdevice; } +void SoundSystem::stopEmitters() +{ + std::unordered_map::iterator it; + for (it = m_Sources.begin(); it != m_Sources.end(); it++) { + if (isPlaying((*it).second->ALsource)) { + stopSound((*it).second); + } + } +} + void SoundSystem::Update() { //deleteInactiveEmitters(); // Not tested @@ -44,14 +69,19 @@ void SoundSystem::Update() void SoundSystem::deleteInactiveEmitters() { auto emitterComponents = m_World->GetComponents("SoundEmitter"); - for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { + for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { EntityID emitter = (*it).EntityID; - if (isPlaying(m_Sources[emitter].ALsource)) { // The sound is still playing, do not remove + if (isPlaying(m_Sources[emitter]->ALsource)) { // The sound is still playing, do not remove + it++; continue; } - alDeleteBuffers(1, &m_Sources[emitter].ALsource); - delete m_Sources[emitter].SoundResource; - m_Sources.erase(emitter); + else { + alDeleteBuffers(1, &m_Sources[emitter]->ALsource); + alDeleteSources(1, &m_Sources[emitter]->ALsource); + //delete m_Sources[emitter]->SoundResource; + m_Sources.erase(emitter); + m_World->DeleteEntity(emitter); + } } } @@ -60,12 +90,19 @@ void SoundSystem::addNewEmitters() auto emitterComponents = m_World->GetComponents("SoundEmitter"); for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { EntityID emitter = (*it).EntityID; - std::unordered_map::iterator i; + std::unordered_map::iterator i; i = m_Sources.find(emitter); if (i == m_Sources.end()) { // Did not exist, add it - Source source; - source.ALsource = createSource(); - source.SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); + Source* source = createSource((std::string)(*it)["FilePath"]); + setSoundProperties ( + source->ALsource, + (float)(double)(*it)["Gain"], + (float)(double)(*it)["Pitch"], + (bool)(*it)["Loop"], + (float)(double)(*it)["MaxDistance"], + (float)(double)(*it)["RollOffFactor"], + (float)(double)(*it)["ReferenceDistance"] + ); m_Sources[emitter] = source; } } @@ -76,13 +113,26 @@ void SoundSystem::updateEmitters() auto emitterComponents = m_World->GetComponents("SoundEmitter"); for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { EntityID emitter = (*it).EntityID; - std::unordered_map::iterator i; + std::unordered_map::iterator i; i = m_Sources.find(emitter); if (i != m_Sources.end()) { - setSourcePos(m_Sources[emitter].ALsource, RenderQueueFactory::AbsolutePosition(m_World, emitter)); + glm::vec3 previousPos; + alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get previous pos + glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); // Get next pos + glm::vec3 velocity = nextPos - previousPos; // Calculate velocity + setSourcePos(m_Sources[emitter]->ALsource, nextPos); // Set next pos + setSourceVel(m_Sources[emitter]->ALsource, velocity); // Set velocity + setSoundProperties( + m_Sources[emitter]->ALsource, + (float)(double)(*it)["Gain"], + (float)(double)(*it)["Pitch"], + (bool)(*it)["Loop"], + (float)(double)(*it)["MaxDistance"], + (float)(double)(*it)["RollOffFactor"], + (float)(double)(*it)["ReferenceDistance"] + ); } // No orientation, emitts in all directions - // Velocity for doppler effect } } @@ -92,47 +142,97 @@ void SoundSystem::updateListener() auto listenerComponents = m_World->GetComponents("Listener"); for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { EntityID listener = (*it).EntityID; - setListenerPos(RenderQueueFactory::AbsolutePosition(m_World, listener)); + glm::vec3 previousPos; + alGetListener3f(AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get previous pos + glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, listener); // Get next (current) pos + glm::vec3 velocity = nextPos - previousPos; // Calculate velocity + setListenerPos(nextPos); + setListenerVel(velocity); setListenerOri(glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, listener))); - // Velocity for doppler effect } } -ALuint SoundSystem::createSource() +Source* SoundSystem::createSource(std::string filePath) { - ALuint source; - alGenSources((ALuint)1, &source); - alSourcei(source, AL_REFERENCE_DISTANCE, 1.0); - alSourcei(source, AL_MAX_DISTANCE, FLT_MAX); + ALuint alSource; + alGenSources((ALuint)1, &alSource); + alSourcei(alSource, AL_REFERENCE_DISTANCE, 1.0); + alSourcei(alSource, AL_MAX_DISTANCE, FLT_MAX); + Source* source = new Source(); + source->ALsource = alSource; + source->SoundResource = ResourceManager::Load(filePath); return source; } -void SoundSystem::playSound(Source source) +void SoundSystem::playSound(Source* source) { - alSourcei(source.ALsource, AL_BUFFER, source.SoundResource->Buffer()); - alSourcePlay(source.ALsource); + alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer()); + alSourcePlay(source->ALsource); } -void SoundSystem::stopSound(Source source) -{ } +void SoundSystem::stopSound(Source* source) +{ + alSourceStop(source->ALsource); +} void SoundSystem::stopEmitter(EntityID emitter) -{ } +{ + +} bool SoundSystem::OnPlaySound(const Events::PlaySound & e) { - //Sound *sound = ResourceManager::Load(e.FilePath); - //if (sound == nullptr) { - // return false; - //} - //ALuint source = createSource(); - //Source sauce; - //sauce.ALsource = source; - //sauce.SoundResource = sound; - //playSound(sauce); + Source* sauce = createSource(e.FilePath); + playSound(sauce); return false; } +bool SoundSystem::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) +{ + Source* source = createSource(e.FilePath); + m_Sources[e.emitterID] = source; + playSound(source); + return false; +} + +bool SoundSystem::OnPlaySoundOnPosition(const Events::PlaySoundOnPosition & e) +{ + Source* source = createSource(e.FilePath); + auto emitterID = m_World->CreateEntity(); + auto transform = m_World->AttachComponent(emitterID, "Transform"); + (glm::vec3&)transform["Position"] = e.Position; + auto emitter = m_World->AttachComponent(emitterID, "SoundEmitter"); + (float&)(double)emitter["Gain"] = e.Gain; + (float&)(double)emitter["Pitch"] = e.Pitch; + (bool&)emitter["Loop"] = e.Loop; + (float&)(double)emitter["MaxDistance"] = e.MaxDistance; + (float&)(double)emitter["RollOffFactor"] = e.RollOffFactor; + (float&)(double)emitter["ReferenceDistance"] = e.ReferenceDistance; + auto model = m_World->AttachComponent(emitterID, "Model"); + (std::string&)model["Resource"] = "Models/Core/UnitCube.obj"; + m_Sources[emitterID] = source; + playSound(source); + return false; +} + +bool SoundSystem::OnPauseSound(const Events::PauseSound & e) +{ + alSourcePause(m_Sources[e.EmitterID]->ALsource); + return false; +} + +bool SoundSystem::OnStopSound(const Events::StopSound & e) +{ + alSourceStop(m_Sources[e.EmitterID]->ALsource); + return false; +} + +bool SoundSystem::OnContinueSound(const Events::ContinueSound & e) +{ + alSourcePlay(m_Sources[e.EmitterID]->ALsource); + return true; +} + void SoundSystem::setListenerOri(glm::vec3 ori) { glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); @@ -156,3 +256,12 @@ bool SoundSystem::isPlaying(ALuint source) return (state == AL_PLAYING); } +void SoundSystem::setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance) +{ + alSourcef(source, AL_GAIN, gain); + alSourcef(source, AL_PITCH, pitch); + alSourcei(source, AL_LOOPING, (int)loop); // YOLO + alSourcef(source, AL_MAX_DISTANCE, maxDistance); + alSourcef(source, AL_ROLLOFF_FACTOR, rollOffFactor); + alSourcef(source, AL_REFERENCE_DISTANCE, referenceDistance); +} diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 1863fa38..86682591 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -130,8 +130,20 @@ void Game::Tick() bool Game::debugOnInputCommand(const Events::InputCommand & e) { if (e.Command == "PlaySound" && e.Value > 0) { - Events::PlaySound e; + Events::PlaySoundOnEntity e; + e.emitterID = 18; e.FilePath = "Audio/crosscounter.wav"; + //e.emitterID = 18; // rofl + m_EventBroker->Publish(e); + } + if (e.Command == "Reload" && e.Value > 0) { + Events::PauseSound e; + e.EmitterID = 18; + m_EventBroker->Publish(e); + } + if (e.Command == "Crouch" && e.Value > 0) { + Events::ContinueSound e; + e.EmitterID = 18; m_EventBroker->Publish(e); } return true; From 976033c2f66368ce26620f8f2084bd708a9580d6 Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 12 Jan 2016 14:03:00 +0100 Subject: [PATCH 06/48] Added events: PlayBackgroundMusic, SetBGMGain, SetSFXGain. Added some editor specific code. --- include/Engine/Sound/EPlayBackgroundMusic.h | 18 +++++ include/Engine/Sound/EPlaySoundOnPosition.h | 2 +- include/Engine/Sound/ESetBGMGain.h | 16 +++++ include/Engine/Sound/ESetSFXGain.h | 16 +++++ include/Engine/Sound/Sound.h | 12 ++-- include/Engine/Sound/SoundSystem.h | 25 +++++-- resources/Schema/Components/SoundEmitter.xml | 2 +- src/Engine/Sound/SoundSystem.cpp | 76 +++++++++++++++----- src/Game/Game.cpp | 15 +--- 9 files changed, 139 insertions(+), 43 deletions(-) create mode 100644 include/Engine/Sound/EPlayBackgroundMusic.h create mode 100644 include/Engine/Sound/ESetBGMGain.h create mode 100644 include/Engine/Sound/ESetSFXGain.h diff --git a/include/Engine/Sound/EPlayBackgroundMusic.h b/include/Engine/Sound/EPlayBackgroundMusic.h new file mode 100644 index 00000000..4276ad4a --- /dev/null +++ b/include/Engine/Sound/EPlayBackgroundMusic.h @@ -0,0 +1,18 @@ +#ifndef Events_PlayBackgroundMusic_h__ +#define Events_PlayBackgroundMusic_h__ + +#include +#include "Core/Entity.h" +#include "Core/Event.h" + +namespace Events +{ + +struct PlayBackgroundMusic : public Event +{ + std::string FilePath = ""; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/EPlaySoundOnPosition.h b/include/Engine/Sound/EPlaySoundOnPosition.h index 6a6b4d13..324b8fae 100644 --- a/include/Engine/Sound/EPlaySoundOnPosition.h +++ b/include/Engine/Sound/EPlaySoundOnPosition.h @@ -2,7 +2,7 @@ #define Events_PlaySoundOnPosition_h__ #include -#include +#include #include "Core/Event.h" namespace Events diff --git a/include/Engine/Sound/ESetBGMGain.h b/include/Engine/Sound/ESetBGMGain.h new file mode 100644 index 00000000..4efe34ce --- /dev/null +++ b/include/Engine/Sound/ESetBGMGain.h @@ -0,0 +1,16 @@ +#ifndef Events_SetBGMGain_h__ +#define Events_SetBGMGain_h__ + +#include "Core/Event.h" + +namespace Events +{ + +struct SetBGMGain : public Event +{ + float Gain; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/ESetSFXGain.h b/include/Engine/Sound/ESetSFXGain.h new file mode 100644 index 00000000..cbc06e97 --- /dev/null +++ b/include/Engine/Sound/ESetSFXGain.h @@ -0,0 +1,16 @@ +#ifndef Events_SetSFXGain_h__ +#define Events_SetSFXGain_h__ + +#include "Core/Event.h" + +namespace Events +{ + +struct SetSFXGain : public Event +{ + float Gain; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Sound/Sound.h b/include/Engine/Sound/Sound.h index ffb9b30e..6b2aac04 100644 --- a/include/Engine/Sound/Sound.h +++ b/include/Engine/Sound/Sound.h @@ -7,7 +7,7 @@ class Sound : public Resource { friend class ResourceManager; public: - Sound(std::string path) { m_Buffer = LoadFile(path); } + Sound(std::string path) { m_Buffer = LoadFile(path); m_Path = path; } ~Sound() { ClearBuffer(); } ALuint Buffer() { return m_Buffer; } std::string Path() { return m_Path; } @@ -35,14 +35,14 @@ private: return m_BufferCache[path]; } - //// Open file + // Open file FILE *fp = fopen(path.c_str(), "rb"); if (!fp) { - printf("Failed to open file %s, no such file exists", path.c_str()); + printf("Sound: Failed to open file %s, no such file exists", path.c_str()); return 0; } - //// CHECK FOR VALID WAVE-FILE + //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') { printf("ERROR: No RIFF in WAVE-file"); @@ -62,7 +62,7 @@ private: return 0; } - //// READ THE DATA FROM WAVE-FILE + // READ THE DATA FROM WAVE-FILE fread(&m_ChunkSize, 4 * sizeof(char), 1, fp); fread(&m_FormatType, 2 * sizeof(char), 1, fp); fread(&m_Channels, 2 * sizeof(char), 1, fp); @@ -83,7 +83,7 @@ private: fread(buf, sizeof(char), m_DataSize, fp); fclose(fp); - //// Create buffer + // Create buffer ALuint format = 0; if (m_BitsPerSample == 8) { if (m_Channels == 1) { diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 5100a54b..21968cd5 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -15,9 +15,12 @@ #include "Sound/EPlaySound.h" #include "Sound/EPlaySoundOnEntity.h" #include "Sound/EPlaySoundOnPosition.h" +#include "Sound/EPlayBackgroundMusic.h" #include "Sound/EPauseSound.h" -#include "Sound/EStopSound.h" #include "Sound/EContinueSound.h" +#include "Sound/EStopSound.h" +#include "Sound/ESetBGMGain.h" +#include "Sound/ESetSFXGain.h" struct Source @@ -31,9 +34,10 @@ class SoundSystem { public: SoundSystem() { } - SoundSystem(World* world, EventBroker* eventBroker); + SoundSystem(World* world, EventBroker* eventBroker, bool editorMode); ~SoundSystem(); - void Update(); // Update emitters + // Update emitters / listener + void Update(); private: // Help functions for working with OpenaAL void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; @@ -54,9 +58,9 @@ private: Source* createSource(std::string filePath); void playSound(Source* source); void stopSound(Source* source); - void stopEmitter(EntityID emitter); void stopEmitters(); bool isPlaying(ALuint source); + void setGain(Source* source, float gain); void setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance); // OpenAL system variables @@ -64,9 +68,12 @@ private: ALCcontext* m_ALCcontext = nullptr; // Logic - World* m_World; - EventBroker* m_EventBroker; + World* m_World = nullptr; + EventBroker* m_EventBroker = nullptr; std::unordered_map m_Sources; + float m_BGMVolumeChannel = 1.f; + float m_SFXVolumeChannel = 1.f; + bool m_EditorEnabled = false; // Events EventRelay m_EPlaySound; @@ -81,6 +88,12 @@ private: bool OnStopSound(const Events::StopSound &e); EventRelay m_EContinueSound; bool OnContinueSound(const Events::ContinueSound &e); + EventRelay m_EPlayBackgroundMusic; + bool OnPlayBackgroundMusic(const Events::PlayBackgroundMusic &e); + EventRelay m_ESetBGMGain; + bool OnSetBGMGain(const Events::SetBGMGain &e); // Not tested + EventRelay m_ESetSFXGain; + bool OnSetSFXGain(const Events::SetSFXGain &e); // Not tested }; #endif \ No newline at end of file diff --git a/resources/Schema/Components/SoundEmitter.xml b/resources/Schema/Components/SoundEmitter.xml index 871919f1..11093e37 100644 --- a/resources/Schema/Components/SoundEmitter.xml +++ b/resources/Schema/Components/SoundEmitter.xml @@ -1,5 +1,5 @@ - Audio/crosscounter.wav + 1.0 1.0 false diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 582ca2db..6481f27c 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -1,9 +1,10 @@ #include "Sound/SoundSystem.h" -SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) +SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode) { m_EventBroker = eventBroker; m_World = world; + m_EditorEnabled = editorMode; initOpenAL(); @@ -17,6 +18,9 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound); EVENT_SUBSCRIBE_MEMBER(m_EPauseSound, &SoundSystem::OnPauseSound); EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundSystem::OnContinueSound); + EVENT_SUBSCRIBE_MEMBER(m_EPlayBackgroundMusic, &SoundSystem::OnPlayBackgroundMusic); + //EVENT_SUBSCRIBE_MEMBER(m_ESetBGMGain, &SoundSystem::SetBGMGain); + //EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundSystem::OnSetSFXGain); } void SoundSystem::initOpenAL() @@ -36,6 +40,7 @@ SoundSystem::~SoundSystem() stopEmitters(); // Stopps emitters deleteInactiveEmitters(); // Deletes stopped emitters + // Delete entities std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end(); it++) { m_World->DeleteEntity((*it).first); @@ -116,12 +121,15 @@ void SoundSystem::updateEmitters() std::unordered_map::iterator i; i = m_Sources.find(emitter); if (i != m_Sources.end()) { + // Get previous pos glm::vec3 previousPos; - alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get previous pos - glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); // Get next pos - glm::vec3 velocity = nextPos - previousPos; // Calculate velocity - setSourcePos(m_Sources[emitter]->ALsource, nextPos); // Set next pos - setSourceVel(m_Sources[emitter]->ALsource, velocity); // Set velocity + alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); + // Get next pos + glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); + // Calculate velocity + glm::vec3 velocity = nextPos - previousPos; + setSourcePos(m_Sources[emitter]->ALsource, nextPos); + setSourceVel(m_Sources[emitter]->ALsource, velocity); setSoundProperties( m_Sources[emitter]->ALsource, (float)(double)(*it)["Gain"], @@ -131,8 +139,16 @@ void SoundSystem::updateEmitters() (float)(double)(*it)["RollOffFactor"], (float)(double)(*it)["ReferenceDistance"] ); + + // To make an emitter play when spawned in editor mode + if (m_EditorEnabled) { + // Path changed + if (m_Sources[emitter]->SoundResource->Path() != (std::string)(*it)["FilePath"]) { + m_Sources[emitter]->SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); + playSound(m_Sources[emitter]); + } + } } - // No orientation, emitts in all directions } } @@ -156,8 +172,8 @@ Source* SoundSystem::createSource(std::string filePath) { ALuint alSource; alGenSources((ALuint)1, &alSource); - alSourcei(alSource, AL_REFERENCE_DISTANCE, 1.0); - alSourcei(alSource, AL_MAX_DISTANCE, FLT_MAX); + alSourcef(alSource, AL_REFERENCE_DISTANCE, 1.0); + alSourcef(alSource, AL_MAX_DISTANCE, FLT_MAX); Source* source = new Source(); source->ALsource = alSource; source->SoundResource = ResourceManager::Load(filePath); @@ -175,11 +191,6 @@ void SoundSystem::stopSound(Source* source) alSourceStop(source->ALsource); } -void SoundSystem::stopEmitter(EntityID emitter) -{ - -} - bool SoundSystem::OnPlaySound(const Events::PlaySound & e) { Source* sauce = createSource(e.FilePath); @@ -190,7 +201,7 @@ bool SoundSystem::OnPlaySound(const Events::PlaySound & e) bool SoundSystem::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) { Source* source = createSource(e.FilePath); - m_Sources[e.emitterID] = source; + m_Sources[e.EmitterID] = source; playSound(source); return false; } @@ -233,6 +244,34 @@ bool SoundSystem::OnContinueSound(const Events::ContinueSound & e) return true; } +bool SoundSystem::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) +{ + auto listenerComponents = m_World->GetComponents("Listener"); + for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { + auto emitterChild = m_World->CreateEntity((*it).EntityID); + auto emitter = m_World->AttachComponent(emitterChild, "SoundEmitter"); + (bool&)emitter["Loop"] = true; + (std::string&)emitter["FilePath"] = e.FilePath; + m_World->AttachComponent(emitterChild, "Transform"); + Source* source = createSource(e.FilePath); + m_Sources[emitterChild] = source; + playSound(source); + } + return false; +} + +bool SoundSystem::OnSetBGMGain(const Events::SetBGMGain & e) +{ + m_BGMVolumeChannel = e.Gain; + return true; +} + +bool SoundSystem::OnSetSFXGain(const Events::SetSFXGain & e) +{ + m_SFXVolumeChannel = e.Gain; + return true; +} + void SoundSystem::setListenerOri(glm::vec3 ori) { glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); @@ -256,9 +295,14 @@ bool SoundSystem::isPlaying(ALuint source) return (state == AL_PLAYING); } +void SoundSystem::setGain(Source * source, float gain) +{ + alSourcef(source->ALsource, AL_GAIN, gain); +} + void SoundSystem::setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance) { - alSourcef(source, AL_GAIN, gain); + alSourcef(source, AL_GAIN, gain * m_SFXVolumeChannel); alSourcef(source, AL_PITCH, pitch); alSourcei(source, AL_LOOPING, (int)loop); // YOLO alSourcef(source, AL_MAX_DISTANCE, maxDistance); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 86682591..d02b480d 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -65,7 +65,7 @@ Game::Game(int argc, char* argv[]) networkFunction(); } EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand); - m_SoundSystem = new SoundSystem(m_World, m_EventBroker); + m_SoundSystem = new SoundSystem(m_World, m_EventBroker, m_Config->Get("Debug.EditorEnabled", false)); auto soundListenerCube = m_World->CreateEntity(); m_World->AttachComponent(soundListenerCube, "Listener"); @@ -130,22 +130,11 @@ void Game::Tick() bool Game::debugOnInputCommand(const Events::InputCommand & e) { if (e.Command == "PlaySound" && e.Value > 0) { - Events::PlaySoundOnEntity e; - e.emitterID = 18; + Events::PlayBackgroundMusic e; e.FilePath = "Audio/crosscounter.wav"; //e.emitterID = 18; // rofl m_EventBroker->Publish(e); } - if (e.Command == "Reload" && e.Value > 0) { - Events::PauseSound e; - e.EmitterID = 18; - m_EventBroker->Publish(e); - } - if (e.Command == "Crouch" && e.Value > 0) { - Events::ContinueSound e; - e.EmitterID = 18; - m_EventBroker->Publish(e); - } return true; } From 5697e8bc85e02f7c55b38973fcf8e2b845b2609b Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 12 Jan 2016 14:51:10 +0100 Subject: [PATCH 07/48] Deleting an entity now tells openal to behave accordingly. --- src/Engine/Sound/SoundSystem.cpp | 109 ++++++++++++++++++------------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 6481f27c..ce93515f 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -5,7 +5,7 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode m_EventBroker = eventBroker; m_World = world; m_EditorEnabled = editorMode; - + initOpenAL(); alSpeedOfSound(340.29f); // Speed of sound @@ -65,7 +65,7 @@ void SoundSystem::stopEmitters() void SoundSystem::Update() { - //deleteInactiveEmitters(); // Not tested + deleteInactiveEmitters(); addNewEmitters(); updateEmitters(); updateListener(); @@ -73,25 +73,39 @@ void SoundSystem::Update() void SoundSystem::deleteInactiveEmitters() { - auto emitterComponents = m_World->GetComponents("SoundEmitter"); - for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { - EntityID emitter = (*it).EntityID; - if (isPlaying(m_Sources[emitter]->ALsource)) { // The sound is still playing, do not remove + //auto emitterComponents = m_World->GetComponents("SoundEmitter"); + //for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { + // EntityID emitter = (*it).EntityID; + // if (isPlaying(m_Sources[emitter]->ALsource)) { // The sound is still playing, do not remove + // it++; + // continue; + // } else { + // alDeleteBuffers(1, &m_Sources[emitter]->ALsource); + // alDeleteSources(1, &m_Sources[emitter]->ALsource); + // //delete m_Sources[emitter]->SoundResource; + // m_Sources.erase(emitter); + // m_World->DeleteEntity(emitter); + // } + //} + + std::unordered_map::iterator it; + for (it = m_Sources.begin(); it != m_Sources.end();) { + if (m_World->ValidEntity((*it).first)) { + // Entity is valid, move on. it++; continue; - } - else { - alDeleteBuffers(1, &m_Sources[emitter]->ALsource); - alDeleteSources(1, &m_Sources[emitter]->ALsource); + } else { + stopSound((*it).second); + alDeleteBuffers(1, &m_Sources[(*it).first]->ALsource); + alDeleteSources(1, &m_Sources[(*it).first]->ALsource); //delete m_Sources[emitter]->SoundResource; - m_Sources.erase(emitter); - m_World->DeleteEntity(emitter); + it = m_Sources.erase(it); } } } void SoundSystem::addNewEmitters() -{ +{ auto emitterComponents = m_World->GetComponents("SoundEmitter"); for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { EntityID emitter = (*it).EntityID; @@ -99,7 +113,7 @@ void SoundSystem::addNewEmitters() i = m_Sources.find(emitter); if (i == m_Sources.end()) { // Did not exist, add it Source* source = createSource((std::string)(*it)["FilePath"]); - setSoundProperties ( + setSoundProperties( source->ALsource, (float)(double)(*it)["Gain"], (float)(double)(*it)["Pitch"], @@ -107,7 +121,7 @@ void SoundSystem::addNewEmitters() (float)(double)(*it)["MaxDistance"], (float)(double)(*it)["RollOffFactor"], (float)(double)(*it)["ReferenceDistance"] - ); + ); m_Sources[emitter] = source; } } @@ -116,37 +130,42 @@ void SoundSystem::addNewEmitters() void SoundSystem::updateEmitters() { auto emitterComponents = m_World->GetComponents("SoundEmitter"); - for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { + for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { EntityID emitter = (*it).EntityID; - std::unordered_map::iterator i; - i = m_Sources.find(emitter); - if (i != m_Sources.end()) { - // Get previous pos - glm::vec3 previousPos; - alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); - // Get next pos - glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); - // Calculate velocity - glm::vec3 velocity = nextPos - previousPos; - setSourcePos(m_Sources[emitter]->ALsource, nextPos); - setSourceVel(m_Sources[emitter]->ALsource, velocity); - setSoundProperties( - m_Sources[emitter]->ALsource, - (float)(double)(*it)["Gain"], - (float)(double)(*it)["Pitch"], - (bool)(*it)["Loop"], - (float)(double)(*it)["MaxDistance"], - (float)(double)(*it)["RollOffFactor"], - (float)(double)(*it)["ReferenceDistance"] - ); + if (!m_World->ValidEntity(emitter)) { // Entity has been deleted + // Delete + } else { + std::unordered_map::iterator i; + i = m_Sources.find(emitter); + if (i != m_Sources.end()) { + // Get previous pos + glm::vec3 previousPos; + alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); + // Get next pos + glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); + // Calculate velocity + glm::vec3 velocity = nextPos - previousPos; + setSourcePos(m_Sources[emitter]->ALsource, nextPos); + setSourceVel(m_Sources[emitter]->ALsource, velocity); + setSoundProperties( + m_Sources[emitter]->ALsource, + (float)(double)(*it)["Gain"], + (float)(double)(*it)["Pitch"], + (bool)(*it)["Loop"], + (float)(double)(*it)["MaxDistance"], + (float)(double)(*it)["RollOffFactor"], + (float)(double)(*it)["ReferenceDistance"] + ); - // To make an emitter play when spawned in editor mode - if (m_EditorEnabled) { - // Path changed - if (m_Sources[emitter]->SoundResource->Path() != (std::string)(*it)["FilePath"]) { - m_Sources[emitter]->SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); - playSound(m_Sources[emitter]); + // To make an emitter play when spawned in editor mode + if (m_EditorEnabled) { + // Path changed + if (m_Sources[emitter]->SoundResource->Path() != (std::string)(*it)["FilePath"]) { + m_Sources[emitter]->SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); + playSound(m_Sources[emitter]); + } } + it++; } } } @@ -187,7 +206,7 @@ void SoundSystem::playSound(Source* source) } void SoundSystem::stopSound(Source* source) -{ +{ alSourceStop(source->ALsource); } @@ -296,7 +315,7 @@ bool SoundSystem::isPlaying(ALuint source) } void SoundSystem::setGain(Source * source, float gain) -{ +{ alSourcef(source->ALsource, AL_GAIN, gain); } From c9f11f62d277f760b6747fce490e53d8de9cd9f7 Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 12 Jan 2016 15:17:06 +0100 Subject: [PATCH 08/48] Does not delete emitters that are paused and has not yet been played. --- include/Engine/Sound/SoundSystem.h | 3 +- src/Engine/Sound/SoundSystem.cpp | 49 ++++++++++++++++-------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 21968cd5..43d670d0 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -26,8 +26,9 @@ struct Source { Source() { } - Sound* SoundResource; + Sound* SoundResource = nullptr; ALuint ALsource; + bool HasBeenPlayed = false; }; class SoundSystem diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index ce93515f..b5c63d13 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -65,28 +65,30 @@ void SoundSystem::stopEmitters() void SoundSystem::Update() { - deleteInactiveEmitters(); addNewEmitters(); + deleteInactiveEmitters(); updateEmitters(); updateListener(); } void SoundSystem::deleteInactiveEmitters() { - //auto emitterComponents = m_World->GetComponents("SoundEmitter"); - //for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { - // EntityID emitter = (*it).EntityID; - // if (isPlaying(m_Sources[emitter]->ALsource)) { // The sound is still playing, do not remove - // it++; - // continue; - // } else { - // alDeleteBuffers(1, &m_Sources[emitter]->ALsource); - // alDeleteSources(1, &m_Sources[emitter]->ALsource); - // //delete m_Sources[emitter]->SoundResource; - // m_Sources.erase(emitter); - // m_World->DeleteEntity(emitter); - // } - //} + auto emitterComponents = m_World->GetComponents("SoundEmitter"); + for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { + EntityID emitter = (*it).EntityID; + Source* source = m_Sources[emitter]; + if (isPlaying(source->ALsource) || !source->HasBeenPlayed) { // The sound is still playing, do not remove + it++; + continue; + } + else { + alDeleteBuffers(1, &source->ALsource); + alDeleteSources(1, &source->ALsource); + //delete m_Sources[emitter]->SoundResource; + m_Sources.erase(emitter); + m_World->DeleteEntity(emitter); + } + } std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end();) { @@ -140,15 +142,15 @@ void SoundSystem::updateEmitters() if (i != m_Sources.end()) { // Get previous pos glm::vec3 previousPos; - alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); + alGetSource3f(i->second->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get next pos glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); // Calculate velocity glm::vec3 velocity = nextPos - previousPos; - setSourcePos(m_Sources[emitter]->ALsource, nextPos); - setSourceVel(m_Sources[emitter]->ALsource, velocity); + setSourcePos(i->second->ALsource, nextPos); + setSourceVel(i->second->ALsource, velocity); setSoundProperties( - m_Sources[emitter]->ALsource, + i->second->ALsource, (float)(double)(*it)["Gain"], (float)(double)(*it)["Pitch"], (bool)(*it)["Loop"], @@ -160,9 +162,11 @@ void SoundSystem::updateEmitters() // To make an emitter play when spawned in editor mode if (m_EditorEnabled) { // Path changed - if (m_Sources[emitter]->SoundResource->Path() != (std::string)(*it)["FilePath"]) { - m_Sources[emitter]->SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); - playSound(m_Sources[emitter]); + if (i->second->SoundResource->Path() != (std::string)(*it)["FilePath"]) { + i->second->SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); + if (i->second->SoundResource->Buffer() != 0) { + playSound(i->second); + } } } it++; @@ -203,6 +207,7 @@ void SoundSystem::playSound(Source* source) { alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer()); alSourcePlay(source->ALsource); + source->HasBeenPlayed = true; } void SoundSystem::stopSound(Source* source) From a58cc33ed0c72833e6478fbb27585ccaad14c438 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 13 Jan 2016 10:58:31 +0100 Subject: [PATCH 09/48] WIP, multithreaded loading based on exceptions, very slow probably. --- include/Engine/Core/ResourceManager.h | 95 +++++++++---- .../Schema/Entities/CollisionTestLevel.xml | 2 +- src/Engine/Core/ResourceManager.cpp | 129 ++++++++++++++---- src/Engine/Rendering/RenderQueueFactory.cpp | 3 +- src/Game/Game.cpp | 1 + 5 files changed, 178 insertions(+), 52 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index 86ed6254..5ba6ba5a 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -40,6 +40,30 @@ private: ResourceManager(); public: + //TODO: Check if this is ever used, and remove it if it isn't. + //Why would a resource load another resource async. in the ctor? + //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. + //Eg. If a Model loads a Texture asyncronously in the constructor, and it is not done yet. + struct StillLoadingException : public std::exception + { + virtual const char* what() const throw() + { + return "Resource is still loading."; + } + }; + //Should be thrown in a Resource's constructor if certain work needs to be handled by + //the main thread, and not a parallel worker thread. Eg. opengl commands. + struct WorkerCannotExecute : public std::exception + { + virtual const char* what() const throw() + { + return "Worker thread is unable to execute, main thread need to handle the code."; + } + }; + + static void AssertIsMainThread(); + static bool IsMainThread(); + /*static ResourceManager& Instance() { static ResourceManager s; @@ -49,15 +73,6 @@ public: template static void RegisterType(std::string typeName); - /** Preloads a resource and caches it for future use - - @tparam T Resource type. - @param resourceName Fully qualified name of the resource to preload. - */ - template - static void Preload(std::string resourceName); - static void Preload(std::string resourceType, std::string resourceName); - /** Checks if a resource is in cache @param resourceType Resource type as string. @@ -66,14 +81,25 @@ public: // TODO: Templateify static bool IsResourceLoaded(std::string resourceType, std::string resourceName); - /** Hot-loads a resource and caches it for future use + /** If the resource is not in cache, starts loading the resource and returns nullptr immediately. + If the resource has been loaded already, return a pointer to it. + + @tparam T Resource type. + @param resourceName Fully qualified name of the resource to load. + */ + template + static T* LoadAsync(std::string resourceName, Resource* parent = nullptr); + static Resource* LoadAsync(std::string resourceType, std::string resourceName, Resource* parent = nullptr); + + /** Hot-loads a resource and caches it for future use. + Fairly safe to assume that return value is a valid pointer, will only return nullptr on error. @tparam T Resource type. @param resourceName Fully qualified name of the resource to load. */ template static T* Load(std::string resourceName, Resource* parent = nullptr); - static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); + static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -87,19 +113,40 @@ public: static void Update(); private: + //Represents pointer values to signify that a resource haven't failed, but is not fully loaded. + class SpecialResourcePointer + { + public: + SpecialResourcePointer() + : m_Val(new Resource()) + {} + ~SpecialResourcePointer() + { + delete m_Val; + } + //Make this class implicitly convertible to the Resource*. + operator Resource* const() const { return m_Val; } + private: + Resource* const m_Val; + }; + //TODO: Check if this is ever used, and remove it if it isn't. + static SpecialResourcePointer m_StillLoading; + static SpecialResourcePointer m_LoadWithMainThread; + static std::unordered_map m_CompilerTypenameToResourceType; static std::unordered_map> m_FactoryFunctions; // type -> factory function static std::unordered_map, Resource*> m_ResourceCache; // (type, name) -> resource static std::unordered_map m_ResourceFromName; // name -> resource static std::unordered_map m_ResourceParents; // resource -> parent resource + static std::unordered_map, boost::thread> m_LoadingThreads; // (type, name) -> loading thread + static boost::recursive_mutex m_Mutex; + // TODO: Getters for IDs static unsigned int m_CurrentResourceTypeID; static std::unordered_map m_ResourceTypeIDs; // Number of resources of a type. Doubles as local ID. static std::unordered_map m_ResourceCount; - // Flag to suppress hot-load warnings when a preloading resource chain loads another resource - static bool m_Preloading; static FileWatcher m_FileWatcher; static void fileWatcherCallback(std::string path, FileWatcher::FileEventFlags flags); @@ -108,20 +155,20 @@ private: static unsigned int GetNewResourceID(unsigned int typeID); // Internal: Create a resource and cache it - static Resource* CreateResource(std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent); }; template T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) { - auto resourceTypename = typeid(T).name(); - auto it = m_CompilerTypenameToResourceType.find(resourceTypename); - if (it == m_CompilerTypenameToResourceType.end()) { - LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return nullptr; - } + auto resourceTypename = typeid(T).name(); + auto it = m_CompilerTypenameToResourceType.find(resourceTypename); + if (it == m_CompilerTypenameToResourceType.end()) { + LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); + return nullptr; + } - return static_cast(Load(it->second, resourceName, parent)); + return static_cast(Load(it->second, resourceName, parent)); } template @@ -132,16 +179,16 @@ void ResourceManager::RegisterType(std::string typeName) } template -void ResourceManager::Preload(std::string resourceName) +T* ResourceManager::LoadAsync(std::string resourceName, Resource* parent /* = nullptr */) { auto resourceTypename = typeid(T).name(); auto it = m_CompilerTypenameToResourceType.find(resourceTypename); if (it == m_CompilerTypenameToResourceType.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return; + return nullptr; } - Preload(it->second, resourceName); + return static_cast(LoadAsync(it->second, resourceName, parent)); } #endif diff --git a/resources/Schema/Entities/CollisionTestLevel.xml b/resources/Schema/Entities/CollisionTestLevel.xml index 99842b88..d5e932c2 100644 --- a/resources/Schema/Entities/CollisionTestLevel.xml +++ b/resources/Schema/Entities/CollisionTestLevel.xml @@ -17,7 +17,7 @@ - Models/ScaleWidget.obj + Models/Core/UnitSphere.obj diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index 62a60f14..32af073d 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -1,4 +1,7 @@ #include "Core/ResourceManager.h" +#include "boost/thread/thread.hpp" +#include "boost/thread/mutex.hpp" +#include "boost/thread/lock_guard.hpp" std::unordered_map ResourceManager::m_CompilerTypenameToResourceType; std::unordered_map> ResourceManager::m_FactoryFunctions; @@ -8,8 +11,11 @@ std::unordered_map ResourceManager::m_ResourceParents; unsigned int ResourceManager::m_CurrentResourceTypeID = 0; std::unordered_map ResourceManager::m_ResourceTypeIDs; std::unordered_map ResourceManager::m_ResourceCount; -bool ResourceManager::m_Preloading = false; FileWatcher ResourceManager::m_FileWatcher; +std::unordered_map, boost::thread> ResourceManager::m_LoadingThreads; +boost::recursive_mutex ResourceManager::m_Mutex; +ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading; +ResourceManager::SpecialResourcePointer ResourceManager::m_LoadWithMainThread; unsigned int ResourceManager::GetTypeID(std::string resourceType) { @@ -74,37 +80,90 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -void ResourceManager::Preload(std::string resourceType, std::string resourceName) +Resource* ResourceManager::LoadAsync(std::string resourceType, std::string resourceName, Resource* parent /*= nullptr*/) { - if (IsResourceLoaded(resourceType, resourceName)) { - //LOG_WARNING("Attempted to preload resource \"%s\" multiple times!", resourceName.c_str()); - return; - } + auto cacheKey = std::make_pair(resourceType, resourceName); + decltype(m_ResourceCache)::iterator it; + //If a thread has already been launched to load this resource. + auto tIt = m_LoadingThreads.find(cacheKey); + if (tIt != m_LoadingThreads.end()) { + //If the thread is still working. + if (tIt->second.joinable()) { + return nullptr; + } + //Else, the thread is done. + m_LoadingThreads.erase(tIt); + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + //If the thread is done, but it cannot complete the rest, main thread must complete construction. + if (it->second == m_LoadWithMainThread) { + AssertIsMainThread(); + return createResource(resourceType, resourceName, parent); + } + return it->second; + } else { + //If cache is still empty at cacheKey after thread finishes, it failed. + return nullptr; + } + } - m_Preloading = true; - LOG_INFO("Preloading resource \"%s\"", resourceName.c_str()); - CreateResource(resourceType, resourceName, nullptr); - m_Preloading = false; + //If resource has already been loaded and cached. + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + //if ConstructByMainThread, createResource from Main. + return it->second; + } + + //Create a thread that loads the resource into cache. + m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + return nullptr; } Resource* ResourceManager::Load(std::string resourceType, std::string resourceName, Resource* parent /*= nullptr*/) { - auto it = m_ResourceCache.find(std::make_pair(resourceType, resourceName)); - if (it != m_ResourceCache.end()) { - return it->second; - } + Resource* resource; + auto cacheKey = std::make_pair(resourceType, resourceName); + decltype(m_ResourceCache)::iterator it; + //If a thread has already been launched to load this resource. + auto tIt = m_LoadingThreads.find(cacheKey); + if (tIt != m_LoadingThreads.end()) { + //Wait for the thread to finish loading. + tIt->second.join(); + //Then delete the thread. + m_LoadingThreads.erase(tIt); + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + //If the thread is done, but it cannot complete the rest, main thread must complete construction. + if (it->second == m_LoadWithMainThread) { + AssertIsMainThread(); + return createResource(resourceType, resourceName, parent); + } + return it->second; + } else { + //If cache is still empty at cacheKey after thread finishes, it failed. + return nullptr; + } + } + //If resource has already been loaded and cached. + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + return it->second; + } - if (m_Preloading) { - LOG_INFO("Preloading resource \"%s\"", resourceName.c_str()); - } else { - LOG_WARNING("Hot-loading resource \"%s\"", resourceName.c_str()); - } - - return CreateResource(resourceType, resourceName, parent); + //If resource is not cached, load and return it. + resource = createResource(resourceType, resourceName, parent); + if (resource == m_LoadWithMainThread) { + //If we entered here then we know the caller is a worker thread. + //And we know the resource must be loaded from master thread. + throw(WorkerCannotExecute()); + } + return resource; } -Resource* ResourceManager::CreateResource(std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent) { + //Lock the mutex immediately, and unlock it when leaving the function. + boost::lock_guard guard(m_Mutex); auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str()); @@ -112,25 +171,43 @@ Resource* ResourceManager::CreateResource(std::string resourceType, std::string } // Call the factory function - Resource* resource; + Resource* resource = nullptr; try { resource = facIt->second(resourceName); + } catch (const WorkerCannotExecute& e) { + resource = m_LoadWithMainThread; + } catch (const std::exception& e) { + LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); + } + if (resource != nullptr && resource != m_LoadWithMainThread) { // Store IDs resource->TypeID = GetTypeID(resourceType); resource->ResourceID = GetNewResourceID(resource->TypeID); - } catch (const std::exception& e) { - resource = nullptr; - LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); } + // Cache m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; m_ResourceFromName[resourceName] = resource; if (parent != nullptr) { m_ResourceParents[resource] = parent; } + if (!boost::filesystem::is_directory(resourceName)) { LOG_DEBUG("Adding watch for %s", resourceName.c_str()); m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); } return resource; } + +bool ResourceManager::IsMainThread() +{ + static boost::thread::id MainThreadId = boost::this_thread::get_id(); + return boost::this_thread::get_id() == MainThreadId; +} + +void ResourceManager::AssertIsMainThread() +{ + if (!IsMainThread()) { + throw WorkerCannotExecute(); + } +} \ No newline at end of file diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 8d5bb420..27651a72 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -84,7 +84,8 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) continue; } glm::vec4 color = modelC["Color"]; - Model* model = ResourceManager::Load(resource); + //Model* model = ResourceManager::Load(resource); + Model* model = ResourceManager::LoadAsync(resource); if (model == nullptr) { model = ResourceManager::Load("Models/Core/Error.obj"); } diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 033db642..ee4c7cbf 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -4,6 +4,7 @@ Game::Game(int argc, char* argv[]) { + ResourceManager::AssertIsMainThread(); ResourceManager::RegisterType("ConfigFile"); ResourceManager::RegisterType("Model"); ResourceManager::RegisterType("Texture"); From 8164a955d6b24f387f244631abd1179923fa6ddf Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Wed, 13 Jan 2016 12:09:24 +0100 Subject: [PATCH 10/48] Renamed OctTree files to Octree (OCD to the max) --- include/Engine/Core/{OctTree.h => Octree.h} | 0 src/Engine/Core/{OctTree.cpp => Octree.cpp} | 2 +- src/Tests/CollisionTest.cpp | 2 +- src/Tests/OctTreeTest.cpp | 2 +- src/Tests/OctTreeTestGameMain.cpp | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename include/Engine/Core/{OctTree.h => Octree.h} (100%) rename src/Engine/Core/{OctTree.cpp => Octree.cpp} (99%) diff --git a/include/Engine/Core/OctTree.h b/include/Engine/Core/Octree.h similarity index 100% rename from include/Engine/Core/OctTree.h rename to include/Engine/Core/Octree.h diff --git a/src/Engine/Core/OctTree.cpp b/src/Engine/Core/Octree.cpp similarity index 99% rename from src/Engine/Core/OctTree.cpp rename to src/Engine/Core/Octree.cpp index c1d8c64e..f00a4a2a 100644 --- a/src/Engine/Core/OctTree.cpp +++ b/src/Engine/Core/Octree.cpp @@ -2,7 +2,7 @@ #include #include -#include "Core/OctTree.h" +#include "Core/Octree.h" #include "Collision/Collision.h" namespace diff --git a/src/Tests/CollisionTest.cpp b/src/Tests/CollisionTest.cpp index e6a29298..f83b4ee9 100644 --- a/src/Tests/CollisionTest.cpp +++ b/src/Tests/CollisionTest.cpp @@ -7,7 +7,7 @@ using boost::unit_test_framework::test_case; #include "Engine/Core/AABB.h" #include "Engine/Core/Ray.h" #include //srand -#include "Engine/Core/OctTree.h" +#include "Engine/Core/Octree.h" //vs model #include #include diff --git a/src/Tests/OctTreeTest.cpp b/src/Tests/OctTreeTest.cpp index 0206ce50..585bac6a 100644 --- a/src/Tests/OctTreeTest.cpp +++ b/src/Tests/OctTreeTest.cpp @@ -3,7 +3,7 @@ using boost::unit_test_framework::test_suite; using boost::unit_test_framework::test_case; #include //srand -#include "Engine/Core/OctTree.h" +#include "Engine/Core/Octree.h" #include "Engine/Core/Ray.h" #include "OldOctTree.h" diff --git a/src/Tests/OctTreeTestGameMain.cpp b/src/Tests/OctTreeTestGameMain.cpp index 43789cea..a248ac43 100644 --- a/src/Tests/OctTreeTestGameMain.cpp +++ b/src/Tests/OctTreeTestGameMain.cpp @@ -7,7 +7,7 @@ using boost::unit_test_framework::test_case; #include "Engine/Core/AABB.h" #include "Engine/Core/Ray.h" #include //srand -#include "Engine/Core/OctTree.h" +#include "Engine/Core/Octree.h" //vs memleaks //#define _CRTDBG_MAP_ALLOC From 311475cbb6d1a9b27c1bb0dccaa119b6e1599eaf Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Wed, 13 Jan 2016 12:15:36 +0100 Subject: [PATCH 11/48] Renamed all OctTree to Octree (More OCD) --- include/Engine/Core/Octree.h | 54 ++++++++++---------- src/Engine/Core/Octree.cpp | 60 +++++++++++------------ src/Tests/CollisionTest.cpp | 4 +- src/Tests/OctTreeTest.cpp | 10 ++-- src/Tests/OctTreeTestGameClass.h | 2 +- src/Tests/OctTreeTestHardCodedTestWorld.h | 8 +-- 6 files changed, 69 insertions(+), 69 deletions(-) diff --git a/include/Engine/Core/Octree.h b/include/Engine/Core/Octree.h index cdb21fbb..dc43343a 100644 --- a/include/Engine/Core/Octree.h +++ b/include/Engine/Core/Octree.h @@ -1,11 +1,11 @@ -#ifndef OctTree_h__ -#define OctTree_h__ +#ifndef Octree_h__ +#define Octree_h__ #include "Core/AABB.h" class Ray; -class OctTree +class Octree { public: struct Output @@ -13,16 +13,16 @@ public: float CollideDistance; }; - OctTree(); - ~OctTree(); - //For the root OctTree, [octTreeBounds] should be a box containing the entire level. - OctTree(const AABB& octTreeBounds, int subDivisions); + Octree(); + ~Octree(); + //For the root Octree, [octreeBounds] should be a box containing the entire level. + Octree(const AABB& octreeBounds, int subDivisions); - //We cannot copy the OctTree as of now, because of the recursive dynamic allocation. - //Define these if the OctTree suddenly needs to be copied, think of the children OctChild* ptrs. - OctTree(const OctTree& other) = delete; - OctTree(const OctTree&& other) = delete; - OctTree& operator= (const OctTree& other) = delete; + //We cannot copy the Octree as of now, because of the recursive dynamic allocation. + //Define these if the Octree suddenly needs to be copied, think of the children Child* ptrs. + Octree(const Octree& other) = delete; + Octree(const Octree&& other) = delete; + Octree& operator= (const Octree& other) = delete; //Add a dynamic object (one that moves around) into the tree. void AddDynamicObject(const AABB& box); //Add a static object (that does not move) into the tree. @@ -42,7 +42,7 @@ public: bool BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected); private: - struct OctChild; //Fwd declaration; + struct Child; //Fwd declaration; struct ContainedObject { ContainedObject() @@ -56,7 +56,7 @@ private: AABB Box; bool Checked; }; - OctChild* m_Root; + Child* m_Root; std::vector m_StaticObjects; std::vector m_DynamicObjects; @@ -67,16 +67,16 @@ private: void falsifyObjectChecks(); - struct OctChild + struct Child { - ~OctChild(); - OctChild(const AABB& octTreeBounds, + ~Child(); + Child(const AABB& octTreeBounds, int subDivisions, - std::vector& staticObjects, - std::vector& dynamicObjects); - OctChild(const OctChild& other) = delete; - OctChild(const OctChild&& other) = delete; - OctChild& operator= (const OctChild& other) = delete; + std::vector& staticObjects, + std::vector& dynamicObjects); + Child(const Child& other) = delete; + Child(const Child&& other) = delete; + Child& operator= (const Child& other) = delete; void AddDynamicObject(const AABB& box); void AddStaticObject(const AABB& box); void BoxesInSameRegion(const AABB& box, std::vector& outBoxes) const; @@ -85,14 +85,14 @@ private: bool RayCollides(const Ray& ray, Output& data) const; bool BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const; - OctChild* m_Children[8]; - //Indices into the lists in OctTree. + Child* m_Children[8]; + //Indices into the lists in Octree. std::vector m_StaticObjIndices; std::vector m_DynamicObjIndices; AABB m_Box; - //Reference to the lists in OctTree. - std::vector& m_StaticObjectsRef; - std::vector& m_DynamicObjectsRef; + //Reference to the lists in Octree. + std::vector& m_StaticObjectsRef; + std::vector& m_DynamicObjectsRef; inline bool hasChildren() const; int childIndexContainingPoint(const glm::vec3& point) const; diff --git a/src/Engine/Core/Octree.cpp b/src/Engine/Core/Octree.cpp index f00a4a2a..dca30cd7 100644 --- a/src/Engine/Core/Octree.cpp +++ b/src/Engine/Core/Octree.cpp @@ -21,65 +21,65 @@ bool isFirstLower(const ChildInfo& first, const ChildInfo& second) } -OctTree::OctTree() - : OctTree(AABB(), 0) +Octree::Octree() + : Octree(AABB(), 0) {} -OctTree::OctTree(const AABB& octTreeBounds, int subDivisions) - : m_Root(new OctChild(octTreeBounds, subDivisions, m_StaticObjects, m_DynamicObjects)) +Octree::Octree(const AABB& octTreeBounds, int subDivisions) + : m_Root(new Child(octTreeBounds, subDivisions, m_StaticObjects, m_DynamicObjects)) , m_UpdatedOnce(false) {} -OctTree::~OctTree() +Octree::~Octree() { delete m_Root; } -void OctTree::AddDynamicObject(const AABB& box) +void Octree::AddDynamicObject(const AABB& box) { m_Root->AddDynamicObject(box); m_DynamicObjects.push_back(box); } -void OctTree::AddStaticObject(const AABB& box) +void Octree::AddStaticObject(const AABB& box) { m_Root->AddStaticObject(box); m_StaticObjects.push_back(box); } -void OctTree::BoxesInSameRegion(const AABB& box, std::vector& outBoxes) +void Octree::BoxesInSameRegion(const AABB& box, std::vector& outBoxes) { falsifyObjectChecks(); m_Root->BoxesInSameRegion(box, outBoxes); } -void OctTree::ClearObjects() +void Octree::ClearObjects() { m_StaticObjects.clear(); m_DynamicObjects.clear(); m_Root->ClearObjects(); } -void OctTree::ClearDynamicObjects() +void Octree::ClearDynamicObjects() { m_DynamicObjects.clear(); m_Root->ClearDynamicObjects(); } -bool OctTree::RayCollides(const Ray& ray, Output& data) +bool Octree::RayCollides(const Ray& ray, Output& data) { falsifyObjectChecks(); data.CollideDistance = -1; return m_Root->RayCollides(ray, data); } -bool OctTree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) +bool Octree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) { falsifyObjectChecks(); return m_Root->BoxCollides(boxToTest, outBoxIntersected); } -void OctTree::falsifyObjectChecks() +void Octree::falsifyObjectChecks() { for (auto& obj : m_StaticObjects) { obj.Checked = false; @@ -89,7 +89,7 @@ void OctTree::falsifyObjectChecks() } } -OctTree::OctChild::OctChild(const AABB& octTreeBounds, +Octree::Child::Child(const AABB& octTreeBounds, int subDivisions, std::vector& staticObjects, std::vector& dynamicObjects) @@ -98,7 +98,7 @@ OctTree::OctChild::OctChild(const AABB& octTreeBounds, , m_DynamicObjectsRef(dynamicObjects) { if (subDivisions == 0) { - for (OctChild*& c : m_Children) { + for (Child*& c : m_Children) { c = nullptr; } } else { @@ -134,14 +134,14 @@ OctTree::OctChild::OctChild(const AABB& octTreeBounds, minPos.z = parentMin.z; maxPos.z = parentCenter.z; } - m_Children[i] = new OctChild(AABB(minPos, maxPos), subDivisions, m_StaticObjectsRef, m_DynamicObjectsRef); + m_Children[i] = new Child(AABB(minPos, maxPos), subDivisions, m_StaticObjectsRef, m_DynamicObjectsRef); } } } -OctTree::OctChild::~OctChild() +Octree::Child::~Child() { - for (OctChild*& c : m_Children) { + for (Child*& c : m_Children) { if (c != nullptr) { delete c; c = nullptr; @@ -149,7 +149,7 @@ OctTree::OctChild::~OctChild() } } -bool OctTree::OctChild::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const +bool Octree::Child::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const { if (hasChildren()) { for (int i : childIndicesContainingBox(boxToTest)) { @@ -182,7 +182,7 @@ bool OctTree::OctChild::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersect return false; } -bool OctTree::OctChild::RayCollides(const Ray& ray, Output& data) const +bool Octree::Child::RayCollides(const Ray& ray, Output& data) const { //If the node AABB is missed, everything it contains is missed. if (Collision::RayAABBIntr(ray, m_Box)) { @@ -234,7 +234,7 @@ bool OctTree::OctChild::RayCollides(const Ray& ray, Output& data) const } -void OctTree::OctChild::AddDynamicObject(const AABB& box) +void Octree::Child::AddDynamicObject(const AABB& box) { if (hasChildren()) { for (auto i : childIndicesContainingBox(box)) { @@ -246,7 +246,7 @@ void OctTree::OctChild::AddDynamicObject(const AABB& box) } } -void OctTree::OctChild::AddStaticObject(const AABB& box) +void Octree::Child::AddStaticObject(const AABB& box) { if (hasChildren()) { for (auto i : childIndicesContainingBox(box)) { @@ -258,7 +258,7 @@ void OctTree::OctChild::AddStaticObject(const AABB& box) } } -void OctTree::OctChild::BoxesInSameRegion(const AABB& box, std::vector& outBoxes) const +void Octree::Child::BoxesInSameRegion(const AABB& box, std::vector& outBoxes) const { if (hasChildren()) { for (auto i : childIndicesContainingBox(box)) { @@ -292,10 +292,10 @@ void OctTree::OctChild::BoxesInSameRegion(const AABB& box, std::vector& ou } } -void OctTree::OctChild::ClearObjects() +void Octree::Child::ClearObjects() { if (hasChildren()) { - for (OctChild*& c : m_Children) { + for (Child*& c : m_Children) { c->ClearObjects(); } } else { @@ -304,10 +304,10 @@ void OctTree::OctChild::ClearObjects() } } -void OctTree::OctChild::ClearDynamicObjects() +void Octree::Child::ClearDynamicObjects() { if (hasChildren()) { - for (OctChild*& c : m_Children) { + for (Child*& c : m_Children) { c->ClearObjects(); } } else { @@ -327,13 +327,13 @@ void OctTree::OctChild::ClearDynamicObjects() // x : - - - - + + + + // y : - - + + - - + + // z : - + - + - + - + -int OctTree::OctChild::childIndexContainingPoint(const glm::vec3& point) const +int Octree::Child::childIndexContainingPoint(const glm::vec3& point) const { const glm::vec3& c = m_Box.Center(); return (1 << 2) * (point.x >= c.x) | (1 << 1) * (point.y >= c.y) | (point.z >= c.z); } -std::vector OctTree::OctChild::childIndicesContainingBox(const AABB& box) const +std::vector Octree::Child::childIndicesContainingBox(const AABB& box) const { int minInd = childIndexContainingPoint(box.MinCorner()); int maxInd = childIndexContainingPoint(box.MaxCorner()); @@ -371,7 +371,7 @@ std::vector OctTree::OctChild::childIndicesContainingBox(const AABB& box) c } } -inline bool OctTree::OctChild::hasChildren() const +inline bool Octree::Child::hasChildren() const { return m_Children[0] != nullptr; } \ No newline at end of file diff --git a/src/Tests/CollisionTest.cpp b/src/Tests/CollisionTest.cpp index f83b4ee9..9f400330 100644 --- a/src/Tests/CollisionTest.cpp +++ b/src/Tests/CollisionTest.cpp @@ -205,9 +205,9 @@ BOOST_AUTO_TEST_CASE(octTest) { glm::vec3 mini = glm::vec3(-1, -1, -1); glm::vec3 maxi = glm::vec3(1, 1, 1); - OctTree tree(AABB(mini, maxi), 2); + Octree tree(AABB(mini, maxi), 2); tree.AddDynamicObject(AABB(mini, -0.9f*maxi)); - OctTree::Output data; + Octree::Output data; glm::vec3 origin = 3.0f * mini; bool rayIntersected = tree.RayCollides(Ray(origin , mini - origin), data); BOOST_CHECK(rayIntersected); diff --git a/src/Tests/OctTreeTest.cpp b/src/Tests/OctTreeTest.cpp index 585bac6a..f437bde0 100644 --- a/src/Tests/OctTreeTest.cpp +++ b/src/Tests/OctTreeTest.cpp @@ -13,7 +13,7 @@ BOOST_AUTO_TEST_CASE(octSameRegionTest) { glm::vec3 mini = glm::vec3(-1, -1, -1); glm::vec3 maxi = glm::vec3(1, 1, 1); - OctTree tree(AABB(mini, maxi), 2); + Octree tree(AABB(mini, maxi), 2); AABB firstQuadrant(mini, 0.8f*mini); tree.AddStaticObject(firstQuadrant); AABB testBox(0.9f*mini, 0.8f*mini); @@ -117,7 +117,7 @@ BOOST_AUTO_TEST_CASE(octRegionPerfTestWithDuplicates) BOOST_AUTO_TEST_CASE(octRegionPerfTestNoDuplicates) { - TestLoop(RegionTest); + TestLoop(RegionTest); BOOST_CHECK(true); } @@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(octBoxPerfTestWithDuplicates) BOOST_AUTO_TEST_CASE(octBoxPerfTestNoDuplicates) { - TestLoop(BoxTest); + TestLoop(BoxTest); BOOST_CHECK(true); } @@ -141,7 +141,7 @@ BOOST_AUTO_TEST_CASE(octRayPerfTestWithDuplicates) BOOST_AUTO_TEST_CASE(octRayPerfTestNoDuplicates) { - TestLoop(RayTest); + TestLoop(RayTest); BOOST_CHECK(true); } @@ -153,7 +153,7 @@ BOOST_AUTO_TEST_CASE(octNopPerfTestWithDuplicates) BOOST_AUTO_TEST_CASE(octNopPerfTestNoDuplicates) { - TestLoop(NopTest); + TestLoop(NopTest); BOOST_CHECK(true); } diff --git a/src/Tests/OctTreeTestGameClass.h b/src/Tests/OctTreeTestGameClass.h index 6dc9404e..4cce489f 100644 --- a/src/Tests/OctTreeTestGameClass.h +++ b/src/Tests/OctTreeTestGameClass.h @@ -55,7 +55,7 @@ private: glm::quat m_PrevOri; glm::vec3 worldSize = glm::vec3(50, 50, 50); - OctTree someOctTree; + Octree someOctTree; }; diff --git a/src/Tests/OctTreeTestHardCodedTestWorld.h b/src/Tests/OctTreeTestHardCodedTestWorld.h index 512716df..7fcae0b2 100644 --- a/src/Tests/OctTreeTestHardCodedTestWorld.h +++ b/src/Tests/OctTreeTestHardCodedTestWorld.h @@ -16,9 +16,9 @@ class HardcodedTestWorld : public World public: struct LinkOctTreeAndModel { EntityID entId; - OctTree::OctChild* child; + Octree::Child* child; glm::vec3 posxyz; - LinkOctTreeAndModel(EntityID eId, OctTree::OctChild* ch, glm::vec3 pos) + LinkOctTreeAndModel(EntityID eId, Octree::Child* ch, glm::vec3 pos) { entId = eId; child = ch; @@ -27,7 +27,7 @@ public: }; EntityID anotherBoxTransformId; std::vector linkOM; - OctTree someOctTree; + Octree someOctTree; //constructor HardcodedTestWorld() @@ -115,7 +115,7 @@ private: model["Resource"] = "Models/Core/UnitBox.obj"; } - void AddBoxModel(const glm::vec3 ¢er, const float &halfSize, OctTree::OctChild* child, EntityID &outEntityId) { + void AddBoxModel(const glm::vec3 ¢er, const float &halfSize, Octree::Child* child, EntityID &outEntityId) { World& world = *this; EntityID entityDummyScene = world.CreateEntity(); From 8ed4ba121930e777ed713d6beb05e73a726e097f Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Wed, 13 Jan 2016 12:16:35 +0100 Subject: [PATCH 12/48] Added missing include for Octree --- include/Engine/Core/Octree.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/Engine/Core/Octree.h b/include/Engine/Core/Octree.h index dc43343a..a2f358ae 100644 --- a/include/Engine/Core/Octree.h +++ b/include/Engine/Core/Octree.h @@ -1,6 +1,7 @@ #ifndef Octree_h__ #define Octree_h__ +#include "../Common.h" #include "Core/AABB.h" class Ray; From d6ef527f25ba86af726ae08944d504b8eb6c6a7a Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Wed, 13 Jan 2016 15:34:43 +0100 Subject: [PATCH 13/48] Basic collisions using Octree. Seems buggy over octree boundaries. --- .../Engine/Collision/CollidableOctreeSystem.h | 19 +++ include/Engine/Collision/Collision.h | 15 +- include/Engine/Collision/CollisionSystem.h | 17 ++- include/Engine/Collision/TriggerSystem.h | 14 +- include/Engine/Core/AABB.h | 6 +- include/Engine/Core/Octree.h | 4 +- include/Engine/Core/System.h | 16 +- include/Engine/Core/SystemPipeline.h | 14 +- include/Game/Game.h | 3 + include/Game/PlayerSystem.h | 3 +- include/Game/RaptorCopterSystem.h | 3 +- resources/Schema/Components.xsd | 1 + resources/Schema/Components/AABB.xml | 4 +- resources/Schema/Components/AABB.xsd | 8 +- resources/Schema/Components/Collidable.xml | 3 + resources/Schema/Components/Collidable.xsd | 13 ++ .../Schema/Entities/CollisionTestLevel.xml | 140 +++--------------- resources/Schema/Entities/Empty.xml | 8 +- resources/Schema/Entities/OctreeTest.xml | 64 ++++++++ resources/Schema/Types/Entity.xsd | 3 + .../Collision/CollidableOctreeSystem.cpp | 20 +++ src/Engine/Collision/Collision.cpp | 52 ++----- src/Engine/Collision/CollisionSystem.cpp | 31 ++-- src/Engine/Collision/TriggerSystem.cpp | 21 ++- src/Engine/Core/AABB.cpp | 13 +- src/Engine/Core/Octree.cpp | 12 +- src/Engine/Editor/EditorSystem.cpp | 3 +- src/Game/Game.cpp | 20 ++- src/Game/HealthSystem.cpp | 3 +- src/Tests/OctTreeTest.cpp | 6 +- src/Tests/OctTreeTestGameClass.cpp | 4 +- src/Tests/OctTreeTestHardCodedTestWorld.h | 8 +- src/Tests/OldOctTree.cpp | 6 +- 33 files changed, 290 insertions(+), 267 deletions(-) create mode 100644 include/Engine/Collision/CollidableOctreeSystem.h create mode 100644 resources/Schema/Components/Collidable.xml create mode 100644 resources/Schema/Components/Collidable.xsd create mode 100644 resources/Schema/Entities/OctreeTest.xml create mode 100644 src/Engine/Collision/CollidableOctreeSystem.cpp diff --git a/include/Engine/Collision/CollidableOctreeSystem.h b/include/Engine/Collision/CollidableOctreeSystem.h new file mode 100644 index 00000000..b7a77346 --- /dev/null +++ b/include/Engine/Collision/CollidableOctreeSystem.h @@ -0,0 +1,19 @@ +#include "../Core/System.h" +#include "../Core/Octree.h" +#include "Collision.h" + +class CollidableOctreeSystem : public ImpureSystem, public PureSystem +{ +public: + CollidableOctreeSystem(EventBroker* eventBroker, Octree* octree) + : System(eventBroker) + , PureSystem("Collidable") + , m_Octree(octree) + { } + + virtual void Update(World* world, double dt) override; + virtual void UpdateComponent(World* world, ComponentWrapper& component, double dt) override; + +private: + Octree* m_Octree; +}; \ No newline at end of file diff --git a/include/Engine/Collision/Collision.h b/include/Engine/Collision/Collision.h index 714cee3f..18520243 100644 --- a/include/Engine/Collision/Collision.h +++ b/include/Engine/Collision/Collision.h @@ -6,11 +6,13 @@ //or you will get "fatal error C1189: #error: gl.h included before glew.h" #include +#include -#include "Core/Ray.h" -#include "Core/AABB.h" -#include "Engine/Rendering/RawModel.h" -#include "Core/Entity.h" +#include "../Core/Ray.h" +#include "../Core/AABB.h" +#include "../Rendering/RawModel.h" +#include "../Rendering/RenderQueueFactory.h" +#include "../Core/Entity.h" class World; struct ComponentWrapper; @@ -50,9 +52,8 @@ bool AABBVsAABB(const AABB& a, const AABB& b); bool AABBVsAABB(const AABB& a, const AABB& b, glm::vec3& minimumTranslation); bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon = 0.0001f); -//Returns true if the entity has a boundingbox. Outputs the aabb in [outBox]. -bool GetEntityBox(World* world, EntityID entity, AABB& outBox, bool forceBoxFromModel = false); -bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox); +// Calculates an absolute AABB from an entity AABB component +boost::optional EntityAbsoluteAABB(World* world, EntityID entity); } diff --git a/include/Engine/Collision/CollisionSystem.h b/include/Engine/Collision/CollisionSystem.h index 254a2461..b4e25072 100644 --- a/include/Engine/Collision/CollisionSystem.h +++ b/include/Engine/Collision/CollisionSystem.h @@ -4,16 +4,19 @@ #include #include -#include "Common.h" -#include "Core/System.h" -#include "Core/EventBroker.h" -#include "Core/EKeyUp.h" +#include "../Common.h" +#include "../Core/System.h" +#include "../Core/EventBroker.h" +#include "../Core/EKeyUp.h" +#include "../Core/Octree.h" class CollisionSystem : public PureSystem { public: - CollisionSystem(EventBroker* eventBroker) - : PureSystem(eventBroker, "AABB") + CollisionSystem(EventBroker* eventBroker, Octree* octree) + : System(eventBroker) + , PureSystem("AABB") + , m_Octree(octree) , zPress(false) { //TODO: Debug stuff, remove later. @@ -23,7 +26,9 @@ public: virtual void UpdateComponent(World* world, ComponentWrapper& cAABB, double dt) override; private: + Octree* m_Octree; bool zPress; + EventRelay m_EKeyUp; bool OnKeyUp(const Events::KeyUp &event); }; diff --git a/include/Engine/Collision/TriggerSystem.h b/include/Engine/Collision/TriggerSystem.h index 7e6ef008..7856a09d 100644 --- a/include/Engine/Collision/TriggerSystem.h +++ b/include/Engine/Collision/TriggerSystem.h @@ -4,8 +4,9 @@ #include #include -#include "Core/System.h" -#include "Core/EventBroker.h" +#include "../Core/System.h" +#include "../Core/EventBroker.h" +#include "../Core/Octree.h" #include "ETrigger.h" class AABB; @@ -13,13 +14,16 @@ class AABB; class TriggerSystem : public PureSystem { public: - TriggerSystem(EventBroker* eventBroker) - : PureSystem(eventBroker, "Trigger") - {} + TriggerSystem(EventBroker* eventBroker, Octree* octree) + : System(eventBroker) + , PureSystem("Trigger") + , m_Octree(octree) + { } virtual void UpdateComponent(World* world, ComponentWrapper& collision, double dt) override; private: + Octree* m_Octree; std::unordered_map> m_EntitiesTouchingTrigger; std::unordered_map> m_EntitiesCompletelyInTrigger; diff --git a/include/Engine/Core/AABB.h b/include/Engine/Core/AABB.h index c8e05248..5b9c6485 100644 --- a/include/Engine/Core/AABB.h +++ b/include/Engine/Core/AABB.h @@ -11,18 +11,18 @@ public: AABB(const glm::vec3& minPos, const glm::vec3& maxPos); AABB(const glm::vec4& minPos, const glm::vec4& maxPos); //No checks are made. Size must consist of non-negative numbers. - virtual void CreateFromCenter(const glm::vec3& center, const glm::vec3& size); + static AABB FromOriginSize(const glm::vec3& origin, const glm::vec3& size); virtual ~AABB(); const glm::vec3& MinCorner() const { return m_MinCorner; } const glm::vec3& MaxCorner() const { return m_MaxCorner; } - const glm::vec3& Center() const { return m_Center; } + const glm::vec3& Origin() const { return m_Origin; } const glm::vec3 Size() const { return 2.0f * m_HalfSize; } const glm::vec3& HalfSize() const { return m_HalfSize; } private: glm::vec3 m_MinCorner; glm::vec3 m_MaxCorner; - glm::vec3 m_Center; + glm::vec3 m_Origin; glm::vec3 m_HalfSize; }; diff --git a/include/Engine/Core/Octree.h b/include/Engine/Core/Octree.h index a2f358ae..954dbcbc 100644 --- a/include/Engine/Core/Octree.h +++ b/include/Engine/Core/Octree.h @@ -2,7 +2,7 @@ #define Octree_h__ #include "../Common.h" -#include "Core/AABB.h" +#include "AABB.h" class Ray; @@ -14,7 +14,7 @@ public: float CollideDistance; }; - Octree(); + Octree() = delete; ~Octree(); //For the root Octree, [octreeBounds] should be a box containing the entire level. Octree(const AABB& octreeBounds, int subDivisions); diff --git a/include/Engine/Core/System.h b/include/Engine/Core/System.h index 75bd3882..cd8e9fc7 100644 --- a/include/Engine/Core/System.h +++ b/include/Engine/Core/System.h @@ -10,6 +10,9 @@ class System friend class SystemPipeline; protected: + System() + : m_EventBroker(nullptr) + { } System(EventBroker* eventBroker) : m_EventBroker(eventBroker) { } @@ -18,14 +21,13 @@ protected: EventBroker* m_EventBroker; }; -class PureSystem : public System +class PureSystem : public virtual System { friend class SystemPipeline; protected: - PureSystem(EventBroker* eventBroker, std::string componentType) - : System(eventBroker) - , m_ComponentType(componentType) + PureSystem(std::string componentType) + : m_ComponentType(componentType) { } virtual ~PureSystem() = default; @@ -34,14 +36,12 @@ protected: virtual void UpdateComponent(World* world, ComponentWrapper& component, double dt) = 0; }; -class ImpureSystem : public System +class ImpureSystem : public virtual System { friend class SystemPipeline; protected: - ImpureSystem(EventBroker* eventBroker) - : System(eventBroker) - { } + ImpureSystem() = default; virtual ~ImpureSystem() = default; virtual void Update(World* world, double dt) = 0; diff --git a/include/Engine/Core/SystemPipeline.h b/include/Engine/Core/SystemPipeline.h index d6a6b371..79d4ace4 100644 --- a/include/Engine/Core/SystemPipeline.h +++ b/include/Engine/Core/SystemPipeline.h @@ -32,8 +32,8 @@ public: System* system = new T(m_EventBroker, args...); group.Systems[typeid(T).name()] = system; - if (std::is_base_of::value) { - PureSystem* pureSystem = static_cast(system); + PureSystem* pureSystem = dynamic_cast(system); + if (pureSystem != nullptr) { if (!pureSystem->m_ComponentType.empty()) { group.PureSystems[pureSystem->m_ComponentType].push_back(pureSystem); } else { @@ -41,8 +41,8 @@ public: } } - if (std::is_base_of::value) { - ImpureSystem* impureSystem = static_cast(system); + ImpureSystem* impureSystem = dynamic_cast(system); + if (impureSystem != nullptr) { group.ImpureSystems.push_back(impureSystem); } } @@ -56,6 +56,9 @@ public: } // Update + for (auto& system : group.ImpureSystems) { + system->Update(world, dt); + } for (auto& pair : group.PureSystems) { const std::string& componentName = pair.first; auto& systems = pair.second; @@ -69,9 +72,6 @@ public: } } } - for (auto& system : group.ImpureSystems) { - system->Update(world, dt); - } } } diff --git a/include/Game/Game.h b/include/Game/Game.h index 1c19aac2..de45c558 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -20,6 +20,7 @@ #include "Editor/EditorSystem.h" #include "Core/EntityFile.h" #include "Core/EntityFileParser.h" +#include "Core/Octree.h" // Network #include @@ -46,6 +47,8 @@ private: InputProxy* m_InputProxy; GUI::Frame* m_FrameStack; World* m_World; + Octree* m_OctreeCollision; + Octree* m_OctreeFrustrumCulling; SystemPipeline* m_SystemPipeline; RenderQueueFactory* m_RenderQueueFactory; // Network variables diff --git a/include/Game/PlayerSystem.h b/include/Game/PlayerSystem.h index 577fbbb0..50492617 100644 --- a/include/Game/PlayerSystem.h +++ b/include/Game/PlayerSystem.h @@ -12,7 +12,8 @@ class PlayerSystem : public PureSystem { public: PlayerSystem(EventBroker* eventBroker) - : PureSystem(eventBroker, "Player") + : System(eventBroker) + , PureSystem("Player") { EVENT_SUBSCRIBE_MEMBER(m_ETouch, &PlayerSystem::OnTouch); EVENT_SUBSCRIBE_MEMBER(m_EEnter, &PlayerSystem::OnEnter); diff --git a/include/Game/RaptorCopterSystem.h b/include/Game/RaptorCopterSystem.h index 913efdb3..14b49f4d 100644 --- a/include/Game/RaptorCopterSystem.h +++ b/include/Game/RaptorCopterSystem.h @@ -5,7 +5,8 @@ class RaptorCopterSystem : public PureSystem { public: RaptorCopterSystem(EventBroker* eventBroker) - : PureSystem(eventBroker, "RaptorCopter") + : System(eventBroker) + , PureSystem("RaptorCopter") { } virtual void UpdateComponent(World* world, ComponentWrapper& raptorCopter, double dt) override diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 7fcdd565..4f32b477 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -9,4 +9,5 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/AABB.xml b/resources/Schema/Components/AABB.xml index 341d1d0d..6996b3dc 100644 --- a/resources/Schema/Components/AABB.xml +++ b/resources/Schema/Components/AABB.xml @@ -1,4 +1,4 @@ - - + + \ No newline at end of file diff --git a/resources/Schema/Components/AABB.xsd b/resources/Schema/Components/AABB.xsd index 8fac860e..daa633a6 100644 --- a/resources/Schema/Components/AABB.xsd +++ b/resources/Schema/Components/AABB.xsd @@ -6,8 +6,12 @@ - - + + Middle point of the bounding box + + + Size of the bounding box + diff --git a/resources/Schema/Components/Collidable.xml b/resources/Schema/Components/Collidable.xml new file mode 100644 index 00000000..0d1bb9b3 --- /dev/null +++ b/resources/Schema/Components/Collidable.xml @@ -0,0 +1,3 @@ + + true + \ No newline at end of file diff --git a/resources/Schema/Components/Collidable.xsd b/resources/Schema/Components/Collidable.xsd new file mode 100644 index 00000000..f835d9a6 --- /dev/null +++ b/resources/Schema/Components/Collidable.xsd @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/CollisionTestLevel.xml b/resources/Schema/Entities/CollisionTestLevel.xml index 99842b88..52420d14 100644 --- a/resources/Schema/Entities/CollisionTestLevel.xml +++ b/resources/Schema/Entities/CollisionTestLevel.xml @@ -1,122 +1,20 @@ - + + - - - - - - - Models/DummyScene.obj - - - - - - - - - - - Models/ScaleWidget.obj - - - - - - - - - - - Models/RotationWidgetX.obj - - - - - - - - - - - - - - - Models/Core/UnitCube.obj - - - - - - - - - - - - - - - - - - - - - - Models/Core/UnitRaptor.obj - - - - - - - - - - - - 20 - - - - - - - - - - - - - Models/Core/UnitCube.obj - - - - - - - - - - - - - Models/Core/UnitCube.obj - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + Models/Test/ObstacleCourse.obj + + + + + + + + diff --git a/resources/Schema/Entities/Empty.xml b/resources/Schema/Entities/Empty.xml index 70581847..6efd8318 100644 --- a/resources/Schema/Entities/Empty.xml +++ b/resources/Schema/Entities/Empty.xml @@ -1,6 +1,10 @@ - + + - \ No newline at end of file + + + + diff --git a/resources/Schema/Entities/OctreeTest.xml b/resources/Schema/Entities/OctreeTest.xml new file mode 100644 index 00000000..819159d0 --- /dev/null +++ b/resources/Schema/Entities/OctreeTest.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 6562f3ed..8790d8a9 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -15,7 +15,10 @@ + + + diff --git a/src/Engine/Collision/CollidableOctreeSystem.cpp b/src/Engine/Collision/CollidableOctreeSystem.cpp new file mode 100644 index 00000000..c7fed0a0 --- /dev/null +++ b/src/Engine/Collision/CollidableOctreeSystem.cpp @@ -0,0 +1,20 @@ +#include "Collision/CollidableOctreeSystem.h" + +void CollidableOctreeSystem::Update(World* world, double dt) +{ + m_Octree->ClearDynamicObjects(); +} + +void CollidableOctreeSystem::UpdateComponent(World* world, ComponentWrapper& cCollidable, double dt) +{ + EntityID entity = cCollidable.EntityID; + + if (world->HasComponent(entity, "AABB")) { + boost::optional absoluteAABB = Collision::EntityAbsoluteAABB(world, entity); + if (absoluteAABB) { + m_Octree->AddDynamicObject(*absoluteAABB); + } + } else if (world->HasComponent(entity, "Model")) { + // TODO: Derive AABB from model + } +} \ No newline at end of file diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index 620e9639..1af09382 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -13,7 +13,7 @@ bool RayAABBIntr(const Ray& ray, const AABB& box) { glm::vec3 w = 75.0f * ray.Direction(); glm::vec3 v = glm::abs(w); - glm::vec3 c = ray.Origin() - box.Center() + w; + glm::vec3 c = ray.Origin() - box.Origin() + w; glm::vec3 half = box.HalfSize(); if (abs(c.x) > v.x + half.x) { @@ -68,8 +68,8 @@ bool RayVsAABB(const Ray& ray, const AABB& box, float& outDistance) bool AABBVsAABB(const AABB& a, const AABB& b) { - const glm::vec3& aCenter = a.Center(); - const glm::vec3& bCenter = b.Center(); + const glm::vec3& aCenter = a.Origin(); + const glm::vec3& bCenter = b.Origin(); const glm::vec3& aHSize = a.HalfSize(); const glm::vec3& bHSize = b.HalfSize(); //Test will probably exit because of the X and Z axes more often, so test them first. @@ -202,8 +202,8 @@ bool RayVsModel(const Ray& ray, bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon) { const glm::vec3& ma1 = first.MaxCorner(); - const glm::vec3& ma2 = first.MaxCorner(); - const glm::vec3& mi1 = second.MinCorner(); + const glm::vec3& ma2 = second.MaxCorner(); + const glm::vec3& mi1 = first.MinCorner(); const glm::vec3& mi2 = second.MinCorner(); return (std::abs(ma1.x - ma2.x) < epsilon) && (std::abs(mi1.x - mi2.x) < epsilon) && @@ -238,45 +238,23 @@ bool attachAABBComponentFromModel(World* world, EntityID id) mini.y = std::min(wPos.y, mini.y); mini.z = std::min(wPos.z, mini.z); } - collision["BoxCenter"] = 0.5f * (maxi + mini); - collision["BoxSize"] = maxi - mini; + collision["Origin"] = 0.5f * (maxi + mini); + collision["Size"] = maxi - mini; return true; } -bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) -{ - ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform"); - ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model"); - Model* modelRes = ResourceManager::Load(model["Resource"]); - outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]); - glm::vec3 mini = outBox.MinCorner(); - glm::vec3 maxi = outBox.MaxCorner(); - - if (modelRes == nullptr) { - return false; - } - glm::mat4 modelMatrix = modelRes->m_Matrix * - glm::translate(glm::mat4(), (glm::vec3)cTrans["Position"]) * - glm::scale((glm::vec3)cTrans["Scale"]); - - outBox = AABB(modelMatrix * glm::vec4(mini.x, mini.y, mini.z, 1), - modelMatrix * glm::vec4(maxi.x, maxi.y, maxi.z, 1)); - return true; -} - -bool GetEntityBox(World* world, EntityID entity, AABB& outBox, bool forceBoxFromModel) +boost::optional EntityAbsoluteAABB(World* world, EntityID entity) { if (!world->HasComponent(entity, "AABB")) { - if (forceBoxFromModel) { - if (!attachAABBComponentFromModel(world, entity)) - return false; - } else { - return false; - } + return boost::none; } - ComponentWrapper& cBox = world->GetComponent(entity, "AABB"); - return GetEntityBox(world, cBox, outBox); + ComponentWrapper& cAABB = world->GetComponent(entity, "AABB"); + glm::vec3 absPosition = RenderQueueFactory::AbsolutePosition(world, entity); + glm::vec3 absScale = RenderQueueFactory::AbsoluteScale(world, entity); + glm::vec3 origin = absPosition + (glm::vec3)cAABB["Origin"]; + glm::vec3 size = (glm::vec3)cAABB["Size"] * absScale; + return AABB::FromOriginSize(origin, size); } } diff --git a/src/Engine/Collision/CollisionSystem.cpp b/src/Engine/Collision/CollisionSystem.cpp index 69929c6d..4ce514da 100644 --- a/src/Engine/Collision/CollisionSystem.cpp +++ b/src/Engine/Collision/CollisionSystem.cpp @@ -2,31 +2,30 @@ #include "Collision/CollisionSystem.h" #include "Core/AABB.h" -void CollisionSystem::UpdateComponent(World * world, ComponentWrapper & cAABB, double dt) +void CollisionSystem::UpdateComponent(World* world, ComponentWrapper& cAABB, double dt) { - //Right now, cAABB is a component attached to any entity that should be collideable. - AABB thisBox; - if (!Collision::GetEntityBox(world, cAABB, thisBox)) { + EntityID entity = cAABB.EntityID; + boost::optional boundingBox = Collision::EntityAbsoluteAABB(world, entity); + if (!boundingBox) { return; } + ComponentWrapper& cTransform = world->GetComponent(entity, "Transform"); + AABB& boxA = *boundingBox; + //Press 'Z' to enable/disable collision. if (zPress) { return; } - //Here, mover should be an object that moves, currently only players. - for (auto& mover : *world->GetComponents("Player")) { - if (cAABB.EntityID == mover.EntityID) { + + std::vector octreeResult; + m_Octree->BoxesInSameRegion(*boundingBox, octreeResult); + for (auto& boxB : octreeResult) { + glm::vec3 resolutionVector; + if (Collision::IsSameBoxProbably(boxA, boxB)) { continue; } - AABB otherBox; - if (!Collision::GetEntityBox(world, mover.EntityID, otherBox)) { - continue; - } - glm::vec3 resolveTranslation; - if (Collision::AABBVsAABB(otherBox, thisBox, resolveTranslation)) { - ComponentWrapper& trans = world->GetComponent(mover.EntityID, "Transform"); - //TODO: Special treatment if both are movers. - trans["Position"] = (glm::vec3)trans["Position"] + resolveTranslation; + if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) { + (glm::vec3&)cTransform["Position"] += resolutionVector; } } } diff --git a/src/Engine/Collision/TriggerSystem.cpp b/src/Engine/Collision/TriggerSystem.cpp index 09092461..2c8c7540 100644 --- a/src/Engine/Collision/TriggerSystem.cpp +++ b/src/Engine/Collision/TriggerSystem.cpp @@ -3,27 +3,27 @@ #include "Core/AABB.h" #include "Rendering/Model.h" -void TriggerSystem::UpdateComponent(World* world, ComponentWrapper& trigger, double dt) +void TriggerSystem::UpdateComponent(World* world, ComponentWrapper& cTrigger, double dt) { //Currently only players can trigger things. auto players = world->GetComponents("Player"); if (players == nullptr) { return; } - EntityID tId = trigger.EntityID; - AABB triggerBox; + EntityID tId = cTrigger.EntityID; + boost::optional triggerBox = Collision::EntityAbsoluteAABB(world, tId); //The trigger *should* have a bounding box, or something, to test against so it can be triggered. - if (!Collision::GetEntityBox(world, tId, triggerBox, true)) { + if (!triggerBox) { return; } for (auto& pc : *players) { EntityID pId = pc.EntityID; - AABB playerBox; + boost::optional playerBox = Collision::EntityAbsoluteAABB(world, pId); //The player can't trigger anything without an AABB. - if (!Collision::GetEntityBox(world, pId, playerBox, true)) { + if (!playerBox) { continue; } - if (!Collision::AABBVsAABB(triggerBox, playerBox)) { + if (!Collision::AABBVsAABB(*triggerBox, *playerBox)) { //Entity is not touching the trigger, //Throw event if it was previously. if (throwLeaveIfWasInTrigger(m_EntitiesTouchingTrigger[tId], pId, tId)) { @@ -34,10 +34,9 @@ void TriggerSystem::UpdateComponent(World* world, ComponentWrapper& trigger, dou throwLeaveIfWasInTrigger(m_EntitiesCompletelyInTrigger[tId], pId, tId); } else { //Entity is at least touching the trigger. - AABB completelyInsideBox; - completelyInsideBox.CreateFromCenter(triggerBox.Center(), triggerBox.Size() - 2.0f * playerBox.Size()); - if (Collision::AABBVsAABB(completelyInsideBox, playerBox) && - glm::all(glm::greaterThan(triggerBox.Size(), playerBox.Size()))) { + AABB completelyInsideBox = AABB::FromOriginSize((*triggerBox).Origin(), (*triggerBox).Size() - 2.0f * (*playerBox).Size()); + if (Collision::AABBVsAABB(completelyInsideBox, *playerBox) && + glm::all(glm::greaterThan((*triggerBox).Size(), (*playerBox).Size()))) { //Entity is completely inside the trigger. //If it was only touching before, it is erased. m_EntitiesTouchingTrigger[tId].erase(pId); diff --git a/src/Engine/Core/AABB.cpp b/src/Engine/Core/AABB.cpp index 0df56229..22272104 100644 --- a/src/Engine/Core/AABB.cpp +++ b/src/Engine/Core/AABB.cpp @@ -4,7 +4,7 @@ AABB::AABB(const glm::vec3& minPos, const glm::vec3& maxPos) : m_MinCorner(minPos) , m_MaxCorner(maxPos) - , m_Center(0.5f * (maxPos + minPos)) + , m_Origin(0.5f * (maxPos + minPos)) , m_HalfSize(0.5f * (maxPos - minPos)) { DEBUG_IF(glm::any(glm::lessThan(m_MaxCorner, m_MinCorner))) { @@ -20,15 +20,12 @@ AABB::AABB(const glm::vec3& minPos, const glm::vec3& maxPos) AABB::AABB(const glm::vec4& minPos, const glm::vec4& maxPos) : AABB(glm::vec3(minPos), glm::vec3(maxPos)) -{} +{ } -void AABB::CreateFromCenter(const glm::vec3& center, const glm::vec3& size) +AABB AABB::FromOriginSize(const glm::vec3& origin, const glm::vec3& size) { - m_Center = center; - m_HalfSize = 0.5f * size; - m_MinCorner = m_Center - m_HalfSize; - m_MaxCorner = m_Center + m_HalfSize; + return AABB(origin - (size/2.f), origin + (size/2.f)); } AABB::~AABB() -{} +{ } diff --git a/src/Engine/Core/Octree.cpp b/src/Engine/Core/Octree.cpp index dca30cd7..47d06add 100644 --- a/src/Engine/Core/Octree.cpp +++ b/src/Engine/Core/Octree.cpp @@ -21,14 +21,10 @@ bool isFirstLower(const ChildInfo& first, const ChildInfo& second) } -Octree::Octree() - : Octree(AABB(), 0) -{} - Octree::Octree(const AABB& octTreeBounds, int subDivisions) : m_Root(new Child(octTreeBounds, subDivisions, m_StaticObjects, m_DynamicObjects)) , m_UpdatedOnce(false) -{} +{ } Octree::~Octree() { @@ -107,7 +103,7 @@ Octree::Child::Child(const AABB& octTreeBounds, glm::vec3 minPos, maxPos; const glm::vec3& parentMin = m_Box.MinCorner(); const glm::vec3& parentMax = m_Box.MaxCorner(); - const glm::vec3& parentCenter = m_Box.Center(); + const glm::vec3& parentCenter = m_Box.Origin(); std::bitset<3> bits(i); //If child is 4,5,6,7. if (bits.test(2)) { @@ -192,7 +188,7 @@ bool Octree::Child::RayCollides(const Ray& ray, Output& data) const std::vector childInfos; childInfos.reserve(8); for (int i = 0; i < 8; ++i) { - childInfos.push_back({ i, glm::distance(ray.Origin(), m_Children[i]->m_Box.Center()) }); + childInfos.push_back({ i, glm::distance(ray.Origin(), m_Children[i]->m_Box.Origin()) }); } std::sort(childInfos.begin(), childInfos.end(), isFirstLower); //Loop through the children, starting with the one closest to the ray origin. I.e the first to be hit. @@ -329,7 +325,7 @@ void Octree::Child::ClearDynamicObjects() // z : - + - + - + - + int Octree::Child::childIndexContainingPoint(const glm::vec3& point) const { - const glm::vec3& c = m_Box.Center(); + const glm::vec3& c = m_Box.Origin(); return (1 << 2) * (point.x >= c.x) | (1 << 1) * (point.y >= c.y) | (point.z >= c.z); } diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index b49cf555..6d3dff3d 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -3,7 +3,8 @@ #include EditorSystem::EditorSystem(EventBroker* eventBroker, IRenderer* renderer) - : ImpureSystem(eventBroker) + : System(eventBroker) + , ImpureSystem() , m_Renderer(renderer) { auto config = ResourceManager::Load("Config.ini"); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index c84c556b..1865dd6c 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -1,4 +1,5 @@ #include "Game.h" +#include "Collision/CollidableOctreeSystem.h" #include "Collision/TriggerSystem.h" #include "Collision/CollisionSystem.h" #include "Game/HealthSystem.h" @@ -56,20 +57,25 @@ Game::Game(int argc, char* argv[]) fp.MergeEntities(m_World); } + // Create Octrees + m_OctreeCollision = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); + m_OctreeFrustrumCulling = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); + // Create system pipeline m_SystemPipeline = new SystemPipeline(m_EventBroker); - - //All systems with orderlevel 0 will be updated first. + // All systems with orderlevel 0 will be updated first. unsigned int updateOrderLevel = 0; m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); m_SystemPipeline->AddSystem(updateOrderLevel); - - //Collision and TriggerSystem should update after player. + // Populate Octree with collidables ++updateOrderLevel; - m_SystemPipeline->AddSystem(updateOrderLevel); - m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); + // Collision and TriggerSystem should update after player. + ++updateOrderLevel; + m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); + m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); // Invoke network if (m_Config->Get("Networking.StartNetwork", false)) { @@ -82,6 +88,8 @@ Game::Game(int argc, char* argv[]) Game::~Game() { delete m_SystemPipeline; + delete m_OctreeFrustrumCulling; + delete m_OctreeCollision; delete m_World; delete m_FrameStack; delete m_InputProxy; diff --git a/src/Game/HealthSystem.cpp b/src/Game/HealthSystem.cpp index 7a1d5005..858355fc 100644 --- a/src/Game/HealthSystem.cpp +++ b/src/Game/HealthSystem.cpp @@ -2,7 +2,8 @@ #include HealthSystem::HealthSystem(EventBroker* eventBroker) - : PureSystem(eventBroker, "Health") + : System(eventBroker) + , PureSystem("Health") { //subscribe/listenTo playerdamage,healthpickup events (using the eventBroker) EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &HealthSystem::OnPlayerDamaged); diff --git a/src/Tests/OctTreeTest.cpp b/src/Tests/OctTreeTest.cpp index f437bde0..3a130354 100644 --- a/src/Tests/OctTreeTest.cpp +++ b/src/Tests/OctTreeTest.cpp @@ -21,9 +21,9 @@ BOOST_AUTO_TEST_CASE(octSameRegionTest) tree.BoxesInSameRegion(testBox, region); BOOST_REQUIRE(region.size() == 1); AABB& box = region[0]; - BOOST_CHECK_CLOSE_FRACTION(box.Center().x, firstQuadrant.Center().x, 0.00001f); - BOOST_CHECK_CLOSE_FRACTION(box.Center().y, firstQuadrant.Center().y, 0.00001f); - BOOST_CHECK_CLOSE_FRACTION(box.Center().z, firstQuadrant.Center().z, 0.00001f); + BOOST_CHECK_CLOSE_FRACTION(box.Origin().x, firstQuadrant.Origin().x, 0.00001f); + BOOST_CHECK_CLOSE_FRACTION(box.Origin().y, firstQuadrant.Origin().y, 0.00001f); + BOOST_CHECK_CLOSE_FRACTION(box.Origin().z, firstQuadrant.Origin().z, 0.00001f); BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().x, firstQuadrant.HalfSize().x, 0.00001f); BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().y, firstQuadrant.HalfSize().y, 0.00001f); BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().z, firstQuadrant.HalfSize().z, 0.00001f); diff --git a/src/Tests/OctTreeTestGameClass.cpp b/src/Tests/OctTreeTestGameClass.cpp index 10d4d6a5..2c45d897 100644 --- a/src/Tests/OctTreeTestGameClass.cpp +++ b/src/Tests/OctTreeTestGameClass.cpp @@ -91,7 +91,7 @@ void Game::Tick() frameCounter = 0; } ComponentWrapper transform = m_World->GetComponent(m_World->anotherBoxTransformId, "Transform"); - transform["Position"] = boxi.Center(); + transform["Position"] = boxi.Origin(); //check all children again in the tree if they have a box in them or not, and colormark them if they do //contentboxarna får man ut - inte childboxarna! @@ -110,7 +110,7 @@ void Game::Tick() //REQUIRED: childIndicesContainingBox must be public to test this! for each (auto someBoxIndex in boxIndex) { - glm::vec3 pos = m_World->someOctTree.m_Root->m_Children[someBoxIndex]->m_Box.Center(); + glm::vec3 pos = m_World->someOctTree.m_Root->m_Children[someBoxIndex]->m_Box.Origin(); if (abs(pos.x - oneLinkedObject.posxyz.x) < 0.005f && abs(pos.y - oneLinkedObject.posxyz.y) < 0.005f && abs(pos.z - oneLinkedObject.posxyz.z) < 0.005f) { diff --git a/src/Tests/OctTreeTestHardCodedTestWorld.h b/src/Tests/OctTreeTestHardCodedTestWorld.h index 7fcae0b2..db0b3b3c 100644 --- a/src/Tests/OctTreeTestHardCodedTestWorld.h +++ b/src/Tests/OctTreeTestHardCodedTestWorld.h @@ -77,7 +77,7 @@ private: auto someAABB = AABB(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f)); //draw main box first - AddBoxModel(someAABB.Center(), someAABB.HalfSize().x, someOctTree.m_Root, tempId); + AddBoxModel(someAABB.Origin(), someAABB.HalfSize().x, someOctTree.m_Root, tempId); //add anotherbox in octTree auto anotherBox = AABB(glm::vec3(0.1f, 0.1f, 0.1f), glm::vec3(0.2f, 0.2f, 0.2f)); @@ -85,19 +85,19 @@ private: someOctTree.AddDynamicObject(anotherBox); //draw anotherbox and save it in anotherBoxTransformId - AddBoxModel(anotherBox.Center(), anotherBox.HalfSize().x, someOctTree.m_Root, anotherBoxTransformId); + AddBoxModel(anotherBox.Origin(), anotherBox.HalfSize().x, someOctTree.m_Root, anotherBoxTransformId); //draw the octTree for (size_t j = 0; j < 8; j++) { - AddBoxModel(someOctTree.m_Root->m_Children[j]->m_Box.Center(), + AddBoxModel(someOctTree.m_Root->m_Children[j]->m_Box.Origin(), someOctTree.m_Root->m_Children[j]->m_Box.HalfSize().x, someOctTree.m_Root->m_Children[j], tempId); auto someChild = someOctTree.m_Root->m_Children[j]; for (size_t i = 0; i < 8; i++) { - AddBoxModel(someChild->m_Children[i]->m_Box.Center(), + AddBoxModel(someChild->m_Children[i]->m_Box.Origin(), someChild->m_Children[i]->m_Box.HalfSize().x, someChild->m_Children[i], tempId); } } diff --git a/src/Tests/OldOctTree.cpp b/src/Tests/OldOctTree.cpp index d3738ee6..0fb92e18 100644 --- a/src/Tests/OldOctTree.cpp +++ b/src/Tests/OldOctTree.cpp @@ -54,7 +54,7 @@ OctTree::OctTree(const AABB& octTreeBounds, int subDivisions) glm::vec3 minPos, maxPos; const glm::vec3& parentMin = m_Box.MinCorner(); const glm::vec3& parentMax = m_Box.MaxCorner(); - const glm::vec3& parentCenter = m_Box.Center(); + const glm::vec3& parentCenter = m_Box.Origin(); std::bitset<3> bits(i); //If child is 4,5,6,7. if (bits.test(2)) { @@ -172,7 +172,7 @@ bool OctTree::RayCollides(const Ray& ray, Output& data) const std::vector childInfos; childInfos.reserve(8); for (int i = 0; i < 8; ++i) { - childInfos.push_back({ i, glm::distance(ray.Origin(), m_Children[i]->m_Box.Center()) }); + childInfos.push_back({ i, glm::distance(ray.Origin(), m_Children[i]->m_Box.Origin()) }); } std::sort(childInfos.begin(), childInfos.end(), isFirstLower); //Loop through the children, starting with the one closest to the ray origin. I.e the first to be hit. @@ -279,7 +279,7 @@ void OctTree::ClearDynamicObjects() // z : - + - + - + - + int OctTree::childIndexContainingPoint(const glm::vec3& point) const { - const glm::vec3& c = m_Box.Center(); + const glm::vec3& c = m_Box.Origin(); return (1 << 2) * (point.x >= c.x) | (1 << 1) * (point.y >= c.y) | (point.z >= c.z); } From a143ae7c858dfd115e7570cfde5d317d951f10c0 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 13 Jan 2016 15:53:55 +0100 Subject: [PATCH 14/48] Changed to relative #include paths. --- include/Engine/Collision/Collision.h | 8 ++++---- include/Engine/Collision/CollisionSystem.h | 8 ++++---- include/Engine/Collision/TriggerSystem.h | 4 ++-- include/Engine/Core/Ray.h | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/Engine/Collision/Collision.h b/include/Engine/Collision/Collision.h index 714cee3f..a441c998 100644 --- a/include/Engine/Collision/Collision.h +++ b/include/Engine/Collision/Collision.h @@ -7,10 +7,10 @@ #include -#include "Core/Ray.h" -#include "Core/AABB.h" -#include "Engine/Rendering/RawModel.h" -#include "Core/Entity.h" +#include "../Core/Ray.h" +#include "../Core/AABB.h" +#include "../Rendering/RawModel.h" +#include "../Core/Entity.h" class World; struct ComponentWrapper; diff --git a/include/Engine/Collision/CollisionSystem.h b/include/Engine/Collision/CollisionSystem.h index 254a2461..f4752ecf 100644 --- a/include/Engine/Collision/CollisionSystem.h +++ b/include/Engine/Collision/CollisionSystem.h @@ -4,10 +4,10 @@ #include #include -#include "Common.h" -#include "Core/System.h" -#include "Core/EventBroker.h" -#include "Core/EKeyUp.h" +#include "../Common.h" +#include "../Core/System.h" +#include "../Core/EventBroker.h" +#include "../Core/EKeyUp.h" class CollisionSystem : public PureSystem { diff --git a/include/Engine/Collision/TriggerSystem.h b/include/Engine/Collision/TriggerSystem.h index 7e6ef008..dfb56a2c 100644 --- a/include/Engine/Collision/TriggerSystem.h +++ b/include/Engine/Collision/TriggerSystem.h @@ -4,8 +4,8 @@ #include #include -#include "Core/System.h" -#include "Core/EventBroker.h" +#include "../Core/System.h" +#include "../Core/EventBroker.h" #include "ETrigger.h" class AABB; diff --git a/include/Engine/Core/Ray.h b/include/Engine/Core/Ray.h index 0fcef01e..a234a488 100644 --- a/include/Engine/Core/Ray.h +++ b/include/Engine/Core/Ray.h @@ -2,7 +2,7 @@ #define Ray_h__ #include "../GLM.h" -#include "Common.h" +#include "../Common.h" class Ray { From 9f185a1e35452404a3987e1349b86e4a751d4f97 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 13 Jan 2016 18:51:45 +0100 Subject: [PATCH 15/48] Wrecked the last commit. Async loading works now. --- include/Engine/Core/ResourceManager.h | 145 ++++++++++++++------ include/Engine/Rendering/Model.h | 1 + include/Engine/Rendering/RawModel.h | 1 + include/Engine/Rendering/Texture.h | 3 + src/Engine/Collision/Collision.cpp | 4 +- src/Engine/Core/ResourceManager.cpp | 93 +------------ src/Engine/Rendering/Model.cpp | 101 +++++++------- src/Engine/Rendering/RawModel.cpp | 16 +++ src/Engine/Rendering/RenderQueueFactory.cpp | 1 - src/Engine/Rendering/Texture.cpp | 27 ++-- src/Game/Game.cpp | 1 - 11 files changed, 194 insertions(+), 199 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index 5ba6ba5a..32122092 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -12,14 +12,33 @@ /** Base Resource class. Implement this class for every resource to be handled by the resource manager. - Implement Create() to return a new object of that type. + + If it should be possible to load the resource asyncronously (on a separate thread in the background), + using LoadAsync() then any necessary OpenGL calls (Eg. glGenBuffers(), glBindBuffer(), etc.) must be + made in GlCommands() method, and not inside the constructor (see Texture.cpp for an example). */ class Resource { friend class ResourceManager; +private: + bool m_FullyConstructed; + protected: - Resource() { } + Resource() : m_FullyConstructed(false) { } + //This method only needs to be overridden if: + // 1: a) It should be possible to load the resource with LoadAsync(), or + // b) This resource will be loaded inside the constructor of another resource that can be loaded with LoadAsync(). + // c) Same as above but recursively. + // 2: a) The resource needs to make OpenGL calls, or + // b) The resource contains a resource that makes OGL calls, or + // c) The resource contains a resource that contains ... ... a resource that makes OGL calls, or + // + //If 2.a: any calls should be made in the GlCommands. + //If 2.b or 2.c: PostCtorGLCommands() should be called on the contained resource(s). + //If GlCommands needs to be overridden and GlCommands is also overridden by a baseclass then the baseclass + //implementation should be called in from the derived class implementation (see Model.cpp for an example). + virtual void GlCommands() { } public: // Pretend that this is a pure virtual function that you have to implement @@ -28,6 +47,16 @@ public: virtual void Reload() { } virtual void OnChildReloaded(Resource* child) { } + //If this resource implements GlCommands and it is loaded in another resource, + //then the containing resource must also implement GlCommands and + //call this method in it (see RawModel.cpp for an example). + void PostCtorGLCommands() + { + if (!m_FullyConstructed) { + GlCommands(); + } + m_FullyConstructed = true; + } unsigned int TypeID; unsigned int ResourceID; @@ -40,30 +69,6 @@ private: ResourceManager(); public: - //TODO: Check if this is ever used, and remove it if it isn't. - //Why would a resource load another resource async. in the ctor? - //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. - //Eg. If a Model loads a Texture asyncronously in the constructor, and it is not done yet. - struct StillLoadingException : public std::exception - { - virtual const char* what() const throw() - { - return "Resource is still loading."; - } - }; - //Should be thrown in a Resource's constructor if certain work needs to be handled by - //the main thread, and not a parallel worker thread. Eg. opengl commands. - struct WorkerCannotExecute : public std::exception - { - virtual const char* what() const throw() - { - return "Worker thread is unable to execute, main thread need to handle the code."; - } - }; - - static void AssertIsMainThread(); - static bool IsMainThread(); - /*static ResourceManager& Instance() { static ResourceManager s; @@ -89,7 +94,6 @@ public: */ template static T* LoadAsync(std::string resourceName, Resource* parent = nullptr); - static Resource* LoadAsync(std::string resourceType, std::string resourceName, Resource* parent = nullptr); /** Hot-loads a resource and caches it for future use. Fairly safe to assume that return value is a valid pointer, will only return nullptr on error. @@ -99,7 +103,6 @@ public: */ template static T* Load(std::string resourceName, Resource* parent = nullptr); - static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -113,25 +116,15 @@ public: static void Update(); private: - //Represents pointer values to signify that a resource haven't failed, but is not fully loaded. - class SpecialResourcePointer + //This is a haxy way to make sure that IsMainThread is run when the program starts, so the master thread id is set. + struct MasterThreadChecker { - public: - SpecialResourcePointer() - : m_Val(new Resource()) - {} - ~SpecialResourcePointer() + MasterThreadChecker() { - delete m_Val; + ResourceManager::IsMainThread(); } - //Make this class implicitly convertible to the Resource*. - operator Resource* const() const { return m_Val; } - private: - Resource* const m_Val; }; - //TODO: Check if this is ever used, and remove it if it isn't. - static SpecialResourcePointer m_StillLoading; - static SpecialResourcePointer m_LoadWithMainThread; + static MasterThreadChecker m_Checker; static std::unordered_map m_CompilerTypenameToResourceType; static std::unordered_map> m_FactoryFunctions; // type -> factory function @@ -156,6 +149,10 @@ private: // Internal: Create a resource and cache it static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent); + + static bool IsMainThread(); + template + static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); }; template @@ -168,7 +165,7 @@ T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr return nullptr; } - return static_cast(Load(it->second, resourceName, parent)); + return static_cast(Load(it->second, resourceName, parent)); } template @@ -188,7 +185,65 @@ T* ResourceManager::LoadAsync(std::string resourceName, Resource* parent /* = nu return nullptr; } - return static_cast(LoadAsync(it->second, resourceName, parent)); + return static_cast(Load(it->second, resourceName, parent)); +} + +template +static Resource* ResourceManager::Load(std::string resourceType, std::string resourceName, Resource* parent /* = nullptr */) +{ + auto cacheKey = std::make_pair(resourceType, resourceName); + decltype(m_ResourceCache)::iterator it; + //If a thread has already been launched to load this resource. + auto tIt = m_LoadingThreads.find(cacheKey); + if (tIt != m_LoadingThreads.end()) { + if (Async) { + //Return null if the thread is still working. + if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { + return nullptr; + } + //Else we know the thread has completed. + } else { + //Wait for the thread to finish loading. + tIt->second.join(); + } + //When the thread is done, delete the thread. + m_LoadingThreads.erase(tIt); + //Find the resource that the thread loaded. + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + //At this point the resource may not be completely + //done since the worker threads cannot do opengl commands. + if (IsMainThread()) { + //Do the gl commands to complete the resource if we are not a worker thread. + it->second->PostCtorGLCommands(); + } + return it->second; + } else { + //If resource is still null at cacheKey after thread finishes, it failed. + return nullptr; + } + } + + //If resource has already been loaded and cached. + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + return it->second; + } + + //If resource is not cached.. + if (Async) { + //Create a thread that loads the resource into cache. + m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + return nullptr; + } else { + //load and return the resource. + Resource* res = createResource(resourceType, resourceName, parent); + if (IsMainThread() && res != nullptr) { + //Do the gl commands to complete the resource if we are not a worker thread. + res->PostCtorGLCommands(); + } + return res; + } } #endif diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index 9cc145af..ffef745c 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -10,6 +10,7 @@ class Model : public RawModel private: Model(std::string fileName); + virtual void GlCommands() override; public: ~Model(); diff --git a/include/Engine/Rendering/RawModel.h b/include/Engine/Rendering/RawModel.h index c8226168..379b81ab 100644 --- a/include/Engine/Rendering/RawModel.h +++ b/include/Engine/Rendering/RawModel.h @@ -24,6 +24,7 @@ class RawModel : public Resource protected: RawModel(std::string fileName); + virtual void GlCommands() override; public: ~RawModel(); diff --git a/include/Engine/Rendering/Texture.h b/include/Engine/Rendering/Texture.h index 16892afc..8cdcaef0 100644 --- a/include/Engine/Rendering/Texture.h +++ b/include/Engine/Rendering/Texture.h @@ -11,7 +11,10 @@ class Texture : public BaseTexture private: Texture(std::string path); + virtual void GlCommands() override; + GLint m_Format; + Image* m_Image; public: ~Texture(); diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index c4af6258..7473c67f 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -220,7 +220,7 @@ bool attachAABBComponentFromModel(World* world, EntityID id) } ComponentWrapper model = world->GetComponent(id, "Model"); ComponentWrapper collision = world->AttachComponent(id, "AABB"); - Model* modelRes = ResourceManager::Load(model["Resource"]); + Model* modelRes = ResourceManager::LoadAsync(model["Resource"]); if (modelRes == nullptr) { return false; } @@ -247,7 +247,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) { ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform"); ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model"); - Model* modelRes = ResourceManager::Load(model["Resource"]); + Model* modelRes = ResourceManager::LoadAsync(model["Resource"]); outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]); glm::vec3 mini = outBox.MinCorner(); glm::vec3 maxi = outBox.MaxCorner(); diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index 32af073d..102e99ba 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -14,8 +14,6 @@ std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; std::unordered_map, boost::thread> ResourceManager::m_LoadingThreads; boost::recursive_mutex ResourceManager::m_Mutex; -ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading; -ResourceManager::SpecialResourcePointer ResourceManager::m_LoadWithMainThread; unsigned int ResourceManager::GetTypeID(std::string resourceType) { @@ -80,86 +78,6 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -Resource* ResourceManager::LoadAsync(std::string resourceType, std::string resourceName, Resource* parent /*= nullptr*/) -{ - auto cacheKey = std::make_pair(resourceType, resourceName); - decltype(m_ResourceCache)::iterator it; - //If a thread has already been launched to load this resource. - auto tIt = m_LoadingThreads.find(cacheKey); - if (tIt != m_LoadingThreads.end()) { - //If the thread is still working. - if (tIt->second.joinable()) { - return nullptr; - } - //Else, the thread is done. - m_LoadingThreads.erase(tIt); - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - //If the thread is done, but it cannot complete the rest, main thread must complete construction. - if (it->second == m_LoadWithMainThread) { - AssertIsMainThread(); - return createResource(resourceType, resourceName, parent); - } - return it->second; - } else { - //If cache is still empty at cacheKey after thread finishes, it failed. - return nullptr; - } - } - - //If resource has already been loaded and cached. - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - //if ConstructByMainThread, createResource from Main. - return it->second; - } - - //Create a thread that loads the resource into cache. - m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); - return nullptr; -} - -Resource* ResourceManager::Load(std::string resourceType, std::string resourceName, Resource* parent /*= nullptr*/) -{ - Resource* resource; - auto cacheKey = std::make_pair(resourceType, resourceName); - decltype(m_ResourceCache)::iterator it; - //If a thread has already been launched to load this resource. - auto tIt = m_LoadingThreads.find(cacheKey); - if (tIt != m_LoadingThreads.end()) { - //Wait for the thread to finish loading. - tIt->second.join(); - //Then delete the thread. - m_LoadingThreads.erase(tIt); - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - //If the thread is done, but it cannot complete the rest, main thread must complete construction. - if (it->second == m_LoadWithMainThread) { - AssertIsMainThread(); - return createResource(resourceType, resourceName, parent); - } - return it->second; - } else { - //If cache is still empty at cacheKey after thread finishes, it failed. - return nullptr; - } - } - //If resource has already been loaded and cached. - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - return it->second; - } - - //If resource is not cached, load and return it. - resource = createResource(resourceType, resourceName, parent); - if (resource == m_LoadWithMainThread) { - //If we entered here then we know the caller is a worker thread. - //And we know the resource must be loaded from master thread. - throw(WorkerCannotExecute()); - } - return resource; -} - Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent) { //Lock the mutex immediately, and unlock it when leaving the function. @@ -174,12 +92,10 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string Resource* resource = nullptr; try { resource = facIt->second(resourceName); - } catch (const WorkerCannotExecute& e) { - resource = m_LoadWithMainThread; } catch (const std::exception& e) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); } - if (resource != nullptr && resource != m_LoadWithMainThread) { + if (resource != nullptr) { // Store IDs resource->TypeID = GetTypeID(resourceType); resource->ResourceID = GetNewResourceID(resource->TypeID); @@ -204,10 +120,3 @@ bool ResourceManager::IsMainThread() static boost::thread::id MainThreadId = boost::this_thread::get_id(); return boost::this_thread::get_id() == MainThreadId; } - -void ResourceManager::AssertIsMainThread() -{ - if (!IsMainThread()) { - throw WorkerCannotExecute(); - } -} \ No newline at end of file diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index f346d9e1..c2aab44f 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -3,58 +3,65 @@ Model::Model(std::string fileName) : RawModel(fileName) { - // Generate GL buffers - GLuint buffer; - glGenBuffers(1, &buffer); - glBindBuffer(GL_ARRAY_BUFFER, buffer); - glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(Vertex), &m_Vertices[0], GL_STATIC_DRAW); +} - glGenBuffers(1, &ElementBuffer); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Indices.size() * sizeof(unsigned int), &m_Indices[0], GL_STATIC_DRAW); +void Model::GlCommands() +{ + //Call the base class method. + RawModel::GlCommands(); - glGenVertexArrays(1, &VAO); - glBindVertexArray(VAO); - GLERROR("GLEW: BufferFail4"); + // Generate GL buffers + GLuint buffer; + glGenBuffers(1, &buffer); + glBindBuffer(GL_ARRAY_BUFFER, buffer); + glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(Vertex), &m_Vertices[0], GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, buffer); - std::vector structSizes = { 3, 3, 3, 3, 2, 4, 4, 4, 4, 4, 4 }; - int stride = 0; - for (int size : structSizes) { - stride += size; - } - stride *= sizeof(GLfloat); - int offset = 0; - { - int element = 0; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * offset)); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - } - GLERROR("GLEW: BufferFail5"); + glGenBuffers(1, &ElementBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Indices.size() * sizeof(unsigned int), &m_Indices[0], GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); - glEnableVertexAttribArray(5); - glEnableVertexAttribArray(6); - glEnableVertexAttribArray(7); - glEnableVertexAttribArray(8); - glEnableVertexAttribArray(9); - glEnableVertexAttribArray(10); - GLERROR("GLEW: BufferFail5"); + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + GLERROR("GLEW: BufferFail4"); - //CreateBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, buffer); + std::vector structSizes = { 3, 3, 3, 3, 2, 4, 4, 4, 4, 4, 4 }; + int stride = 0; + for (int size : structSizes) { + stride += size; + } + stride *= sizeof(GLfloat); + int offset = 0; + { + int element = 0; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * offset)); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + } + GLERROR("GLEW: BufferFail5"); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glEnableVertexAttribArray(3); + glEnableVertexAttribArray(4); + glEnableVertexAttribArray(5); + glEnableVertexAttribArray(6); + glEnableVertexAttribArray(7); + glEnableVertexAttribArray(8); + glEnableVertexAttribArray(9); + glEnableVertexAttribArray(10); + GLERROR("GLEW: BufferFail5"); + + //CreateBuffers(); } Model::~Model() diff --git a/src/Engine/Rendering/RawModel.cpp b/src/Engine/Rendering/RawModel.cpp index 95a75a15..9428f21e 100644 --- a/src/Engine/Rendering/RawModel.cpp +++ b/src/Engine/Rendering/RawModel.cpp @@ -292,6 +292,22 @@ RawModel::RawModel(std::string fileName) } } +void RawModel::GlCommands() +{ + //Since RawModel contains Textures that was loaded in the constructor, their GlCommands must be run. + for (auto& texGroup : TextureGroups) { + if (texGroup.Texture) { + texGroup.Texture->PostCtorGLCommands(); + } + if (texGroup.NormalMap) { + texGroup.NormalMap->PostCtorGLCommands(); + } + if (texGroup.SpecularMap) { + texGroup.SpecularMap->PostCtorGLCommands(); + } + } +} + RawModel::~RawModel() { if (m_Skeleton) { diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 27651a72..71b2e086 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -84,7 +84,6 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) continue; } glm::vec4 color = modelC["Color"]; - //Model* model = ResourceManager::Load(resource); Model* model = ResourceManager::LoadAsync(resource); if (model == nullptr) { model = ResourceManager::Load("Models/Core/Error.obj"); diff --git a/src/Engine/Rendering/Texture.cpp b/src/Engine/Rendering/Texture.cpp index 19c829bf..7e1cd65b 100644 --- a/src/Engine/Rendering/Texture.cpp +++ b/src/Engine/Rendering/Texture.cpp @@ -2,39 +2,44 @@ Texture::Texture(std::string path) { - PNG image(path); + m_Image = new PNG(path); - if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { - image = PNG("Textures/Core/ErrorTexture.png"); - if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { + if (m_Image->Width == 0 && m_Image->Height == 0 || m_Image->Format == Image::ImageFormat::Unknown) { + delete m_Image; + m_Image = new PNG("Textures/Core/ErrorTexture.png"); + if (m_Image->Width == 0 && m_Image->Height == 0 || m_Image->Format == Image::ImageFormat::Unknown) { LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed."); return; } } - this->Width = image.Width; - this->Height = image.Height; + this->Width = m_Image->Width; + this->Height = m_Image->Height; - GLint format; - switch (image.Format) { + switch (m_Image->Format) { case Image::ImageFormat::RGB: - format = GL_RGB; + m_Format = GL_RGB; break; case Image::ImageFormat::RGBA: - format = GL_RGBA; + m_Format = GL_RGBA; break; } +} +void Texture::GlCommands() +{ // Construct the OpenGL texture glGenTextures(1, &m_Texture); glBindTexture(GL_TEXTURE_2D, m_Texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, format, image.Width, image.Height, 0, format, GL_UNSIGNED_BYTE, image.Data); + glTexImage2D(GL_TEXTURE_2D, 0, m_Format, Width, Height, 0, m_Format, GL_UNSIGNED_BYTE, m_Image->Data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); GLERROR("Texture load"); + delete m_Image; + m_Image = nullptr; } Texture::~Texture() diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index ee4c7cbf..033db642 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -4,7 +4,6 @@ Game::Game(int argc, char* argv[]) { - ResourceManager::AssertIsMainThread(); ResourceManager::RegisterType("ConfigFile"); ResourceManager::RegisterType("Model"); ResourceManager::RegisterType("Texture"); From cca64dca24f7d747a1d7deae04dc9dd7dd5d58c4 Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 14 Jan 2016 13:54:38 +0100 Subject: [PATCH 16/48] Refactoring and commenting. --- include/Engine/Sound/EContinueSound.h | 2 +- include/Engine/Sound/EPauseSound.h | 2 +- include/Engine/Sound/EPlayBackgroundMusic.h | 2 +- include/Engine/Sound/EPlaySound.h | 18 -- include/Engine/Sound/EPlaySoundOnEntity.h | 1 + include/Engine/Sound/EPlaySoundOnPosition.h | 2 +- include/Engine/Sound/ESetBGMGain.h | 2 +- include/Engine/Sound/ESetSFXGain.h | 2 +- include/Engine/Sound/EStopSound.h | 2 +- include/Engine/Sound/SoundSystem.h | 25 +-- src/Engine/Sound/SoundSystem.cpp | 186 ++++++++------------ src/Game/Game.cpp | 2 +- 12 files changed, 95 insertions(+), 151 deletions(-) delete mode 100644 include/Engine/Sound/EPlaySound.h diff --git a/include/Engine/Sound/EContinueSound.h b/include/Engine/Sound/EContinueSound.h index 2c624b0b..707d5c6b 100644 --- a/include/Engine/Sound/EContinueSound.h +++ b/include/Engine/Sound/EContinueSound.h @@ -6,7 +6,7 @@ namespace Events { - +// Continues to play a sound from where it was paused. struct ContinueSound : Event { EntityID EmitterID; diff --git a/include/Engine/Sound/EPauseSound.h b/include/Engine/Sound/EPauseSound.h index f9e2e6f0..33d57428 100644 --- a/include/Engine/Sound/EPauseSound.h +++ b/include/Engine/Sound/EPauseSound.h @@ -6,7 +6,7 @@ namespace Events { - +// Pauses a playing sound struct PauseSound : Event { EntityID EmitterID; diff --git a/include/Engine/Sound/EPlayBackgroundMusic.h b/include/Engine/Sound/EPlayBackgroundMusic.h index 4276ad4a..711954ee 100644 --- a/include/Engine/Sound/EPlayBackgroundMusic.h +++ b/include/Engine/Sound/EPlayBackgroundMusic.h @@ -7,7 +7,7 @@ namespace Events { - +// Play a sound that will be heared the same anywhere struct PlayBackgroundMusic : public Event { std::string FilePath = ""; diff --git a/include/Engine/Sound/EPlaySound.h b/include/Engine/Sound/EPlaySound.h deleted file mode 100644 index 9c9eab0a..00000000 --- a/include/Engine/Sound/EPlaySound.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef Events_PlaySound_h__ -#define Events_PlaySound_h__ - -#include "Core/EventBroker.h" -#include "Core/Entity.h" - -namespace Events -{ - - struct PlaySound : Event -{ - std::string FilePath; - EntityID EmitterID; -}; - -} - -#endif \ No newline at end of file diff --git a/include/Engine/Sound/EPlaySoundOnEntity.h b/include/Engine/Sound/EPlaySoundOnEntity.h index 39dea432..fb4b7a15 100644 --- a/include/Engine/Sound/EPlaySoundOnEntity.h +++ b/include/Engine/Sound/EPlaySoundOnEntity.h @@ -10,6 +10,7 @@ namespace Events // Plays a sound on an entity with a SoundEmitter component attached. // Sound behavior is thereby specified in the SoundEmitter component. +// ?(???)?? struct PlaySoundOnEntity : public Event { EntityID EmitterID = 0; diff --git a/include/Engine/Sound/EPlaySoundOnPosition.h b/include/Engine/Sound/EPlaySoundOnPosition.h index 324b8fae..9c6f8f26 100644 --- a/include/Engine/Sound/EPlaySoundOnPosition.h +++ b/include/Engine/Sound/EPlaySoundOnPosition.h @@ -8,7 +8,7 @@ namespace Events { -// Plays a sound on a given position +// Plays a sound on a given position. Idk if this would be useful. struct PlaySoundOnPosition : public Event { glm::vec3 Position = glm::vec3(0); diff --git a/include/Engine/Sound/ESetBGMGain.h b/include/Engine/Sound/ESetBGMGain.h index 4efe34ce..1be6aa24 100644 --- a/include/Engine/Sound/ESetBGMGain.h +++ b/include/Engine/Sound/ESetBGMGain.h @@ -5,7 +5,7 @@ namespace Events { - +// Set the "volume" for all background sounds struct SetBGMGain : public Event { float Gain; diff --git a/include/Engine/Sound/ESetSFXGain.h b/include/Engine/Sound/ESetSFXGain.h index cbc06e97..88465cc1 100644 --- a/include/Engine/Sound/ESetSFXGain.h +++ b/include/Engine/Sound/ESetSFXGain.h @@ -5,7 +5,7 @@ namespace Events { - +// Set the "volume" for all effect sounds struct SetSFXGain : public Event { float Gain; diff --git a/include/Engine/Sound/EStopSound.h b/include/Engine/Sound/EStopSound.h index b6cd37b8..62644fbe 100644 --- a/include/Engine/Sound/EStopSound.h +++ b/include/Engine/Sound/EStopSound.h @@ -6,7 +6,7 @@ namespace Events { - +// Stops a sound emitter, and will also delete it. struct StopSound : Event { EntityID EmitterID; diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 43d670d0..ae30a682 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -12,7 +12,6 @@ #include "Core/EventBroker.h" #include "Rendering/RenderQueueFactory.h" // Absolute transform #include "Sound/Sound.h" -#include "Sound/EPlaySound.h" #include "Sound/EPlaySoundOnEntity.h" #include "Sound/EPlaySoundOnPosition.h" #include "Sound/EPlayBackgroundMusic.h" @@ -22,13 +21,17 @@ #include "Sound/ESetBGMGain.h" #include "Sound/ESetSFXGain.h" +enum class SoundType { + SFX, + BGM +}; struct Source { Source() { } Sound* SoundResource = nullptr; ALuint ALsource; - bool HasBeenPlayed = false; + SoundType Type; }; class SoundSystem @@ -42,15 +45,15 @@ public: private: // Help functions for working with OpenaAL void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; - glm::vec3 listnerPos() { glm::vec3 pos; alGetListener3f(AL_POSITION, &pos.x, &pos.y, &pos.z); return pos; }; void setListenerVel(glm::vec3 vel) { alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z); }; - glm::vec3 listenerVel() { glm::vec3 vel; alGetListener3f(AL_VELOCITY, &vel.x, &vel.y, &vel.z); return vel; }; void setListenerOri(glm::vec3 ori); + glm::vec3 listnerPos() { glm::vec3 pos; alGetListener3f(AL_POSITION, &pos.x, &pos.y, &pos.z); return pos; }; + glm::vec3 listenerVel() { glm::vec3 vel; alGetListener3f(AL_VELOCITY, &vel.x, &vel.y, &vel.z); return vel; }; glm::vec3 listenerOri() { glm::vec3 ori; alGetListener3f(AL_ORIENTATION, &ori.x, &ori.y, &ori.z); return ori; }; void setSourcePos(ALuint source, glm::vec3 pos) { ALfloat spos[3] = { pos.x, pos.y, pos.z }; alSourcefv(source, AL_POSITION, spos); }; void setSourceVel(ALuint source, glm::vec3 vel) { ALfloat svel[3] = { vel.x, vel.y, vel.z }; alSourcefv(source, AL_VELOCITY, svel); }; - // Logic + // Logic void initOpenAL(); void updateEmitters(); void updateListener(); @@ -60,9 +63,9 @@ private: void playSound(Source* source); void stopSound(Source* source); void stopEmitters(); - bool isPlaying(ALuint source); + ALenum getSourceState(ALuint source); void setGain(Source* source, float gain); - void setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance); + void setSoundProperties(ALuint source, ComponentWrapper* soundComponent); // OpenAL system variables ALCdevice* m_ALCdevice = nullptr; @@ -72,25 +75,23 @@ private: World* m_World = nullptr; EventBroker* m_EventBroker = nullptr; std::unordered_map m_Sources; - float m_BGMVolumeChannel = 1.f; + float m_BGMVolumeChannel = 1.0f; float m_SFXVolumeChannel = 1.f; bool m_EditorEnabled = false; // Events - EventRelay m_EPlaySound; - bool OnPlaySound(const Events::PlaySound &e); EventRelay m_EPlaySoundOnEntity; bool OnPlaySoundOnEntity(const Events::PlaySoundOnEntity &e); EventRelay m_EPlaySoundOnPosition; bool OnPlaySoundOnPosition(const Events::PlaySoundOnPosition &e); + EventRelay m_EPlayBackgroundMusic; + bool OnPlayBackgroundMusic(const Events::PlayBackgroundMusic &e); EventRelay m_EPauseSound; bool OnPauseSound(const Events::PauseSound &e); EventRelay m_EStopSound; bool OnStopSound(const Events::StopSound &e); EventRelay m_EContinueSound; bool OnContinueSound(const Events::ContinueSound &e); - EventRelay m_EPlayBackgroundMusic; - bool OnPlayBackgroundMusic(const Events::PlayBackgroundMusic &e); EventRelay m_ESetBGMGain; bool OnSetBGMGain(const Events::SetBGMGain &e); // Not tested EventRelay m_ESetSFXGain; diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index b5c63d13..ced5f484 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -8,38 +8,24 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode initOpenAL(); - alSpeedOfSound(340.29f); // Speed of sound + alSpeedOfSound(340.29f); alDistanceModel(AL_LINEAR_DISTANCE); alDopplerFactor(1); - EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound); EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnEntity, &SoundSystem::OnPlaySoundOnEntity); EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnPosition, &SoundSystem::OnPlaySoundOnPosition); + EVENT_SUBSCRIBE_MEMBER(m_EPlayBackgroundMusic, &SoundSystem::OnPlayBackgroundMusic); EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound); EVENT_SUBSCRIBE_MEMBER(m_EPauseSound, &SoundSystem::OnPauseSound); EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundSystem::OnContinueSound); - EVENT_SUBSCRIBE_MEMBER(m_EPlayBackgroundMusic, &SoundSystem::OnPlayBackgroundMusic); - //EVENT_SUBSCRIBE_MEMBER(m_ESetBGMGain, &SoundSystem::SetBGMGain); - //EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundSystem::OnSetSFXGain); -} - -void SoundSystem::initOpenAL() -{ - // 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."); - } + EVENT_SUBSCRIBE_MEMBER(m_ESetBGMGain, &SoundSystem::OnSetBGMGain); + EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundSystem::OnSetSFXGain); } SoundSystem::~SoundSystem() { stopEmitters(); // Stopps emitters deleteInactiveEmitters(); // Deletes stopped emitters - // Delete entities std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end(); it++) { @@ -49,58 +35,49 @@ SoundSystem::~SoundSystem() alcDestroyContext(m_ALCcontext); alcCloseDevice(m_ALCdevice); - delete m_ALCcontext; - delete m_ALCdevice; } void SoundSystem::stopEmitters() { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end(); it++) { - if (isPlaying((*it).second->ALsource)) { + if (getSourceState((*it).second->ALsource) == AL_PLAYING) { stopSound((*it).second); } } } void SoundSystem::Update() -{ - addNewEmitters(); - deleteInactiveEmitters(); +{ + addNewEmitters(); // can be optimized with "EEntityCreated" + deleteInactiveEmitters(); // can be optimized with "EEntityDeleted" updateEmitters(); updateListener(); } void SoundSystem::deleteInactiveEmitters() { - auto emitterComponents = m_World->GetComponents("SoundEmitter"); - for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { - EntityID emitter = (*it).EntityID; - Source* source = m_Sources[emitter]; - if (isPlaying(source->ALsource) || !source->HasBeenPlayed) { // The sound is still playing, do not remove - it++; - continue; - } - else { - alDeleteBuffers(1, &source->ALsource); - alDeleteSources(1, &source->ALsource); - //delete m_Sources[emitter]->SoundResource; - m_Sources.erase(emitter); - m_World->DeleteEntity(emitter); - } - } - std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end();) { if (m_World->ValidEntity((*it).first)) { - // Entity is valid, move on. - it++; - continue; + if (getSourceState(it->second->ALsource) != AL_STOPPED) { + // Nothing to see here, move along + it++; + continue; + } else { + // Sound has been stopped / finished playing + alDeleteBuffers(1, &it->second->ALsource); + alDeleteSources(1, &it->second->ALsource); + delete it->second->SoundResource; + m_World->DeleteEntity(it->first); + it = m_Sources.erase(it); + } } else { + // Entity has been removed stopSound((*it).second); - alDeleteBuffers(1, &m_Sources[(*it).first]->ALsource); - alDeleteSources(1, &m_Sources[(*it).first]->ALsource); - //delete m_Sources[emitter]->SoundResource; + alDeleteBuffers(1, &it->second->ALsource); + alDeleteSources(1, &it->second->ALsource); + delete it->second->SoundResource; it = m_Sources.erase(it); } } @@ -111,19 +88,10 @@ void SoundSystem::addNewEmitters() auto emitterComponents = m_World->GetComponents("SoundEmitter"); for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { EntityID emitter = (*it).EntityID; - std::unordered_map::iterator i; - i = m_Sources.find(emitter); - if (i == m_Sources.end()) { // Did not exist, add it + std::unordered_map::iterator source; + source = m_Sources.find(emitter); + if (source == m_Sources.end()) { // Did not exist, add it Source* source = createSource((std::string)(*it)["FilePath"]); - setSoundProperties( - source->ALsource, - (float)(double)(*it)["Gain"], - (float)(double)(*it)["Pitch"], - (bool)(*it)["Loop"], - (float)(double)(*it)["MaxDistance"], - (float)(double)(*it)["RollOffFactor"], - (float)(double)(*it)["ReferenceDistance"] - ); m_Sources[emitter] = source; } } @@ -131,45 +99,30 @@ void SoundSystem::addNewEmitters() void SoundSystem::updateEmitters() { - auto emitterComponents = m_World->GetComponents("SoundEmitter"); - for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { - EntityID emitter = (*it).EntityID; - if (!m_World->ValidEntity(emitter)) { // Entity has been deleted - // Delete - } else { - std::unordered_map::iterator i; - i = m_Sources.find(emitter); - if (i != m_Sources.end()) { - // Get previous pos - glm::vec3 previousPos; - alGetSource3f(i->second->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); - // Get next pos - glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); - // Calculate velocity - glm::vec3 velocity = nextPos - previousPos; - setSourcePos(i->second->ALsource, nextPos); - setSourceVel(i->second->ALsource, velocity); - setSoundProperties( - i->second->ALsource, - (float)(double)(*it)["Gain"], - (float)(double)(*it)["Pitch"], - (bool)(*it)["Loop"], - (float)(double)(*it)["MaxDistance"], - (float)(double)(*it)["RollOffFactor"], - (float)(double)(*it)["ReferenceDistance"] - ); + std::unordered_map::iterator it; + for (it = m_Sources.begin(); it != m_Sources.end(); it++) { + // Get previous pos + glm::vec3 previousPos; + alGetSource3f(it->second->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); + // Get next pos + glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, it->first); + // Calculate velocity + glm::vec3 velocity = nextPos - previousPos; + setSourcePos(it->second->ALsource, nextPos); + setSourceVel(it->second->ALsource, velocity); + float gain; + (bool)(it->second->Type) ? gain = m_SFXVolumeChannel : gain = m_BGMVolumeChannel; + auto emitter = m_World->GetComponent(it->first, "SoundEmitter"); + setSoundProperties(it->second->ALsource, &emitter); - // To make an emitter play when spawned in editor mode - if (m_EditorEnabled) { - // Path changed - if (i->second->SoundResource->Path() != (std::string)(*it)["FilePath"]) { - i->second->SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); - if (i->second->SoundResource->Buffer() != 0) { - playSound(i->second); - } - } + // To make an emitter play when spawned in editor mode + if (m_EditorEnabled) { + // Path changed + if (it->second->SoundResource->Path() != (std::string)emitter["FilePath"]) { + it->second->SoundResource = ResourceManager::Load((std::string)emitter["FilePath"]); + if (it->second->SoundResource->Buffer() != 0) { + playSound(it->second); } - it++; } } } @@ -207,7 +160,6 @@ void SoundSystem::playSound(Source* source) { alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer()); alSourcePlay(source->ALsource); - source->HasBeenPlayed = true; } void SoundSystem::stopSound(Source* source) @@ -215,16 +167,10 @@ void SoundSystem::stopSound(Source* source) alSourceStop(source->ALsource); } -bool SoundSystem::OnPlaySound(const Events::PlaySound & e) -{ - Source* sauce = createSource(e.FilePath); - playSound(sauce); - return false; -} - bool SoundSystem::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) { Source* source = createSource(e.FilePath); + source->Type = SoundType::SFX; m_Sources[e.EmitterID] = source; playSound(source); return false; @@ -245,6 +191,7 @@ bool SoundSystem::OnPlaySoundOnPosition(const Events::PlaySoundOnPosition & e) (float&)(double)emitter["ReferenceDistance"] = e.ReferenceDistance; auto model = m_World->AttachComponent(emitterID, "Model"); (std::string&)model["Resource"] = "Models/Core/UnitCube.obj"; + source->Type = SoundType::SFX; m_Sources[emitterID] = source; playSound(source); return false; @@ -278,6 +225,7 @@ bool SoundSystem::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) (std::string&)emitter["FilePath"] = e.FilePath; m_World->AttachComponent(emitterChild, "Transform"); Source* source = createSource(e.FilePath); + source->Type = SoundType::BGM; m_Sources[emitterChild] = source; playSound(source); } @@ -312,11 +260,11 @@ void SoundSystem::setListenerOri(glm::vec3 ori) alListenerfv(AL_ORIENTATION, lOri); } -bool SoundSystem::isPlaying(ALuint source) +ALenum SoundSystem::getSourceState(ALuint source) { ALenum state; alGetSourcei(source, AL_SOURCE_STATE, &state); - return (state == AL_PLAYING); + return state; } void SoundSystem::setGain(Source * source, float gain) @@ -324,12 +272,24 @@ void SoundSystem::setGain(Source * source, float gain) alSourcef(source->ALsource, AL_GAIN, gain); } -void SoundSystem::setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance) +void SoundSystem::setSoundProperties(ALuint source, ComponentWrapper* soundComponent) { - alSourcef(source, AL_GAIN, gain * m_SFXVolumeChannel); - alSourcef(source, AL_PITCH, pitch); - alSourcei(source, AL_LOOPING, (int)loop); // YOLO - alSourcef(source, AL_MAX_DISTANCE, maxDistance); - alSourcef(source, AL_ROLLOFF_FACTOR, rollOffFactor); - alSourcef(source, AL_REFERENCE_DISTANCE, referenceDistance); + alSourcef(source, AL_GAIN, (float)(double)(*soundComponent)["Gain"]); + alSourcef(source, AL_PITCH, (float)(double)(*soundComponent)["Pitch"]); + alSourcei(source, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]); // YOLO + alSourcef(source, AL_MAX_DISTANCE, (float)(double)(*soundComponent)["MaxDistance"]); + alSourcef(source, AL_ROLLOFF_FACTOR, (float)(double)(*soundComponent)["RollOffFactor"]); + alSourcef(source, AL_REFERENCE_DISTANCE, (float)(double)(*soundComponent)["ReferenceDistance"]); } + +void SoundSystem::initOpenAL() +{ + // 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."); + } +} \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 95846e52..640f77c0 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -144,7 +144,7 @@ bool Game::debugOnInputCommand(const Events::InputCommand & e) { if (e.Command == "PlaySound" && e.Value > 0) { Events::PlayBackgroundMusic e; - e.FilePath = "Audio/crosscounter.wav"; + e.FilePath = "Audio/5dollar.wav"; //e.emitterID = 18; // rofl m_EventBroker->Publish(e); } From 7f0ff6c4d7d9219ef1a8c81103159c959081d839 Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 14 Jan 2016 14:20:34 +0100 Subject: [PATCH 17/48] Added a sound testing level. --- resources/Schema/Entities/SoundTestLevel.xml | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 resources/Schema/Entities/SoundTestLevel.xml diff --git a/resources/Schema/Entities/SoundTestLevel.xml b/resources/Schema/Entities/SoundTestLevel.xml new file mode 100644 index 00000000..01ae111e --- /dev/null +++ b/resources/Schema/Entities/SoundTestLevel.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + From 71d936f8d1fc86f0e43b4ec8272e60be95724612 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Wed, 13 Jan 2016 17:53:54 +0100 Subject: [PATCH 18/48] Start of a movement system with collisions against AABBs --- .../Engine/Collision/CollidableOctreeSystem.h | 7 +++- include/Engine/Collision/Collision.h | 6 +++ include/Engine/Core/EComponentAttached.h | 20 ++++++++++ include/Engine/Core/Entity.h | 18 +++++++++ include/Game/PlayerMovementSystem.h | 12 ++++++ resources/Schema/Entities/CollisionTest1.xml | 38 ++++++++++++++++++ resources/Schema/Entities/OctreeTest.xml | 26 +++++------- src/Engine/Collision/Collision.cpp | 40 +++++++++++++++++++ src/Engine/Collision/CollisionSystem.cpp | 23 +++++++++++ src/Game/PlayerMovementSystem.cpp | 7 ++++ 10 files changed, 179 insertions(+), 18 deletions(-) create mode 100644 include/Engine/Core/EComponentAttached.h create mode 100644 include/Game/PlayerMovementSystem.h create mode 100644 resources/Schema/Entities/CollisionTest1.xml create mode 100644 src/Game/PlayerMovementSystem.cpp diff --git a/include/Engine/Collision/CollidableOctreeSystem.h b/include/Engine/Collision/CollidableOctreeSystem.h index b7a77346..760503e9 100644 --- a/include/Engine/Collision/CollidableOctreeSystem.h +++ b/include/Engine/Collision/CollidableOctreeSystem.h @@ -1,3 +1,6 @@ +#ifndef CollidableOctreeSystem_h__ +#define CollidableOctreeSystem_h__ + #include "../Core/System.h" #include "../Core/Octree.h" #include "Collision.h" @@ -16,4 +19,6 @@ public: private: Octree* m_Octree; -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Collision/Collision.h b/include/Engine/Collision/Collision.h index 18520243..2854d07b 100644 --- a/include/Engine/Collision/Collision.h +++ b/include/Engine/Collision/Collision.h @@ -45,6 +45,12 @@ bool RayVsModel(const Ray& ray, float& outUCoord, float& outVCoord); +bool AABBvsTriangles(const AABB& box, + const std::vector& modelVertices, + const std::vector& modelIndices, + const glm::mat4& modelMatrix, + glm::vec3& outResolutionVector); + //Return true if the boxes are intersecting. bool AABBVsAABB(const AABB& a, const AABB& b); //Return true if the boxes are intersecting. diff --git a/include/Engine/Core/EComponentAttached.h b/include/Engine/Core/EComponentAttached.h new file mode 100644 index 00000000..b4ab9f78 --- /dev/null +++ b/include/Engine/Core/EComponentAttached.h @@ -0,0 +1,20 @@ +#ifndef EComponentAttached_h__ +#define EComponentAttached_h__ + +#include "EventBroker.h" +#include "World.h" +#include "Entity.h" +#include "ComponentWrapper.h" + +namespace Events +{ + +struct ComponentAttached : Event +{ + EntityWrapper Entity; + ComponentWrapper Component; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Core/Entity.h b/include/Engine/Core/Entity.h index 1f40b9b7..c6730924 100644 --- a/include/Engine/Core/Entity.h +++ b/include/Engine/Core/Entity.h @@ -4,4 +4,22 @@ typedef unsigned int EntityID; const static unsigned int EntityID_Invalid = -1; +class World; +struct EntityWrapper +{ + EntityWrapper(::World* world, EntityID id) + : World(world) + , ID(id) + { } + + ::World* World; + EntityID ID; + + bool operator==(const EntityWrapper& e) + { + return (this->World == e.World) && (this->ID == e.ID); + } + operator EntityID() { return this->ID; } +}; + #endif \ No newline at end of file diff --git a/include/Game/PlayerMovementSystem.h b/include/Game/PlayerMovementSystem.h new file mode 100644 index 00000000..aa40b5f6 --- /dev/null +++ b/include/Game/PlayerMovementSystem.h @@ -0,0 +1,12 @@ +#include "Common.h" +#include "Core/System.h" + +class PlayerMovementSystem : public PureSystem +{ +public: + PlayerMovementSystem(EventBroker* eventBroker) + : PureSystem(eventBroker, "Player") + { } + + virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt); +}; \ No newline at end of file diff --git a/resources/Schema/Entities/CollisionTest1.xml b/resources/Schema/Entities/CollisionTest1.xml new file mode 100644 index 00000000..f0b310e1 --- /dev/null +++ b/resources/Schema/Entities/CollisionTest1.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + false + + + + + + + + + + + false + + + Models/Core/UnitCube.obj + + + + + + + + + + + diff --git a/resources/Schema/Entities/OctreeTest.xml b/resources/Schema/Entities/OctreeTest.xml index 819159d0..4d7fe709 100644 --- a/resources/Schema/Entities/OctreeTest.xml +++ b/resources/Schema/Entities/OctreeTest.xml @@ -15,7 +15,7 @@ - + @@ -23,38 +23,30 @@ - + + false + Models/Core/UnitCube.obj - + - - + + false + Models/Core/UnitCube.obj - - - - - - - - - Models/Core/UnitCube.obj - - - + diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index 1af09382..a1f24f59 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -199,6 +199,46 @@ bool RayVsModel(const Ray& ray, return hit; } +bool AABBvsTriangles(const AABB& box, const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix, glm::vec3& outResolutionVector) +{ + bool hit = false; + + const glm::vec3& origin = box.Origin(); + const glm::vec3& min = box.MinCorner(); + const glm::vec3& max = box.MaxCorner(); + + outResolutionVector.x = INFINITY; + + for (int i = 0; i < modelIndices.size(); ++i) { + glm::vec3 p = modelVertices[i].Position; + p = glm::vec3(modelMatrix * glm::vec4(p.x, p.y, p.z, 1)); + + float distFromOrigin = glm::abs(origin.x - p.x); + float penetration = box.HalfSize().x - distFromOrigin; + if (penetration > 0 && penetration < glm::abs(outResolutionVector.x)) { + if (p.x > origin.x) { + outResolutionVector.x = -penetration; + } else { + outResolutionVector.x = penetration; + } + hit = true; + } + //glm::vec3 pLocal = origin - p; + //for (int axis = 0; axis < 3; ++axis) { + // if (p[axis] < min[axis] || p[axis] > max[axis]) { + // continue; + // } + + // if (glm::abs(pLocal[axis]) < box.HalfSize()[axis]) { + // outResolutionVector[axis] = (glm::sign(pLocal[axis]) * box.HalfSize()[axis]) - pLocal[axis]; + // hit = true; + // } + //} + } + + return hit; +} + bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon) { const glm::vec3& ma1 = first.MaxCorner(); diff --git a/src/Engine/Collision/CollisionSystem.cpp b/src/Engine/Collision/CollisionSystem.cpp index 4ce514da..ec2f14a7 100644 --- a/src/Engine/Collision/CollisionSystem.cpp +++ b/src/Engine/Collision/CollisionSystem.cpp @@ -17,6 +17,7 @@ void CollisionSystem::UpdateComponent(World* world, ComponentWrapper& cAABB, dou return; } + // Collide against octree std::vector octreeResult; m_Octree->BoxesInSameRegion(*boundingBox, octreeResult); for (auto& boxB : octreeResult) { @@ -28,6 +29,28 @@ void CollisionSystem::UpdateComponent(World* world, ComponentWrapper& cAABB, dou (glm::vec3&)cTransform["Position"] += resolutionVector; } } + + // HACK: Temporarily collide against all collidable models since they're not in the octree yet + //auto otherCollidables = world->GetComponents("Model"); + //for (auto& cModel : *otherCollidables) { + // if (cModel.EntityID == entity) { + // continue; + // } + // if (!world->HasComponent(cModel.EntityID, "Collidable")) { + // continue; + // } + + // auto absPosition = RenderQueueFactory::AbsolutePosition(world, cModel.EntityID); + // auto absOrientation = RenderQueueFactory::AbsoluteOrientation(world, cModel.EntityID); + // auto absScale = RenderQueueFactory::AbsoluteScale(world, cModel.EntityID); + // glm::mat4 modelMatrix = glm::translate(absPosition); // *glm::toMat4(absOrientation) * glm::scale(absScale); + + // auto model = ResourceManager::Load(cModel["Resource"]); + // glm::vec3 resolutionVector; + // if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, resolutionVector)) { + // (glm::vec3&)cTransform["Position"] += resolutionVector; + // } + //} } bool CollisionSystem::OnKeyUp(const Events::KeyUp & event) diff --git a/src/Game/PlayerMovementSystem.cpp b/src/Game/PlayerMovementSystem.cpp new file mode 100644 index 00000000..f9ffac44 --- /dev/null +++ b/src/Game/PlayerMovementSystem.cpp @@ -0,0 +1,7 @@ +#include "RaptorCopterSystem.h" + +void PlayerMovementSystem::UpdateComponent(World* world, ComponentWrapper& player, double dt) +{ + +} + From c916185a7c6653d21ab00fce5295b72b6aab708b Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 14 Jan 2016 15:06:26 +0100 Subject: [PATCH 19/48] Does not crash when removing soundemitter component. --- src/Engine/Sound/SoundSystem.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index d21d1cd8..3c37d2b2 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -59,16 +59,16 @@ void SoundSystem::deleteInactiveEmitters() { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end();) { - if (m_World->ValidEntity((*it).first)) { + if (m_World->ValidEntity(it->first) + && m_World->HasComponent(it->first, "SoundEmitter")) { if (getSourceState(it->second->ALsource) != AL_STOPPED) { // Nothing to see here, move along it++; continue; } else { - // Sound has been stopped / finished playing + // Sound has been stopped / finished playing. And has correct component. alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); - delete it->second->SoundResource; m_World->DeleteEntity(it->first); it = m_Sources.erase(it); } @@ -77,7 +77,6 @@ void SoundSystem::deleteInactiveEmitters() stopSound((*it).second); alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); - delete it->second->SoundResource; it = m_Sources.erase(it); } } From b80ecc29d8d0b468f174f40bccb8cd85d602d544 Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 14 Jan 2016 15:32:53 +0100 Subject: [PATCH 20/48] Added comments and cleaned up in Game.cpp/h --- include/Game/Game.h | 8 ++------ src/Engine/Sound/SoundSystem.cpp | 6 ++++-- src/Game/Game.cpp | 10 +++------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/include/Game/Game.h b/include/Game/Game.h index eb854350..c8d48ed8 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -29,10 +29,6 @@ // Sound #include "Sound/SoundSystem.h" -//#include "Sound/EPlaySound.h" -//#include "Sound/EPlaySoundOnEntity.h" -//#include "Sound/EPlaySoundOnPosition.h" - class Game { @@ -65,8 +61,8 @@ private: // Sound SoundSystem* m_SoundSystem; - EventRelay m_EInputCommand; - bool debugOnInputCommand(const Events::InputCommand& e); + //EventRelay m_EInputCommand; + //bool debugOnInputCommand(const Events::InputCommand& e); void debugInitialize(); void debugTick(double dt); diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 3c37d2b2..1b7c5268 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -49,6 +49,7 @@ void SoundSystem::stopEmitters() void SoundSystem::Update() { + m_EventBroker->Process(); addNewEmitters(); // can be optimized with "EEntityCreated" deleteInactiveEmitters(); // can be optimized with "EEntityDeleted" updateEmitters(); @@ -66,14 +67,14 @@ void SoundSystem::deleteInactiveEmitters() it++; continue; } else { - // Sound has been stopped / finished playing. And has correct component. + // Sound has been stopped / finished playing. alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); m_World->DeleteEntity(it->first); it = m_Sources.erase(it); } } else { - // Entity has been removed + // Entity / Component has been removed stopSound((*it).second); alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); @@ -251,6 +252,7 @@ bool SoundSystem::OnSetSFXGain(const Events::SetSFXGain & e) void SoundSystem::setListenerOri(glm::vec3 ori) { + // Calculate forward and up vector. glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); forward = glm::rotateX(forward, ori.x); forward = glm::rotateY(forward, ori.y); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index c5af30a9..97c23862 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -77,7 +77,8 @@ Game::Game(int argc, char* argv[]) //boost::thread workerThread(&Game::networkFunction, this); networkFunction(); } - EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand); + + // Invoke sound system m_SoundSystem = new SoundSystem(m_World, m_EventBroker, m_Config->Get("Debug.EditorEnabled", false)); m_LastTime = glfwGetTime(); @@ -86,6 +87,7 @@ Game::Game(int argc, char* argv[]) Game::~Game() { delete m_SystemPipeline; + delete m_SoundSystem; delete m_World; delete m_FrameStack; delete m_InputProxy; @@ -122,7 +124,6 @@ void Game::Tick() debugTick(dt); m_Renderer->Update(dt); m_EventBroker->Process(); - m_EventBroker->Process(); m_SoundSystem->Update(); m_RenderQueueFactory->Update(m_World); GLERROR("Game::Tick m_RenderQueueFactory->Update"); @@ -132,11 +133,6 @@ void Game::Tick() m_EventBroker->Clear(); } -bool Game::debugOnInputCommand(const Events::InputCommand & e) -{ - return true; -} - void Game::debugTick(double dt) { m_EventBroker->Process(); From 28392def793e12597cfcf7387fd0d44e203bbefe Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 14 Jan 2016 15:42:55 +0100 Subject: [PATCH 21/48] Made string passing in World a little more effective --- include/Engine/Core/World.h | 10 +++++----- src/Engine/Core/World.cpp | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/Engine/Core/World.h b/include/Engine/Core/World.h index 0a121728..3c666ceb 100644 --- a/include/Engine/Core/World.h +++ b/include/Engine/Core/World.h @@ -21,15 +21,15 @@ public: // Register a component type and allocate space for it void RegisterComponent(ComponentInfo& ci); // Attach a component to an entity and fill it with default values - ComponentWrapper AttachComponent(EntityID entity, std::string componentType); + ComponentWrapper AttachComponent(EntityID entity, const std::string& componentType); // Check if an entity has a component - bool HasComponent(EntityID entity, std::string componentType) const; + bool HasComponent(EntityID entity, const std::string& componentType) const; // Get a component of an entity - ComponentWrapper GetComponent(EntityID entity, std::string componentType); + ComponentWrapper GetComponent(EntityID entity, const std::string& componentType); // Delete a component off an entity - void DeleteComponent(EntityID entity, std::string componentType); + void DeleteComponent(EntityID entity, const std::string& componentType); // Get all components of the specified type - const ComponentPool* GetComponents(std::string componentType); + const ComponentPool* GetComponents(const std::string& componentType); // Get entity parent EntityID GetParent(EntityID entity); // Change the parent of an entity diff --git a/src/Engine/Core/World.cpp b/src/Engine/Core/World.cpp index c94cb8b3..811de45f 100644 --- a/src/Engine/Core/World.cpp +++ b/src/Engine/Core/World.cpp @@ -66,7 +66,7 @@ void World::RegisterComponent(ComponentInfo& ci) } } -ComponentWrapper World::AttachComponent(EntityID entity, std::string componentType) +ComponentWrapper World::AttachComponent(EntityID entity, const std::string& componentType) { // TODO: Allocate dynamic pool if component isn't registered ComponentPool* pool = m_ComponentPools.at(componentType); @@ -80,26 +80,26 @@ ComponentWrapper World::AttachComponent(EntityID entity, std::string componentTy return c; } -bool World::HasComponent(EntityID entity, std::string componentType) const +bool World::HasComponent(EntityID entity, const std::string& componentType) const { ComponentPool* pool = m_ComponentPools.at(componentType); return pool->KnowsEntity(entity); } -ComponentWrapper World::GetComponent(EntityID entity, std::string componentType) +ComponentWrapper World::GetComponent(EntityID entity, const std::string& componentType) { ComponentPool* pool = m_ComponentPools.at(componentType); return pool->GetByEntity(entity); } -void World::DeleteComponent(EntityID entity, std::string componentType) +void World::DeleteComponent(EntityID entity, const std::string& componentType) { ComponentPool* pool = m_ComponentPools.at(componentType); ComponentWrapper c = pool->GetByEntity(entity); return pool->Delete(c); } -const ComponentPool* World::GetComponents(std::string componentType) +const ComponentPool* World::GetComponents(const std::string& componentType) { auto it = m_ComponentPools.find(componentType); return (it != m_ComponentPools.end()) ? it->second : nullptr; From d34470256b24273230bc32c9be63c567c197fe41 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 14 Jan 2016 15:43:19 +0100 Subject: [PATCH 22/48] EntityWrapper to uniquely identify entities and make using them a little easier --- .../Engine/Collision/CollidableOctreeSystem.h | 2 +- include/Engine/Collision/Collision.h | 3 +- include/Engine/Collision/CollisionSystem.h | 2 +- include/Engine/Collision/TriggerSystem.h | 2 +- include/Engine/Core/Entity.h | 17 ----------- include/Engine/Core/EntityWrapper.h | 25 +++++++++++++++++ include/Engine/Core/System.h | 3 +- include/Engine/Core/SystemPipeline.h | 2 +- include/Game/HealthSystem.h | 2 +- include/Game/PlayerMovementSystem.h | 2 +- include/Game/PlayerSystem.h | 2 +- include/Game/RaptorCopterSystem.h | 6 ++-- .../Collision/CollidableOctreeSystem.cpp | 10 +++---- src/Engine/Collision/Collision.cpp | 10 +++---- src/Engine/Collision/CollisionSystem.cpp | 7 ++--- src/Engine/Collision/TriggerSystem.cpp | 8 +++--- src/Engine/Core/EntityWrapper.cpp | 28 +++++++++++++++++++ src/Game/HealthSystem.cpp | 14 +++++----- src/Game/PlayerMovementSystem.cpp | 7 ++--- src/Game/PlayerSystem.cpp | 26 ++++++++--------- 20 files changed, 106 insertions(+), 72 deletions(-) create mode 100644 include/Engine/Core/EntityWrapper.h create mode 100644 src/Engine/Core/EntityWrapper.cpp diff --git a/include/Engine/Collision/CollidableOctreeSystem.h b/include/Engine/Collision/CollidableOctreeSystem.h index 760503e9..8fa1f0a4 100644 --- a/include/Engine/Collision/CollidableOctreeSystem.h +++ b/include/Engine/Collision/CollidableOctreeSystem.h @@ -15,7 +15,7 @@ public: { } virtual void Update(World* world, double dt) override; - virtual void UpdateComponent(World* world, ComponentWrapper& component, double dt) override; + virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override; private: Octree* m_Octree; diff --git a/include/Engine/Collision/Collision.h b/include/Engine/Collision/Collision.h index 2854d07b..4ea45a55 100644 --- a/include/Engine/Collision/Collision.h +++ b/include/Engine/Collision/Collision.h @@ -13,6 +13,7 @@ #include "../Rendering/RawModel.h" #include "../Rendering/RenderQueueFactory.h" #include "../Core/Entity.h" +#include "../Core/EntityWrapper.h" class World; struct ComponentWrapper; @@ -59,7 +60,7 @@ bool AABBVsAABB(const AABB& a, const AABB& b, glm::vec3& minimumTranslation); bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon = 0.0001f); // Calculates an absolute AABB from an entity AABB component -boost::optional EntityAbsoluteAABB(World* world, EntityID entity); +boost::optional EntityAbsoluteAABB(EntityWrapper& entity); } diff --git a/include/Engine/Collision/CollisionSystem.h b/include/Engine/Collision/CollisionSystem.h index b4e25072..ea6004d9 100644 --- a/include/Engine/Collision/CollisionSystem.h +++ b/include/Engine/Collision/CollisionSystem.h @@ -23,7 +23,7 @@ public: EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &CollisionSystem::OnKeyUp); } - virtual void UpdateComponent(World* world, ComponentWrapper& cAABB, double dt) override; + virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override; private: Octree* m_Octree; diff --git a/include/Engine/Collision/TriggerSystem.h b/include/Engine/Collision/TriggerSystem.h index 7856a09d..ee53ad9b 100644 --- a/include/Engine/Collision/TriggerSystem.h +++ b/include/Engine/Collision/TriggerSystem.h @@ -20,7 +20,7 @@ public: , m_Octree(octree) { } - virtual void UpdateComponent(World* world, ComponentWrapper& collision, double dt) override; + virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override; private: Octree* m_Octree; diff --git a/include/Engine/Core/Entity.h b/include/Engine/Core/Entity.h index c6730924..653ba650 100644 --- a/include/Engine/Core/Entity.h +++ b/include/Engine/Core/Entity.h @@ -4,22 +4,5 @@ typedef unsigned int EntityID; const static unsigned int EntityID_Invalid = -1; -class World; -struct EntityWrapper -{ - EntityWrapper(::World* world, EntityID id) - : World(world) - , ID(id) - { } - - ::World* World; - EntityID ID; - - bool operator==(const EntityWrapper& e) - { - return (this->World == e.World) && (this->ID == e.ID); - } - operator EntityID() { return this->ID; } -}; #endif \ No newline at end of file diff --git a/include/Engine/Core/EntityWrapper.h b/include/Engine/Core/EntityWrapper.h new file mode 100644 index 00000000..541dfe0d --- /dev/null +++ b/include/Engine/Core/EntityWrapper.h @@ -0,0 +1,25 @@ +#ifndef EntityWrapper_h__ +#define EntityWrapper_h__ + +#include +#include "ComponentWrapper.h" + +class World; +struct EntityWrapper +{ + EntityWrapper(::World* world, EntityID id) + : World(world) + , ID(id) + { } + + ::World* World; + EntityID ID; + + bool HasComponent(const std::string& componentName); + + ComponentWrapper operator[](const std::string& componentName); + bool operator==(const EntityWrapper& e); + explicit operator EntityID(); +}; + +#endif diff --git a/include/Engine/Core/System.h b/include/Engine/Core/System.h index cd8e9fc7..b7de9dc2 100644 --- a/include/Engine/Core/System.h +++ b/include/Engine/Core/System.h @@ -3,6 +3,7 @@ #include "EventBroker.h" #include "World.h" +#include "EntityWrapper.h" #include "ComponentWrapper.h" class System @@ -33,7 +34,7 @@ protected: const std::string m_ComponentType; - virtual void UpdateComponent(World* world, ComponentWrapper& component, double dt) = 0; + virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) = 0; }; class ImpureSystem : public virtual System diff --git a/include/Engine/Core/SystemPipeline.h b/include/Engine/Core/SystemPipeline.h index 79d4ace4..c0cd8ed6 100644 --- a/include/Engine/Core/SystemPipeline.h +++ b/include/Engine/Core/SystemPipeline.h @@ -68,7 +68,7 @@ public: } for (auto& component : *pool) { for (auto& system : systems) { - system->UpdateComponent(world, component, dt); + system->UpdateComponent(world, EntityWrapper(world, component.EntityID), component, dt); } } } diff --git a/include/Game/HealthSystem.h b/include/Game/HealthSystem.h index a836e797..234244c2 100644 --- a/include/Game/HealthSystem.h +++ b/include/Game/HealthSystem.h @@ -19,7 +19,7 @@ public: HealthSystem(EventBroker* eventBroker); //updatecomponent - virtual void UpdateComponent(World* world, ComponentWrapper& health, double dt) override; + virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override; private: //methods which will take care of specific events diff --git a/include/Game/PlayerMovementSystem.h b/include/Game/PlayerMovementSystem.h index aa40b5f6..35f8a22c 100644 --- a/include/Game/PlayerMovementSystem.h +++ b/include/Game/PlayerMovementSystem.h @@ -8,5 +8,5 @@ public: : PureSystem(eventBroker, "Player") { } - virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt); + virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt); }; \ No newline at end of file diff --git a/include/Game/PlayerSystem.h b/include/Game/PlayerSystem.h index 50492617..a74cbb9f 100644 --- a/include/Game/PlayerSystem.h +++ b/include/Game/PlayerSystem.h @@ -20,7 +20,7 @@ public: EVENT_SUBSCRIBE_MEMBER(m_ELeave, &PlayerSystem::OnLeave); } - virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt) override; + virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override; private: float m_Speed = 5; EventRelay m_EEnter; diff --git a/include/Game/RaptorCopterSystem.h b/include/Game/RaptorCopterSystem.h index 14b49f4d..57a8de86 100644 --- a/include/Game/RaptorCopterSystem.h +++ b/include/Game/RaptorCopterSystem.h @@ -9,9 +9,9 @@ public: , PureSystem("RaptorCopter") { } - virtual void UpdateComponent(World* world, ComponentWrapper& raptorCopter, double dt) override + virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override { - ComponentWrapper& transform = world->GetComponent(raptorCopter.EntityID, "Transform"); - (glm::vec3&)transform["Orientation"] += (float)(double)raptorCopter["Speed"] * (float)dt * (glm::vec3)raptorCopter["Axis"]; + ComponentWrapper& transform = world->GetComponent(component.EntityID, "Transform"); + (glm::vec3&)transform["Orientation"] += (float)(double)component["Speed"] * (float)dt * (glm::vec3)component["Axis"]; } }; \ No newline at end of file diff --git a/src/Engine/Collision/CollidableOctreeSystem.cpp b/src/Engine/Collision/CollidableOctreeSystem.cpp index c7fed0a0..62d742f5 100644 --- a/src/Engine/Collision/CollidableOctreeSystem.cpp +++ b/src/Engine/Collision/CollidableOctreeSystem.cpp @@ -5,16 +5,14 @@ void CollidableOctreeSystem::Update(World* world, double dt) m_Octree->ClearDynamicObjects(); } -void CollidableOctreeSystem::UpdateComponent(World* world, ComponentWrapper& cCollidable, double dt) +void CollidableOctreeSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) { - EntityID entity = cCollidable.EntityID; - - if (world->HasComponent(entity, "AABB")) { - boost::optional absoluteAABB = Collision::EntityAbsoluteAABB(world, entity); + if (entity.HasComponent("AABB")) { + boost::optional absoluteAABB = Collision::EntityAbsoluteAABB(entity); if (absoluteAABB) { m_Octree->AddDynamicObject(*absoluteAABB); } - } else if (world->HasComponent(entity, "Model")) { + } else if (entity.HasComponent("Model")) { // TODO: Derive AABB from model } } \ No newline at end of file diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index a1f24f59..92048e70 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -283,15 +283,15 @@ bool attachAABBComponentFromModel(World* world, EntityID id) return true; } -boost::optional EntityAbsoluteAABB(World* world, EntityID entity) +boost::optional EntityAbsoluteAABB(EntityWrapper& entity) { - if (!world->HasComponent(entity, "AABB")) { + if (!entity.HasComponent("AABB")) { return boost::none; } - ComponentWrapper& cAABB = world->GetComponent(entity, "AABB"); - glm::vec3 absPosition = RenderQueueFactory::AbsolutePosition(world, entity); - glm::vec3 absScale = RenderQueueFactory::AbsoluteScale(world, entity); + ComponentWrapper& cAABB = entity["AABB"]; + glm::vec3 absPosition = RenderQueueFactory::AbsolutePosition(entity.World, entity.ID); + glm::vec3 absScale = RenderQueueFactory::AbsoluteScale(entity.World, entity.ID); glm::vec3 origin = absPosition + (glm::vec3)cAABB["Origin"]; glm::vec3 size = (glm::vec3)cAABB["Size"] * absScale; return AABB::FromOriginSize(origin, size); diff --git a/src/Engine/Collision/CollisionSystem.cpp b/src/Engine/Collision/CollisionSystem.cpp index ec2f14a7..92b7f7db 100644 --- a/src/Engine/Collision/CollisionSystem.cpp +++ b/src/Engine/Collision/CollisionSystem.cpp @@ -2,14 +2,13 @@ #include "Collision/CollisionSystem.h" #include "Core/AABB.h" -void CollisionSystem::UpdateComponent(World* world, ComponentWrapper& cAABB, double dt) +void CollisionSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) { - EntityID entity = cAABB.EntityID; - boost::optional boundingBox = Collision::EntityAbsoluteAABB(world, entity); + boost::optional boundingBox = Collision::EntityAbsoluteAABB(entity); if (!boundingBox) { return; } - ComponentWrapper& cTransform = world->GetComponent(entity, "Transform"); + ComponentWrapper& cTransform = entity["Transform"]; AABB& boxA = *boundingBox; //Press 'Z' to enable/disable collision. diff --git a/src/Engine/Collision/TriggerSystem.cpp b/src/Engine/Collision/TriggerSystem.cpp index 2c8c7540..58d1e332 100644 --- a/src/Engine/Collision/TriggerSystem.cpp +++ b/src/Engine/Collision/TriggerSystem.cpp @@ -3,22 +3,22 @@ #include "Core/AABB.h" #include "Rendering/Model.h" -void TriggerSystem::UpdateComponent(World* world, ComponentWrapper& cTrigger, double dt) +void TriggerSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) { //Currently only players can trigger things. auto players = world->GetComponents("Player"); if (players == nullptr) { return; } - EntityID tId = cTrigger.EntityID; - boost::optional triggerBox = Collision::EntityAbsoluteAABB(world, tId); + EntityID tId = component.EntityID; + boost::optional triggerBox = Collision::EntityAbsoluteAABB(entity); //The trigger *should* have a bounding box, or something, to test against so it can be triggered. if (!triggerBox) { return; } for (auto& pc : *players) { EntityID pId = pc.EntityID; - boost::optional playerBox = Collision::EntityAbsoluteAABB(world, pId); + boost::optional playerBox = Collision::EntityAbsoluteAABB(EntityWrapper(world, pId)); //The player can't trigger anything without an AABB. if (!playerBox) { continue; diff --git a/src/Engine/Core/EntityWrapper.cpp b/src/Engine/Core/EntityWrapper.cpp new file mode 100644 index 00000000..9d15dd39 --- /dev/null +++ b/src/Engine/Core/EntityWrapper.cpp @@ -0,0 +1,28 @@ +#include "Core/EntityWrapper.h" +#include "Core/World.h" + +bool EntityWrapper::operator==(const EntityWrapper& e) +{ + return (this->World == e.World) && (this->ID == e.ID); +} + +bool EntityWrapper::HasComponent(const std::string& componentName) +{ + return World->HasComponent(ID, componentName); +} + +ComponentWrapper EntityWrapper::operator[](const std::string& componentName) +{ + if (World->HasComponent(ID, componentName)) { + return World->GetComponent(ID, componentName); + } else { + LOG_WARNING("EntityWrapper implicitly attached \"%s\" component to #%i as a result of a fetch request!", componentName.c_str(), ID); + return World->AttachComponent(ID, componentName); + } +} + +EntityWrapper::operator EntityID() +{ + return this->ID; +} + diff --git a/src/Game/HealthSystem.cpp b/src/Game/HealthSystem.cpp index 858355fc..7986db9d 100644 --- a/src/Game/HealthSystem.cpp +++ b/src/Game/HealthSystem.cpp @@ -10,24 +10,24 @@ HealthSystem::HealthSystem(EventBroker* eventBroker) EVENT_SUBSCRIBE_MEMBER(m_EPlayerHealthPickup, &HealthSystem::OnPlayerHealthPickup); } -void HealthSystem::UpdateComponent(World *world, ComponentWrapper &health, double dt) +void HealthSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) { //if entityID of health is 9 then the players ID is also 9 (player,health are connected to the same entity) - ComponentWrapper player = world->GetComponent(health.EntityID, "Player"); - double maxHealth = (double)health["MaxHealth"]; + ComponentWrapper player = world->GetComponent(component.EntityID, "Player"); + double maxHealth = (double)component["MaxHealth"]; //process the DeltaHealthVector and change the entitys health accordingly for (size_t i = m_DeltaHealthVector.size(); i > 0; i--) { auto deltaHP = m_DeltaHealthVector[i - 1]; //if we have a healthchange for the current player and health is greater than 0, then apply it - if (std::get<0>(deltaHP) == player.EntityID && (double)health["Health"] > 0.0f) { + if (std::get<0>(deltaHP) == player.EntityID && (double)component["Health"] > 0.0f) { //get the deltaHP value from the tuple and make sure you dont get more than maxHealth - double newHealth = std::min((double)health["Health"] + (double)std::get<1>(deltaHP), maxHealth); - health["Health"] = newHealth; + double newHealth = std::min((double)component["Health"] + (double)std::get<1>(deltaHP), maxHealth); + component["Health"] = newHealth; m_DeltaHealthVector.erase(m_DeltaHealthVector.begin() + i - 1); //check if health is <= 0 - if ((double)health["Health"] <= 0.0f) { + if ((double)component["Health"] <= 0.0f) { //publish death event Events::PlayerDeath e; e.PlayerID = player.EntityID; diff --git a/src/Game/PlayerMovementSystem.cpp b/src/Game/PlayerMovementSystem.cpp index f9ffac44..c0946134 100644 --- a/src/Game/PlayerMovementSystem.cpp +++ b/src/Game/PlayerMovementSystem.cpp @@ -1,7 +1,6 @@ #include "RaptorCopterSystem.h" -void PlayerMovementSystem::UpdateComponent(World* world, ComponentWrapper& player, double dt) +void PlayerMovementSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) { - -} - + ComponentWrapper& transform = world->GetComponent(component.EntityID); +} \ No newline at end of file diff --git a/src/Game/PlayerSystem.cpp b/src/Game/PlayerSystem.cpp index 17b9a7f5..2d4d83c6 100644 --- a/src/Game/PlayerSystem.cpp +++ b/src/Game/PlayerSystem.cpp @@ -1,25 +1,25 @@ #include "PlayerSystem.h" -void PlayerSystem::UpdateComponent(World * world, ComponentWrapper & player, double dt) +void PlayerSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) { - player["Velocity"] = glm::vec3(0.f, 0.f, 0.f); - if ((bool&)player["Forward"] == true) { - ((glm::vec3&)player["Velocity"]).z = m_Speed * float(dt) * -1; + component["Velocity"] = glm::vec3(0.f, 0.f, 0.f); + if ((bool&)component["Forward"] == true) { + ((glm::vec3&)component["Velocity"]).z = m_Speed * float(dt) * -1; } - if ((bool&)player["Left"] == true) { - ((glm::vec3&)player["Velocity"]).x = m_Speed * float(dt) * -1; + if ((bool&)component["Left"] == true) { + ((glm::vec3&)component["Velocity"]).x = m_Speed * float(dt) * -1; } - if ((bool&)player["Back"] == true) { - ((glm::vec3&)player["Velocity"]).z = m_Speed * float(dt); + if ((bool&)component["Back"] == true) { + ((glm::vec3&)component["Velocity"]).z = m_Speed * float(dt); } - if ((bool&)player["Right"] == true) { - ((glm::vec3&)player["Velocity"]).x = m_Speed * float(dt); + if ((bool&)component["Right"] == true) { + ((glm::vec3&)component["Velocity"]).x = m_Speed * float(dt); } - if ((glm::vec3)player["Velocity"] != glm::vec3(0.f)) { - ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform"); - (glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"]; + if ((glm::vec3)component["Velocity"] != glm::vec3(0.f)) { + ComponentWrapper& transform = world->GetComponent(component.EntityID, "Transform"); + (glm::vec3&)transform["Position"] += (glm::vec3)component["Velocity"]; } } From 71047b08ec5d4c9b986d49c5533aa491f19581ef Mon Sep 17 00:00:00 2001 From: William Moberg Date: Thu, 14 Jan 2016 16:27:45 +0100 Subject: [PATCH 23/48] Async load works, using exceptions for special resources. --- include/Engine/Core/ResourceManager.h | 177 +++++++++----------- include/Engine/Rendering/BaseTexture.h | 2 +- include/Engine/Rendering/Model.h | 7 +- include/Engine/Rendering/RawModel.h | 4 +- include/Engine/Rendering/Texture.h | 3 - src/Engine/Collision/Collision.cpp | 10 +- src/Engine/Core/ResourceManager.cpp | 3 + src/Engine/Rendering/Model.cpp | 27 ++- src/Engine/Rendering/RawModel.cpp | 28 +--- src/Engine/Rendering/RenderQueueFactory.cpp | 6 +- src/Engine/Rendering/Renderer.cpp | 4 +- src/Engine/Rendering/Texture.cpp | 69 ++++---- src/Game/Game.cpp | 1 + 13 files changed, 160 insertions(+), 181 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index 32122092..9ebf545a 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -12,33 +12,13 @@ /** Base Resource class. Implement this class for every resource to be handled by the resource manager. - - If it should be possible to load the resource asyncronously (on a separate thread in the background), - using LoadAsync() then any necessary OpenGL calls (Eg. glGenBuffers(), glBindBuffer(), etc.) must be - made in GlCommands() method, and not inside the constructor (see Texture.cpp for an example). */ class Resource { friend class ResourceManager; -private: - bool m_FullyConstructed; - protected: - Resource() : m_FullyConstructed(false) { } - //This method only needs to be overridden if: - // 1: a) It should be possible to load the resource with LoadAsync(), or - // b) This resource will be loaded inside the constructor of another resource that can be loaded with LoadAsync(). - // c) Same as above but recursively. - // 2: a) The resource needs to make OpenGL calls, or - // b) The resource contains a resource that makes OGL calls, or - // c) The resource contains a resource that contains ... ... a resource that makes OGL calls, or - // - //If 2.a: any calls should be made in the GlCommands. - //If 2.b or 2.c: PostCtorGLCommands() should be called on the contained resource(s). - //If GlCommands needs to be overridden and GlCommands is also overridden by a baseclass then the baseclass - //implementation should be called in from the derived class implementation (see Model.cpp for an example). - virtual void GlCommands() { } + Resource() { } public: // Pretend that this is a pure virtual function that you have to implement @@ -47,21 +27,30 @@ public: virtual void Reload() { } virtual void OnChildReloaded(Resource* child) { } - //If this resource implements GlCommands and it is loaded in another resource, - //then the containing resource must also implement GlCommands and - //call this method in it (see RawModel.cpp for an example). - void PostCtorGLCommands() - { - if (!m_FullyConstructed) { - GlCommands(); - } - m_FullyConstructed = true; - } unsigned int TypeID; unsigned int ResourceID; }; +//Any class inheriting from this class will always be loaded on the master thread, not on a parallel worker thread. +//This is important in case some instructions must be executed on the main thread, e.g. OpenGL commands, like glBindBuffer. +//This resource can still be loaded asyncronously, but it will not be loaded in a thread, instead it's constructor will +//be called once on every ResourceManager::Load, just throw StillLoadingException in the constructor if it is not done yet. +class ThreadUnsafeResource : public Resource +{ + friend class ResourceManager; +protected: + //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. + //Not actually an error, just a message to the ResourceManager. + struct StillLoadingException : public std::exception + { + virtual const char* what() const throw() + { + return "Resource is still loading."; + } + }; +}; + /** Singleton resource manager to keep track of and cache any external engine assets */ class ResourceManager { @@ -85,24 +74,20 @@ public: */ // TODO: Templateify static bool IsResourceLoaded(std::string resourceType, std::string resourceName); - - /** If the resource is not in cache, starts loading the resource and returns nullptr immediately. + + /** If Async is false: Hot-loads a resource and caches it for future use. + Fairly safe to assume that return value is always a valid pointer, will only return nullptr on error. + + If Async is true: If the resource is not loaded yet, starts loading the resource + in the background and returns nullptr immediately. If the resource has been loaded already, return a pointer to it. - @tparam T Resource type. - @param resourceName Fully qualified name of the resource to load. - */ - template - static T* LoadAsync(std::string resourceName, Resource* parent = nullptr); - - /** Hot-loads a resource and caches it for future use. - Fairly safe to assume that return value is a valid pointer, will only return nullptr on error. - @tparam T Resource type. + @tparam Async Set this to true if the resource should be loaded asyncronously. @param resourceName Fully qualified name of the resource to load. */ - template - static T* Load(std::string resourceName, Resource* parent = nullptr); + template + static T* Load(std::string resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -116,6 +101,22 @@ public: static void Update(); private: + //Represents a pointer value to signify that a resource haven't failed, but is not fully loaded. + class SpecialResourcePointer + { + public: + SpecialResourcePointer() + : m_Val(new Resource()) + {} + ~SpecialResourcePointer() + { + delete m_Val; + } + //Make this class implicitly convertible to the Resource*. + operator Resource* const() const { return m_Val; } + private: + Resource* const m_Val; + }; //This is a haxy way to make sure that IsMainThread is run when the program starts, so the master thread id is set. struct MasterThreadChecker { @@ -124,7 +125,8 @@ private: ResourceManager::IsMainThread(); } }; - static MasterThreadChecker m_Checker; + const static SpecialResourcePointer m_StillLoading; + const static MasterThreadChecker m_Checker; static std::unordered_map m_CompilerTypenameToResourceType; static std::unordered_map> m_FactoryFunctions; // type -> factory function @@ -151,23 +153,8 @@ private: static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent); static bool IsMainThread(); - template - static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); }; -template -T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) -{ - auto resourceTypename = typeid(T).name(); - auto it = m_CompilerTypenameToResourceType.find(resourceTypename); - if (it == m_CompilerTypenameToResourceType.end()) { - LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return nullptr; - } - - return static_cast(Load(it->second, resourceName, parent)); -} - template void ResourceManager::RegisterType(std::string typeName) { @@ -175,28 +162,29 @@ void ResourceManager::RegisterType(std::string typeName) m_FactoryFunctions[typeName] = [](std::string resourceName) { return new T(resourceName); }; } -template -T* ResourceManager::LoadAsync(std::string resourceName, Resource* parent /* = nullptr */) +template +static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) { - auto resourceTypename = typeid(T).name(); - auto it = m_CompilerTypenameToResourceType.find(resourceTypename); - if (it == m_CompilerTypenameToResourceType.end()) { - LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return nullptr; - } + auto resourceTypename = typeid(T).name(); + auto iter = m_CompilerTypenameToResourceType.find(resourceTypename); + if (iter == m_CompilerTypenameToResourceType.end()) { + LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); + return nullptr; + } - return static_cast(Load(it->second, resourceName, parent)); -} + std::string resourceType = iter->second; + constexpr bool mustNotLoadInThread = std::is_base_of::value; + if (mustNotLoadInThread && !IsMainThread()) { + LOG_ERROR("Failed to Load \"%s\": ThreadUnsafeResource type \"%s\" load in the constructor of another resource that is loaded asyncronously.", resourceName.c_str(), iter->second.c_str()); + return nullptr; + } -template -static Resource* ResourceManager::Load(std::string resourceType, std::string resourceName, Resource* parent /* = nullptr */) -{ auto cacheKey = std::make_pair(resourceType, resourceName); decltype(m_ResourceCache)::iterator it; //If a thread has already been launched to load this resource. auto tIt = m_LoadingThreads.find(cacheKey); if (tIt != m_LoadingThreads.end()) { - if (Async) { + if (async) { //Return null if the thread is still working. if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { return nullptr; @@ -211,38 +199,39 @@ static Resource* ResourceManager::Load(std::string resourceType, std::string res //Find the resource that the thread loaded. it = m_ResourceCache.find(cacheKey); if (it != m_ResourceCache.end()) { - //At this point the resource may not be completely - //done since the worker threads cannot do opengl commands. - if (IsMainThread()) { - //Do the gl commands to complete the resource if we are not a worker thread. - it->second->PostCtorGLCommands(); - } - return it->second; + //Threads should not be able to throw StillLoadingException, so no check should be needed. + return static_cast(it->second); } else { - //If resource is still null at cacheKey after thread finishes, it failed. + //If cacheKey does not exist after thread finishes, it failed. return nullptr; } } - //If resource has already been loaded and cached. + //If resource has already been cached and completely loaded. it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - return it->second; + if (it != m_ResourceCache.end() && it->second != m_StillLoading) { + return static_cast(it->second); } + Resource* res = nullptr; //If resource is not cached.. - if (Async) { - //Create a thread that loads the resource into cache. - m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + if (async) { + if (mustNotLoadInThread) { + res = createResource(resourceType, resourceName, parent); + if (res != m_StillLoading) { + return static_cast(res); + } + } else { + //Create a thread that loads the resource into cache. + m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + } return nullptr; } else { //load and return the resource. - Resource* res = createResource(resourceType, resourceName, parent); - if (IsMainThread() && res != nullptr) { - //Do the gl commands to complete the resource if we are not a worker thread. - res->PostCtorGLCommands(); - } - return res; + do { + res = createResource(resourceType, resourceName, parent); + } while (res == m_StillLoading); + return static_cast(res); } } diff --git a/include/Engine/Rendering/BaseTexture.h b/include/Engine/Rendering/BaseTexture.h index 26ae399d..96df5561 100644 --- a/include/Engine/Rendering/BaseTexture.h +++ b/include/Engine/Rendering/BaseTexture.h @@ -3,7 +3,7 @@ #include "../Core/ResourceManager.h" -class BaseTexture : public Resource +class BaseTexture : public ThreadUnsafeResource { friend class ResourceManager; diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index ffef745c..77e3df76 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -4,21 +4,24 @@ #include "RawModel.h" #include "../OpenGL.h" -class Model : public RawModel +class Model : public ThreadUnsafeResource { friend class ResourceManager; private: Model(std::string fileName); - virtual void GlCommands() override; public: ~Model(); + const std::vector& TextureGroups() const { return m_RawModel->TextureGroups; } + const glm::mat4& Matrix() const { return m_RawModel->m_Matrix; } + const std::vector& Vertices() const { return m_RawModel->m_Vertices; } GLuint VAO; GLuint ElementBuffer; private: + RawModel* m_RawModel; GLuint VertexBuffer; GLuint DiffuseVertexColorBuffer; GLuint SpecularVertexColorBuffer; diff --git a/include/Engine/Rendering/RawModel.h b/include/Engine/Rendering/RawModel.h index 379b81ab..82619c86 100644 --- a/include/Engine/Rendering/RawModel.h +++ b/include/Engine/Rendering/RawModel.h @@ -24,7 +24,6 @@ class RawModel : public Resource protected: RawModel(std::string fileName); - virtual void GlCommands() override; public: ~RawModel(); @@ -47,8 +46,11 @@ public: struct MaterialGroup { float Shininess; + std::string TexturePath; std::shared_ptr<::Texture> Texture; + std::string NormalMapPath; std::shared_ptr<::Texture> NormalMap; + std::string SpecularMapPath; std::shared_ptr<::Texture> SpecularMap; unsigned int StartIndex; unsigned int EndIndex; diff --git a/include/Engine/Rendering/Texture.h b/include/Engine/Rendering/Texture.h index 8cdcaef0..16892afc 100644 --- a/include/Engine/Rendering/Texture.h +++ b/include/Engine/Rendering/Texture.h @@ -11,10 +11,7 @@ class Texture : public BaseTexture private: Texture(std::string path); - virtual void GlCommands() override; - GLint m_Format; - Image* m_Image; public: ~Texture(); diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index 7473c67f..7abc63ea 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -220,16 +220,16 @@ bool attachAABBComponentFromModel(World* world, EntityID id) } ComponentWrapper model = world->GetComponent(id, "Model"); ComponentWrapper collision = world->AttachComponent(id, "AABB"); - Model* modelRes = ResourceManager::LoadAsync(model["Resource"]); + Model* modelRes = ResourceManager::Load(model["Resource"]); if (modelRes == nullptr) { return false; } - glm::mat4 modelMatrix = modelRes->m_Matrix; + glm::mat4 modelMatrix = modelRes->Matrix(); glm::vec3 mini = glm::vec3(INFINITY, INFINITY, INFINITY); glm::vec3 maxi = glm::vec3(-INFINITY, -INFINITY, -INFINITY); - for (const auto& v : modelRes->m_Vertices) { + for (const auto& v : modelRes->Vertices()) { const auto& wPos = modelMatrix * glm::vec4(v.Position.x, v.Position.y, v.Position.z, 1); maxi.x = std::max(wPos.x, maxi.x); maxi.y = std::max(wPos.y, maxi.y); @@ -247,7 +247,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) { ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform"); ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model"); - Model* modelRes = ResourceManager::LoadAsync(model["Resource"]); + Model* modelRes = ResourceManager::Load(model["Resource"]); outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]); glm::vec3 mini = outBox.MinCorner(); glm::vec3 maxi = outBox.MaxCorner(); @@ -255,7 +255,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) if (modelRes == nullptr) { return false; } - glm::mat4 modelMatrix = modelRes->m_Matrix * + glm::mat4 modelMatrix = modelRes->Matrix() * glm::translate(glm::mat4(), (glm::vec3)cTrans["Position"]) * glm::scale((glm::vec3)cTrans["Scale"]); diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index 102e99ba..ffd6c76c 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -14,6 +14,7 @@ std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; std::unordered_map, boost::thread> ResourceManager::m_LoadingThreads; boost::recursive_mutex ResourceManager::m_Mutex; +const ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading; unsigned int ResourceManager::GetTypeID(std::string resourceType) { @@ -92,6 +93,8 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string Resource* resource = nullptr; try { resource = facIt->second(resourceName); + } catch (const ThreadUnsafeResource::StillLoadingException&) { + resource = m_StillLoading; } catch (const std::exception& e) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); } diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index c2aab44f..8e24b704 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -1,24 +1,35 @@ #include "Rendering/Model.h" Model::Model(std::string fileName) - : RawModel(fileName) { -} + //Load the RawModel asyncronously, this will be done on a separate thread in the background. + m_RawModel = ResourceManager::Load(fileName); + //If it is null, the model is not done yet, so tell resourceManager to try constructing me again later. + if (m_RawModel == nullptr) { + throw StillLoadingException(); + } -void Model::GlCommands() -{ - //Call the base class method. - RawModel::GlCommands(); + for (auto& group : m_RawModel->TextureGroups) { + if (!group.TexturePath.empty()) { + group.Texture = std::shared_ptr(ResourceManager::Load(group.TexturePath)); + } + if (!group.NormalMapPath.empty()) { + group.NormalMap = std::shared_ptr(ResourceManager::Load(group.NormalMapPath)); + } + if (!group.SpecularMapPath.empty()) { + group.SpecularMap = std::shared_ptr(ResourceManager::Load(group.SpecularMapPath)); + } + } // Generate GL buffers GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); - glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(Vertex), &m_Vertices[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, m_RawModel->m_Vertices.size() * sizeof(RawModel::Vertex), &m_RawModel->m_Vertices[0], GL_STATIC_DRAW); glGenBuffers(1, &ElementBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Indices.size() * sizeof(unsigned int), &m_Indices[0], GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_RawModel->m_Indices.size() * sizeof(unsigned int), &m_RawModel->m_Indices[0], GL_STATIC_DRAW); glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); diff --git a/src/Engine/Rendering/RawModel.cpp b/src/Engine/Rendering/RawModel.cpp index 9428f21e..fbb406f6 100644 --- a/src/Engine/Rendering/RawModel.cpp +++ b/src/Engine/Rendering/RawModel.cpp @@ -141,9 +141,7 @@ RawModel::RawModel(std::string fileName) aiString path; aiTextureMapping mapping; material->GetTexture(aiTextureType_DIFFUSE, 0, &path, &mapping); - std::string absolutePath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); - //LOG_DEBUG("Diffuse texture: %s", absolutePath.c_str()); - matGroup.Texture = std::shared_ptr(ResourceManager::Load(absolutePath)); + matGroup.TexturePath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); } // Normal map //LOG_DEBUG("%i normal maps found", material->GetTextureCount(aiTextureType_HEIGHT)); @@ -151,9 +149,7 @@ RawModel::RawModel(std::string fileName) aiString path; aiTextureMapping mapping; material->GetTexture(aiTextureType_HEIGHT, 0, &path, &mapping); - std::string absolutePath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); - //LOG_DEBUG("Normal map: %s", absolutePath.c_str()); - matGroup.NormalMap = std::shared_ptr(ResourceManager::Load(absolutePath)); + matGroup.NormalMapPath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); } // Specular map //LOG_DEBUG("%i specular maps found", material->GetTextureCount(aiTextureType_SPECULAR)); @@ -161,9 +157,7 @@ RawModel::RawModel(std::string fileName) aiString path; aiTextureMapping mapping; material->GetTexture(aiTextureType_SPECULAR, 0, &path, &mapping); - std::string absolutePath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); - //LOG_DEBUG("Specular map: %s", absolutePath.c_str()); - matGroup.SpecularMap = std::shared_ptr(ResourceManager::Load(absolutePath)); + matGroup.SpecularMapPath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); } TextureGroups.push_back(matGroup); @@ -292,22 +286,6 @@ RawModel::RawModel(std::string fileName) } } -void RawModel::GlCommands() -{ - //Since RawModel contains Textures that was loaded in the constructor, their GlCommands must be run. - for (auto& texGroup : TextureGroups) { - if (texGroup.Texture) { - texGroup.Texture->PostCtorGLCommands(); - } - if (texGroup.NormalMap) { - texGroup.NormalMap->PostCtorGLCommands(); - } - if (texGroup.SpecularMap) { - texGroup.SpecularMap->PostCtorGLCommands(); - } - } -} - RawModel::~RawModel() { if (m_Skeleton) { diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 71b2e086..66eacd67 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -84,12 +84,12 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) continue; } glm::vec4 color = modelC["Color"]; - Model* model = ResourceManager::LoadAsync(resource); + Model* model = ResourceManager::Load(resource); if (model == nullptr) { model = ResourceManager::Load("Models/Core/Error.obj"); } - for (auto texGroup : model->TextureGroups) { + for (auto texGroup : model->TextureGroups()) { ModelJob job; job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; job.DiffuseTexture = texGroup.Texture.get(); @@ -98,7 +98,7 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) job.Model = model; job.StartIndex = texGroup.StartIndex; job.EndIndex = texGroup.EndIndex; - job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); + job.ModelMatrix = model->Matrix() * ModelMatrix(world, modelC.EntityID); job.Color = color; //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 208f339b..d0788b94 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -161,8 +161,8 @@ void Renderer::DrawScreenQuad(GLuint textureToDraw) glBindVertexArray(m_ScreenQuad->VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer); - glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->TextureGroups[0].EndIndex - m_ScreenQuad->TextureGroups[0].StartIndex +1 - , GL_UNSIGNED_INT, 0, m_ScreenQuad->TextureGroups[0].StartIndex); + glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->TextureGroups()[0].EndIndex - m_ScreenQuad->TextureGroups()[0].StartIndex +1 + , GL_UNSIGNED_INT, 0, m_ScreenQuad->TextureGroups()[0].StartIndex); } void Renderer::InitializeTextures() diff --git a/src/Engine/Rendering/Texture.cpp b/src/Engine/Rendering/Texture.cpp index 7e1cd65b..57f3ca36 100644 --- a/src/Engine/Rendering/Texture.cpp +++ b/src/Engine/Rendering/Texture.cpp @@ -2,53 +2,48 @@ Texture::Texture(std::string path) { - m_Image = new PNG(path); + PNG image(path); - if (m_Image->Width == 0 && m_Image->Height == 0 || m_Image->Format == Image::ImageFormat::Unknown) { - delete m_Image; - m_Image = new PNG("Textures/Core/ErrorTexture.png"); - if (m_Image->Width == 0 && m_Image->Height == 0 || m_Image->Format == Image::ImageFormat::Unknown) { - LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed."); - return; - } - } + if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { + image = PNG("Textures/Core/ErrorTexture.png"); + if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { + LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed."); + return; + } + } - this->Width = m_Image->Width; - this->Height = m_Image->Height; + this->Width = image.Width; + this->Height = image.Height; - switch (m_Image->Format) { - case Image::ImageFormat::RGB: - m_Format = GL_RGB; - break; - case Image::ImageFormat::RGBA: - m_Format = GL_RGBA; - break; - } -} + GLint format; + switch (image.Format) { + case Image::ImageFormat::RGB: + format = GL_RGB; + break; + case Image::ImageFormat::RGBA: + format = GL_RGBA; + break; + } -void Texture::GlCommands() -{ - // Construct the OpenGL texture - glGenTextures(1, &m_Texture); - glBindTexture(GL_TEXTURE_2D, m_Texture); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, m_Format, Width, Height, 0, m_Format, GL_UNSIGNED_BYTE, m_Image->Data); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - GLERROR("Texture load"); - delete m_Image; - m_Image = nullptr; + // Construct the OpenGL texture + glGenTextures(1, &m_Texture); + glBindTexture(GL_TEXTURE_2D, m_Texture); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, 0, format, image.Width, image.Height, 0, format, GL_UNSIGNED_BYTE, image.Data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + GLERROR("Texture load"); } Texture::~Texture() { - glDeleteTextures(1, &m_Texture); + glDeleteTextures(1, &m_Texture); } void Texture::Bind(GLenum textureUnit /* = GL_TEXTURE0 */) { - glActiveTexture(textureUnit); - glBindTexture(GL_TEXTURE_2D, m_Texture); + glActiveTexture(textureUnit); + glBindTexture(GL_TEXTURE_2D, m_Texture); } diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 033db642..b6c8b21e 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -6,6 +6,7 @@ Game::Game(int argc, char* argv[]) { ResourceManager::RegisterType("ConfigFile"); ResourceManager::RegisterType("Model"); + ResourceManager::RegisterType("RawModel"); ResourceManager::RegisterType("Texture"); ResourceManager::RegisterType("EntityXMLFile"); ResourceManager::RegisterType("ShaderProgram"); From 99406c32cf990aadb864944adf5706a245591130 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 14 Jan 2016 17:01:39 +0100 Subject: [PATCH 24/48] Better error handling for EntityFilePreprocessor --- src/Engine/Core/EntityFilePreprocessor.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Engine/Core/EntityFilePreprocessor.cpp b/src/Engine/Core/EntityFilePreprocessor.cpp index ca445042..4b720976 100644 --- a/src/Engine/Core/EntityFilePreprocessor.cpp +++ b/src/Engine/Core/EntityFilePreprocessor.cpp @@ -103,21 +103,25 @@ void EntityFilePreprocessor::parseComponentInfo() } } } else { - LOG_WARNING("Component is missing an annotation!"); + LOG_WARNING("Component \"%s\" is missing an annotation!", compInfo.Name.c_str()); } // auto typeDefinition = element->getTypeDefinition(); + // Allow empty components + if (typeDefinition == nullptr) { + continue; + } if (typeDefinition->getTypeCategory() != XSTypeDefinition::COMPLEX_TYPE) { - LOG_ERROR("Type definition wasn't COMPLEX_TYPE! Skipping."); + LOG_ERROR("Failed to parse component definition for \"%s\": Type definition wasn't COMPLEX_TYPE!", compInfo.Name.c_str()); continue; } auto complexTypeDefinition = dynamic_cast(typeDefinition); // auto modelGroupParticle = complexTypeDefinition->getParticle(); - if (modelGroupParticle->getTermType() != XSParticle::TERM_MODELGROUP) { - LOG_ERROR("Model group particle wasn't TERM_MODELGROUP! Skipping."); + if (modelGroupParticle == nullptr || modelGroupParticle->getTermType() != XSParticle::TERM_MODELGROUP) { + LOG_ERROR("Failed to parse component definition for \"%s\": Model group particle was null or wasn't TERM_MODELGROUP!", compInfo.Name.c_str()); continue; } auto modelGroup = modelGroupParticle->getModelGroupTerm(); @@ -129,7 +133,7 @@ void EntityFilePreprocessor::parseComponentInfo() for (unsigned int i = 0; i < particles->size(); ++i) { auto particle = particles->elementAt(i); if (particle->getTermType() != XSParticle::TERM_ELEMENT) { - LOG_ERROR("Particle wasn't TERM_ELEMENT! Skipping."); + LOG_ERROR("Failed to parse a field definition in component \"%s\": Particle wasn't TERM_ELEMENT! Skipping.", compInfo.Name.c_str()); continue; } auto elementDeclaration = particle->getElementTerm(); @@ -139,7 +143,7 @@ void EntityFilePreprocessor::parseComponentInfo() size_t stride = EntityFile::GetTypeStride(type); if (stride == 0) { - std::cout << "Warning: Field \"" << name << "\" in component \"" << compInfo.Name << "\" uses unexpected field type \"" << type << "\". Skipping." << std::endl; + LOG_WARNING("Field \"%s\" in component \"%s\" uses unexpected field type \"%s\". Skipping.", name.c_str(), compInfo.Name.c_str(), type.c_str()); continue; } From 986af010918548b3ec8dd5c5110ea28a8ffa6d8f Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 14 Jan 2016 17:02:13 +0100 Subject: [PATCH 25/48] Fixed editor component fields with the same name across components being treated as the same value --- src/Engine/Editor/EditorSystem.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 6d3dff3d..b5e527f0 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -492,7 +492,8 @@ void EditorSystem::drawUI(World* world, double dt) const std::string& fieldName = kv.first; auto& field = kv.second; - ImGui::PushID(fieldName.c_str()); + std::string uniqueID = componentType + fieldName; + ImGui::PushID(uniqueID.c_str()); if (field.Type == "Vector") { auto& val = component.Property(fieldName); if (fieldName == "Scale") { From 6ad1839def3f0fbc40250e445714294fd0e8b1bd Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 14 Jan 2016 17:03:42 +0100 Subject: [PATCH 26/48] Initial work on PlayerMovementSystem --- assets | 2 +- include/Game/Game.h | 2 - include/Game/{ => Systems}/HealthSystem.h | 6 +- .../Game/{ => Systems}/PlayerMovementSystem.h | 4 +- include/Game/{ => Systems}/PlayerSystem.h | 0 .../Game/{ => Systems}/RaptorCopterSystem.h | 0 resources/Schema/Components.xsd | 1 + resources/Schema/Components/Collidable.xml | 1 - resources/Schema/Components/Collidable.xsd | 5 -- resources/Schema/Components/Physics.xml | 3 + resources/Schema/Components/Physics.xsd | 16 +++++ resources/Schema/Components/Transform.xsd | 2 +- resources/Schema/Entities/MovementTest.xml | 64 +++++++++++++++++++ resources/Schema/Entities/Player.xml | 17 +++++ resources/Schema/Types/Entity.xsd | 1 + src/Engine/Collision/CollisionSystem.cpp | 10 ++- src/Game/CMakeLists.txt | 9 ++- src/Game/Game.cpp | 6 +- src/Game/PlayerMovementSystem.cpp | 6 -- src/Game/{ => Systems}/HealthSystem.cpp | 3 +- src/Game/Systems/PlayerMovementSystem.cpp | 16 +++++ src/Game/{ => Systems}/PlayerSystem.cpp | 2 +- 22 files changed, 146 insertions(+), 30 deletions(-) rename include/Game/{ => Systems}/HealthSystem.h (89%) rename include/Game/{ => Systems}/PlayerMovementSystem.h (77%) rename include/Game/{ => Systems}/PlayerSystem.h (100%) rename include/Game/{ => Systems}/RaptorCopterSystem.h (100%) create mode 100644 resources/Schema/Components/Physics.xml create mode 100644 resources/Schema/Components/Physics.xsd create mode 100644 resources/Schema/Entities/MovementTest.xml create mode 100644 resources/Schema/Entities/Player.xml delete mode 100644 src/Game/PlayerMovementSystem.cpp rename src/Game/{ => Systems}/HealthSystem.cpp (98%) create mode 100644 src/Game/Systems/PlayerMovementSystem.cpp rename src/Game/{ => Systems}/PlayerSystem.cpp (97%) diff --git a/assets b/assets index c8e631f4..a0d1615e 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit c8e631f449515cdbe3647b96ce472839748e28f9 +Subproject commit a0d1615e515a4d7db0073cc987d1d593ca942471 diff --git a/include/Game/Game.h b/include/Game/Game.h index de45c558..728070f3 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -15,8 +15,6 @@ #include "Core/EKeyDown.h" #include "Core/EntityFilePreprocessor.h" #include "Core/SystemPipeline.h" -#include "RaptorCopterSystem.h" -#include "PlayerSystem.h" #include "Editor/EditorSystem.h" #include "Core/EntityFile.h" #include "Core/EntityFileParser.h" diff --git a/include/Game/HealthSystem.h b/include/Game/Systems/HealthSystem.h similarity index 89% rename from include/Game/HealthSystem.h rename to include/Game/Systems/HealthSystem.h index 234244c2..0db3ec41 100644 --- a/include/Game/HealthSystem.h +++ b/include/Game/Systems/HealthSystem.h @@ -6,9 +6,9 @@ #include "Common.h" #include "Core/System.h" -#include "Core\EPlayerDamage.h"; -#include "Core\EPlayerHealthPickup.h"; -#include "Core\EPlayerDeath.h"; +#include "Core/EPlayerDamage.h" +#include "Core/EPlayerHealthPickup.h" +#include "Core/EPlayerDeath.h" #include #include diff --git a/include/Game/PlayerMovementSystem.h b/include/Game/Systems/PlayerMovementSystem.h similarity index 77% rename from include/Game/PlayerMovementSystem.h rename to include/Game/Systems/PlayerMovementSystem.h index 35f8a22c..6dc2dc31 100644 --- a/include/Game/PlayerMovementSystem.h +++ b/include/Game/Systems/PlayerMovementSystem.h @@ -1,11 +1,13 @@ #include "Common.h" +#include "GLM.h" #include "Core/System.h" class PlayerMovementSystem : public PureSystem { public: PlayerMovementSystem(EventBroker* eventBroker) - : PureSystem(eventBroker, "Player") + : System(eventBroker) + , PureSystem("Player") { } virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt); diff --git a/include/Game/PlayerSystem.h b/include/Game/Systems/PlayerSystem.h similarity index 100% rename from include/Game/PlayerSystem.h rename to include/Game/Systems/PlayerSystem.h diff --git a/include/Game/RaptorCopterSystem.h b/include/Game/Systems/RaptorCopterSystem.h similarity index 100% rename from include/Game/RaptorCopterSystem.h rename to include/Game/Systems/RaptorCopterSystem.h diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 4f32b477..e1440b69 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -2,6 +2,7 @@ + diff --git a/resources/Schema/Components/Collidable.xml b/resources/Schema/Components/Collidable.xml index 0d1bb9b3..4f9c9033 100644 --- a/resources/Schema/Components/Collidable.xml +++ b/resources/Schema/Components/Collidable.xml @@ -1,3 +1,2 @@ - true \ No newline at end of file diff --git a/resources/Schema/Components/Collidable.xsd b/resources/Schema/Components/Collidable.xsd index f835d9a6..84c66f11 100644 --- a/resources/Schema/Components/Collidable.xsd +++ b/resources/Schema/Components/Collidable.xsd @@ -4,10 +4,5 @@ - - - - - \ No newline at end of file diff --git a/resources/Schema/Components/Physics.xml b/resources/Schema/Components/Physics.xml new file mode 100644 index 00000000..1ad81de0 --- /dev/null +++ b/resources/Schema/Components/Physics.xml @@ -0,0 +1,3 @@ + + + diff --git a/resources/Schema/Components/Physics.xsd b/resources/Schema/Components/Physics.xsd new file mode 100644 index 00000000..001dd2c8 --- /dev/null +++ b/resources/Schema/Components/Physics.xsd @@ -0,0 +1,16 @@ + + + + + + + + Physics stuff + + + + + + + + diff --git a/resources/Schema/Components/Transform.xsd b/resources/Schema/Components/Transform.xsd index b8d24777..f0db2472 100644 --- a/resources/Schema/Components/Transform.xsd +++ b/resources/Schema/Components/Transform.xsd @@ -17,4 +17,4 @@ - \ No newline at end of file + diff --git a/resources/Schema/Entities/MovementTest.xml b/resources/Schema/Entities/MovementTest.xml new file mode 100644 index 00000000..08e05417 --- /dev/null +++ b/resources/Schema/Entities/MovementTest.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + + + + + + + + + + + Models/Assault.obj + + + + + + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + + + + + diff --git a/resources/Schema/Entities/Player.xml b/resources/Schema/Entities/Player.xml new file mode 100644 index 00000000..6406a795 --- /dev/null +++ b/resources/Schema/Entities/Player.xml @@ -0,0 +1,17 @@ + + + + + + + + Models/Core/UnitSphere.obj + + + + + + + + + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 8790d8a9..88237697 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -11,6 +11,7 @@ + diff --git a/src/Engine/Collision/CollisionSystem.cpp b/src/Engine/Collision/CollisionSystem.cpp index 92b7f7db..96e49153 100644 --- a/src/Engine/Collision/CollisionSystem.cpp +++ b/src/Engine/Collision/CollisionSystem.cpp @@ -4,6 +4,11 @@ void CollisionSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) { + if (!entity.HasComponent("Physics")) { + return; + } + ComponentWrapper& cPhysics = entity["Physics"]; + boost::optional boundingBox = Collision::EntityAbsoluteAABB(entity); if (!boundingBox) { return; @@ -25,7 +30,10 @@ void CollisionSystem::UpdateComponent(World* world, EntityWrapper& entity, Compo continue; } if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) { - (glm::vec3&)cTransform["Position"] += resolutionVector; + if (entity.HasComponent("Physics")) { + (glm::vec3&)cTransform["Position"] += resolutionVector; + cPhysics["Velocity"] = glm::vec3(0, 0, 0); + } } } diff --git a/src/Game/CMakeLists.txt b/src/Game/CMakeLists.txt index 04146670..4be61127 100644 --- a/src/Game/CMakeLists.txt +++ b/src/Game/CMakeLists.txt @@ -11,16 +11,15 @@ include_directories( ) file(GLOB SOURCE_FILES - "${INCLUDE_PATH}/*.h" - #"*.cpp" + "${INCLUDE_PATH}/Systems/*.h" + "Systems/*.cpp" ) -#source_group(Core FILES ${SOURCE_FILES}) +source_group(Systems FILES ${SOURCE_FILES_Systems}) set(SOURCE_FILES ${SOURCE_FILES} "Game.cpp" - "HealthSystem.cpp" - "PlayerSystem.cpp" + ${SOURCE_FILES_Systems} ) set(LIBRARIES diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 1865dd6c..29b9316b 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -2,7 +2,10 @@ #include "Collision/CollidableOctreeSystem.h" #include "Collision/TriggerSystem.h" #include "Collision/CollisionSystem.h" -#include "Game/HealthSystem.h" +#include "Systems/RaptorCopterSystem.h" +#include "Systems/PlayerSystem.h" +#include "Systems/HealthSystem.h" +#include "Systems/PlayerMovementSystem.h" #include "Core/EntityFileWriter.h" Game::Game(int argc, char* argv[]) @@ -69,6 +72,7 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel); // Populate Octree with collidables ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); diff --git a/src/Game/PlayerMovementSystem.cpp b/src/Game/PlayerMovementSystem.cpp deleted file mode 100644 index c0946134..00000000 --- a/src/Game/PlayerMovementSystem.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "RaptorCopterSystem.h" - -void PlayerMovementSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) -{ - ComponentWrapper& transform = world->GetComponent(component.EntityID); -} \ No newline at end of file diff --git a/src/Game/HealthSystem.cpp b/src/Game/Systems/HealthSystem.cpp similarity index 98% rename from src/Game/HealthSystem.cpp rename to src/Game/Systems/HealthSystem.cpp index 7986db9d..b6d46dc9 100644 --- a/src/Game/HealthSystem.cpp +++ b/src/Game/Systems/HealthSystem.cpp @@ -1,5 +1,4 @@ -#include "HealthSystem.h" -#include +#include "Systems/HealthSystem.h" HealthSystem::HealthSystem(EventBroker* eventBroker) : System(eventBroker) diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp new file mode 100644 index 00000000..536624b3 --- /dev/null +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -0,0 +1,16 @@ +#include "Systems/PlayerMovementSystem.h" + +void PlayerMovementSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) +{ + ComponentWrapper& cTransform = entity["Transform"]; + if (!entity.HasComponent("Physics")) { + return; + } + ComponentWrapper& cPhysics = entity["Physics"]; + + glm::vec3& velocity = cPhysics["Velocity"]; + velocity.y -= 9.82 * dt; + + glm::vec3& position = cTransform["Position"]; + position += velocity * (float)dt; +} \ No newline at end of file diff --git a/src/Game/PlayerSystem.cpp b/src/Game/Systems/PlayerSystem.cpp similarity index 97% rename from src/Game/PlayerSystem.cpp rename to src/Game/Systems/PlayerSystem.cpp index 2d4d83c6..8cdddf84 100644 --- a/src/Game/PlayerSystem.cpp +++ b/src/Game/Systems/PlayerSystem.cpp @@ -1,4 +1,4 @@ -#include "PlayerSystem.h" +#include "Systems/PlayerSystem.h" void PlayerSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) { From 5e721325a4ba8a535535d21d3a85981980bba17f Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 14 Jan 2016 18:20:51 +0100 Subject: [PATCH 27/48] Merge fixes --- include/Engine/Collision/Collision.h | 2 +- include/Engine/Rendering/RenderSystem.h | 10 +- resources/Schema/Components/Camera.xml | 2 +- resources/Schema/Components/Camera.xsd | 4 +- resources/Schema/Entities/MovementTest.xml | 7 ++ src/Engine/Collision/Collision.cpp | 4 +- src/Engine/Editor/EditorSystem.cpp | 3 + src/Engine/Rendering/PickingPass.cpp | 6 +- src/Engine/Rendering/RenderSystem.cpp | 122 ++++++++++----------- src/Game/Game.cpp | 4 +- 10 files changed, 85 insertions(+), 79 deletions(-) diff --git a/include/Engine/Collision/Collision.h b/include/Engine/Collision/Collision.h index 4ea45a55..148f688d 100644 --- a/include/Engine/Collision/Collision.h +++ b/include/Engine/Collision/Collision.h @@ -11,7 +11,7 @@ #include "../Core/Ray.h" #include "../Core/AABB.h" #include "../Rendering/RawModel.h" -#include "../Rendering/RenderQueueFactory.h" +#include "../Core/Transform.h" #include "../Core/Entity.h" #include "../Core/EntityWrapper.h" diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index 93e7233f..efe3b88c 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -14,22 +14,24 @@ #include "ModelJob.h" #include "Renderer.h" #include "../Core/Transform.h" +#include "DebugCameraInputController.h" class RenderSystem : public ImpureSystem { public: RenderSystem(EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame); + ~RenderSystem(); virtual void Update(World* world, double dt) override; private: World* m_World = nullptr; - const IRenderer* m_Renderer = nullptr; + const IRenderer* m_Renderer; RenderFrame* m_RenderFrame; bool m_SwitchCamera = false; - Camera* m_Camera = nullptr; - Camera* m_DefaultCamera = nullptr; + Camera* m_Camera; + DebugCameraInputController* m_DebugCameraInputController; std::list m_CameraComponents; @@ -41,8 +43,6 @@ private: void updateCamera(World* world, double dt); void updateProjectionMatrix(ComponentWrapper& cameraComponent); - glm::mat4 m_ViewMatrix; - glm::mat4 m_ProjectionMatrix; void fillModels(std::list>& jobs, World* world); diff --git a/resources/Schema/Components/Camera.xml b/resources/Schema/Components/Camera.xml index 92225dde..4f613ded 100644 --- a/resources/Schema/Components/Camera.xml +++ b/resources/Schema/Components/Camera.xml @@ -1,6 +1,6 @@ cam - 60.0 + 45 0.01 5000 \ No newline at end of file diff --git a/resources/Schema/Components/Camera.xsd b/resources/Schema/Components/Camera.xsd index 4f02deb0..2b896c74 100644 --- a/resources/Schema/Components/Camera.xsd +++ b/resources/Schema/Components/Camera.xsd @@ -10,7 +10,9 @@ - + + Vertical Field of View in degrees + diff --git a/resources/Schema/Entities/MovementTest.xml b/resources/Schema/Entities/MovementTest.xml index 08e05417..93b4d374 100644 --- a/resources/Schema/Entities/MovementTest.xml +++ b/resources/Schema/Entities/MovementTest.xml @@ -6,6 +6,13 @@ + + + + + + + diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index 92048e70..6a66be75 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -290,8 +290,8 @@ boost::optional EntityAbsoluteAABB(EntityWrapper& entity) } ComponentWrapper& cAABB = entity["AABB"]; - glm::vec3 absPosition = RenderQueueFactory::AbsolutePosition(entity.World, entity.ID); - glm::vec3 absScale = RenderQueueFactory::AbsoluteScale(entity.World, entity.ID); + glm::vec3 absPosition = Transform::AbsolutePosition(entity.World, entity.ID); + glm::vec3 absScale = Transform::AbsoluteScale(entity.World, entity.ID); glm::vec3 origin = absPosition + (glm::vec3)cAABB["Origin"]; glm::vec3 size = (glm::vec3)cAABB["Size"] * absScale; return AABB::FromOriginSize(origin, size); diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 714d3d10..69889be5 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -125,6 +125,9 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e) if (m_Selection == 0) { return false; } + if (m_Camera == nullptr) { + return false; + } auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); glm::vec3 widgetOrientation = widgetTransform["Orientation"]; diff --git a/src/Engine/Rendering/PickingPass.cpp b/src/Engine/Rendering/PickingPass.cpp index aa42a961..6800df1d 100644 --- a/src/Engine/Rendering/PickingPass.cpp +++ b/src/Engine/Rendering/PickingPass.cpp @@ -109,7 +109,7 @@ void PickingPass::ClearPicking() { m_PickingColorsToEntity.clear(); m_EntityColors.clear(); - m_ColorCounter[0] = 0; + m_ColorCounter[0] = 1; m_ColorCounter[1] = 0; m_PickingBuffer.Bind(); @@ -138,10 +138,10 @@ PickData PickingPass::Pick(glm::vec2 screenCoord) pickInfo = it->second; } else { pickData.Entity = EntityID_Invalid; + return pickData; } - pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, resolution, pickInfo.Camera->ProjectionMatrix(), pickInfo.Camera->ViewMatrix()); - + pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, resolution, pickInfo.Camera->ProjectionMatrix(), pickInfo.Camera->ViewMatrix()); pickData.Entity = pickInfo.Entity; pickData.Camera = pickInfo.Camera; pickData.World = pickInfo.World; diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index d6cdb9b3..1435e219 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -1,18 +1,21 @@ #include "Rendering/RenderSystem.h" -#include "Rendering/DebugCameraInputController.h" -RenderSystem::RenderSystem(EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame) :ImpureSystem(eventBrokerer) +RenderSystem::RenderSystem(EventBroker* eventBroker, const IRenderer* renderer, RenderFrame* renderFrame) + : System(eventBroker) + , m_Renderer(renderer) + , m_RenderFrame(renderFrame) { - m_Renderer = renderer; - m_RenderFrame = renderFrame; EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand); - m_DefaultCamera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f); - m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10)); - if (m_Camera == nullptr) { - m_Camera = m_DefaultCamera; - } + m_Camera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f); + m_DebugCameraInputController = new DebugCameraInputController(eventBroker, -1); +} + +RenderSystem::~RenderSystem() +{ + delete m_Camera; + delete m_DebugCameraInputController; } bool RenderSystem::OnSetCamera(const Events::SetCamera &event) @@ -54,14 +57,11 @@ void RenderSystem::switchCamera(EntityID entity) void RenderSystem::updateProjectionMatrix(ComponentWrapper& cameraComponent) { double fov = cameraComponent["FOV"]; - double aspectRatio = m_Renderer->Resolution().Width / m_Renderer->Resolution().Height; + double aspectRatio = (float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height; double nearClip = cameraComponent["NearClip"]; double farClip = cameraComponent["FarClip"]; - double fovY = atan(tan(glm::radians(fov)/2.0) * aspectRatio) * 2.0; - m_ProjectionMatrix = glm::perspective(fovY, aspectRatio, nearClip, farClip); - - m_Camera->SetFOV(fovY); + m_Camera->SetFOV(glm::radians(fov)); m_Camera->SetAspectRatio(aspectRatio); m_Camera->SetNearClip(nearClip); m_Camera->SetFarClip(farClip); @@ -129,66 +129,62 @@ void RenderSystem::Update(World* world, double dt) void RenderSystem::updateCamera(World* world, double dt) { - - static DebugCameraInputController firstPersonInputController(m_EventBroker, -1); - - if (m_SwitchCamera) { - auto cameras = world->GetComponents("Camera"); - for (auto it = cameras->begin(); it != cameras->end(); it++) { - if ((*it).EntityID == m_CurrentCamera) { - it++; - if (it != cameras->end()) { - switchCamera((*it).EntityID); - } else { - switchCamera((*cameras->begin()).EntityID); - } - break; + if (m_SwitchCamera) { + auto cameras = world->GetComponents("Camera"); + for (auto it = cameras->begin(); it != cameras->end(); it++) { + if ((*it).EntityID == m_CurrentCamera) { + it++; + if (it != cameras->end()) { + switchCamera((*it).EntityID); + } else { + switchCamera((*cameras->begin()).EntityID); } + break; } + } + ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); + ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); + + m_DebugCameraInputController->SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); + m_DebugCameraInputController->SetPosition(cameraTransform["Position"]); + } + + if (m_World->ValidEntity(m_CurrentCamera)) { + if (world->HasComponent(m_CurrentCamera, "Camera") && world->HasComponent(m_CurrentCamera, "Transform")) { ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); - firstPersonInputController.SetPosition(cameraTransform["Position"]); - - } + m_DebugCameraInputController->Update(dt); + (glm::vec3&)cameraTransform["Orientation"] = glm::eulerAngles(m_DebugCameraInputController->Orientation()); + (glm::vec3&)cameraTransform["Position"] = m_DebugCameraInputController->Position(); + + glm::vec3 position = Transform::AbsolutePosition(world, m_CurrentCamera); + glm::quat orientation = Transform::AbsoluteOrientation(world, m_CurrentCamera); + + m_Camera->SetPosition(position); + m_Camera->SetOrientation(orientation); + + updateProjectionMatrix(cameraComponent); + + } + } else { + m_Camera = m_Camera; + + auto cameras = world->GetComponents("Camera"); + if (cameras != nullptr) { + if (cameras->begin() != cameras->end()) { + ComponentWrapper& cameraC = *cameras->begin(); + switchCamera(cameraC.EntityID); - if (m_World->ValidEntity(m_CurrentCamera)) { - if (world->HasComponent(m_CurrentCamera, "Camera") && world->HasComponent(m_CurrentCamera, "Transform")) { ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - firstPersonInputController.Update(dt); - (glm::vec3&)cameraTransform["Orientation"] = glm::eulerAngles(firstPersonInputController.Orientation()); - (glm::vec3&)cameraTransform["Position"] = firstPersonInputController.Position(); - - glm::vec3 position = Transform::AbsolutePosition(world, m_CurrentCamera); - glm::quat orientation = Transform::AbsoluteOrientation(world, m_CurrentCamera); - - m_Camera->SetPosition(position); - m_Camera->SetOrientation(orientation); - - updateProjectionMatrix(cameraComponent); - - } - } else { - m_Camera = m_DefaultCamera; - - auto cameras = world->GetComponents("Camera"); - if (cameras != nullptr) { - if (cameras->begin() != cameras->end()) { - ComponentWrapper& cameraC = *cameras->begin(); - switchCamera(cameraC.EntityID); - - ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); - ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - - firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); - firstPersonInputController.SetPosition(cameraTransform["Position"]); - } + m_DebugCameraInputController->SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); + m_DebugCameraInputController->SetPosition(cameraTransform["Position"]); } } + } - m_Camera->UpdateViewMatrix(); + m_Camera->UpdateViewMatrix(); } diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 2be1cb28..a49171bb 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -35,6 +35,7 @@ Game::Game(int argc, char* argv[]) )); m_Renderer->Initialize(); //m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get("Video.FOV", 90.f))); + m_RenderFrame = new RenderFrame(); // Create input manager m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker); @@ -59,8 +60,6 @@ Game::Game(int argc, char* argv[]) fp.MergeEntities(m_World); } - m_RenderFrame = new RenderFrame(); - // Create Octrees m_OctreeCollision = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); m_OctreeFrustrumCulling = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); @@ -82,7 +81,6 @@ Game::Game(int argc, char* argv[]) ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); - ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer, m_RenderFrame); From 3e2cb33bc0d05f00865de28a2fea0511942bbac8 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 15 Jan 2016 11:23:29 +0100 Subject: [PATCH 28/48] =?UTF-8?q?Played=20around=20with=20new=20map=20MapV?= =?UTF-8?q?ersion1.=20Was=20fun=20=E3=83=BD(=C2=B4=E2=96=BD`)/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets | 2 +- include/Engine/Sound/EPlaySoundOnEntity.h | 1 - src/Engine/Sound/SoundSystem.cpp | 14 ++++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/assets b/assets index 6cbf2365..a3c92ac8 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6cbf2365d49e6280750ea3bcd0f9c271779e6f15 +Subproject commit a3c92ac876dd061776c36d1594bd82264372f028 diff --git a/include/Engine/Sound/EPlaySoundOnEntity.h b/include/Engine/Sound/EPlaySoundOnEntity.h index fb4b7a15..39dea432 100644 --- a/include/Engine/Sound/EPlaySoundOnEntity.h +++ b/include/Engine/Sound/EPlaySoundOnEntity.h @@ -10,7 +10,6 @@ namespace Events // Plays a sound on an entity with a SoundEmitter component attached. // Sound behavior is thereby specified in the SoundEmitter component. -// ?(???)?? struct PlaySoundOnEntity : public Event { EntityID EmitterID = 0; diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 8fe1c2bb..ed65351f 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -41,8 +41,8 @@ void SoundSystem::stopEmitters() { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end(); it++) { - if (getSourceState((*it).second->ALsource) == AL_PLAYING) { - stopSound((*it).second); + if (getSourceState(it->second->ALsource) == AL_PLAYING) { + stopSound(it->second); } } } @@ -71,6 +71,7 @@ void SoundSystem::deleteInactiveEmitters() alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); m_World->DeleteEntity(it->first); + delete it->second; it = m_Sources.erase(it); } } else { @@ -78,6 +79,7 @@ void SoundSystem::deleteInactiveEmitters() stopSound((*it).second); alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); + delete it->second; it = m_Sources.erase(it); } } @@ -200,19 +202,19 @@ bool SoundSystem::OnPlaySoundOnPosition(const Events::PlaySoundOnPosition & e) source->Type = SoundType::SFX; m_Sources[emitterID] = source; playSound(source); - return false; + return true; } bool SoundSystem::OnPauseSound(const Events::PauseSound & e) { alSourcePause(m_Sources[e.EmitterID]->ALsource); - return false; + return true; } bool SoundSystem::OnStopSound(const Events::StopSound & e) { alSourceStop(m_Sources[e.EmitterID]->ALsource); - return false; + return true; } bool SoundSystem::OnContinueSound(const Events::ContinueSound & e) @@ -235,7 +237,7 @@ bool SoundSystem::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) m_Sources[emitterChild] = source; playSound(source); } - return false; + return true; } bool SoundSystem::OnSetBGMGain(const Events::SetBGMGain & e) From 76f90ab0317d35871a19fc4c7a0aed014c00373b Mon Sep 17 00:00:00 2001 From: William Moberg Date: Fri, 15 Jan 2016 11:29:46 +0100 Subject: [PATCH 29/48] The main thread is not mutex blocked while child threads load anymore. --- assets | 2 +- include/Engine/Rendering/Model.h | 2 +- include/Engine/Rendering/ModelJob.h | 14 ++++---- include/Engine/Rendering/RawModel.h | 2 +- resources/Schema/Entities/ThreadTestMap.xml | 30 ++++++++++++++++ src/Engine/Core/ResourceManager.cpp | 38 ++++++++++++--------- src/Engine/Rendering/Model.cpp | 2 +- src/Engine/Rendering/RawModel.cpp | 2 +- src/Engine/Rendering/RenderSystem.cpp | 6 ++-- src/Engine/Rendering/Renderer.cpp | 4 +-- 10 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 resources/Schema/Entities/ThreadTestMap.xml diff --git a/assets b/assets index 6cbf2365..a3c92ac8 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6cbf2365d49e6280750ea3bcd0f9c271779e6f15 +Subproject commit a3c92ac876dd061776c36d1594bd82264372f028 diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index 77e3df76..280fe5af 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -13,7 +13,7 @@ private: public: ~Model(); - const std::vector& TextureGroups() const { return m_RawModel->TextureGroups; } + const std::vector& MaterialGroups() const { return m_RawModel->MaterialGroups; } const glm::mat4& Matrix() const { return m_RawModel->m_Matrix; } const std::vector& Vertices() const { return m_RawModel->m_Vertices; } diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index d598766f..353d1bbe 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -15,16 +15,16 @@ struct ModelJob : RenderJob { - ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::Model::MaterialGroup texGroup, ComponentWrapper modelComponent, World* world) + ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialGroup matGroup, ComponentWrapper modelComponent, World* world) : RenderJob() { Model = model; - TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; - DiffuseTexture = texGroup.Texture.get(); - NormalTexture = texGroup.NormalMap.get(); - SpecularTexture = texGroup.SpecularMap.get(); - StartIndex = texGroup.StartIndex; - EndIndex = texGroup.EndIndex; + TextureID = (matGroup.Texture) ? matGroup.Texture->ResourceID : 0; + DiffuseTexture = matGroup.Texture.get(); + NormalTexture = matGroup.NormalMap.get(); + SpecularTexture = matGroup.SpecularMap.get(); + StartIndex = matGroup.StartIndex; + EndIndex = matGroup.EndIndex; Matrix = matrix; Color = modelComponent["Color"]; Entity = modelComponent.EntityID; diff --git a/include/Engine/Rendering/RawModel.h b/include/Engine/Rendering/RawModel.h index 5117f953..0477edb1 100644 --- a/include/Engine/Rendering/RawModel.h +++ b/include/Engine/Rendering/RawModel.h @@ -57,7 +57,7 @@ public: unsigned int EndIndex; }; - std::vector TextureGroups; + std::vector MaterialGroups; std::vector m_Vertices; std::vector m_Indices; diff --git a/resources/Schema/Entities/ThreadTestMap.xml b/resources/Schema/Entities/ThreadTestMap.xml new file mode 100644 index 00000000..b4b358c0 --- /dev/null +++ b/resources/Schema/Entities/ThreadTestMap.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index ffd6c76c..30614e5c 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -81,8 +81,6 @@ void ResourceManager::Update() Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent) { - //Lock the mutex immediately, and unlock it when leaving the function. - boost::lock_guard guard(m_Mutex); auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str()); @@ -98,23 +96,29 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string } catch (const std::exception& e) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); } - if (resource != nullptr) { - // Store IDs - resource->TypeID = GetTypeID(resourceType); - resource->ResourceID = GetNewResourceID(resource->TypeID); + + { + //Lock the mutex immediately, and unlock it when leaving the code block. + boost::lock_guard guard(m_Mutex); + if (resource != nullptr) { + // Store IDs + resource->TypeID = GetTypeID(resourceType); + resource->ResourceID = GetNewResourceID(resource->TypeID); + } + + // Cache + m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; + m_ResourceFromName[resourceName] = resource; + if (parent != nullptr) { + m_ResourceParents[resource] = parent; + } + + if (!boost::filesystem::is_directory(resourceName)) { + LOG_DEBUG("Adding watch for %s", resourceName.c_str()); + m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); + } } - // Cache - m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; - m_ResourceFromName[resourceName] = resource; - if (parent != nullptr) { - m_ResourceParents[resource] = parent; - } - - if (!boost::filesystem::is_directory(resourceName)) { - LOG_DEBUG("Adding watch for %s", resourceName.c_str()); - m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); - } return resource; } diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index 8e24b704..c1afa037 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -9,7 +9,7 @@ Model::Model(std::string fileName) throw StillLoadingException(); } - for (auto& group : m_RawModel->TextureGroups) { + for (auto& group : m_RawModel->MaterialGroups) { if (!group.TexturePath.empty()) { group.Texture = std::shared_ptr(ResourceManager::Load(group.TexturePath)); } diff --git a/src/Engine/Rendering/RawModel.cpp b/src/Engine/Rendering/RawModel.cpp index 5b0b7260..256346f1 100644 --- a/src/Engine/Rendering/RawModel.cpp +++ b/src/Engine/Rendering/RawModel.cpp @@ -163,7 +163,7 @@ RawModel::RawModel(std::string fileName) material->GetTexture(aiTextureType_SPECULAR, 0, &path, &mapping); matGroup.SpecularMapPath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); } - TextureGroups.push_back(matGroup); + MaterialGroups.push_back(matGroup); // Bones std::map>> vertexWeights; diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 1f2bac9d..e228b33e 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -84,15 +84,15 @@ void RenderSystem::fillModels(std::list>& jobs, World continue; } - Model* model = ResourceManager::Load<::Model>(resource); + Model* model = ResourceManager::Load<::Model, true>(resource); if (model == nullptr) { model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); } glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, world); - for (auto texGroup : model->TextureGroups) { - std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, texGroup, modelComponent, world)); + for (auto matGroup : model->MaterialGroups()) { + std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, matGroup, modelComponent, world)); jobs.push_back(modelJob); } } diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index a59a6bca..867398bd 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -131,8 +131,8 @@ void Renderer::DrawScreenQuad(GLuint textureToDraw) glBindVertexArray(m_ScreenQuad->VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer); - glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->TextureGroups()[0].EndIndex - m_ScreenQuad->TextureGroups()[0].StartIndex +1 - , GL_UNSIGNED_INT, 0, m_ScreenQuad->TextureGroups()[0].StartIndex); + glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->MaterialGroups()[0].EndIndex - m_ScreenQuad->MaterialGroups()[0].StartIndex +1 + , GL_UNSIGNED_INT, 0, m_ScreenQuad->MaterialGroups()[0].StartIndex); } void Renderer::InitializeTextures() From d9dd7f535fb02a796257e4f75ae4f02b0c3ea8c7 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 15 Jan 2016 13:45:08 +0100 Subject: [PATCH 30/48] When changing camera listener is updated. Also fixed some crashes in RenderSystem when there were no cameras available. --- src/Engine/Rendering/RenderSystem.cpp | 19 +++++++++++++++---- src/Engine/Sound/SoundSystem.cpp | 7 ++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 1f2bac9d..69eb5775 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -39,11 +39,17 @@ void RenderSystem::switchCamera(EntityID entity) if (m_World->HasComponent(m_CurrentCamera, "Model")) { m_World->GetComponent(m_CurrentCamera, "Model")["Visible"] = true; } + if (m_World->HasComponent(m_CurrentCamera, "Listener")) { + m_World->DeleteComponent(m_CurrentCamera, "Listener"); + } } if (m_World->HasComponent(entity, "Model")) { m_World->GetComponent(entity, "Model")["Visible"] = false; } + if (!m_World->HasComponent(entity, "Listener")) { + m_World->AttachComponent(entity, "Listener"); + } m_CurrentCamera = entity; m_SwitchCamera = false; @@ -130,6 +136,9 @@ void RenderSystem::updateCamera(World* world, double dt) { if (m_SwitchCamera) { auto cameras = world->GetComponents("Camera"); + if (cameras == nullptr) { + return; + } for (auto it = cameras->begin(); it != cameras->end(); it++) { if ((*it).EntityID == m_CurrentCamera) { it++; @@ -141,11 +150,13 @@ void RenderSystem::updateCamera(World* world, double dt) break; } } - ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); - ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); + if (m_World->HasComponent(m_CurrentCamera, "Camera")) { + ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); + ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - m_DebugCameraInputController->SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); - m_DebugCameraInputController->SetPosition(cameraTransform["Position"]); + m_DebugCameraInputController->SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); + m_DebugCameraInputController->SetPosition(cameraTransform["Position"]); + } } if (m_World->ValidEntity(m_CurrentCamera)) { diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index ed65351f..d51ec50e 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -135,6 +135,7 @@ void SoundSystem::updateEmitters() void SoundSystem::updateListener() { + int testremove = 0; // Should only be one listener. auto listenerComponents = m_World->GetComponents("Listener"); if (listenerComponents == nullptr) { @@ -147,8 +148,12 @@ void SoundSystem::updateListener() glm::vec3 nextPos = Transform::AbsolutePosition(m_World, listener); // Get next (current) pos glm::vec3 velocity = nextPos - previousPos; // Calculate velocity setListenerPos(nextPos); - setListenerVel(velocity); + //setListenerVel(velocity); setListenerOri(glm::eulerAngles(Transform::AbsoluteOrientation(m_World, listener))); + testremove++; + } + if (testremove > 1) { + testremove = 0; } } From f5ea4d513cf176ce2ae8f82f081a7fc740d1c3bb Mon Sep 17 00:00:00 2001 From: William Moberg Date: Fri, 15 Jan 2016 15:25:01 +0100 Subject: [PATCH 31/48] ResourceManager::Load never returns null, throws exceptions instead. --- include/Engine/Core/ResourceManager.h | 118 +++++++++++++------------- src/Engine/Collision/Collision.cpp | 4 +- src/Engine/Core/ResourceManager.cpp | 70 +++++++++------ src/Engine/Rendering/Model.cpp | 6 +- src/Engine/Rendering/RenderSystem.cpp | 16 +++- 5 files changed, 117 insertions(+), 97 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index 9ebf545a..cb78ce13 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -21,6 +21,23 @@ protected: Resource() { } public: + //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. + //Not actually an error, just a message to the ResourceManager. + struct StillLoadingException : public std::exception + { + virtual const char* what() const throw() + { + return "Resource is still loading."; + } + }; + struct FailedLoadingException : public std::exception + { + virtual const char* what() const throw() + { + return "Resource is failed to load."; + } + }; + // Pretend that this is a pure virtual function that you have to implement // FIXME: Why did we do this again instead of just using the constructor? // static Resource* Create(std::string resourceName); @@ -39,16 +56,6 @@ public: class ThreadUnsafeResource : public Resource { friend class ResourceManager; -protected: - //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. - //Not actually an error, just a message to the ResourceManager. - struct StillLoadingException : public std::exception - { - virtual const char* what() const throw() - { - return "Resource is still loading."; - } - }; }; /** Singleton resource manager to keep track of and cache any external engine assets */ @@ -75,18 +82,18 @@ public: // TODO: Templateify static bool IsResourceLoaded(std::string resourceType, std::string resourceName); - /** If Async is false: Hot-loads a resource and caches it for future use. - Fairly safe to assume that return value is always a valid pointer, will only return nullptr on error. - + /** Return value should always be a valid pointer, will throw an exception on error. + If the resource has been loaded already, returns a pointer to it. + + If Async is false: Hot-loads a resource, caches it for future use, and returns a pointer to it. If Async is true: If the resource is not loaded yet, starts loading the resource - in the background and returns nullptr immediately. - If the resource has been loaded already, return a pointer to it. + in the background and throws Resource::StillLoadingException immediately. @tparam T Resource type. - @tparam Async Set this to true if the resource should be loaded asyncronously. + @tparam async Set this to true if the resource should be loaded asyncronously. @param resourceName Fully qualified name of the resource to load. */ - template + template static T* Load(std::string resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -101,22 +108,6 @@ public: static void Update(); private: - //Represents a pointer value to signify that a resource haven't failed, but is not fully loaded. - class SpecialResourcePointer - { - public: - SpecialResourcePointer() - : m_Val(new Resource()) - {} - ~SpecialResourcePointer() - { - delete m_Val; - } - //Make this class implicitly convertible to the Resource*. - operator Resource* const() const { return m_Val; } - private: - Resource* const m_Val; - }; //This is a haxy way to make sure that IsMainThread is run when the program starts, so the master thread id is set. struct MasterThreadChecker { @@ -125,7 +116,6 @@ private: ResourceManager::IsMainThread(); } }; - const static SpecialResourcePointer m_StillLoading; const static MasterThreadChecker m_Checker; static std::unordered_map m_CompilerTypenameToResourceType; @@ -135,6 +125,7 @@ private: static std::unordered_map m_ResourceParents; // resource -> parent resource static std::unordered_map, boost::thread> m_LoadingThreads; // (type, name) -> loading thread + static std::unordered_map, std::exception_ptr> m_LoadingThreadExceptions; // (type, name) -> exceptions static boost::recursive_mutex m_Mutex; // TODO: Getters for IDs @@ -150,7 +141,9 @@ private: static unsigned int GetNewResourceID(unsigned int typeID); // Internal: Create a resource and cache it - static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception); + static Resource* cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent); static bool IsMainThread(); }; @@ -169,14 +162,14 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = auto iter = m_CompilerTypenameToResourceType.find(resourceTypename); if (iter == m_CompilerTypenameToResourceType.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return nullptr; + throw Resource::FailedLoadingException(); } std::string resourceType = iter->second; constexpr bool mustNotLoadInThread = std::is_base_of::value; if (mustNotLoadInThread && !IsMainThread()) { LOG_ERROR("Failed to Load \"%s\": ThreadUnsafeResource type \"%s\" load in the constructor of another resource that is loaded asyncronously.", resourceName.c_str(), iter->second.c_str()); - return nullptr; + throw Resource::FailedLoadingException(); } auto cacheKey = std::make_pair(resourceType, resourceName); @@ -185,9 +178,9 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = auto tIt = m_LoadingThreads.find(cacheKey); if (tIt != m_LoadingThreads.end()) { if (async) { - //Return null if the thread is still working. + //Throw StillLoadingException if the thread is still working. if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { - return nullptr; + throw Resource::StillLoadingException(); } //Else we know the thread has completed. } else { @@ -196,42 +189,51 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = } //When the thread is done, delete the thread. m_LoadingThreads.erase(tIt); - //Find the resource that the thread loaded. - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - //Threads should not be able to throw StillLoadingException, so no check should be needed. - return static_cast(it->second); - } else { - //If cacheKey does not exist after thread finishes, it failed. - return nullptr; + //Rethrow the thread exception if it threw any. + auto excIt = m_LoadingThreadExceptions.find(cacheKey); + std::exception_ptr exception = excIt->second; + m_LoadingThreadExceptions.erase(excIt); + if (exception) { + std::rethrow_exception(exception); } } //If resource has already been cached and completely loaded. it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end() && it->second != m_StillLoading) { - return static_cast(it->second); + if (it != m_ResourceCache.end()) { + if (it->second != nullptr) { + return static_cast(it->second); + } else { + //Don't return null on failure, exception instead. + throw Resource::FailedLoadingException(); + } } Resource* res = nullptr; //If resource is not cached.. if (async) { if (mustNotLoadInThread) { - res = createResource(resourceType, resourceName, parent); - if (res != m_StillLoading) { - return static_cast(res); + try { + return static_cast(createResourceThrowing(resourceType, resourceName, parent)); + } catch (const Resource::StillLoadingException&) { + throw; } } else { //Create a thread that loads the resource into cache. - m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent, m_LoadingThreadExceptions[cacheKey]); + throw Resource::StillLoadingException(); } - return nullptr; } else { //load and return the resource. - do { - res = createResource(resourceType, resourceName, parent); - } while (res == m_StillLoading); - return static_cast(res); + while (true) { + try { + return static_cast(createResourceThrowing(resourceType, resourceName, parent)); + } catch (const Resource::StillLoadingException&) { + continue; + } catch (const std::exception&) { + throw; + } + } } } diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index c0b2c658..c74af3ad 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -220,7 +220,7 @@ bool attachAABBComponentFromModel(World* world, EntityID id) } ComponentWrapper model = world->GetComponent(id, "Model"); ComponentWrapper collision = world->AttachComponent(id, "AABB"); - Model* modelRes = ResourceManager::Load(model["Resource"]); + Model* modelRes = ResourceManager::Load(model["Resource"]); if (modelRes == nullptr) { return false; } @@ -247,7 +247,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) { ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform"); ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model"); - Model* modelRes = ResourceManager::Load(model["Resource"]); + Model* modelRes = ResourceManager::Load(model["Resource"]); outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]); glm::vec3 mini = outBox.MinCorner(); glm::vec3 maxi = outBox.MaxCorner(); diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index 30614e5c..ed79e4f7 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -13,8 +13,8 @@ std::unordered_map ResourceManager::m_ResourceTypeIDs std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; std::unordered_map, boost::thread> ResourceManager::m_LoadingThreads; +std::unordered_map, std::exception_ptr> ResourceManager::m_LoadingThreadExceptions; boost::recursive_mutex ResourceManager::m_Mutex; -const ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading; unsigned int ResourceManager::GetTypeID(std::string resourceType) { @@ -79,47 +79,61 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception) { auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str()); - return nullptr; + cacheResource(nullptr, resourceType, resourceName, parent); + //This basically throws an exception. + exception = std::make_exception_ptr(Resource::FailedLoadingException()); return nullptr; } // Call the factory function - Resource* resource = nullptr; try { - resource = facIt->second(resourceName); - } catch (const ThreadUnsafeResource::StillLoadingException&) { - resource = m_StillLoading; + return cacheResource(facIt->second(resourceName), resourceType, resourceName, parent); + } catch (const Resource::StillLoadingException&) { + exception = std::current_exception(); return nullptr; } catch (const std::exception& e) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); + cacheResource(nullptr, resourceType, resourceName, parent); + exception = std::current_exception(); return nullptr; + } +} + + +Resource* ResourceManager::createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent) +{ + std::exception_ptr exception; + Resource* res = createResource(resourceType, resourceName, parent, exception); + if (exception) { + std::rethrow_exception(exception); + } + return res; +} + +Resource* ResourceManager::cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent) +{ + //Lock the mutex immediately, and unlock it when leaving the code block. + boost::lock_guard guard(m_Mutex); + if (resource != nullptr) { + // Store IDs + resource->TypeID = GetTypeID(resourceType); + resource->ResourceID = GetNewResourceID(resource->TypeID); } - { - //Lock the mutex immediately, and unlock it when leaving the code block. - boost::lock_guard guard(m_Mutex); - if (resource != nullptr) { - // Store IDs - resource->TypeID = GetTypeID(resourceType); - resource->ResourceID = GetNewResourceID(resource->TypeID); - } - - // Cache - m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; - m_ResourceFromName[resourceName] = resource; - if (parent != nullptr) { - m_ResourceParents[resource] = parent; - } - - if (!boost::filesystem::is_directory(resourceName)) { - LOG_DEBUG("Adding watch for %s", resourceName.c_str()); - m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); - } + // Cache + m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; + m_ResourceFromName[resourceName] = resource; + if (parent != nullptr) { + m_ResourceParents[resource] = parent; } - return resource; + //if (!boost::filesystem::is_directory(resourceName)) { + // LOG_DEBUG("Adding watch for %s", resourceName.c_str()); + // m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); + //} + return resource; } bool ResourceManager::IsMainThread() diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index c1afa037..33539a14 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -2,12 +2,8 @@ Model::Model(std::string fileName) { - //Load the RawModel asyncronously, this will be done on a separate thread in the background. + //Try loading the model asyncronously, if it throws any exceptions then let it propagate back to caller. m_RawModel = ResourceManager::Load(fileName); - //If it is null, the model is not done yet, so tell resourceManager to try constructing me again later. - if (m_RawModel == nullptr) { - throw StillLoadingException(); - } for (auto& group : m_RawModel->MaterialGroups) { if (!group.TexturePath.empty()) { diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index e228b33e..6431bde2 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -84,13 +84,21 @@ void RenderSystem::fillModels(std::list>& jobs, World continue; } - Model* model = ResourceManager::Load<::Model, true>(resource); - if (model == nullptr) { - model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); + Model* model; + try { + model = ResourceManager::Load<::Model, true>(resource); + } catch (const Resource::StillLoadingException&) { + //continue; + model = ResourceManager::Load<::Model>("Models/Core/UnitRaptor.obj"); + } catch (const std::exception&) { + try { + model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); + } catch (const std::exception&) { + continue; + } } glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, world); - for (auto matGroup : model->MaterialGroups()) { std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, matGroup, modelComponent, world)); jobs.push_back(modelJob); From 63704b2e6b42515d3b175c22f239628d6f9147ed Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 15 Jan 2016 15:43:20 +0100 Subject: [PATCH 32/48] Now calculating the velocity for the listener correctly. --- include/Engine/Sound/SoundSystem.h | 8 ++++---- src/Engine/Sound/SoundSystem.cpp | 23 +++++++++-------------- src/Game/Game.cpp | 2 +- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 7e144667..b9cc2589 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -41,7 +41,7 @@ public: SoundSystem(World* world, EventBroker* eventBroker, bool editorMode); ~SoundSystem(); // Update emitters / listener - void Update(); + void Update(double dt); private: // Help functions for working with OpenaAL void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; @@ -55,10 +55,10 @@ private: // Logic void initOpenAL(); - void updateEmitters(); - void updateListener(); + void updateEmitters(double dt); + void updateListener(double dt); void deleteInactiveEmitters(); - void addNewEmitters(); + void addNewEmitters(double dt); Source* createSource(std::string filePath); void playSound(Source* source); void stopSound(Source* source); diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index d51ec50e..f1f90b18 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -47,13 +47,13 @@ void SoundSystem::stopEmitters() } } -void SoundSystem::Update() +void SoundSystem::Update(double dt) { m_EventBroker->Process(); - addNewEmitters(); // can be optimized with "EEntityCreated" + addNewEmitters(dt); // can be optimized with "EEntityCreated" deleteInactiveEmitters(); // can be optimized with "EEntityDeleted" - updateEmitters(); - updateListener(); + updateEmitters( dt); + updateListener( dt); } void SoundSystem::deleteInactiveEmitters() @@ -85,7 +85,7 @@ void SoundSystem::deleteInactiveEmitters() } } -void SoundSystem::addNewEmitters() +void SoundSystem::addNewEmitters(double dt) { auto emitterComponents = m_World->GetComponents("SoundEmitter"); if (emitterComponents == nullptr) { @@ -102,7 +102,7 @@ void SoundSystem::addNewEmitters() } } -void SoundSystem::updateEmitters() +void SoundSystem::updateEmitters(double dt) { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end(); it++) { @@ -133,9 +133,8 @@ void SoundSystem::updateEmitters() } } -void SoundSystem::updateListener() +void SoundSystem::updateListener(double dt) { - int testremove = 0; // Should only be one listener. auto listenerComponents = m_World->GetComponents("Listener"); if (listenerComponents == nullptr) { @@ -146,14 +145,10 @@ void SoundSystem::updateListener() glm::vec3 previousPos; alGetListener3f(AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get previous pos glm::vec3 nextPos = Transform::AbsolutePosition(m_World, listener); // Get next (current) pos - glm::vec3 velocity = nextPos - previousPos; // Calculate velocity + glm::vec3 velocity = glm::vec3(nextPos - previousPos) / (float)dt; // Calculate velocity setListenerPos(nextPos); - //setListenerVel(velocity); + setListenerVel(velocity); setListenerOri(glm::eulerAngles(Transform::AbsoluteOrientation(m_World, listener))); - testremove++; - } - if (testremove > 1) { - testremove = 0; } } diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 42794939..cde3d4b3 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -127,7 +127,7 @@ void Game::Tick() debugTick(dt); m_Renderer->Update(dt); m_EventBroker->Process(); - m_SoundSystem->Update(); + m_SoundSystem->Update(dt); GLERROR("Game::Tick m_RenderQueueFactory->Update"); m_Renderer->Draw(*m_RenderFrame); GLERROR("Game::Tick m_Renderer->Draw"); From be33dfbd696b0b25784ceefe3d010144b6265e48 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 15 Jan 2016 16:10:02 +0100 Subject: [PATCH 33/48] Forgot to change the velocity for the emitters as well... --- src/Engine/Sound/SoundSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index f1f90b18..1292e0b1 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -112,7 +112,7 @@ void SoundSystem::updateEmitters(double dt) // Get next pos glm::vec3 nextPos = Transform::AbsolutePosition(m_World, it->first); // Calculate velocity - glm::vec3 velocity = nextPos - previousPos; + glm::vec3 velocity = glm::vec3(nextPos - previousPos) / (float)dt; setSourcePos(it->second->ALsource, nextPos); setSourceVel(it->second->ALsource, velocity); float gain; From c09fe4ae558223c063407010860cf4d01bc08081 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Fri, 15 Jan 2016 16:18:39 +0100 Subject: [PATCH 34/48] Added a bool option to disable threads in in the configfile regarding resource loading. --- include/Engine/Core/ResourceManager.h | 16 ++++++++-------- resources/DefaultConfig.ini | 5 ++++- src/Engine/Core/ResourceManager.cpp | 7 ++++--- src/Game/Game.cpp | 1 + 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index cb78ce13..15a551e7 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -65,6 +65,7 @@ private: ResourceManager(); public: + static bool UseThreading; /*static ResourceManager& Instance() { static ResourceManager s; @@ -94,7 +95,7 @@ public: @param resourceName Fully qualified name of the resource to load. */ template - static T* Load(std::string resourceName, Resource* parent = nullptr); + static T* Load(const std::string& resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -141,9 +142,9 @@ private: static unsigned int GetNewResourceID(unsigned int typeID); // Internal: Create a resource and cache it - static Resource* createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent); - static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception); - static Resource* cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResourceThrowing(const std::string& resourceType, const std::string& resourceName, Resource* parent); + static Resource* createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception); + static Resource* cacheResource(Resource* resource, const std::string& resourceType, const std::string& resourceName, Resource* parent); static bool IsMainThread(); }; @@ -156,7 +157,7 @@ void ResourceManager::RegisterType(std::string typeName) } template -static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) +static T* ResourceManager::Load(const std::string& resourceName, Resource* parent /* = nullptr */) { auto resourceTypename = typeid(T).name(); auto iter = m_CompilerTypenameToResourceType.find(resourceTypename); @@ -176,7 +177,7 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = decltype(m_ResourceCache)::iterator it; //If a thread has already been launched to load this resource. auto tIt = m_LoadingThreads.find(cacheKey); - if (tIt != m_LoadingThreads.end()) { + if (UseThreading && tIt != m_LoadingThreads.end()) { if (async) { //Throw StillLoadingException if the thread is still working. if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { @@ -209,9 +210,8 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = } } - Resource* res = nullptr; //If resource is not cached.. - if (async) { + if (UseThreading && async) { if (mustNotLoadInThread) { try { return static_cast(createResourceThrowing(resourceType, resourceName, parent)); diff --git a/resources/DefaultConfig.ini b/resources/DefaultConfig.ini index 3ab402ad..d84b15bb 100644 --- a/resources/DefaultConfig.ini +++ b/resources/DefaultConfig.ini @@ -16,4 +16,7 @@ StartNetwork=false IsServer=false Name=Bob Address=127.0.0.1 -Port=13 \ No newline at end of file +Port=13 + +[Multithreading] +ResourceLoading=true diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index ed79e4f7..54228efa 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -9,6 +9,7 @@ std::unordered_map, Resource*> ResourceManag std::unordered_map ResourceManager::m_ResourceFromName; std::unordered_map ResourceManager::m_ResourceParents; unsigned int ResourceManager::m_CurrentResourceTypeID = 0; +bool ResourceManager::UseThreading = false; std::unordered_map ResourceManager::m_ResourceTypeIDs; std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; @@ -79,7 +80,7 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception) +Resource* ResourceManager::createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception) { auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { @@ -102,7 +103,7 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string } -Resource* ResourceManager::createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::createResourceThrowing(const std::string& resourceType, const std::string& resourceName, Resource* parent) { std::exception_ptr exception; Resource* res = createResource(resourceType, resourceName, parent, exception); @@ -112,7 +113,7 @@ Resource* ResourceManager::createResourceThrowing(std::string resourceType, std: return res; } -Resource* ResourceManager::cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::cacheResource(Resource* resource, const std::string& resourceType, const std::string& resourceName, Resource* parent) { //Lock the mutex immediately, and unlock it when leaving the code block. boost::lock_guard guard(m_Mutex); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index efa773f5..e6589676 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -14,6 +14,7 @@ Game::Game(int argc, char* argv[]) ResourceManager::RegisterType("EntityFile"); m_Config = ResourceManager::Load("Config.ini"); + ResourceManager::UseThreading = m_Config->Get("Multithreading.ResourceLoading", true); LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get("Debug.LogLevel", 1)); // Create the core event broker From 046946002988af38fc29cab057f82df16a302bdf Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 15 Jan 2016 16:32:04 +0100 Subject: [PATCH 35/48] Input binding format changed to allow custom values for bindings. Relevant code must clamp input values to prevent speedhaxx! --- include/Engine/Input/InputProxy.h | 1 + resources/DefaultInput.ini | 8 ++++---- src/Engine/Input/InputProxy.cpp | 23 ++++++++++++----------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/Engine/Input/InputProxy.h b/include/Engine/Input/InputProxy.h index c7817c22..c9ba5ada 100644 --- a/include/Engine/Input/InputProxy.h +++ b/include/Engine/Input/InputProxy.h @@ -1,6 +1,7 @@ #ifndef InputProxy_h__ #define InputProxy_h__ +#include #include "../Common.h" #include "../Core/ResourceManager.h" #include "../Core/ConfigFile.h" diff --git a/resources/DefaultInput.ini b/resources/DefaultInput.ini index 95ebbc22..5ed46241 100644 --- a/resources/DefaultInput.ini +++ b/resources/DefaultInput.ini @@ -6,10 +6,10 @@ InvertPitch=false MouseLeft=PrimaryFire MouseX=Yaw MouseY=Pitch -W=+Forward -S=-Forward -D=+Right -A=-Right +W=Forward,1 +S=Forward,-1 +D=Right,1 +A=Right,-1 R=Reload Space=Jump LeftControl=Crouch diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp index ad589df3..7e977542 100644 --- a/src/Engine/Input/InputProxy.cpp +++ b/src/Engine/Input/InputProxy.cpp @@ -20,15 +20,16 @@ void InputProxy::LoadBindings(std::string file) for (auto& origin : config->GetAll("Bindings")) { Events::BindOrigin e; e.Origin = origin.first; - e.Command = origin.second; - e.Value = 1.f; - if (!e.Command.empty()) { - char prefix = e.Command.at(0); - if (prefix == '+' || prefix == '-') { - e.Command = e.Command.substr(1); - if (prefix == '-') { - e.Value *= -1.f; - } + const std::string& command = origin.second; + if (!command.empty()) { + boost::char_separator separator(", "); + boost::tokenizer tokenizer(command, separator); + auto token = tokenizer.begin(); + e.Command = *token; + if (++token != tokenizer.end()) { + e.Value = boost::lexical_cast(*token); + } else { + e.Value = 1.f; } OnBindOrigin(e); } @@ -62,7 +63,7 @@ void InputProxy::Process() e.Command = command; e.Value = currentValue; m_EventBroker->Publish(e); - //LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); + LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); m_LastCommandValues[command] = currentValue; } } @@ -78,7 +79,7 @@ void InputProxy::Process() } //e.Value = std::max(-1.f, std::min(e.Value, 1.f)); m_EventBroker->Publish(e); - //LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); + LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); } m_CommandQueue.clear(); } From d28583ca9ecb724c13240689f29c2a3135264e90 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 15 Jan 2016 17:20:11 +0100 Subject: [PATCH 36/48] Fixes for #34 (Licenses) --- README.md | 4 +- resources/Licenses/OpenAL.txt | 510 ++++++++++++++++++++++++++++++++++ tools/deploy.bat | 4 +- 3 files changed, 515 insertions(+), 3 deletions(-) create mode 100644 resources/Licenses/OpenAL.txt diff --git a/README.md b/README.md index a5681631..905af8ee 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ Libraries bundled along with binaries for Windows (MSVC14), available as a submo | **[libpng](http://www.libpng.org/pub/png/libpng.html)** | 1.6.19 | [libpng License](http://www.libpng.org/pub/png/src/libpng-LICENSE.txt) | | **[Xerces-C++](https://xerces.apache.org/xerces-c)** | 3.1.2 | [Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) | | **[ImGui](https://github.com/ocornut/imgui)** | 2015-12-12 | [MIT License](https://github.com/ocornut/imgui/blob/de3a154f3801de22c8e0bd2aeabf663a70c05972/LICENSE) | -| **[nativefiledialog](https://github.com/mlabbe/nativefiledialog) | 2016-01-08 | https://github.com/mlabbe/nativefiledialog/blob/master/LICENSE | -| **[OpenAL](https://www.openal.org/)** | 1.1 | [OpenAL License]() | +| **[nativefiledialog](https://github.com/mlabbe/nativefiledialog)** | 2016-01-08 | [nativefiledialog Licence](https://github.com/mlabbe/nativefiledialog/blob/master/LICENSE) | +| **[OpenAL](https://www.openal.org/)** | 1.1 | [OpenAL License](resources/Licenses/OpenAL.txt) #### External libraries Libraries that are too big to be bundled with the project. diff --git a/resources/Licenses/OpenAL.txt b/resources/Licenses/OpenAL.txt new file mode 100644 index 00000000..c89492b2 --- /dev/null +++ b/resources/Licenses/OpenAL.txt @@ -0,0 +1,510 @@ +This file is part of the OpenAL software. + +The licenses which components of this software fall under are as follows. +All components are under a LGPL license. + + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library 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 2.1 of the License, or (at your option) any later version. + + This library 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 this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/tools/deploy.bat b/tools/deploy.bat index f629058c..29dc9e62 100755 --- a/tools/deploy.bat +++ b/tools/deploy.bat @@ -31,8 +31,10 @@ ECHO Deploying %1 binaries to %DeployLocation% COPY "deps\bin\%1\x64\*.dll" "%DeployLocation%" :: Licenses -::ECHO Copying licenses %DeployLocation% +ECHO Copying licenses to %DeployLocation% ::COPY "libs\assimp-3.1.1\LICENSE" "%ConfigPath%\Assimp License.txt" ::COPY "libs\glew-1.11.0\LICENSE.txt" "%ConfigPath%\GLEW License.txt" ::COPY "libs\glm-0.9.5.4\copying.txt" "%ConfigPath%\GLM License.txt" ::COPY "libs\assimp-3.1.1\LICENSE" "%ConfigPath%\Assimp License.txt" +RMDIR "%DeployLocation%\Licenses" +MKLINK "%DeployLocation%\Licenses\" "resources\Licenses\" /J From 97e12ff55054c6a497ac847c18b1549495afe064 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 15 Jan 2016 18:31:32 +0100 Subject: [PATCH 37/48] Made EntityFileParser::MergeEntities return the base entity that was created, and also take an optional parent entity to create it under. --- include/Engine/Core/EntityFile.h | 2 +- include/Engine/Core/EntityFileParser.h | 3 ++- src/Engine/Core/EntityFileParser.cpp | 8 ++++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/Engine/Core/EntityFile.h b/include/Engine/Core/EntityFile.h index 27115264..b65c9f7d 100644 --- a/include/Engine/Core/EntityFile.h +++ b/include/Engine/Core/EntityFile.h @@ -77,7 +77,7 @@ public: : m_Handler(handler) , m_Reader(reader) { - // 0 is imaginary world entity + // 0 is imaginary base parent m_EntityStack.push(0); m_StateStack.push(State::Unknown); } diff --git a/include/Engine/Core/EntityFileParser.h b/include/Engine/Core/EntityFileParser.h index d46b508b..b4eee5b2 100644 --- a/include/Engine/Core/EntityFileParser.h +++ b/include/Engine/Core/EntityFileParser.h @@ -9,12 +9,13 @@ class EntityFileParser public: EntityFileParser(const EntityFile* entityFile); - void MergeEntities(World* world); + EntityID MergeEntities(World* world, EntityID baseParent = EntityID_Invalid); private: const EntityFile* m_EntityFile; EntityFileHandler m_Handler; World* m_World = nullptr; + EntityID m_FirstEntity = EntityID_Invalid; // Maps EntityIDs local to the file to real IDs in the world after they've been // created in order to resolve parent-child relationships. std::map m_EntityIDMapper; diff --git a/src/Engine/Core/EntityFileParser.cpp b/src/Engine/Core/EntityFileParser.cpp index 541da01d..9f338d71 100644 --- a/src/Engine/Core/EntityFileParser.cpp +++ b/src/Engine/Core/EntityFileParser.cpp @@ -9,17 +9,21 @@ EntityFileParser::EntityFileParser(const EntityFile* entityFile) m_Handler.SetStartFieldDataCallback(std::bind(&EntityFileParser::onFieldData, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); } -void EntityFileParser::MergeEntities(World* world) +EntityID EntityFileParser::MergeEntities(World* world, EntityID baseParent /*= EntityID_Invalid */) { m_World = world; - m_EntityIDMapper[0] = 0; + m_EntityIDMapper[0] = baseParent; m_EntityFile->Parse(&m_Handler); + return m_FirstEntity; } void EntityFileParser::onStartEntity(EntityID entity, EntityID parent, const std::string& name) { EntityID realParent = m_EntityIDMapper.at(parent); EntityID realEntity = m_World->CreateEntity(realParent); + if (m_FirstEntity == EntityID_Invalid) { + m_FirstEntity = realEntity; + } if (!name.empty()) { m_World->SetName(realEntity, name); } From db1b972cd1738335e61aa3e5965b4738944cabc0 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 15 Jan 2016 18:32:47 +0100 Subject: [PATCH 38/48] Spawner, SpawnPoint and PlayerSpawn components, and base work on their systems --- assets | 2 +- include/Engine/Core/EntityWrapper.h | 5 ++ include/Engine/Core/EventBroker.h | 4 +- include/Engine/Core/World.h | 2 + include/Game/Events/ESpawnerSpawn.h | 18 +++++++ include/Game/Systems/PlayerSpawnSystem.h | 17 ++++++ include/Game/Systems/SpawnerSystem.h | 20 +++++++ resources/Schema/Components.xsd | 3 ++ resources/Schema/Components/PlayerSpawn.xml | 3 ++ resources/Schema/Components/PlayerSpawn.xsd | 18 +++++++ resources/Schema/Components/SpawnPoint.xml | 1 + resources/Schema/Components/SpawnPoint.xsd | 11 ++++ resources/Schema/Components/Spawner.xml | 3 ++ resources/Schema/Components/Spawner.xsd | 18 +++++++ resources/Schema/Entities/Player.xml | 1 + resources/Schema/Entities/SpawnTest.xml | 59 +++++++++++++++++++++ resources/Schema/Types/Entity.xsd | 3 ++ src/Engine/Core/World.cpp | 5 ++ src/Game/CMakeLists.txt | 9 +++- src/Game/Game.cpp | 4 ++ src/Game/Systems/PlayerSpawnSystem.cpp | 39 ++++++++++++++ src/Game/Systems/SpawnerSystem.cpp | 47 ++++++++++++++++ 22 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 include/Game/Events/ESpawnerSpawn.h create mode 100644 include/Game/Systems/PlayerSpawnSystem.h create mode 100644 include/Game/Systems/SpawnerSystem.h create mode 100644 resources/Schema/Components/PlayerSpawn.xml create mode 100644 resources/Schema/Components/PlayerSpawn.xsd create mode 100644 resources/Schema/Components/SpawnPoint.xml create mode 100644 resources/Schema/Components/SpawnPoint.xsd create mode 100644 resources/Schema/Components/Spawner.xml create mode 100644 resources/Schema/Components/Spawner.xsd create mode 100644 resources/Schema/Entities/SpawnTest.xml create mode 100644 src/Game/Systems/PlayerSpawnSystem.cpp create mode 100644 src/Game/Systems/SpawnerSystem.cpp diff --git a/assets b/assets index a0d1615e..a3c92ac8 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit a0d1615e515a4d7db0073cc987d1d593ca942471 +Subproject commit a3c92ac876dd061776c36d1594bd82264372f028 diff --git a/include/Engine/Core/EntityWrapper.h b/include/Engine/Core/EntityWrapper.h index 541dfe0d..4cc8bbea 100644 --- a/include/Engine/Core/EntityWrapper.h +++ b/include/Engine/Core/EntityWrapper.h @@ -7,6 +7,11 @@ class World; struct EntityWrapper { + EntityWrapper() + : World(nullptr) + , ID(EntityID_Invalid) + { } + EntityWrapper(::World* world, EntityID id) : World(world) , ID(id) diff --git a/include/Engine/Core/EventBroker.h b/include/Engine/Core/EventBroker.h index dde5242a..dc1babc0 100644 --- a/include/Engine/Core/EventBroker.h +++ b/include/Engine/Core/EventBroker.h @@ -43,7 +43,7 @@ template class EventRelay : public BaseEventRelay { public: - typedef std::function CallbackType; + typedef std::function CallbackType; EventRelay() : m_Callback(nullptr) @@ -65,7 +65,7 @@ template bool EventRelay::Receive(const std::shared_ptr event) { if (m_Callback != nullptr) { - return m_Callback(*static_cast(event.get())); + return m_Callback(*static_cast(event.get())); } else { return false; } diff --git a/include/Engine/Core/World.h b/include/Engine/Core/World.h index 3c666ceb..b201d4ac 100644 --- a/include/Engine/Core/World.h +++ b/include/Engine/Core/World.h @@ -34,6 +34,8 @@ public: EntityID GetParent(EntityID entity); // Change the parent of an entity void SetParent(EntityID entity, EntityID parent); + // Get children of an entity + const std::pair::const_iterator, std::unordered_multimap::const_iterator> GetChildren(EntityID entity); // Get all component pools const std::unordered_map& GetComponentPools() const { return m_ComponentPools; } // Get the entity children map diff --git a/include/Game/Events/ESpawnerSpawn.h b/include/Game/Events/ESpawnerSpawn.h new file mode 100644 index 00000000..7af4f63e --- /dev/null +++ b/include/Game/Events/ESpawnerSpawn.h @@ -0,0 +1,18 @@ +#ifndef ESpawnerSpawn_h__ +#define ESpawnerSpawn_h__ + +#include "Core/Event.h" +#include "Core/EntityWrapper.h" + +namespace Events +{ + +struct SpawnerSpawn : Event +{ + EntityWrapper Spawner; + EntityWrapper Parent; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Game/Systems/PlayerSpawnSystem.h b/include/Game/Systems/PlayerSpawnSystem.h new file mode 100644 index 00000000..414d32d8 --- /dev/null +++ b/include/Game/Systems/PlayerSpawnSystem.h @@ -0,0 +1,17 @@ +#include "Core/System.h" +#include "Input/EInputCommand.h" +#include "Events/ESpawnerSpawn.h" + +class PlayerSpawnSystem : public ImpureSystem +{ +public: + PlayerSpawnSystem(EventBroker* eventBroker); + + virtual void Update(World* world, double dt) override; + +private: + EventRelay m_OnInputCommand; + bool OnInputCommand(const Events::InputCommand& e); + + std::vector m_SpawnRequests; +}; \ No newline at end of file diff --git a/include/Game/Systems/SpawnerSystem.h b/include/Game/Systems/SpawnerSystem.h new file mode 100644 index 00000000..cbb7c100 --- /dev/null +++ b/include/Game/Systems/SpawnerSystem.h @@ -0,0 +1,20 @@ +#include +#include "Common.h" +#include "GLM.h" +#include "Core/System.h" +#include "Events/ESpawnerSpawn.h" +#include "Core/Transform.h" +#include "Core/ResourceManager.h" +#include "Core/EntityFileParser.h" + +class SpawnerSystem : public System +{ +public: + SpawnerSystem(EventBroker* eventBroker); + +private: + EventRelay m_OnSpawnerSpawn; + bool OnSpawnerSpawn(Events::SpawnerSpawn& e); + + void spawnEntity(EntityWrapper spawner, EntityID parent, glm::vec3 position); +}; \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 7d323df6..bcea14e9 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -12,4 +12,7 @@ + + + \ No newline at end of file diff --git a/resources/Schema/Components/PlayerSpawn.xml b/resources/Schema/Components/PlayerSpawn.xml new file mode 100644 index 00000000..b286e56f --- /dev/null +++ b/resources/Schema/Components/PlayerSpawn.xml @@ -0,0 +1,3 @@ + + 1 + \ No newline at end of file diff --git a/resources/Schema/Components/PlayerSpawn.xsd b/resources/Schema/Components/PlayerSpawn.xsd new file mode 100644 index 00000000..b076de27 --- /dev/null +++ b/resources/Schema/Components/PlayerSpawn.xsd @@ -0,0 +1,18 @@ + + + + + + + + Combined with a Spawner, defines a spawn point for a player team. + + + + + 1 = Spectator, 2 = Red, 3 = Blue + + + + + diff --git a/resources/Schema/Components/SpawnPoint.xml b/resources/Schema/Components/SpawnPoint.xml new file mode 100644 index 00000000..3f392555 --- /dev/null +++ b/resources/Schema/Components/SpawnPoint.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/SpawnPoint.xsd b/resources/Schema/Components/SpawnPoint.xsd new file mode 100644 index 00000000..67169a9c --- /dev/null +++ b/resources/Schema/Components/SpawnPoint.xsd @@ -0,0 +1,11 @@ + + + + + + + + Defines this entity as a spawn point for a parent Spawner + + + diff --git a/resources/Schema/Components/Spawner.xml b/resources/Schema/Components/Spawner.xml new file mode 100644 index 00000000..f2ac77a8 --- /dev/null +++ b/resources/Schema/Components/Spawner.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/resources/Schema/Components/Spawner.xsd b/resources/Schema/Components/Spawner.xsd new file mode 100644 index 00000000..dcf74aea --- /dev/null +++ b/resources/Schema/Components/Spawner.xsd @@ -0,0 +1,18 @@ + + + + + + + + Randomly selects a child SpawnPoint component and spawns a copy of an entity template when receiving a SpawnerSpawn event. If no SpawnPoint is found it spawns from its own position. + + + + + The entity template to spawn + + + + + diff --git a/resources/Schema/Entities/Player.xml b/resources/Schema/Entities/Player.xml index 6406a795..6c3b39c3 100644 --- a/resources/Schema/Entities/Player.xml +++ b/resources/Schema/Entities/Player.xml @@ -3,6 +3,7 @@ + Models/Core/UnitSphere.obj diff --git a/resources/Schema/Entities/SpawnTest.xml b/resources/Schema/Entities/SpawnTest.xml new file mode 100644 index 00000000..f4af726d --- /dev/null +++ b/resources/Schema/Entities/SpawnTest.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + Schema/Entities/Player.xml + + + + + + + + + + + Models/Core/UnitSphere.obj + + + + + + + + + + + + + Models/Core/UnitSphere.obj + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 88237697..e1681407 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -20,6 +20,9 @@ + + + diff --git a/src/Engine/Core/World.cpp b/src/Engine/Core/World.cpp index 811de45f..3104e4e9 100644 --- a/src/Engine/Core/World.cpp +++ b/src/Engine/Core/World.cpp @@ -125,6 +125,11 @@ void World::SetParent(EntityID entity, EntityID parent) m_EntityChildren.insert(std::make_pair(parent, entity)); } +const std::pair::const_iterator, std::unordered_multimap::const_iterator> World::GetChildren(EntityID entity) +{ + return m_EntityChildren.equal_range(entity); +} + void World::SetName(EntityID entity, const std::string& name) { m_EntityNames[entity] = name; diff --git a/src/Game/CMakeLists.txt b/src/Game/CMakeLists.txt index 4be61127..4aaff273 100644 --- a/src/Game/CMakeLists.txt +++ b/src/Game/CMakeLists.txt @@ -10,16 +10,23 @@ include_directories( ${Boost_INCLUDE_DIRS} ) -file(GLOB SOURCE_FILES +file(GLOB SOURCE_FILES_Systems "${INCLUDE_PATH}/Systems/*.h" "Systems/*.cpp" ) source_group(Systems FILES ${SOURCE_FILES_Systems}) +file(GLOB SOURCE_FILES_Events + "${INCLUDE_PATH}/Events/*.h" + "Events/*.cpp" +) +source_group(Events FILES ${SOURCE_FILES_Events}) + set(SOURCE_FILES ${SOURCE_FILES} "Game.cpp" ${SOURCE_FILES_Systems} + ${SOURCE_FILES_Events} ) set(LIBRARIES diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index a3df754d..84b330c4 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -6,6 +6,8 @@ #include "Systems/PlayerSystem.h" #include "Systems/HealthSystem.h" #include "Systems/PlayerMovementSystem.h" +#include "Systems/SpawnerSystem.h" +#include "Systems/PlayerSpawnSystem.h" #include "Core/EntityFileWriter.h" Game::Game(int argc, char* argv[]) @@ -73,6 +75,8 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel); // Populate Octree with collidables ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); diff --git a/src/Game/Systems/PlayerSpawnSystem.cpp b/src/Game/Systems/PlayerSpawnSystem.cpp new file mode 100644 index 00000000..a522f679 --- /dev/null +++ b/src/Game/Systems/PlayerSpawnSystem.cpp @@ -0,0 +1,39 @@ +#include "Systems/PlayerSpawnSystem.h" + +PlayerSpawnSystem::PlayerSpawnSystem(EventBroker* eventBroker) + : System(eventBroker) +{ + EVENT_SUBSCRIBE_MEMBER(m_OnInputCommand, &PlayerSpawnSystem::OnInputCommand); +} + +void PlayerSpawnSystem::Update(World* world, double dt) +{ + auto componentPools = world->GetComponentPools(); + auto spawnerPool = componentPools.find("Spawner"); + if (spawnerPool == componentPools.end()) { + return; + } +; + for (auto& team : m_SpawnRequests) { + for (auto& spawner : *spawnerPool->second) { + Events::SpawnerSpawn e; + e.Spawner = EntityWrapper(world, spawner.EntityID); + m_EventBroker->Publish(e); + } + } + m_SpawnRequests.clear(); +} + +bool PlayerSpawnSystem::OnInputCommand(const Events::InputCommand& e) +{ + if (e.Command != "PickTeam") { + return false; + } + + if (e.Value != 0) { + m_SpawnRequests.push_back((int)e.Value); + } + + return true; +} + diff --git a/src/Game/Systems/SpawnerSystem.cpp b/src/Game/Systems/SpawnerSystem.cpp new file mode 100644 index 00000000..c1122d98 --- /dev/null +++ b/src/Game/Systems/SpawnerSystem.cpp @@ -0,0 +1,47 @@ +#include "Systems/SpawnerSystem.h" + +SpawnerSystem::SpawnerSystem(EventBroker* eventBroker) : System(eventBroker) +{ + EVENT_SUBSCRIBE_MEMBER(m_OnSpawnerSpawn, &SpawnerSystem::OnSpawnerSpawn); +} + +bool SpawnerSystem::OnSpawnerSpawn(Events::SpawnerSpawn& e) +{ + EntityWrapper& spawner = e.Spawner; + + auto children = spawner.World->GetChildren(spawner.ID); + std::vector spawnPoints; + for (auto kv = children.first; kv != children.second; ++kv) { + const EntityID& child = kv->second; + if (spawner.World->HasComponent(child, "SpawnPoint")) { + spawnPoints.push_back(child); + } + } + + EntityID spawnPoint = spawner.ID; + if (!spawnPoints.empty()) { + // Select a random spawn point + static std::random_device randomDevice; + static std::mt19937 randomGenerator(randomDevice()); + std::uniform_int_distribution<> distribution(0, std::distance(spawnPoints.begin(), spawnPoints.end()) - 1); + auto randomSpawnPointIt = spawnPoints.begin(); + std::advance(randomSpawnPointIt, distribution(randomGenerator)); + spawnPoint = *randomSpawnPointIt; + } + spawnEntity(spawner, e.Parent.ID, Transform::AbsolutePosition(spawner.World, spawnPoint)); + + return true; +} + +void SpawnerSystem::spawnEntity(EntityWrapper spawner, EntityID parent, glm::vec3 position) +{ + const std::string& entityFilePath = spawner["Spawner"]["EntityFile"]; + auto entityFile = ResourceManager::Load(entityFilePath); + if (entityFile == nullptr) { + return; + } + + EntityFileParser parser(entityFile); + EntityWrapper spawnedEntity(spawner.World, parser.MergeEntities(spawner.World, parent)); + spawnedEntity["Transform"]["Position"] = position; +} \ No newline at end of file From 8bd5a428b902d1800a73595c351e142ebb62413c Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 15 Jan 2016 20:25:48 +0100 Subject: [PATCH 39/48] EntityFileParser now ignores component fields not present in component definition instead of crashing. --- src/Engine/Core/EntityFileParser.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Engine/Core/EntityFileParser.cpp b/src/Engine/Core/EntityFileParser.cpp index 9f338d71..1633bbc7 100644 --- a/src/Engine/Core/EntityFileParser.cpp +++ b/src/Engine/Core/EntityFileParser.cpp @@ -42,7 +42,12 @@ void EntityFileParser::onStartComponentField(EntityID entity, const std::string& { EntityID realEntity = m_EntityIDMapper.at(entity); ComponentWrapper component = m_World->GetComponent(realEntity, componentType); - auto& field = component.Info.Fields.at(fieldName); + auto fieldIt = component.Info.Fields.find(fieldName); + if (fieldIt == component.Info.Fields.end()) { + LOG_ERROR("Tried to set unknown field \"%s\" of component type \"%s\"! Ignoring.", fieldName.c_str(), componentType.c_str()); + return; + } + auto& field = fieldIt->second; LOG_DEBUG("Field \"%s\" type \"%s\"", fieldName.c_str(), field.Type.c_str()); LOG_DEBUG("Attributes:"); @@ -58,7 +63,11 @@ void EntityFileParser::onFieldData(EntityID entity, const std::string& component { EntityID realEntity = m_EntityIDMapper.at(entity); ComponentWrapper component = m_World->GetComponent(realEntity, componentType); - auto& field = component.Info.Fields.at(fieldName); + auto fieldIt = component.Info.Fields.find(fieldName); + if (fieldIt == component.Info.Fields.end()) { + return; + } + auto& field = fieldIt->second; char* data = component.Data + field.Offset; EntityFile::WriteValueData(data, field, fieldData); From bbabc841273027376b5eb29cb32c8a7f251fc029 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 15 Jan 2016 20:26:30 +0100 Subject: [PATCH 40/48] Fixed default values for CPlayerSpawn --- resources/Schema/Components/PlayerSpawn.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/Schema/Components/PlayerSpawn.xml b/resources/Schema/Components/PlayerSpawn.xml index b286e56f..2082822e 100644 --- a/resources/Schema/Components/PlayerSpawn.xml +++ b/resources/Schema/Components/PlayerSpawn.xml @@ -1,3 +1,3 @@ - - 1 - \ No newline at end of file + + 1 + \ No newline at end of file From f65ac65e30ba0d55f0dfc1f00de54c9d968ec982 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 16 Jan 2016 17:03:00 +0100 Subject: [PATCH 41/48] Updated component defaults files to match component schema and made schema validation strict. --- resources/Schema/Components/AABB.xml | 5 ++-- resources/Schema/Components/Camera.xml | 5 ++-- resources/Schema/Components/Collidable.xml | 5 ++-- resources/Schema/Components/Health.xml | 5 ++-- resources/Schema/Components/Listener.xml | 5 ++-- resources/Schema/Components/Model.xml | 5 ++-- resources/Schema/Components/Physics.xml | 5 ++-- resources/Schema/Components/Player.xml | 5 ++-- resources/Schema/Components/PlayerSpawn.xml | 5 ++-- resources/Schema/Components/PointLight.xml | 5 ++-- resources/Schema/Components/PointLight.xsd | 9 +++++- resources/Schema/Components/RaptorCopter.xml | 5 ++-- resources/Schema/Components/SoundEmitter.xml | 5 ++-- resources/Schema/Components/SoundEmitter.xsd | 30 ++++++++++++-------- resources/Schema/Components/SpawnPoint.xml | 3 +- resources/Schema/Components/Spawner.xml | 5 ++-- resources/Schema/Components/Transform.xml | 5 ++-- resources/Schema/Components/Trigger.xml | 5 ++-- resources/Schema/Types/Entity.xsd | 1 + src/Engine/Core/EntityFile.cpp | 2 ++ src/Engine/Core/EntityFilePreprocessor.cpp | 4 +++ 21 files changed, 79 insertions(+), 45 deletions(-) diff --git a/resources/Schema/Components/AABB.xml b/resources/Schema/Components/AABB.xml index 6996b3dc..9c909ea1 100644 --- a/resources/Schema/Components/AABB.xml +++ b/resources/Schema/Components/AABB.xml @@ -1,4 +1,5 @@ - + + - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/Camera.xml b/resources/Schema/Components/Camera.xml index 4f613ded..ccb12f01 100644 --- a/resources/Schema/Components/Camera.xml +++ b/resources/Schema/Components/Camera.xml @@ -1,6 +1,7 @@ - + + cam 45 0.01 5000 - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/Collidable.xml b/resources/Schema/Components/Collidable.xml index 4f9c9033..9046ea99 100644 --- a/resources/Schema/Components/Collidable.xml +++ b/resources/Schema/Components/Collidable.xml @@ -1,2 +1,3 @@ - - \ No newline at end of file + + + \ No newline at end of file diff --git a/resources/Schema/Components/Health.xml b/resources/Schema/Components/Health.xml index 143a91d1..f53217b9 100644 --- a/resources/Schema/Components/Health.xml +++ b/resources/Schema/Components/Health.xml @@ -1,4 +1,5 @@ - + + 100 100 - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/Listener.xml b/resources/Schema/Components/Listener.xml index 7d1fac13..d7e20f92 100644 --- a/resources/Schema/Components/Listener.xml +++ b/resources/Schema/Components/Listener.xml @@ -1,3 +1,2 @@ - - - \ No newline at end of file + + \ No newline at end of file diff --git a/resources/Schema/Components/Model.xml b/resources/Schema/Components/Model.xml index bd20147f..8f78b9ee 100644 --- a/resources/Schema/Components/Model.xml +++ b/resources/Schema/Components/Model.xml @@ -1,5 +1,6 @@ - + + true - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/Physics.xml b/resources/Schema/Components/Physics.xml index 1ad81de0..7dce027c 100644 --- a/resources/Schema/Components/Physics.xml +++ b/resources/Schema/Components/Physics.xml @@ -1,3 +1,4 @@ - + + - + diff --git a/resources/Schema/Components/Player.xml b/resources/Schema/Components/Player.xml index 190f2ed0..caefd6e6 100644 --- a/resources/Schema/Components/Player.xml +++ b/resources/Schema/Components/Player.xml @@ -1,7 +1,8 @@ - + + false false false false - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/PlayerSpawn.xml b/resources/Schema/Components/PlayerSpawn.xml index 2082822e..1c621ef9 100644 --- a/resources/Schema/Components/PlayerSpawn.xml +++ b/resources/Schema/Components/PlayerSpawn.xml @@ -1,3 +1,4 @@ - + + 1 - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/PointLight.xml b/resources/Schema/Components/PointLight.xml index 6b1382db..814d7f4c 100644 --- a/resources/Schema/Components/PointLight.xml +++ b/resources/Schema/Components/PointLight.xml @@ -1,7 +1,8 @@ - + + 1.0 0.8 0.3 true - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/PointLight.xsd b/resources/Schema/Components/PointLight.xsd index 039e7117..9b802d8c 100644 --- a/resources/Schema/Components/PointLight.xsd +++ b/resources/Schema/Components/PointLight.xsd @@ -12,7 +12,14 @@ - + + + + + + + + diff --git a/resources/Schema/Components/RaptorCopter.xml b/resources/Schema/Components/RaptorCopter.xml index cc1ece52..1dec4a44 100644 --- a/resources/Schema/Components/RaptorCopter.xml +++ b/resources/Schema/Components/RaptorCopter.xml @@ -1,4 +1,5 @@ - + + 0 - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/SoundEmitter.xml b/resources/Schema/Components/SoundEmitter.xml index 11093e37..e0a38d2f 100644 --- a/resources/Schema/Components/SoundEmitter.xml +++ b/resources/Schema/Components/SoundEmitter.xml @@ -1,4 +1,5 @@ - + + 1.0 1.0 @@ -6,4 +7,4 @@ 20.0 1.0 1.0 - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/SoundEmitter.xsd b/resources/Schema/Components/SoundEmitter.xsd index c734e6ba..9a73949b 100644 --- a/resources/Schema/Components/SoundEmitter.xsd +++ b/resources/Schema/Components/SoundEmitter.xsd @@ -7,18 +7,24 @@ - - The "volume" of the emitter. A value betweeen 0-1 - - The pitch of the emitter. A value betweeen 0-1 - - If the sound should loop or not. - - The distance where there will no longer be any attenuation. - - The rolloff rate of the source. - - The distance that the source will be the loudest. + + The "volume" of the emitter. A value betweeen 0-1 + + + The pitch of the emitter. A value betweeen 0-1 + + + If the sound should loop or not. + + + The distance where there will no longer be any attenuation. + + + The rolloff rate of the source. + + + The distance that the source will be the loudest. + diff --git a/resources/Schema/Components/SpawnPoint.xml b/resources/Schema/Components/SpawnPoint.xml index 3f392555..6b2f7ae3 100644 --- a/resources/Schema/Components/SpawnPoint.xml +++ b/resources/Schema/Components/SpawnPoint.xml @@ -1 +1,2 @@ - \ No newline at end of file + + \ No newline at end of file diff --git a/resources/Schema/Components/Spawner.xml b/resources/Schema/Components/Spawner.xml index f2ac77a8..da659c69 100644 --- a/resources/Schema/Components/Spawner.xml +++ b/resources/Schema/Components/Spawner.xml @@ -1,3 +1,4 @@ - + + - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/Transform.xml b/resources/Schema/Components/Transform.xml index 00202aa4..7e2bcd83 100644 --- a/resources/Schema/Components/Transform.xml +++ b/resources/Schema/Components/Transform.xml @@ -1,5 +1,6 @@ - + + - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/Trigger.xml b/resources/Schema/Components/Trigger.xml index 4c8aad58..38c6fce9 100644 --- a/resources/Schema/Components/Trigger.xml +++ b/resources/Schema/Components/Trigger.xml @@ -1,2 +1,3 @@ - - \ No newline at end of file + + + \ No newline at end of file diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index f64bae3b..8fc52e1e 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -16,6 +16,7 @@ + diff --git a/src/Engine/Core/EntityFile.cpp b/src/Engine/Core/EntityFile.cpp index 1e381df5..5589eddf 100644 --- a/src/Engine/Core/EntityFile.cpp +++ b/src/Engine/Core/EntityFile.cpp @@ -23,6 +23,8 @@ void EntityFile::Parse(const EntityFileHandler* handler) const EntityFileSAXHandler saxHandler(handler, nullptr); m_SAX2XMLReader->setFeature(XMLUni::fgXercesCacheGrammarFromParse, true); m_SAX2XMLReader->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true); + m_SAX2XMLReader->setFeature(XMLUni::fgXercesSchema, true); + m_SAX2XMLReader->setFeature(XMLUni::fgXercesSchemaFullChecking, true); m_SAX2XMLReader->setContentHandler(&saxHandler); m_SAX2XMLReader->setErrorHandler(&saxHandler); m_SAX2XMLReader->setDeclarationHandler(&saxHandler); diff --git a/src/Engine/Core/EntityFilePreprocessor.cpp b/src/Engine/Core/EntityFilePreprocessor.cpp index 4b720976..f9aae3d4 100644 --- a/src/Engine/Core/EntityFilePreprocessor.cpp +++ b/src/Engine/Core/EntityFilePreprocessor.cpp @@ -174,7 +174,11 @@ void EntityFilePreprocessor::parseDefaults() memset(ci.second.Defaults.get(), 0, ci.second.Meta.Stride); XercesDOMParser parser(nullptr, XMLPlatformUtils::fgMemoryManager); + parser.setDoSchema(true); + parser.setDoNamespaces(true); parser.setErrorHandler(&errorHandler); + parser.setValidationScheme(XercesDOMParser::Val_Always); + parser.setValidationSchemaFullChecking(true); std::string componentName = ci.first; LOG_DEBUG("Parsing defaults for component %s", componentName.c_str()); From 9ec32464bc6d9b46753799ad0b23c01999dfbf98 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 16 Jan 2016 22:48:08 +0100 Subject: [PATCH 42/48] Added enum support to entity file format, see "Team" component for an example. Use ComponentInfo::Enum to convert enum string keys to its corresponding integer representation at runtime. --- include/Engine/Core/ComponentInfo.h | 6 +- include/Engine/Core/ComponentPool.h | 2 +- include/Engine/Core/ComponentWrapper.h | 19 +- include/Engine/Core/EntityFactory.h | 16 +- include/Engine/Core/EntityFile.h | 165 ++-------------- include/Engine/Core/EntityFilePreprocessor.h | 2 + resources/Schema/Components.xsd | 1 + resources/Schema/Components/Team.xml | 4 + resources/Schema/Components/Team.xsd | 28 +++ resources/Schema/Types.xsd | 3 + resources/Schema/Types/Entity.xsd | 1 + src/Engine/Core/EntityFile.cpp | 187 ++++++++++++++++++- src/Engine/Core/EntityFilePreprocessor.cpp | 139 +++++++++----- src/Engine/Core/EntityFileWriter.cpp | 2 +- src/Engine/Core/World.cpp | 2 +- src/Engine/Editor/EditorSystem.cpp | 24 ++- src/Tests/ComponentPoolTest.cpp | 4 +- 17 files changed, 383 insertions(+), 222 deletions(-) create mode 100755 resources/Schema/Components/Team.xml create mode 100755 resources/Schema/Components/Team.xsd diff --git a/include/Engine/Core/ComponentInfo.h b/include/Engine/Core/ComponentInfo.h index 985a057e..def2a323 100644 --- a/include/Engine/Core/ComponentInfo.h +++ b/include/Engine/Core/ComponentInfo.h @@ -9,7 +9,8 @@ struct ComponentInfo { std::string Annotation; unsigned int Allocation = 0; - unsigned int Stride = 0; + std::map FieldAnnotations; + std::map> FieldEnumDefinitions; }; struct Field_t @@ -23,8 +24,9 @@ struct ComponentInfo std::string Name; std::unordered_map Fields; std::vector FieldsInOrder; - Meta_t Meta; + unsigned int Stride = 0; std::shared_ptr Defaults = nullptr; + std::shared_ptr Meta = nullptr; }; template<> diff --git a/include/Engine/Core/ComponentPool.h b/include/Engine/Core/ComponentPool.h index 619aade8..ed81d72e 100644 --- a/include/Engine/Core/ComponentPool.h +++ b/include/Engine/Core/ComponentPool.h @@ -43,7 +43,7 @@ public: ComponentPool(const ::ComponentInfo& ci) : m_ComponentInfo(ci) - , m_Pool(ci.Meta.Allocation, sizeof(EntityID) + ci.Meta.Stride) + , m_Pool(ci.Meta->Allocation, sizeof(EntityID) + ci.Stride) { } ComponentPool(const ComponentPool& other) = delete; ComponentPool(const ComponentPool&& other) = delete; diff --git a/include/Engine/Core/ComponentWrapper.h b/include/Engine/Core/ComponentWrapper.h index 922b3d79..9d27b0f8 100644 --- a/include/Engine/Core/ComponentWrapper.h +++ b/include/Engine/Core/ComponentWrapper.h @@ -6,6 +6,7 @@ #include "ComponentInfo.h" #include "Util/Any.h" +// TODO: Change all instances "Property" to "Field" to remain consistent with ComponentInfo struct ComponentWrapper { ComponentWrapper(const ComponentInfo& componentInfo, char* data) @@ -18,6 +19,11 @@ struct ComponentWrapper const ::EntityID EntityID; char* Data; + int Enum(const char* fieldName, const char* enumKey) + { + return Info.Meta->FieldEnumDefinitions.at(fieldName).at(enumKey); + } + template T& Property(std::string name) { @@ -33,7 +39,7 @@ struct ComponentWrapper // Specialization for string literals template void SetProperty(std::string name, const char(&value)[N]) { Property(name) = std::string(value); } - + struct SubscriptProxy { friend struct ComponentWrapper; @@ -47,6 +53,9 @@ struct ComponentWrapper std::string m_PropertyName; public: + // Return the integer value of an enum type key for this field + int Enum(const char* enumKey) { return m_Component->Enum(m_PropertyName.c_str(), enumKey); } + template operator T&() { return m_Component->Property(m_PropertyName); } @@ -71,7 +80,7 @@ public: ComponentWrapperFactory(std::string componentTypeName, std::size_t allocation = 0) { m_ComponentInfo.Name = componentTypeName; - m_ComponentInfo.Meta.Allocation = allocation; + m_ComponentInfo.Meta->Allocation = allocation; } template @@ -79,14 +88,14 @@ public: { m_DefaultValues.push_back(defaultValue); m_ComponentInfo.Fields[fieldName].Name = typeid(T).name(); - m_ComponentInfo.Fields[fieldName].Offset = m_ComponentInfo.Meta.Stride; + m_ComponentInfo.Fields[fieldName].Offset = m_ComponentInfo.Stride; m_ComponentInfo.Fields[fieldName].Stride = sizeof(T); - m_ComponentInfo.Meta.Stride += sizeof(T); + m_ComponentInfo.Stride += sizeof(T); } ComponentInfo& Finalize() { - m_ComponentInfo.Defaults = std::shared_ptr(new char[m_ComponentInfo.Meta.Stride]); + m_ComponentInfo.Defaults = std::shared_ptr(new char[m_ComponentInfo.Stride]); std::size_t offset = 0; for (auto& val : m_DefaultValues) { memcpy(m_ComponentInfo.Defaults.get() + offset, val.Data.get(), val.Size); diff --git a/include/Engine/Core/EntityFactory.h b/include/Engine/Core/EntityFactory.h index d1dbce03..c3c964bc 100644 --- a/include/Engine/Core/EntityFactory.h +++ b/include/Engine/Core/EntityFactory.h @@ -285,7 +285,7 @@ private: XSValue::Status status; XSValue* val = XSValue::getActualValue(child->getNodeValue(), XSValue::dt_integer, status); - compInfo.Meta.Allocation += val->fData.fValue.f_int; + compInfo.Meta->Allocation += val->fData.fValue.f_int; } // Save documentation string @@ -293,11 +293,11 @@ private: if (documentationTags->getLength() != 0) { auto child = documentationTags->item(0)->getFirstChild(); if (child != nullptr) { - compInfo.Meta.Annotation = XSTR(child->getNodeValue()); + compInfo.Meta->Annotation = XSTR(child->getNodeValue()); } } // TODO: Parse annotation string XML - // compInfo.Meta.Allocation = ... + // compInfo.Meta->Allocation = ... } else { std::cout << "Warning: Component is missing an annotation!" << std::endl; } @@ -344,7 +344,7 @@ private: fieldOffset += getTypeStride(type); } - compInfo.Meta.Stride = fieldOffset; + compInfo.Stride = fieldOffset; m_ComponentInfo[compInfo.Name] = compInfo; } } @@ -367,14 +367,14 @@ private: std::string componentName = XSTR(component->getLocalName()); auto& compInfo = m_ComponentInfo.at(componentName); - compInfo.Meta.Allocation += 1; + compInfo.Meta->Allocation += 1; } std::cout << "COMPONENT INFO" << std::endl; for (auto& pair : m_ComponentInfo) { ComponentInfo& ci = pair.second; - std::cout << "Component: " << ci.Name << " (" << ci.Meta.Annotation << ")" << std::endl; - std::cout << " Allocation: " << ci.Meta.Allocation << std::endl; + std::cout << "Component: " << ci.Name << " (" << ci.Meta->Annotation << ")" << std::endl; + std::cout << " Allocation: " << ci.Meta->Allocation << std::endl; std::cout << " Fields:" << std::endl; // Calculate component size @@ -393,7 +393,7 @@ private: cs.ComponentName = ci.Name; cs.Stride = stride; cs.Info = ci; - cs.Data = new char[stride*ci.Meta.Allocation]; + cs.Data = new char[stride*ci.Meta->Allocation]; m_ComponentStore[cs.ComponentName] = cs; } } diff --git a/include/Engine/Core/EntityFile.h b/include/Engine/Core/EntityFile.h index b65c9f7d..2549ed06 100644 --- a/include/Engine/Core/EntityFile.h +++ b/include/Engine/Core/EntityFile.h @@ -73,88 +73,15 @@ public: ComponentField }; - EntityFileSAXHandler(const EntityFileHandler* handler, xercesc::SAX2XMLReader* reader) - : m_Handler(handler) - , m_Reader(reader) - { - // 0 is imaginary base parent - m_EntityStack.push(0); - m_StateStack.push(State::Unknown); - } + EntityFileSAXHandler(const EntityFileHandler* handler, xercesc::SAX2XMLReader* reader); - void startElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname, const xercesc::Attributes& attrs) override - { - std::string name = XS::ToString(_localName); + void startElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname, const xercesc::Attributes& attrs) override; + void characters(const XMLCh* const chars, const XMLSize_t length) override; + void endElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname) override; - if (m_StateStack.top() == State::Unknown || m_StateStack.top() == State::Entity) { - if (name == "Entity") { - m_StateStack.push(State::Entity); - onStartEntity(attrs); - return; - } - if (name == "EntityRef") { - onStartEntityRef(attrs); - return; - } - } - - std::string uri = XS::ToString(_uri); - if (m_StateStack.top() == State::Entity) { - if (uri == "components") { - m_StateStack.push(State::Component); - onStartComponent(name); - return; - } - } - - if (m_StateStack.top() == State::Component) { - m_StateStack.push(State::ComponentField); - onStartComponentField(name, attrs); - return; - } - } - - void characters(const XMLCh* const chars, const XMLSize_t length) override - { - if (m_StateStack.top() == State::ComponentField) { - char* transcoded = xercesc::XMLString::transcode(chars); - onFieldData(transcoded); - } - } - - void endElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname) override - { - std::string name = XS::ToString(_localName); - if (m_StateStack.top() == State::Entity) { - if (name == "Entity") { - m_StateStack.pop(); - onEndEntity(); - return; - } - } - - std::string uri = XS::ToString(_uri); - if (m_StateStack.top() == State::Component) { - //if (uri == "components") { - m_StateStack.pop(); - onEndComponent(name); - return; - //} - } - - if (m_StateStack.top() == State::ComponentField) { - m_StateStack.pop(); - onEndComponentField(name); - return; - } - } - - void fatalError(const xercesc::SAXParseException& e) - { - XS::ToString s(e.getMessage()); - LOG_ERROR("SAXParseException: %s", ((std::string)s).c_str()); - //throw e; - } + void warning(const xercesc::SAXParseException& e); + void error(const xercesc::SAXParseException& e); + void fatalError(const xercesc::SAXParseException& e); private: const EntityFileHandler* m_Handler; @@ -168,73 +95,14 @@ private: std::string m_CurrentField; std::map m_CurrentAttributes; - void onStartEntity(const xercesc::Attributes& attrs) - { - EntityID parent = m_EntityStack.top(); - - if (m_Handler->m_OnStartEntityCallback) { - std::string name; - auto xName = attrs.getValue(XS::ToXMLCh("name")); - if (xName != nullptr) { - name = XS::ToString(xName); - } - m_Handler->m_OnStartEntityCallback(m_NextEntityID, parent, name); - } - - m_EntityStack.push(m_NextEntityID); - m_NextEntityID++; - } - void onEndEntity() - { - m_EntityStack.pop(); - } - void onStartEntityRef(const xercesc::Attributes& attrs) - { - std::string path = XS::ToString(attrs.getValue(XS::ToXMLCh("file"))); - - xercesc::SAX2XMLReader* parser = xercesc::XMLReaderFactory::createXMLReader(); - parser->setContentHandler(this); - parser->setErrorHandler(this); - parser->parse(path.c_str()); - delete parser; - } - void onStartComponent(const std::string& name) - { - //LOG_DEBUG(" Component: %s", name.c_str()); - m_CurrentComponent = name; - if (m_Handler->m_OnStartComponentCallback) { - m_Handler->m_OnStartComponentCallback(m_EntityStack.top(), name); - } - } - void onEndComponent(const std::string& name) { } - void onStartComponentField(const std::string& field, const xercesc::Attributes& attrs) - { - //LOG_DEBUG(" Field: %s", field.c_str()); - m_CurrentField = field; - m_CurrentAttributes.clear(); - for (int i = 0; i < attrs.getLength(); i++) { - auto name = attrs.getQName(i); - auto value = attrs.getValue(name); - //LOG_DEBUG(" %s = %s", (char*)XS::ToString(name), (char*)XS::ToString(value)); - m_CurrentAttributes[XS::ToString(name).operator std::string()] = XS::ToString(value).operator std::string(); - } - - if (m_Handler->m_OnStartFieldCallback) { - m_Handler->m_OnStartFieldCallback(m_EntityStack.top(), m_CurrentComponent, field, m_CurrentAttributes); - } - } - void onEndComponentField(const std::string& field) { } - - void onFieldData(char* data) - { - //LOG_DEBUG(" Data: %s", data); - - if (m_Handler->m_OnStartFieldDataCallback) { - m_Handler->m_OnStartFieldDataCallback(m_EntityStack.top(), m_CurrentComponent, m_CurrentField, data); - } - - xercesc::XMLString::release(&data); - } + void onStartEntity(const xercesc::Attributes& attrs); + void onEndEntity(); + void onStartEntityRef(const xercesc::Attributes& attrs); + void onStartComponent(const std::string& name); + void onEndComponent(const std::string& name); + void onStartComponentField(const std::string& field, const xercesc::Attributes& attrs); + void onEndComponentField(const std::string& field); + void onFieldData(char* data); }; class EntityFileXMLErrorHandler : public xercesc::ErrorHandler @@ -269,6 +137,7 @@ private: class EntityFile : public Resource { friend class ResourceManager; + friend class EntityFileSAXHandler; private: EntityFile(boost::filesystem::path path); ~EntityFile(); @@ -288,6 +157,8 @@ private: xercesc::SAX2XMLReader* m_SAX2XMLReader; //std::map m_ComponentInfo; //std::vector m_EntityReferences; + + static void setReaderFeatures(xercesc::SAX2XMLReader* reader); }; #endif \ No newline at end of file diff --git a/include/Engine/Core/EntityFilePreprocessor.h b/include/Engine/Core/EntityFilePreprocessor.h index 3c3998b1..3139169f 100644 --- a/include/Engine/Core/EntityFilePreprocessor.h +++ b/include/Engine/Core/EntityFilePreprocessor.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ private: void onStartComponent(EntityID entity, std::string type); void parseComponentInfo(); void parseDefaults(); + std::string parseAnnotationXML(const XMLCh* xml); }; #endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 291cf938..88f09d6c 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -18,4 +18,5 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/Team.xml b/resources/Schema/Components/Team.xml new file mode 100755 index 00000000..3eeb93bf --- /dev/null +++ b/resources/Schema/Components/Team.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/resources/Schema/Components/Team.xsd b/resources/Schema/Components/Team.xsd new file mode 100755 index 00000000..163d4a7f --- /dev/null +++ b/resources/Schema/Components/Team.xsd @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + Represents entity team affiliation + + + + + + + + diff --git a/resources/Schema/Types.xsd b/resources/Schema/Types.xsd index cd464201..fb584c64 100644 --- a/resources/Schema/Types.xsd +++ b/resources/Schema/Types.xsd @@ -4,6 +4,9 @@ + + + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 8fc52e1e..3ca6e545 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -27,6 +27,7 @@ + diff --git a/src/Engine/Core/EntityFile.cpp b/src/Engine/Core/EntityFile.cpp index 5589eddf..c1125e10 100644 --- a/src/Engine/Core/EntityFile.cpp +++ b/src/Engine/Core/EntityFile.cpp @@ -21,16 +21,24 @@ void EntityFile::Parse(const EntityFileHandler* handler) const using namespace xercesc; EntityFileSAXHandler saxHandler(handler, nullptr); - m_SAX2XMLReader->setFeature(XMLUni::fgXercesCacheGrammarFromParse, true); - m_SAX2XMLReader->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true); - m_SAX2XMLReader->setFeature(XMLUni::fgXercesSchema, true); - m_SAX2XMLReader->setFeature(XMLUni::fgXercesSchemaFullChecking, true); + setReaderFeatures(m_SAX2XMLReader); m_SAX2XMLReader->setContentHandler(&saxHandler); m_SAX2XMLReader->setErrorHandler(&saxHandler); m_SAX2XMLReader->setDeclarationHandler(&saxHandler); m_SAX2XMLReader->parse(m_FilePath.string().c_str()); } +void EntityFile::setReaderFeatures(xercesc::SAX2XMLReader* reader) +{ + using namespace xercesc; + reader->setFeature(XMLUni::fgXercesCacheGrammarFromParse, true); + reader->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true); + reader->setFeature(XMLUni::fgSAX2CoreValidation, true); + reader->setFeature(XMLUni::fgSAX2CoreNameSpaces, true); + reader->setFeature(XMLUni::fgXercesSchema, true); + reader->setFeature(XMLUni::fgXercesSchemaFullChecking, true); +} + std::size_t EntityFile::GetTypeStride(std::string typeName) { std::map typeStrides{ @@ -39,6 +47,7 @@ std::size_t EntityFile::GetTypeStride(std::string typeName) { "float", sizeof(float) }, { "double", sizeof(double) }, { "string", sizeof(std::string) }, + { "enum", sizeof(int) }, { "Vector", sizeof(glm::vec3) }, { "Quaternion", sizeof(glm::quat) }, { "Color", sizeof(glm::vec4) } @@ -77,7 +86,7 @@ void EntityFile::WriteAttributeData(char* outData, const ComponentInfo::Field_t& void EntityFile::WriteValueData(char* outData, const ComponentInfo::Field_t& field, const char* valueData) { - if (field.Type == "int") { + if (field.Type == "int" || field.Type == "enum") { int value = boost::lexical_cast(valueData); memcpy(outData, reinterpret_cast(&value), field.Stride); } else if (field.Type == "float") { @@ -95,3 +104,171 @@ void EntityFile::WriteValueData(char* outData, const ComponentInfo::Field_t& fie LOG_WARNING("Unknown value data type: %s", field.Type.c_str()); } } + +EntityFileSAXHandler::EntityFileSAXHandler(const EntityFileHandler* handler, xercesc::SAX2XMLReader* reader) : m_Handler(handler) +, m_Reader(reader) +{ + // 0 is imaginary base parent + m_EntityStack.push(0); + m_StateStack.push(State::Unknown); +} + +void EntityFileSAXHandler::startElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname, const xercesc::Attributes& attrs) +{ + std::string name = XS::ToString(_localName); + + if (m_StateStack.top() == State::Unknown || m_StateStack.top() == State::Entity) { + if (name == "Entity") { + m_StateStack.push(State::Entity); + onStartEntity(attrs); + return; + } + if (name == "EntityRef") { + onStartEntityRef(attrs); + return; + } + } + + std::string uri = XS::ToString(_uri); + if (m_StateStack.top() == State::Entity) { + if (uri == "components") { + m_StateStack.push(State::Component); + onStartComponent(name); + return; + } + } + + if (m_StateStack.top() == State::Component) { + m_StateStack.push(State::ComponentField); + onStartComponentField(name, attrs); + return; + } +} + +void EntityFileSAXHandler::endElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname) +{ + std::string name = XS::ToString(_localName); + if (m_StateStack.top() == State::Entity) { + if (name == "Entity") { + m_StateStack.pop(); + onEndEntity(); + return; + } + } + + std::string uri = XS::ToString(_uri); + if (m_StateStack.top() == State::Component) { + //if (uri == "components") { + m_StateStack.pop(); + onEndComponent(name); + return; + //} + } + + if (m_StateStack.top() == State::ComponentField) { + m_StateStack.pop(); + onEndComponentField(name); + return; + } +} + +void EntityFileSAXHandler::characters(const XMLCh* const chars, const XMLSize_t length) +{ + if (m_StateStack.top() == State::ComponentField) { + char* transcoded = xercesc::XMLString::transcode(chars); + onFieldData(transcoded); + } +} + +void EntityFileSAXHandler::fatalError(const xercesc::SAXParseException& e) +{ + XS::ToString s(e.getMessage()); + LOG_ERROR("SAXParseException: %s", ((std::string)s).c_str()); + //throw e; +} + +void EntityFileSAXHandler::error(const xercesc::SAXParseException& e) +{ + XS::ToString s(e.getMessage()); + LOG_ERROR("SAXParseException: %s", ((std::string)s).c_str()); +} + +void EntityFileSAXHandler::warning(const xercesc::SAXParseException& e) +{ + XS::ToString s(e.getMessage()); + LOG_ERROR("SAXParseException: %s", ((std::string)s).c_str()); +} + +void EntityFileSAXHandler::onStartEntity(const xercesc::Attributes& attrs) +{ + EntityID parent = m_EntityStack.top(); + + if (m_Handler->m_OnStartEntityCallback) { + std::string name; + auto xName = attrs.getValue(XS::ToXMLCh("name")); + if (xName != nullptr) { + name = XS::ToString(xName); + } + m_Handler->m_OnStartEntityCallback(m_NextEntityID, parent, name); + } + + m_EntityStack.push(m_NextEntityID); + m_NextEntityID++; +} + +void EntityFileSAXHandler::onEndEntity() +{ + m_EntityStack.pop(); +} + +void EntityFileSAXHandler::onStartEntityRef(const xercesc::Attributes& attrs) +{ + std::string path = XS::ToString(attrs.getValue(XS::ToXMLCh("file"))); + + xercesc::SAX2XMLReader* reader = xercesc::XMLReaderFactory::createXMLReader(); + EntityFile::setReaderFeatures(reader); + reader->setContentHandler(this); + reader->setErrorHandler(this); + reader->parse(path.c_str()); + delete reader; +} + +void EntityFileSAXHandler::onStartComponentField(const std::string& field, const xercesc::Attributes& attrs) +{ + //LOG_DEBUG(" Field: %s", field.c_str()); + m_CurrentField = field; + m_CurrentAttributes.clear(); + for (int i = 0; i < attrs.getLength(); i++) { + auto name = attrs.getQName(i); + auto value = attrs.getValue(name); + //LOG_DEBUG(" %s = %s", (char*)XS::ToString(name), (char*)XS::ToString(value)); + m_CurrentAttributes[XS::ToString(name).operator std::string()] = XS::ToString(value).operator std::string(); + } + + if (m_Handler->m_OnStartFieldCallback) { + m_Handler->m_OnStartFieldCallback(m_EntityStack.top(), m_CurrentComponent, field, m_CurrentAttributes); + } +} + +void EntityFileSAXHandler::onEndComponent(const std::string& name) { } + +void EntityFileSAXHandler::onStartComponent(const std::string& name) +{ + //LOG_DEBUG(" Component: %s", name.c_str()); + m_CurrentComponent = name; + if (m_Handler->m_OnStartComponentCallback) { + m_Handler->m_OnStartComponentCallback(m_EntityStack.top(), name); + } +} + +void EntityFileSAXHandler::onEndComponentField(const std::string& field) { } + +void EntityFileSAXHandler::onFieldData(char* data) +{ + //LOG_DEBUG(" Data: %s", data); + if (m_Handler->m_OnStartFieldDataCallback) { + m_Handler->m_OnStartFieldDataCallback(m_EntityStack.top(), m_CurrentComponent, m_CurrentField, data); + } + + xercesc::XMLString::release(&data); +} diff --git a/src/Engine/Core/EntityFilePreprocessor.cpp b/src/Engine/Core/EntityFilePreprocessor.cpp index f9aae3d4..90151941 100644 --- a/src/Engine/Core/EntityFilePreprocessor.cpp +++ b/src/Engine/Core/EntityFilePreprocessor.cpp @@ -16,12 +16,12 @@ EntityFilePreprocessor::EntityFilePreprocessor(const EntityFile* entityFile) for (auto& kv : m_ComponentInfo) { auto& info = kv.second; - LOG_DEBUG("Component: %s (%s)", info.Name.c_str(), info.Meta.Annotation.c_str()); - LOG_DEBUG("Stride: %i", info.Meta.Stride); - LOG_DEBUG("Allocation: %i", info.Meta.Allocation); + LOG_DEBUG("Component: %s (%s)", info.Name.c_str(), info.Meta->Annotation.c_str()); + LOG_DEBUG("Stride: %i", info.Stride); + LOG_DEBUG("Allocation: %i", info.Meta->Allocation); for (auto& kv : info.Fields) { auto& field = kv.second; - LOG_DEBUG("\t%i\t%s %s", field.Offset, field.Type, kv.first.c_str()); + LOG_DEBUG("\t%i\t%s %s", field.Offset, field.Type.c_str(), kv.first.c_str()); } } @@ -62,46 +62,16 @@ void EntityFilePreprocessor::parseComponentInfo() } ComponentInfo compInfo; + compInfo.Meta = std::make_shared(); // Name compInfo.Name = XS::ToString(element->getName()); // Known allocation - compInfo.Meta.Allocation = m_ComponentCounts[compInfo.Name]; + compInfo.Meta->Allocation = m_ComponentCounts[compInfo.Name]; // Annotation auto componentAnnotation = element->getAnnotation(); if (componentAnnotation != nullptr) { - // Parse annotation XML - char* annotationString = XMLString::transcode(componentAnnotation->getAnnotationString()); - MemBufInputSource annotationInput(reinterpret_cast(annotationString), strlen(annotationString), "MemBuf: Annotation String"); - XercesDOMParser parser(nullptr, XMLPlatformUtils::fgMemoryManager, grammarPool); - parser.setErrorHandler(&errorHandler); - parser.parse(annotationInput); - XMLString::release(&annotationString); - auto doc = parser.getDocument(); - - // TODO: Add allocation estimations from external file on map-to-map basis - // Add allocation estimation(s) - //auto allocationTags = doc->getElementsByTagName(XSTR("meta:allocation")); - //for (int i = 0; i < allocationTags->getLength(); ++i) { - // auto allocation = dynamic_cast(allocationTags->item(i)); - // auto child = allocation->getFirstChild(); - // if (child == nullptr) { - // continue; - // } - - // XSValue::Status status; - // XSValue* val = XSValue::getActualValue(child->getNodeValue(), XSValue::dt_integer, status); - // compInfo.Meta.Allocation += val->fData.fValue.f_int; - //} - - // Save documentation string - auto documentationTags = doc->getElementsByTagName(XS::ToXMLCh("xs:documentation")); - if (documentationTags->getLength() != 0) { - auto child = documentationTags->item(0)->getFirstChild(); - if (child != nullptr) { - compInfo.Meta.Annotation = XS::ToString(child->getNodeValue()); - } - } + compInfo.Meta->Annotation = parseAnnotationXML(componentAnnotation->getAnnotationString()); } else { LOG_WARNING("Component \"%s\" is missing an annotation!", compInfo.Name.c_str()); } @@ -140,24 +110,58 @@ void EntityFilePreprocessor::parseComponentInfo() std::string name = XS::ToString(elementDeclaration->getName()); std::string type = XS::ToString(elementDeclaration->getTypeDefinition()->getName()); + std::string typeNamespace = XS::ToString(elementDeclaration->getTypeDefinition()->getNamespace()); + std::string baseType = XS::ToString(elementDeclaration->getTypeDefinition()->getBaseType()->getName()); + std::string effectiveType = type; size_t stride = EntityFile::GetTypeStride(type); if (stride == 0) { - LOG_WARNING("Field \"%s\" in component \"%s\" uses unexpected field type \"%s\". Skipping.", name.c_str(), compInfo.Name.c_str(), type.c_str()); - continue; + stride = EntityFile::GetTypeStride(baseType); + if (stride == 0) { + LOG_WARNING("Field \"%s\" in component \"%s\" uses unexpected field type \"%s\" with base type \"%s\". Skipping.", name.c_str(), compInfo.Name.c_str(), type.c_str(), baseType.c_str()); + continue; + } + effectiveType = baseType; + } + + // Annotation + auto fieldAnnotation = elementDeclaration->getAnnotation(); + if (fieldAnnotation != nullptr) { + compInfo.Meta->FieldAnnotations[name] = parseAnnotationXML(fieldAnnotation->getAnnotationString()); + } else { + LOG_WARNING("Component field \"%s.%s\" is missing an annotation!", compInfo.Name.c_str(), name.c_str()); + } + + if (effectiveType == "enum") { + // Parse potential enum type definition for field type + if (compInfo.Meta->FieldEnumDefinitions.count(name) == 0) { + auto enumTypeDefinition = xsModel->getTypeDefinition(XS::ToXMLCh(type), XS::ToXMLCh("components")); + auto xsComplexType = dynamic_cast(enumTypeDefinition); + auto xsComplexContent = xsComplexType->getParticle(); + auto xsExtension = xsComplexContent->getModelGroupTerm(); + auto xsExtensionParticles = xsExtension->getParticles(); + auto xsChoice = xsExtensionParticles->elementAt(0)->getModelGroupTerm(); + auto xsChoiceParticles = xsChoice->getParticles(); + for (int i = 0; i < xsChoiceParticles->size(); ++i) { + auto enumElement = xsChoiceParticles->elementAt(i)->getElementTerm(); + std::string enumName = XS::ToString(enumElement->getName()); + std::string enumValue = XS::ToString(enumElement->getConstraintValue()); + compInfo.Meta->FieldEnumDefinitions[name][enumName] = boost::lexical_cast(enumValue); + LOG_DEBUG("ENUM %s = %s", enumName.c_str(), enumValue.c_str()); + } + } } auto& field = compInfo.Fields[name]; field.Name = name; - field.Type = type; + field.Type = effectiveType; field.Offset = fieldOffset; field.Stride = stride; compInfo.FieldsInOrder.push_back(name); - fieldOffset += stride; } - compInfo.Meta.Stride = fieldOffset; + compInfo.Stride = fieldOffset; m_ComponentInfo[compInfo.Name] = compInfo; } } @@ -170,8 +174,10 @@ void EntityFilePreprocessor::parseDefaults() for (auto& ci : m_ComponentInfo) { // Allocate memory for default values - ci.second.Defaults = std::shared_ptr(new char[ci.second.Meta.Stride]); - memset(ci.second.Defaults.get(), 0, ci.second.Meta.Stride); + ci.second.Defaults = std::shared_ptr(new char[ci.second.Stride]); + memset(ci.second.Defaults.get(), 0, ci.second.Stride); + + std::string componentName = ci.first; XercesDOMParser parser(nullptr, XMLPlatformUtils::fgMemoryManager); parser.setDoSchema(true); @@ -179,8 +185,11 @@ void EntityFilePreprocessor::parseDefaults() parser.setErrorHandler(&errorHandler); parser.setValidationScheme(XercesDOMParser::Val_Always); parser.setValidationSchemaFullChecking(true); + //parser.setDoNamespaces(true); + //boost::filesystem::path schemaLocation = "Schema/Components/" + componentName + ".xsd"; + //std::string namespaceSchema = schemaLocation.string(); + //parser.setExternalNoNamespaceSchemaLocation("Teamasdasdasdasd.xsd"); - std::string componentName = ci.first; LOG_DEBUG("Parsing defaults for component %s", componentName.c_str()); boost::filesystem::path defaultsFile = "Schema/Components/" + componentName + ".xml"; @@ -192,7 +201,7 @@ void EntityFilePreprocessor::parseDefaults() } // Find the node in the components namespace matching the component name - std::string tagName = "c:" + componentName; + std::string tagName = componentName; auto rootNodes = doc->getElementsByTagName(XS::ToXMLCh(tagName)); if (rootNodes->getLength() == 0) { LOG_ERROR("Couldn't find defaults for component \"%s\"! Skipping.", componentName.c_str()); @@ -225,9 +234,19 @@ void EntityFilePreprocessor::parseDefaults() EntityFile::WriteAttributeData(data, field, attributes); } - // Handle potential field values auto childNode = fieldElement->getFirstChild(); - if (childNode != nullptr && childNode->getNodeType() == DOMNode::TEXT_NODE) { + if (childNode == nullptr) { + continue; + } + + // An enum will either have an element node with a text node inside, + // or contain a text node directly. + if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) { + childNode = childNode->getFirstChild(); + } + + // Handle potential field values + if (childNode->getNodeType() == DOMNode::TEXT_NODE) { char* cstrValue = XMLString::transcode(childNode->getNodeValue()); EntityFile::WriteValueData(data, field, cstrValue); XMLString::release(&cstrValue); @@ -236,3 +255,27 @@ void EntityFilePreprocessor::parseDefaults() } } +std::string EntityFilePreprocessor::parseAnnotationXML(const XMLCh* xml) +{ + using namespace xercesc; + + // Parse annotation XML + char* annotationString = XMLString::transcode(xml); + MemBufInputSource annotationInput(reinterpret_cast(annotationString), strlen(annotationString), "MemBuf: Annotation String"); + XercesDOMParser parser(nullptr, XMLPlatformUtils::fgMemoryManager); + //parser.setErrorHandler(&errorHandler); + parser.parse(annotationInput); + XMLString::release(&annotationString); + auto doc = parser.getDocument(); + + // Save documentation string + auto documentationTags = doc->getElementsByTagName(XS::ToXMLCh("xs:documentation")); + if (documentationTags->getLength() != 0) { + auto child = documentationTags->item(0)->getFirstChild(); + if (child != nullptr) { + return XS::ToString(child->getNodeValue()); + } + } + + return std::string(); +} diff --git a/src/Engine/Core/EntityFileWriter.cpp b/src/Engine/Core/EntityFileWriter.cpp index 5b6c466d..4d46be06 100644 --- a/src/Engine/Core/EntityFileWriter.cpp +++ b/src/Engine/Core/EntityFileWriter.cpp @@ -114,7 +114,7 @@ void EntityFileWriter::appentEntityComponents(xercesc::DOMElement* parentElement fieldElement->setAttribute(X("Y"), X(boost::lexical_cast(q.y))); fieldElement->setAttribute(X("Z"), X(boost::lexical_cast(q.z))); fieldElement->setAttribute(X("W"), X(boost::lexical_cast(q.w))); - } else if (field.Type == "int") { + } else if (field.Type == "int" || field.Type == "enum") { const int& value = c[fieldName]; fieldElement->appendChild(doc->createTextNode(X(boost::lexical_cast(value)))); } else if (field.Type == "float") { diff --git a/src/Engine/Core/World.cpp b/src/Engine/Core/World.cpp index 3104e4e9..477e2ab2 100644 --- a/src/Engine/Core/World.cpp +++ b/src/Engine/Core/World.cpp @@ -75,7 +75,7 @@ ComponentWrapper World::AttachComponent(EntityID entity, const std::string& comp // Allocate space for the component ComponentWrapper c = pool->Allocate(entity); // Write default values - memcpy(c.Data, ci.Defaults.get(), ci.Meta.Stride); + memcpy(c.Data, ci.Defaults.get(), ci.Stride); return c; } diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 69889be5..22e1bfc0 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -484,8 +484,8 @@ void EditorSystem::drawUI(World* world, double dt) } if (ImGui::CollapsingHeader(componentType.c_str())) { - if (!ci.Meta.Annotation.empty()) { - ImGui::Text(ci.Meta.Annotation.c_str()); + if (!ci.Meta->Annotation.empty()) { + ImGui::Text(ci.Meta->Annotation.c_str()); } auto& component = world->GetComponent(m_Selection, componentType); @@ -529,6 +529,26 @@ void EditorSystem::drawUI(World* world, double dt) if (ImGui::InputFloat("", &tempVal, 0.01f, 1.f)) { component.SetProperty(fieldName, static_cast(tempVal)); } + } else if (field.Type == "int") { + int val = component.Property(fieldName); + ImGui::InputInt("", &val); + } else if (field.Type == "enum") { + int currentValue = component.Property(fieldName); + int item = -1; + std::stringstream enumKeys; + std::vector enumValues; + int i = 0; + for (auto& kv : ci.Meta->FieldEnumDefinitions.at(fieldName)) { + enumKeys << kv.first << " (" << kv.second << ")" << '\0'; + enumValues.push_back(kv.second); + if (currentValue == kv.second) { + item = i; + } + i++; + } + if (ImGui::Combo("", &item, enumKeys.str().c_str())) { + component.SetProperty(fieldName, enumValues.at(item)); + } } else if (field.Type == "bool") { auto& val = component.Property(fieldName); ImGui::Checkbox("", &val); diff --git a/src/Tests/ComponentPoolTest.cpp b/src/Tests/ComponentPoolTest.cpp index 25bdf1d3..1df13c10 100644 --- a/src/Tests/ComponentPoolTest.cpp +++ b/src/Tests/ComponentPoolTest.cpp @@ -10,8 +10,8 @@ BOOST_AUTO_TEST_CASE(ComponentPoolTest) //ci.Name = "Test"; //ci.FieldTypes["Field"] = "int"; //ci.FieldOffsets["Field"] = 0; - //ci.Meta.Allocation = 3; - //ci.Meta.Stride = sizeof(EntityID) + sizeof(int); + //ci.Meta->Allocation = 3; + //ci.Stride = sizeof(EntityID) + sizeof(int); //std::vector wrappers; //ComponentPool pool(ci); From d8a24bbfde07562a2419c425912be2a21a1b8381 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 16 Jan 2016 23:24:45 +0100 Subject: [PATCH 43/48] Renamed all instances of "Property" to "Field" in ComponentWrapper to maintain consistent naming with ComponentInfo --- include/Engine/Core/ComponentWrapper.h | 21 ++++++++++----------- src/Engine/Editor/EditorSystem.cpp | 18 +++++++++--------- src/Tests/WorldTest.cpp | 2 +- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/include/Engine/Core/ComponentWrapper.h b/include/Engine/Core/ComponentWrapper.h index 9d27b0f8..b1e5f9ad 100644 --- a/include/Engine/Core/ComponentWrapper.h +++ b/include/Engine/Core/ComponentWrapper.h @@ -5,8 +5,7 @@ #include "Entity.h" #include "ComponentInfo.h" #include "Util/Any.h" - -// TODO: Change all instances "Property" to "Field" to remain consistent with ComponentInfo +Minecraft hard drilling struct ComponentWrapper { ComponentWrapper(const ComponentInfo& componentInfo, char* data) @@ -25,20 +24,20 @@ struct ComponentWrapper } template - T& Property(std::string name) + T& Field(std::string name) { unsigned int offset = Info.Fields.at(name).Offset; return *reinterpret_cast(&Data[offset]); } template - void SetProperty(std::string name, const T value) { Property(name) = value; } + void SetField(std::string name, const T value) { Field(name) = value; } //template - //void SetProperty(std::string name, T& value) { Property(name) = value; } + //void SetField(std::string name, T& value) { Field(name) = value; } // Specialization for string literals template - void SetProperty(std::string name, const char(&value)[N]) { Property(name) = std::string(value); } + void SetField(std::string name, const char(&value)[N]) { Field(name) = std::string(value); } struct SubscriptProxy { @@ -57,17 +56,17 @@ struct ComponentWrapper int Enum(const char* enumKey) { return m_Component->Enum(m_PropertyName.c_str(), enumKey); } template - operator T&() { return m_Component->Property(m_PropertyName); } + operator T&() { return m_Component->Field(m_PropertyName); } template - void operator=(const T val) { m_Component->SetProperty(m_PropertyName, val); } + void operator=(const T val) { m_Component->SetField(m_PropertyName, val); } // TODO: Pass by reference and rvalue (universal reference?) //template - //void operator=(T& val) { m_Component->SetProperty(m_PropertyName, val); } + //void operator=(T& val) { m_Component->SetField(m_PropertyName, val); } // Specialization for string literals - template - void operator=(const char(&val)[N]) { m_Component->SetProperty(m_PropertyName, val); } + template + void operator=(const char(&val)[N]) { m_Component->SetField(m_PropertyName, val); } }; SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); } }; diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 22e1bfc0..d5863814 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -496,7 +496,7 @@ void EditorSystem::drawUI(World* world, double dt) std::string uniqueID = componentType + fieldName; ImGui::PushID(uniqueID.c_str()); if (field.Type == "Vector") { - auto& val = component.Property(fieldName); + auto& val = component.Field(fieldName); if (fieldName == "Scale") { ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, 0.f, std::numeric_limits::max()); } else if (fieldName == "Orientation") { @@ -508,10 +508,10 @@ void EditorSystem::drawUI(World* world, double dt) ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, std::numeric_limits::lowest(), std::numeric_limits::max()); } } else if (field.Type == "Color") { - auto& val = component.Property(fieldName); + auto& val = component.Field(fieldName); ImGui::ColorEdit4("", glm::value_ptr(val), true); } else if (field.Type == "string") { - std::string& val = component.Property(fieldName); + std::string& val = component.Field(fieldName); char tempString[1024]; memcpy(tempString, val.c_str(), std::min(val.length() + 1, sizeof(tempString))); if (ImGui::InputText("", tempString, sizeof(tempString))) { @@ -525,15 +525,15 @@ void EditorSystem::drawUI(World* world, double dt) } } else if (field.Type == "double") { - float tempVal = static_cast(component.Property(fieldName)); + float tempVal = static_cast(component.Field(fieldName)); if (ImGui::InputFloat("", &tempVal, 0.01f, 1.f)) { - component.SetProperty(fieldName, static_cast(tempVal)); + component.SetField(fieldName, static_cast(tempVal)); } } else if (field.Type == "int") { - int val = component.Property(fieldName); + int val = component.Field(fieldName); ImGui::InputInt("", &val); } else if (field.Type == "enum") { - int currentValue = component.Property(fieldName); + int currentValue = component.Field(fieldName); int item = -1; std::stringstream enumKeys; std::vector enumValues; @@ -547,10 +547,10 @@ void EditorSystem::drawUI(World* world, double dt) i++; } if (ImGui::Combo("", &item, enumKeys.str().c_str())) { - component.SetProperty(fieldName, enumValues.at(item)); + component.SetField(fieldName, enumValues.at(item)); } } else if (field.Type == "bool") { - auto& val = component.Property(fieldName); + auto& val = component.Field(fieldName); ImGui::Checkbox("", &val); } else { ImGui::TextDisabled(field.Type.c_str()); diff --git a/src/Tests/WorldTest.cpp b/src/Tests/WorldTest.cpp index 8d92a328..03008562 100644 --- a/src/Tests/WorldTest.cpp +++ b/src/Tests/WorldTest.cpp @@ -20,7 +20,7 @@ BOOST_AUTO_TEST_CASE(WorldTestSingleAllocation, * boost::unit_test::tolerance(0. ComponentWrapper c = w.AttachComponent(e, "Test"); // Check default values - BOOST_TEST((int)c["TestInteger"] == c.Property("TestInteger")); + BOOST_TEST((int)c["TestInteger"] == c.Field("TestInteger")); BOOST_TEST((int)c["TestInteger"] == 1337); BOOST_TEST((double)c["TestDouble"] == 13.37); BOOST_TEST((std::string)c["TestString"] == "Carlito"); From 6385a98ecae2f4c3895dc6462fffbe4e3e087110 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sun, 17 Jan 2016 00:53:31 +0100 Subject: [PATCH 44/48] Added runtime type size check to ComponentWrapper to avoid corruption when types don't match --- include/Engine/Common.h | 1 + include/Engine/Core/ComponentWrapper.h | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/Engine/Common.h b/include/Engine/Common.h index ebdc90d0..7d6f520d 100644 --- a/include/Engine/Common.h +++ b/include/Engine/Common.h @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/include/Engine/Core/ComponentWrapper.h b/include/Engine/Core/ComponentWrapper.h index b1e5f9ad..f124d3d5 100644 --- a/include/Engine/Core/ComponentWrapper.h +++ b/include/Engine/Core/ComponentWrapper.h @@ -5,7 +5,7 @@ #include "Entity.h" #include "ComponentInfo.h" #include "Util/Any.h" -Minecraft hard drilling + struct ComponentWrapper { ComponentWrapper(const ComponentInfo& componentInfo, char* data) @@ -26,8 +26,13 @@ struct ComponentWrapper template T& Field(std::string name) { - unsigned int offset = Info.Fields.at(name).Offset; - return *reinterpret_cast(&Data[offset]); + const ComponentInfo::Field_t& field = Info.Fields.at(name); + if (sizeof(T) > field.Stride) { + std::stringstream message; + message << "Type size of \"" << typeid(T).name() << "\" doesn't match size of component field \"" << Info.Name << "." << name << "\"!"; + throw new std::runtime_error(message.str().c_str()); + } + return *reinterpret_cast(&Data[field.Offset]); } template From f281d220de9e466a701d8d0f9200833fa00a5cab Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sun, 17 Jan 2016 01:00:48 +0100 Subject: [PATCH 45/48] Working Spawner, SpawnPoint, PlayerSpawn and Team components, along with their systems. --- include/Engine/Core/EntityWrapper.h | 2 + include/Game/Systems/PlayerSpawnSystem.h | 1 + include/Game/Systems/SpawnerSystem.h | 9 +++- resources/Schema/Components.xsd | 1 - resources/Schema/Components/PlayerSpawn.xml | 4 +- resources/Schema/Components/PlayerSpawn.xsd | 9 +--- resources/Schema/Entities/SpawnTest.xml | 9 +++- resources/Schema/Entities/TeamTest.xml | 13 +++++ resources/Schema/Types/Entity.xsd | 1 - src/Engine/Core/EntityWrapper.cpp | 2 + src/Engine/Input/InputProxy.cpp | 4 +- src/Game/Systems/PlayerSpawnSystem.cpp | 28 +++++++--- src/Game/Systems/SpawnerSystem.cpp | 60 +++++++++++++-------- 13 files changed, 93 insertions(+), 50 deletions(-) create mode 100755 resources/Schema/Entities/TeamTest.xml diff --git a/include/Engine/Core/EntityWrapper.h b/include/Engine/Core/EntityWrapper.h index 4cc8bbea..55b64c6f 100644 --- a/include/Engine/Core/EntityWrapper.h +++ b/include/Engine/Core/EntityWrapper.h @@ -20,6 +20,8 @@ struct EntityWrapper ::World* World; EntityID ID; + static const EntityWrapper Invalid; + bool HasComponent(const std::string& componentName); ComponentWrapper operator[](const std::string& componentName); diff --git a/include/Game/Systems/PlayerSpawnSystem.h b/include/Game/Systems/PlayerSpawnSystem.h index 414d32d8..f0e10949 100644 --- a/include/Game/Systems/PlayerSpawnSystem.h +++ b/include/Game/Systems/PlayerSpawnSystem.h @@ -1,5 +1,6 @@ #include "Core/System.h" #include "Input/EInputCommand.h" +#include "Systems/SpawnerSystem.h" #include "Events/ESpawnerSpawn.h" class PlayerSpawnSystem : public ImpureSystem diff --git a/include/Game/Systems/SpawnerSystem.h b/include/Game/Systems/SpawnerSystem.h index cbb7c100..2c094528 100644 --- a/include/Game/Systems/SpawnerSystem.h +++ b/include/Game/Systems/SpawnerSystem.h @@ -1,3 +1,6 @@ +#ifndef SpawnerSystem_h__ +#define SpawnerSystem_h__ + #include #include "Common.h" #include "GLM.h" @@ -12,9 +15,11 @@ class SpawnerSystem : public System public: SpawnerSystem(EventBroker* eventBroker); + static EntityWrapper Spawn(EntityWrapper spawner, EntityWrapper parent = EntityWrapper::Invalid); + private: EventRelay m_OnSpawnerSpawn; bool OnSpawnerSpawn(Events::SpawnerSpawn& e); +}; - void spawnEntity(EntityWrapper spawner, EntityID parent, glm::vec3 position); -}; \ No newline at end of file +#endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 88f09d6c..8d837aea 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -4,7 +4,6 @@ - diff --git a/resources/Schema/Components/PlayerSpawn.xml b/resources/Schema/Components/PlayerSpawn.xml index 1c621ef9..edc1394e 100644 --- a/resources/Schema/Components/PlayerSpawn.xml +++ b/resources/Schema/Components/PlayerSpawn.xml @@ -1,4 +1,2 @@ - - 1 - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/PlayerSpawn.xsd b/resources/Schema/Components/PlayerSpawn.xsd index b076de27..6e321724 100644 --- a/resources/Schema/Components/PlayerSpawn.xsd +++ b/resources/Schema/Components/PlayerSpawn.xsd @@ -5,14 +5,7 @@ - Combined with a Spawner, defines a spawn point for a player team. + Combined with a Spawner and a Team component, defines a spawn point for a player team. - - - - 1 = Spectator, 2 = Red, 3 = Blue - - - diff --git a/resources/Schema/Entities/SpawnTest.xml b/resources/Schema/Entities/SpawnTest.xml index f4af726d..a5574027 100644 --- a/resources/Schema/Entities/SpawnTest.xml +++ b/resources/Schema/Entities/SpawnTest.xml @@ -8,9 +8,13 @@ + Schema/Entities/Player.xml + + 2 + @@ -47,9 +51,10 @@ + - - + + diff --git a/resources/Schema/Entities/TeamTest.xml b/resources/Schema/Entities/TeamTest.xml new file mode 100755 index 00000000..69be6141 --- /dev/null +++ b/resources/Schema/Entities/TeamTest.xml @@ -0,0 +1,13 @@ + + + + + + 3 + + + + + + + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 3ca6e545..1aaa8497 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -13,7 +13,6 @@ - diff --git a/src/Engine/Core/EntityWrapper.cpp b/src/Engine/Core/EntityWrapper.cpp index 9d15dd39..7b321a63 100644 --- a/src/Engine/Core/EntityWrapper.cpp +++ b/src/Engine/Core/EntityWrapper.cpp @@ -1,6 +1,8 @@ #include "Core/EntityWrapper.h" #include "Core/World.h" +const EntityWrapper EntityWrapper::Invalid = EntityWrapper(nullptr, EntityID_Invalid); + bool EntityWrapper::operator==(const EntityWrapper& e) { return (this->World == e.World) && (this->ID == e.ID); diff --git a/src/Engine/Input/InputProxy.cpp b/src/Engine/Input/InputProxy.cpp index 7e977542..6c3591e3 100644 --- a/src/Engine/Input/InputProxy.cpp +++ b/src/Engine/Input/InputProxy.cpp @@ -63,7 +63,7 @@ void InputProxy::Process() e.Command = command; e.Value = currentValue; m_EventBroker->Publish(e); - LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); + //LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); m_LastCommandValues[command] = currentValue; } } @@ -79,7 +79,7 @@ void InputProxy::Process() } //e.Value = std::max(-1.f, std::min(e.Value, 1.f)); m_EventBroker->Publish(e); - LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); + //LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID); } m_CommandQueue.clear(); } diff --git a/src/Game/Systems/PlayerSpawnSystem.cpp b/src/Game/Systems/PlayerSpawnSystem.cpp index a522f679..0bf5bdea 100644 --- a/src/Game/Systems/PlayerSpawnSystem.cpp +++ b/src/Game/Systems/PlayerSpawnSystem.cpp @@ -8,17 +8,29 @@ PlayerSpawnSystem::PlayerSpawnSystem(EventBroker* eventBroker) void PlayerSpawnSystem::Update(World* world, double dt) { - auto componentPools = world->GetComponentPools(); - auto spawnerPool = componentPools.find("Spawner"); - if (spawnerPool == componentPools.end()) { + auto playerSpawns = world->GetComponents("PlayerSpawn"); + if (playerSpawns == nullptr) { return; } -; + for (auto& team : m_SpawnRequests) { - for (auto& spawner : *spawnerPool->second) { - Events::SpawnerSpawn e; - e.Spawner = EntityWrapper(world, spawner.EntityID); - m_EventBroker->Publish(e); + for (auto& cPlayerSpawn : *playerSpawns) { + EntityWrapper spawner(world, cPlayerSpawn.EntityID); + if (!spawner.HasComponent("Spawner")) { + continue; + } + + // If the spawner has a team affiliation, check it + if (spawner.HasComponent("Team")) { + if ((int)spawner["Team"]["Team"] != team) { + continue; + } + } + + // Spawn the player! + EntityWrapper player = SpawnerSystem::Spawn(spawner); + // Set the player team affiliation + player["Team"]["Team"] = team; } } m_SpawnRequests.clear(); diff --git a/src/Game/Systems/SpawnerSystem.cpp b/src/Game/Systems/SpawnerSystem.cpp index c1122d98..3454c58b 100644 --- a/src/Game/Systems/SpawnerSystem.cpp +++ b/src/Game/Systems/SpawnerSystem.cpp @@ -5,43 +5,57 @@ SpawnerSystem::SpawnerSystem(EventBroker* eventBroker) : System(eventBroker) EVENT_SUBSCRIBE_MEMBER(m_OnSpawnerSpawn, &SpawnerSystem::OnSpawnerSpawn); } -bool SpawnerSystem::OnSpawnerSpawn(Events::SpawnerSpawn& e) +EntityWrapper SpawnerSystem::Spawn(EntityWrapper spawner, EntityWrapper parent /*= EntityWrapper::Invalid*/) { - EntityWrapper& spawner = e.Spawner; + // Spawn the entity in the parent's world if it exists, otherwise in the spawner's world + World* world = parent.World; + if (world == nullptr) { + world = spawner.World; + } + // Find any SpawnPoints existing as children of spawner auto children = spawner.World->GetChildren(spawner.ID); - std::vector spawnPoints; + std::vector spawnPoints; for (auto kv = children.first; kv != children.second; ++kv) { const EntityID& child = kv->second; if (spawner.World->HasComponent(child, "SpawnPoint")) { - spawnPoints.push_back(child); + spawnPoints.push_back(EntityWrapper(spawner.World, child)); } } - EntityID spawnPoint = spawner.ID; + // Choose a random SpawnPoint + EntityWrapper spawnPoint = spawner; if (!spawnPoints.empty()) { - // Select a random spawn point - static std::random_device randomDevice; - static std::mt19937 randomGenerator(randomDevice()); - std::uniform_int_distribution<> distribution(0, std::distance(spawnPoints.begin(), spawnPoints.end()) - 1); - auto randomSpawnPointIt = spawnPoints.begin(); - std::advance(randomSpawnPointIt, distribution(randomGenerator)); - spawnPoint = *randomSpawnPointIt; + if (spawnPoints.size() > 1) { + static std::random_device randomDevice; + static std::mt19937 randomGenerator(randomDevice()); + std::uniform_int_distribution<> distribution(0, std::distance(spawnPoints.begin(), spawnPoints.end()) - 1); + auto randomSpawnPointIt = spawnPoints.begin(); + std::advance(randomSpawnPointIt, distribution(randomGenerator)); + spawnPoint = *randomSpawnPointIt; + } else { + spawnPoint = spawnPoints.front(); + } } - spawnEntity(spawner, e.Parent.ID, Transform::AbsolutePosition(spawner.World, spawnPoint)); - - return true; -} - -void SpawnerSystem::spawnEntity(EntityWrapper spawner, EntityID parent, glm::vec3 position) -{ + + // Load the entity file and parse it const std::string& entityFilePath = spawner["Spawner"]["EntityFile"]; auto entityFile = ResourceManager::Load(entityFilePath); if (entityFile == nullptr) { - return; + return EntityWrapper::Invalid; } - EntityFileParser parser(entityFile); - EntityWrapper spawnedEntity(spawner.World, parser.MergeEntities(spawner.World, parent)); - spawnedEntity["Transform"]["Position"] = position; + EntityWrapper spawnedEntity(world, parser.MergeEntities(world, parent.ID)); + + // Set its position and orientation to that of the SpawnPoint + spawnedEntity["Transform"]["Position"] = Transform::AbsolutePosition(spawnPoint.World, spawnPoint.ID); + spawnedEntity["Transform"]["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(spawnPoint.World, spawnPoint.ID)); + + return spawnedEntity; +} + +bool SpawnerSystem::OnSpawnerSpawn(Events::SpawnerSpawn& e) +{ + EntityWrapper spawnedEntity = Spawn(e.Spawner, e.Parent); + return true; } \ No newline at end of file From a346a329b32cfa887cc0d4fa3f229aeccd33706d Mon Sep 17 00:00:00 2001 From: William Moberg Date: Mon, 18 Jan 2016 10:16:35 +0100 Subject: [PATCH 46/48] Added an option to disable the Pool allocation in Config.ini, in case there is a mess with the MemoryPool. --- include/Engine/Core/MemoryPool.h | 18 ++++++++++++++---- resources/DefaultConfig.ini | 4 +++- src/Engine/Core/MemoryPool.cpp | 5 +++++ src/Game/Game.cpp | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 src/Engine/Core/MemoryPool.cpp diff --git a/include/Engine/Core/MemoryPool.h b/include/Engine/Core/MemoryPool.h index ff24ac80..3a1cc069 100644 --- a/include/Engine/Core/MemoryPool.h +++ b/include/Engine/Core/MemoryPool.h @@ -5,6 +5,14 @@ template class MemoryPoolForwardIterator; +namespace DisableMemoryPool +{ +//if true -> Pool allocation is not used when calling Allocate/Free, just use regular dynamic allocation. +//if false -> Use pool allocation. +//Should default to false, unless the DisableMemoryPool is true in the Config.ini files. +extern bool Value; +} + //This is the class to use if you want to allocate blocks (slots) of raw memory, with a fixed maximum size (stride). //Additionally, if you know that every memory-block will contain one object of a specific type, (i.e. the stride for the slot //will the size of the object type) you should use ObjectPool instead, your life will become easier. @@ -80,8 +88,8 @@ public: //If element cannot be allocated in the pool, because the memory ran out, memory is allocated dynamically with malloc() "outside the pool". char* Allocate() { - for (; m_CurrentAllocSlot < m_NumSlots && m_SlotIsAllocated[m_CurrentAllocSlot]; ++m_CurrentAllocSlot); - if (m_CurrentAllocSlot < m_NumSlots) { + for (; m_CurrentAllocSlot < m_NumSlots && m_SlotIsAllocated[m_CurrentAllocSlot] && !DisableMemoryPool::Value; ++m_CurrentAllocSlot); + if (m_CurrentAllocSlot < m_NumSlots && !DisableMemoryPool::Value) { if (m_LowestAllocatedSlot > m_CurrentAllocSlot) m_LowestAllocatedSlot = m_CurrentAllocSlot; //Mark the slot as allocated. @@ -93,7 +101,9 @@ public: else { m_ExtraMemory.push_back((char*)malloc(m_Stride)); //We should preferably not enter here to avoid performance issues. Set more numMaxElements in constructor instead. - LOG_WARNING("Allocated slots exceed Pool size, extra memory allocated dynamically. Pool size: %u, dynamic size: %u.", m_NumSlots, m_ExtraMemory.size()); + if (!DisableMemoryPool::Value) { + LOG_WARNING("Allocated slots exceed Pool size, extra memory allocated dynamically. Pool size: %u, dynamic size: %u.", m_NumSlots, m_ExtraMemory.size()); + } return m_ExtraMemory.back(); } } @@ -108,7 +118,7 @@ public: //(i.e. IsAllocatedInPool may give false positives) //if it was malloc():ed //so, we may enter here even if we shouldn't. - if (IsAllocatedInPool(obj)) { + if (!DisableMemoryPool::Value && IsAllocatedInPool(obj)) { --m_NumAllocatedSlots; const size_t freeSlot = (obj - m_StartAddress) / m_Stride; m_SlotIsAllocated[freeSlot] = false; diff --git a/resources/DefaultConfig.ini b/resources/DefaultConfig.ini index d84b15bb..fb490ec8 100644 --- a/resources/DefaultConfig.ini +++ b/resources/DefaultConfig.ini @@ -2,7 +2,9 @@ LogLevel=1 LoadMap= EditorEnabled=false - +; if true -> Pool allocation is not used when calling Allocate/Free, just use regular dynamic allocation. +; if false -> Use pool allocation. +DisableMemoryPool=false [Video] Fullscreen=false diff --git a/src/Engine/Core/MemoryPool.cpp b/src/Engine/Core/MemoryPool.cpp new file mode 100644 index 00000000..8978641b --- /dev/null +++ b/src/Engine/Core/MemoryPool.cpp @@ -0,0 +1,5 @@ +#include "Core/MemoryPool.h" +namespace DisableMemoryPool +{ +bool Value = false; +} \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index c0cad6d3..80b45eb8 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -16,6 +16,7 @@ Game::Game(int argc, char* argv[]) m_Config = ResourceManager::Load("Config.ini"); ResourceManager::UseThreading = m_Config->Get("Multithreading.ResourceLoading", true); + DisableMemoryPool::Value = m_Config->Get("Debug.DisableMemoryPool", false); LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get("Debug.LogLevel", 1)); // Create the core event broker From 942064c450977355aba5928619273a8e094692b4 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Mon, 18 Jan 2016 13:07:04 +0100 Subject: [PATCH 47/48] Removed class PlayerSystem and associated files. --- include/Engine/Collision/TriggerSystem.h | 14 +++++++- include/Game/Systems/PlayerSystem.h | 34 ------------------- src/Engine/Collision/TriggerSystem.cpp | 17 ++++++++++ src/Game/Game.cpp | 2 -- src/Game/Systems/PlayerSystem.cpp | 42 ------------------------ 5 files changed, 30 insertions(+), 79 deletions(-) delete mode 100644 include/Game/Systems/PlayerSystem.h delete mode 100644 src/Game/Systems/PlayerSystem.cpp diff --git a/include/Engine/Collision/TriggerSystem.h b/include/Engine/Collision/TriggerSystem.h index ee53ad9b..65e7c271 100644 --- a/include/Engine/Collision/TriggerSystem.h +++ b/include/Engine/Collision/TriggerSystem.h @@ -18,7 +18,11 @@ public: : System(eventBroker) , PureSystem("Trigger") , m_Octree(octree) - { } + { + EVENT_SUBSCRIBE_MEMBER(m_ETouch, &TriggerSystem::OnTouch); + EVENT_SUBSCRIBE_MEMBER(m_EEnter, &TriggerSystem::OnEnter); + EVENT_SUBSCRIBE_MEMBER(m_ELeave, &TriggerSystem::OnLeave); + } virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override; @@ -27,6 +31,14 @@ private: std::unordered_map> m_EntitiesTouchingTrigger; std::unordered_map> m_EntitiesCompletelyInTrigger; + //TODO: Only exists for debug purposes, remove later. + EventRelay m_EEnter; + bool OnEnter(const Events::TriggerEnter &event); + EventRelay m_ETouch; + bool OnTouch(const Events::TriggerTouch &event); + EventRelay m_ELeave; + bool OnLeave(const Events::TriggerLeave &event); + //True if leave event was thrown. bool throwLeaveIfWasInTrigger(std::unordered_set& triggerSet, EntityID pId, EntityID tId); template diff --git a/include/Game/Systems/PlayerSystem.h b/include/Game/Systems/PlayerSystem.h deleted file mode 100644 index a74cbb9f..00000000 --- a/include/Game/Systems/PlayerSystem.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef PlayerSystem_h__ -#define PlayerSystem_h__ - -#include -#include - -#include "Common.h" -#include "Core/System.h" -#include "Collision/ETrigger.h" - -class PlayerSystem : public PureSystem -{ -public: - PlayerSystem(EventBroker* eventBroker) - : System(eventBroker) - , PureSystem("Player") - { - EVENT_SUBSCRIBE_MEMBER(m_ETouch, &PlayerSystem::OnTouch); - EVENT_SUBSCRIBE_MEMBER(m_EEnter, &PlayerSystem::OnEnter); - EVENT_SUBSCRIBE_MEMBER(m_ELeave, &PlayerSystem::OnLeave); - } - - virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override; -private: - float m_Speed = 5; - EventRelay m_EEnter; - bool OnEnter(const Events::TriggerEnter &event); - EventRelay m_ETouch; - bool PlayerSystem::OnTouch(const Events::TriggerTouch &event); - EventRelay m_ELeave; - bool PlayerSystem::OnLeave(const Events::TriggerLeave &event); -}; - -#endif \ No newline at end of file diff --git a/src/Engine/Collision/TriggerSystem.cpp b/src/Engine/Collision/TriggerSystem.cpp index 58d1e332..0ff4345e 100644 --- a/src/Engine/Collision/TriggerSystem.cpp +++ b/src/Engine/Collision/TriggerSystem.cpp @@ -78,3 +78,20 @@ bool TriggerSystem::throwLeaveIfWasInTrigger(std::unordered_set& trigg return false; } +bool TriggerSystem::OnTouch(const Events::TriggerTouch &event) +{ + LOG_INFO("Player entity %i touched trigger entity %i.", event.Entity, event.Trigger); + return true; +} + +bool TriggerSystem::OnEnter(const Events::TriggerEnter &event) +{ + LOG_INFO("Player entity %i entered trigger entity %i.", event.Entity, event.Trigger); + return true; +} + +bool TriggerSystem::OnLeave(const Events::TriggerLeave &event) +{ + LOG_INFO("Player entity %i left trigger entity %i.", event.Entity, event.Trigger); + return true; +} \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index ff1e28fd..b0ffa646 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -3,7 +3,6 @@ #include "Collision/TriggerSystem.h" #include "Collision/CollisionSystem.h" #include "Systems/RaptorCopterSystem.h" -#include "Systems/PlayerSystem.h" #include "Systems/HealthSystem.h" #include "Systems/PlayerMovementSystem.h" #include "Systems/SpawnerSystem.h" @@ -76,7 +75,6 @@ Game::Game(int argc, char* argv[]) // All systems with orderlevel 0 will be updated first. unsigned int updateOrderLevel = 0; m_SystemPipeline->AddSystem(updateOrderLevel); - m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); diff --git a/src/Game/Systems/PlayerSystem.cpp b/src/Game/Systems/PlayerSystem.cpp deleted file mode 100644 index 8cdddf84..00000000 --- a/src/Game/Systems/PlayerSystem.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "Systems/PlayerSystem.h" - -void PlayerSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) -{ - component["Velocity"] = glm::vec3(0.f, 0.f, 0.f); - if ((bool&)component["Forward"] == true) { - ((glm::vec3&)component["Velocity"]).z = m_Speed * float(dt) * -1; - - } - if ((bool&)component["Left"] == true) { - ((glm::vec3&)component["Velocity"]).x = m_Speed * float(dt) * -1; - } - if ((bool&)component["Back"] == true) { - ((glm::vec3&)component["Velocity"]).z = m_Speed * float(dt); - } - if ((bool&)component["Right"] == true) { - ((glm::vec3&)component["Velocity"]).x = m_Speed * float(dt); - } - - if ((glm::vec3)component["Velocity"] != glm::vec3(0.f)) { - ComponentWrapper& transform = world->GetComponent(component.EntityID, "Transform"); - (glm::vec3&)transform["Position"] += (glm::vec3)component["Velocity"]; - } -} - -bool PlayerSystem::OnTouch(const Events::TriggerTouch &event) -{ - LOG_INFO("Player entity %i touched widget (entity %i).", event.Entity, event.Trigger); - return false; -} - -bool PlayerSystem::OnEnter(const Events::TriggerEnter &event) -{ - LOG_INFO("Player entity %i entered widget (entity %i).", event.Entity, event.Trigger); - return false; -} - -bool PlayerSystem::OnLeave(const Events::TriggerLeave &event) -{ - LOG_INFO("Player entity %i left widget (entity %i).", event.Entity, event.Trigger); - return false; -} \ No newline at end of file From 0669d917679b4ab55f4de8ef65774cfbe14d65c8 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Mon, 18 Jan 2016 13:29:10 +0100 Subject: [PATCH 48/48] CollisionSystem loops over Collidable components. --- include/Engine/Collision/CollisionSystem.h | 2 +- src/Engine/Collision/CollisionSystem.cpp | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/include/Engine/Collision/CollisionSystem.h b/include/Engine/Collision/CollisionSystem.h index ea6004d9..561c5158 100644 --- a/include/Engine/Collision/CollisionSystem.h +++ b/include/Engine/Collision/CollisionSystem.h @@ -15,7 +15,7 @@ class CollisionSystem : public PureSystem public: CollisionSystem(EventBroker* eventBroker, Octree* octree) : System(eventBroker) - , PureSystem("AABB") + , PureSystem("Collidable") , m_Octree(octree) , zPress(false) { diff --git a/src/Engine/Collision/CollisionSystem.cpp b/src/Engine/Collision/CollisionSystem.cpp index 96e49153..d841c75e 100644 --- a/src/Engine/Collision/CollisionSystem.cpp +++ b/src/Engine/Collision/CollisionSystem.cpp @@ -30,10 +30,8 @@ void CollisionSystem::UpdateComponent(World* world, EntityWrapper& entity, Compo continue; } if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) { - if (entity.HasComponent("Physics")) { - (glm::vec3&)cTransform["Position"] += resolutionVector; - cPhysics["Velocity"] = glm::vec3(0, 0, 0); - } + (glm::vec3&)cTransform["Position"] += resolutionVector; + cPhysics["Velocity"] = glm::vec3(0, 0, 0); } }