From e65db077d566dc3bc0f322fdac8f189a22369405 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 7 Mar 2014 01:08:54 +0100 Subject: [PATCH 1/4] Refactored factory and component system. --- Escape-the-Dawn.sln | 3 ++ Escape-the-Dawn/AABB.cpp | 46 ----------------- Escape-the-Dawn/AABB.h | 22 +++----- Escape-the-Dawn/Component.h | 4 +- Escape-the-Dawn/ComponentFactory.h | 51 ------------------- Escape-the-Dawn/Components/Bounds.h | 15 +++--- Escape-the-Dawn/Components/Camera.h | 19 ++++--- Escape-the-Dawn/Components/Collision.h | 18 +++---- Escape-the-Dawn/Components/DirectionalLight.h | 21 ++++---- Escape-the-Dawn/Components/Input.h | 20 ++++---- Escape-the-Dawn/Components/Model.h | 19 +++---- Escape-the-Dawn/Components/ParticleEmitter.h | 31 ++++++----- Escape-the-Dawn/Components/PointLight.h | 21 ++++---- Escape-the-Dawn/Components/PowerUp.h | 15 +++--- Escape-the-Dawn/Components/SoundEmitter.h | 21 ++++---- Escape-the-Dawn/Components/Sprite.h | 19 +++---- Escape-the-Dawn/Components/Stat.h | 17 +++---- Escape-the-Dawn/Components/Template.h | 9 ++-- Escape-the-Dawn/Components/Transform.h | 19 +++++++ .../Components/TransformComponent.h | 19 ------- Escape-the-Dawn/Escape-the-Dawn.vcxproj | 6 +-- .../Escape-the-Dawn.vcxproj.filters | 39 +++++++------- Escape-the-Dawn/Factory.h | 35 +++++++++++++ Escape-the-Dawn/Systems/CollisionSystem.h | 3 +- Escape-the-Dawn/Systems/InputSystem.cpp | 12 +++++ Escape-the-Dawn/Systems/InputSystem.h | 27 ++++++++++ Escape-the-Dawn/World.cpp | 17 ++++++- Escape-the-Dawn/World.h | 22 +++++++- Tests/Tests.vcxproj | 14 +++++ Tests/main.cpp | 13 ++++- 30 files changed, 313 insertions(+), 284 deletions(-) delete mode 100644 Escape-the-Dawn/AABB.cpp delete mode 100644 Escape-the-Dawn/ComponentFactory.h create mode 100644 Escape-the-Dawn/Components/Transform.h delete mode 100644 Escape-the-Dawn/Components/TransformComponent.h create mode 100644 Escape-the-Dawn/Factory.h diff --git a/Escape-the-Dawn.sln b/Escape-the-Dawn.sln index 75bbb7b..213283c 100644 --- a/Escape-the-Dawn.sln +++ b/Escape-the-Dawn.sln @@ -4,6 +4,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Escape-the-Dawn", "Escape-the-Dawn\Escape-the-Dawn.vcxproj", "{FE405CFA-8FCE-4867-92CF-797E73B36482}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tests", "Tests\Tests.vcxproj", "{B9A8F4AA-BE85-45EA-84DD-652D13DD22BE}" + ProjectSection(ProjectDependencies) = postProject + {A9755CAA-62DC-442D-B82C-F548546CFD38} = {A9755CAA-62DC-442D-B82C-F548546CFD38} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Executable", "Executable\Executable.vcxproj", "{A9755CAA-62DC-442D-B82C-F548546CFD38}" EndProject diff --git a/Escape-the-Dawn/AABB.cpp b/Escape-the-Dawn/AABB.cpp deleted file mode 100644 index 72022a6..0000000 --- a/Escape-the-Dawn/AABB.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "AABB.h" - -AABB::AABB(glm::vec3 _volumeVector, glm::vec3 _origin) -{ - volumeVector = _volumeVector; - origin = _origin; - - CreateBB(); -} - -void AABB::CreateBB() -{ - // Cube 1x1x1, centered on origin - GLfloat vertices[] = { - origin.x - 0.5, origin.y - 0.5, origin.z - 0.5, 1.0, - origin.x + 0.5, origin.y - 0.5, origin.z - 0.5, 1.0, - origin.x + 0.5, origin.y + 0.5, origin.z - 0.5, 1.0, - origin.x - 0.5, origin.y + 0.5, origin.z - 0.5, 1.0, - origin.x - 0.5, origin.y - 0.5, origin.z + 0.5, 1.0, - origin.x + 0.5, origin.y - 0.5, origin.z + 0.5, 1.0, - origin.x + 0.5, origin.y + 0.5, origin.z + 0.5, 1.0, - origin.x - 0.5, origin.y + 0.5, origin.z + 0.5, 1.0, - }; - GLuint vbo_vertices; - glGenBuffers(1, &vbo_vertices); - glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - - GLushort elements[] = { - 0, 1, 2, 3, - 4, 5, 6, 7, - 0, 4, 1, 5, - 2, 6, 3, 7 - }; - GLuint ibo_elements; - glGenBuffers(1, &ibo_elements); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - glm::vec3 size = glm::vec3(glm::abs(origin.x-volumeVector.x), glm::abs(origin.y-volumeVector.y), glm::abs(origin.z-volumeVector.z)); - glm::mat4 transform = glm::translate(glm::mat4(1), origin) * glm::scale(glm::mat4(1), size); - - for (int i = 0; i < ) -} \ No newline at end of file diff --git a/Escape-the-Dawn/AABB.h b/Escape-the-Dawn/AABB.h index c56d5c0..85f07cb 100644 --- a/Escape-the-Dawn/AABB.h +++ b/Escape-the-Dawn/AABB.h @@ -1,23 +1,15 @@ #ifndef AABB_h__ #define AABB_h__ -#include -//#define GLFW_INCLUDE_GLU -//#include -//#include -#include -#include +#include -class AABB +struct AABB { -public: - AABB(glm::vec3, glm::vec3); - void CreateBB(void); - + AABB(glm::vec3 origin, glm::vec3 volumeVector) + : Origin(origin), VolumeVector(volumeVector) { } -private: - glm::vec3 origin; - glm::vec3 volumeVector; //The vector that defines the volume of the BB, it goes from one corner to the opposite one - + glm::vec3 Origin; + glm::vec3 VolumeVector; //The vector that defines the volume of the BB, it goes from the origin to a corner }; + #endif // !AABB_h__ diff --git a/Escape-the-Dawn/Component.h b/Escape-the-Dawn/Component.h index c3b49f4..221108c 100644 --- a/Escape-the-Dawn/Component.h +++ b/Escape-the-Dawn/Component.h @@ -1,7 +1,7 @@ #ifndef Component_h__ #define Component_h__ -#include "ComponentFactory.h" +#include "Factory.h" #include "Entity.h" struct Component @@ -9,4 +9,6 @@ struct Component EntityID Entity; }; +//&static ComponentFactory componentFactory; + #endif // Component_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/ComponentFactory.h b/Escape-the-Dawn/ComponentFactory.h deleted file mode 100644 index 4596c0b..0000000 --- a/Escape-the-Dawn/ComponentFactory.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef ComponentFactory_h__ -#define ComponentFactory_h__ - -#include -#include -#include -#include - -struct Component; - -#define REGISTER_COMPONENT(NAME, TYPE) \ - static ComponentRegistrar registrar(NAME); - -template -class ComponentRegistrar -{ -public: - ComponentRegistrar(std::string name) - { - ComponentFactory::Instance()->Register(name, [](void) -> std::shared_ptr { return std::shared_ptr(new T()); }); - } -}; - -class ComponentFactory -{ -public: - static ComponentFactory* Instance() - { - static ComponentFactory factory; - return &factory; - } - - void Register(std::string name, std::function(void)> factoryFunction) - { - m_FactoryFunctions[name] = factoryFunction; - } - - std::shared_ptr Create(std::string name) - { - auto it = m_FactoryFunctions.find(name); - if (it != m_FactoryFunctions.end()) - return it->second(); - else - return nullptr; - } - -private: - std::map(void)>> m_FactoryFunctions; -}; - -#endif // ComponentFactory_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Bounds.h b/Escape-the-Dawn/Components/Bounds.h index da97403..b717776 100644 --- a/Escape-the-Dawn/Components/Bounds.h +++ b/Escape-the-Dawn/Components/Bounds.h @@ -1,5 +1,5 @@ -#ifndef Bounds_h__ -#define Bounds_h__ +#ifndef Components_Bounds_h__ +#define Components_Bounds_h__ #include "Component.h" #include "AABB.h" @@ -7,11 +7,10 @@ namespace Components { - struct Bounds : Component - { - AABB BB; //Axis Aligned Bounding Box - }; - REGISTER_COMPONENT("Bounds", Bounds); +struct Bounds : Component +{ + AABB BoundingBox; //Axis Aligned Bounding Box +}; } -#endif // !Bounds_h__ \ No newline at end of file +#endif // !Components_Bounds_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Camera.h b/Escape-the-Dawn/Components/Camera.h index 44226e8..c836df4 100644 --- a/Escape-the-Dawn/Components/Camera.h +++ b/Escape-the-Dawn/Components/Camera.h @@ -1,18 +1,17 @@ -#ifndef Camera_h__ -#define Camera_h__ +#ifndef Components_Camera_h__ +#define Components_Camera_h__ #include "Component.h" namespace Components { - struct Camera : Component - { - int FOV; - int NearClip; - int FarClip; - }; - REGISTER_COMPONENT("Camera", Camera); +struct Camera : Component +{ + int FOV; + int NearClip; + int FarClip; +}; } -#endif // !Camera_h__ \ No newline at end of file +#endif // !Components_Camera_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Collision.h b/Escape-the-Dawn/Components/Collision.h index d471d1c..b9f44a5 100644 --- a/Escape-the-Dawn/Components/Collision.h +++ b/Escape-the-Dawn/Components/Collision.h @@ -1,18 +1,18 @@ -#ifndef Collision_h__ -#define Collision_h__ +#ifndef Components_Collision_h__ +#define Components_Collision_h__ +#include "Entity.h" #include "Component.h" #include namespace Components { - struct Collision : Component - { - bool Phantom; - std::vector CollidingEntities; - }; - REGISTER_COMPONENT("Collision", Collision); +struct Collision : Component +{ + bool Phantom; + std::vector CollidingEntities; +}; } -#endif // !Collision_h__ \ No newline at end of file +#endif // !Components_Collision_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/DirectionalLight.h b/Escape-the-Dawn/Components/DirectionalLight.h index 17c578d..ee2dd72 100644 --- a/Escape-the-Dawn/Components/DirectionalLight.h +++ b/Escape-the-Dawn/Components/DirectionalLight.h @@ -1,5 +1,5 @@ -#ifndef DirectionalLight_h__ -#define DirectionalLight_h__ +#ifndef Components_DirectionalLight_h__ +#define Components_DirectionalLight_h__ #include "Component.h" #include "Color.h" @@ -7,14 +7,13 @@ namespace Components { - struct DirectionalLight : Component - { - float Intensity; - float MaxRange; - float SpecularIntensity; - Color color; - }; - REGISTER_COMPONENT("DirectionalLight", DirectionalLight); +struct DirectionalLight : Component +{ + float Intensity; + float MaxRange; + float SpecularIntensity; + Color Color; +}; } -#endif // !DirectionalLight_h__ \ No newline at end of file +#endif // !Components_DirectionalLight_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Input.h b/Escape-the-Dawn/Components/Input.h index 9d0c346..f5c9e2a 100644 --- a/Escape-the-Dawn/Components/Input.h +++ b/Escape-the-Dawn/Components/Input.h @@ -1,5 +1,5 @@ -#ifndef Input_h__ -#define Input_h__ +#ifndef Components_Input_h__ +#define Components_Input_h__ #include "Component.h" #include @@ -7,13 +7,13 @@ namespace Components { - struct Input : Component - { - int KeyState[GLFW_KEY_LAST]; - float MouseState[2]; - float dX, dY; - }; - REGISTER_COMPONENT("Input", Input); +struct Input : Component +{ + int KeyState[GLFW_KEY_LAST]; + float MouseState[2]; + float dX, dY; +}; } -#endif // !Input_h__ \ No newline at end of file + +#endif // !Components_Input_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Model.h b/Escape-the-Dawn/Components/Model.h index 37dbdf0..ce27b3b 100644 --- a/Escape-the-Dawn/Components/Model.h +++ b/Escape-the-Dawn/Components/Model.h @@ -1,5 +1,7 @@ -#ifndef Model_h__ -#define Model_h__ +#ifndef Components_Model_h__ +#define Components_Model_h__ + +#include #include "Component.h" #include "Color.h" @@ -7,12 +9,11 @@ namespace Components { - struct Model : Component - { - std::string ModelName; - Color color; - }; - REGISTER_COMPONENT("Model", Model); +struct Model : Component +{ + std::string ModelFile; + Color Color; +}; } -#endif // !Model_h__ \ No newline at end of file +#endif // !Components_Model_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/ParticleEmitter.h b/Escape-the-Dawn/Components/ParticleEmitter.h index 5850f24..06a1658 100644 --- a/Escape-the-Dawn/Components/ParticleEmitter.h +++ b/Escape-the-Dawn/Components/ParticleEmitter.h @@ -1,5 +1,5 @@ -#ifndef ParticleEmitter_h__ -#define ParticleEmitter_h__ +#ifndef Components_ParticleEmitter_h__ +#define Components_ParticleEmitter_h__ #include "Component.h" #include "Color.h" @@ -8,19 +8,18 @@ namespace Components { - struct ParticleEmitter : Component - { - int ParticleTemplate; - float SpawnFrequency; - int SpawnCount; - std::vector ColorSpectrum; - std::vector ScaleSpectrum; - float SpreadAngle; - float LifeTime; - std::vector VelocitySpectrum; - std::vector AngularVelocitySpectrum; - }; - REGISTER_COMPONENT("ParticleEmitter", ParticleEmitter); +struct ParticleEmitter : Component +{ + int ParticleTemplate; + float SpawnFrequency; + int SpawnCount; + std::vector ColorSpectrum; + std::vector ScaleSpectrum; + float SpreadAngle; + float LifeTime; + std::vector VelocitySpectrum; + std::vector AngularVelocitySpectrum; +}; } -#endif // !ParticleEmitter_h__ \ No newline at end of file +#endif // !Components_ParticleEmitter_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/PointLight.h b/Escape-the-Dawn/Components/PointLight.h index 70ec5ac..daf3aa9 100644 --- a/Escape-the-Dawn/Components/PointLight.h +++ b/Escape-the-Dawn/Components/PointLight.h @@ -1,5 +1,5 @@ -#ifndef PointLight_h__ -#define PointLight_h__ +#ifndef Components_PointLight_h__ +#define Components_PointLight_h__ #include "Component.h" #include "Color.h" @@ -7,14 +7,13 @@ namespace Components { - struct PointLight : Component - { - float Intensity; - float MaxRange; - float SpecularIntensity; - Color color; - }; - REGISTER_COMPONENT("PointLight", PointLight); +struct PointLight : Component +{ + float Intensity; + float MaxRange; + float SpecularIntensity; + Color Color; +}; } -#endif // !PointLight_h__ \ No newline at end of file +#endif // !Components_PointLight_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/PowerUp.h b/Escape-the-Dawn/Components/PowerUp.h index 012b85c..fce5d6e 100644 --- a/Escape-the-Dawn/Components/PowerUp.h +++ b/Escape-the-Dawn/Components/PowerUp.h @@ -1,16 +1,15 @@ -#ifndef PowerUp_h__ -#define PowerUp_h__ +#ifndef Components_PowerUp_h__ +#define Components_PowerUp_h__ #include "Component.h" namespace Components { - struct PowerUp : Component - { - int Speed; - }; - REGISTER_COMPONENT("PowerUp", PowerUp); +struct PowerUp : Component +{ + float Speed; +}; } -#endif // !PowerUp_h__ \ No newline at end of file +#endif // !Components_PowerUp_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/SoundEmitter.h b/Escape-the-Dawn/Components/SoundEmitter.h index 6409ab1..01772cd 100644 --- a/Escape-the-Dawn/Components/SoundEmitter.h +++ b/Escape-the-Dawn/Components/SoundEmitter.h @@ -1,18 +1,19 @@ -#ifndef SoundEmitter_h__ -#define SoundEmitter_h__ +#ifndef Components_SoundEmitter_h__ +#define Components_SoundEmitter_h__ + +#include #include "Component.h" namespace Components { - struct SoundEmitter : Component - { - std::string SoundFile; - float Volume; - float MaxRange; - }; - REGISTER_COMPONENT("SoundEmitter", SoundEmitter); +struct SoundEmitter : Component +{ + std::string SoundFile; + float Volume; + float MaxRange; +}; } -#endif // !SoundEmitter_h__ \ No newline at end of file +#endif // !Components_SoundEmitter_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Sprite.h b/Escape-the-Dawn/Components/Sprite.h index a64a3f1..f54d3d0 100644 --- a/Escape-the-Dawn/Components/Sprite.h +++ b/Escape-the-Dawn/Components/Sprite.h @@ -1,5 +1,7 @@ -#ifndef Sprite_h__ -#define Sprite_h__ +#ifndef Components_Sprite_h__ +#define Components_Sprite_h__ + +#include #include "Component.h" #include "Color.h" @@ -7,12 +9,11 @@ namespace Components { - struct Sprite : Component - { - std::string SpriteFile; - Color color; - }; - REGISTER_COMPONENT("Sprite", Sprite); +struct Sprite : Component +{ + std::string SpriteFile; + Color Color; +}; } -#endif // !Sprite_h__ \ No newline at end of file +#endif // !Components_Sprite_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Stat.h b/Escape-the-Dawn/Components/Stat.h index 618adb7..31a3dc3 100644 --- a/Escape-the-Dawn/Components/Stat.h +++ b/Escape-the-Dawn/Components/Stat.h @@ -1,17 +1,16 @@ -#ifndef Stat_h__ -#define Stat_h__ +#ifndef Components_Stat_h__ +#define Components_Stat_h__ #include "Component.h" namespace Components { - struct Stat : Component - { - int Health; - bool Destroyable; - }; - REGISTER_COMPONENT("Stat", Stat); +struct Stat : Component +{ + float Health; + bool Destroyable; +}; } -#endif // !Stat_h__ \ No newline at end of file +#endif // !Components_Stat_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Template.h b/Escape-the-Dawn/Components/Template.h index 465187e..4bcb25c 100644 --- a/Escape-the-Dawn/Components/Template.h +++ b/Escape-the-Dawn/Components/Template.h @@ -1,13 +1,12 @@ -#ifndef Template_h__ -#define Template_h__ +#ifndef Components_Template_h__ +#define Components_Template_h__ #include "Component.h" namespace Components { - struct Template : Component { }; - REGISTER_COMPONENT("Template", Template); +struct Template : Component { }; } -#endif // !Template_h__ \ No newline at end of file +#endif // !Components_Template_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Components/Transform.h b/Escape-the-Dawn/Components/Transform.h new file mode 100644 index 0000000..9f2df7a --- /dev/null +++ b/Escape-the-Dawn/Components/Transform.h @@ -0,0 +1,19 @@ +#ifndef Components_Transform_h__ +#define Components_Transform_h__ + +#include "Component.h" +#include + +namespace Components +{ + +struct Transform : Component +{ + glm::vec3 Position; + glm::quat Orientation; + glm::vec3 Velocity; +}; + +} + +#endif // Components_Transform_h__ diff --git a/Escape-the-Dawn/Components/TransformComponent.h b/Escape-the-Dawn/Components/TransformComponent.h deleted file mode 100644 index 66cdb17..0000000 --- a/Escape-the-Dawn/Components/TransformComponent.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef Transform_h__ -#define Transform_h__ - -#include "Component.h" -#include - -namespace Components -{ - - struct Transform : Component - { - float Position[3]; - glm::fquat qtrn; //Quaternion with floats - float Velocity[3]; - }; - REGISTER_COMPONENT("Transform", Transform); - -} -#endif // Transform_h__ diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 133bbc3..22a9e55 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -98,8 +98,6 @@ - - @@ -115,9 +113,9 @@ - - + + diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters index 65cd581..99a8f67 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters @@ -1,7 +1,6 @@  - @@ -27,14 +26,10 @@ - - - - @@ -64,18 +59,6 @@ Components - - Components - - - Components - - - Components - - - Components - Components @@ -105,6 +88,25 @@ + + + Components + + + Components + + + Components + + + Components + + + Util + + + Util + @@ -124,5 +126,8 @@ {54f1eb6f-dc00-4e97-a3ef-ac255d755e2e} + + {d985d7a7-c041-46fe-ae03-aa40295a7aad} + \ No newline at end of file diff --git a/Escape-the-Dawn/Factory.h b/Escape-the-Dawn/Factory.h new file mode 100644 index 0000000..1a82f5b --- /dev/null +++ b/Escape-the-Dawn/Factory.h @@ -0,0 +1,35 @@ +#ifndef ComponentFactory_h__ +#define ComponentFactory_h__ + +#include +#include +#include +#include + +template +class Factory +{ +public: + void Register(std::string name, std::function factoryFunction) + { + m_FactoryFunctions[name] = factoryFunction; + } + + T Create(std::string name) + { + auto it = m_FactoryFunctions.find(name); + if (it != m_FactoryFunctions.end()) { + return it->second(); + } else { + return nullptr; + } + } + +private: + std::map> m_FactoryFunctions; +}; + +struct Component; +class ComponentFactory : public Factory { }; + +#endif // ComponentFactory_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/CollisionSystem.h b/Escape-the-Dawn/Systems/CollisionSystem.h index 008115a..2648e70 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.h +++ b/Escape-the-Dawn/Systems/CollisionSystem.h @@ -4,8 +4,7 @@ #include "System.h" #include "logging.h" -#include "Components/TransformComponent.h" - +#include "Components/Transform.h" namespace Systems { diff --git a/Escape-the-Dawn/Systems/InputSystem.cpp b/Escape-the-Dawn/Systems/InputSystem.cpp index e69de29..15f0790 100644 --- a/Escape-the-Dawn/Systems/InputSystem.cpp +++ b/Escape-the-Dawn/Systems/InputSystem.cpp @@ -0,0 +1,12 @@ +#include "InputSystem.h" +#include "World.h" + +void Systems::InputSystem::Update(double dt, EntityID entity, EntityID parent) +{ + auto input = m_World->GetComponent(entity, "Input"); + if (input == nullptr) + return; + + +} + diff --git a/Escape-the-Dawn/Systems/InputSystem.h b/Escape-the-Dawn/Systems/InputSystem.h index e69de29..951a75f 100644 --- a/Escape-the-Dawn/Systems/InputSystem.h +++ b/Escape-the-Dawn/Systems/InputSystem.h @@ -0,0 +1,27 @@ +#ifndef InputSystem_h__ +#define InputSystem_h__ + +#include + +#include "System.h" + +#include "Components/Input.h" + +namespace Systems +{ + +class InputSystem : public System +{ +public: + InputSystem(World* world) + : System(world) + { + + } + + void Update(double dt, EntityID entity, EntityID parent) override; +}; + +} + +#endif // InputSystem_h__ diff --git a/Escape-the-Dawn/World.cpp b/Escape-the-Dawn/World.cpp index fe130a8..43dd8d8 100644 --- a/Escape-the-Dawn/World.cpp +++ b/Escape-the-Dawn/World.cpp @@ -84,4 +84,19 @@ World::World() m_LastEntityID = 0; m_Systems.push_back(std::make_shared(this)); -} \ No newline at end of file + + RegisterComponents(); + RegisterSystems(); +} + +void World::RegisterComponents() +{ + m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); }); + m_ComponentFactory.Register("Input", []() { return new Components::Input(); }); + m_ComponentFactory.Register("DirectionalLight", []() { return new Components::DirectionalLight(); }); +} + +void World::RegisterSystems() +{ + +} diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index 1dec1c5..921c273 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -8,7 +8,11 @@ #include "Entity.h" #include "Component.h" -#include "ComponentFactory.h" +#include "Factory.h" +#include "Components/Transform.h" +#include "Components/Input.h" +#include "Components/DirectionalLight.h" + #include "System.h" #include "Systems/CollisionSystem.h" @@ -18,6 +22,9 @@ public: World(); ~World(); + virtual void RegisterComponents(); + virtual void RegisterSystems(); + EntityID CreateEntity(EntityID parent = 0); void RemoveEntity(EntityID entity); @@ -29,12 +36,22 @@ public: template std::shared_ptr AddComponent(EntityID entity, std::string componentType) { - std::shared_ptr component = std::static_pointer_cast(ComponentFactory::Instance()->Create(componentType)); + std::shared_ptr component = std::shared_ptr(static_cast(m_ComponentFactory.Create(componentType))); + if (component == nullptr) { + LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType.c_str(), entity); + return nullptr; + } + m_ComponentsOfType[componentType].push_back(component); m_EntityComponents[entity][componentType] = component; return component; } + std::shared_ptr AddComponent(EntityID entity, std::string componentType) + { + return AddComponent(entity, componentType); + } + template std::shared_ptr GetComponent(EntityID entity, std::string componentType) { @@ -56,6 +73,7 @@ private: // A bottom to top tree. A map of child entities to parent entities. std::unordered_map m_EntityParents; + ComponentFactory m_ComponentFactory; std::unordered_map>> m_ComponentsOfType; std::unordered_map>> m_EntityComponents; std::vector> m_Systems; diff --git a/Tests/Tests.vcxproj b/Tests/Tests.vcxproj index 75763b5..eeb8982 100644 --- a/Tests/Tests.vcxproj +++ b/Tests/Tests.vcxproj @@ -63,6 +63,13 @@ + + + $(OutDir)$(TargetName)$(TargetExt) --log_level=test_suite + + + Running tests + @@ -78,6 +85,13 @@ true boost_unit_test_framework-vc110-1_55.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + $(OutDir)$(TargetName)$(TargetExt) --log_level=test_suite + + + Running tests + diff --git a/Tests/main.cpp b/Tests/main.cpp index 4668e93..027263d 100644 --- a/Tests/main.cpp +++ b/Tests/main.cpp @@ -1,5 +1,7 @@ +#include "logging.h" + #include "World.h" -#include "Components/TransformComponent.h" +#include "Components/Transform.h" #define BOOST_TEST_MODULE World #define BOOST_TEST_DYN_LINK @@ -50,4 +52,13 @@ BOOST_AUTO_TEST_CASE(Recycling) world.RemoveEntity(ent11); EntityID ent12 = world.CreateEntity(); // 1 BOOST_REQUIRE(ent11 == ent12); +} + +BOOST_AUTO_TEST_CASE(ComponentCreation) +{ + World world; + + EntityID ent = world.CreateEntity(); + BOOST_CHECK(world.AddComponent(ent, "Transform") != nullptr); + BOOST_CHECK(world.AddComponent(ent, "Input") != nullptr); } \ No newline at end of file From b1f0bc7cced0103f03fbe6423cc19b31eca2f4c6 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 7 Mar 2014 02:28:19 +0100 Subject: [PATCH 2/4] Renderer refactoring. WIP InputSystem. --- Escape-the-Dawn/Components/Input.h | 2 +- Escape-the-Dawn/Escape-the-Dawn.vcxproj | 2 +- .../Escape-the-Dawn.vcxproj.filters | 2 +- Escape-the-Dawn/Renderer.h | 26 +++++++++--- Escape-the-Dawn/Renderer/Renderer.h | 42 ------------------- Escape-the-Dawn/Systems/InputSystem.cpp | 6 +++ Escape-the-Dawn/Systems/InputSystem.h | 15 ++++--- Escape-the-Dawn/World.h | 3 +- 8 files changed, 39 insertions(+), 59 deletions(-) delete mode 100644 Escape-the-Dawn/Renderer/Renderer.h diff --git a/Escape-the-Dawn/Components/Input.h b/Escape-the-Dawn/Components/Input.h index f5c9e2a..ca7dfe6 100644 --- a/Escape-the-Dawn/Components/Input.h +++ b/Escape-the-Dawn/Components/Input.h @@ -10,7 +10,7 @@ namespace Components struct Input : Component { int KeyState[GLFW_KEY_LAST]; - float MouseState[2]; + int MouseState[GLFW_MOUSE_BUTTON_LAST]; float dX, dY; }; diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 22a9e55..6dd87d1 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -132,7 +132,7 @@ - + diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters index 99a8f67..a27572c 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters @@ -30,7 +30,6 @@ - @@ -107,6 +106,7 @@ Util + diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index 4dc117b..3280816 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -1,3 +1,6 @@ +#ifndef Renderer_h__ +#define Renderer_h__ + #include #include #include @@ -16,19 +19,32 @@ #include #include "glerror.h" - +#include "ShaderProgram.h" class Renderer { public: - GLuint shaderProgram; + std::shared_ptr m_ShaderProgram; GLuint VAO; + std::vector> Vertices; + std::vector> Normals; + std::vector> TextureCoords; + Renderer(); - + void Draw(); void DrawText(); - GLuint CompileShader(GLenum, std::string); + void AddObjectToDraw(); + void AddTextToDraw(); -}; \ No newline at end of file + void LoadContent(); + + GLFWwindow* GetWindow() const { return m_Window; } + +private: + GLFWwindow* m_Window; +}; + +#endif // Renderer_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Renderer/Renderer.h b/Escape-the-Dawn/Renderer/Renderer.h deleted file mode 100644 index 77f37f5..0000000 --- a/Escape-the-Dawn/Renderer/Renderer.h +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include -#include -#include - -#define _CRT_SECURE_NO_WARNINGS -#include -#define GLFW_INCLUDE_GLU -#include -#include -#define GLM_FORCE_RADIANS -#include -#include -#include -#include - -#include "glerror.h" -#include "ShaderProgram.h" - -class Renderer -{ -public: - std::shared_ptr m_ShaderProgram; - GLuint VAO; - - std::vector> Vertices; - std::vector> Normals; - std::vector> TextureCoords; - - Renderer(); - - void Draw(); - void DrawText(); - - void AddObjectToDraw(); - void AddTextToDraw(); - - void LoadContent(); - GLuint CompileShader(GLenum, std::string); - -}; \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/InputSystem.cpp b/Escape-the-Dawn/Systems/InputSystem.cpp index 15f0790..c0d6822 100644 --- a/Escape-the-Dawn/Systems/InputSystem.cpp +++ b/Escape-the-Dawn/Systems/InputSystem.cpp @@ -7,6 +7,12 @@ void Systems::InputSystem::Update(double dt, EntityID entity, EntityID parent) if (input == nullptr) return; + for (int i = 0; i <= GLFW_KEY_LAST; ++i) { + input->KeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i); + } + for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) { + input->MouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i); + } } diff --git a/Escape-the-Dawn/Systems/InputSystem.h b/Escape-the-Dawn/Systems/InputSystem.h index 951a75f..ff51ec2 100644 --- a/Escape-the-Dawn/Systems/InputSystem.h +++ b/Escape-the-Dawn/Systems/InputSystem.h @@ -1,10 +1,8 @@ #ifndef InputSystem_h__ #define InputSystem_h__ -#include - #include "System.h" - +#include "Renderer.h" #include "Components/Input.h" namespace Systems @@ -13,13 +11,14 @@ namespace Systems class InputSystem : public System { public: - InputSystem(World* world) - : System(world) - { - - } + InputSystem(World* world, Renderer* renderer) + : System(world), m_Renderer(renderer) { } void Update(double dt, EntityID entity, EntityID parent) override; + +private: + Renderer* m_Renderer; + float m_LastMouseX, m_LastMouseY; }; } diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index 921c273..5ff403d 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -6,6 +6,8 @@ #include #include +#include "Renderer.h" + #include "Entity.h" #include "Component.h" #include "Factory.h" @@ -64,7 +66,6 @@ public: // Recursively update through the scene graph void Update(double dt); - void RecursiveUpdate(std::shared_ptr system, double dt, EntityID parentEntity); private: From 95a36794e33ec8edd3c40df1df38710cc25c2946 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 7 Mar 2014 02:53:52 +0100 Subject: [PATCH 3/4] Input system complete (untested) --- Escape-the-Dawn/Components/Input.h | 11 +++++-- Escape-the-Dawn/System.h | 5 +++- Escape-the-Dawn/Systems/InputSystem.cpp | 38 +++++++++++++++++++------ Escape-the-Dawn/Systems/InputSystem.h | 9 ++++++ Escape-the-Dawn/World.cpp | 1 + Escape-the-Dawn/World.h | 2 +- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/Escape-the-Dawn/Components/Input.h b/Escape-the-Dawn/Components/Input.h index ca7dfe6..d278aa7 100644 --- a/Escape-the-Dawn/Components/Input.h +++ b/Escape-the-Dawn/Components/Input.h @@ -1,16 +1,21 @@ #ifndef Components_Input_h__ #define Components_Input_h__ -#include "Component.h" +#include + #include +#include "Component.h" + namespace Components { struct Input : Component { - int KeyState[GLFW_KEY_LAST]; - int MouseState[GLFW_MOUSE_BUTTON_LAST]; + std::array KeyState; + std::array LastKeyState; + std::array MouseState; + std::array LastMouseState; float dX, dY; }; diff --git a/Escape-the-Dawn/System.h b/Escape-the-Dawn/System.h index 4ea04a1..f747476 100644 --- a/Escape-the-Dawn/System.h +++ b/Escape-the-Dawn/System.h @@ -11,7 +11,10 @@ public: System(World* world) : m_World(world) { } virtual ~System() { } - virtual void Update(double dt, EntityID entity, EntityID parent) = 0; + // Called once per system every tick + virtual void Update(double dt) { } + // Called once for every entity in the world every tick + virtual void Update(double dt, EntityID entity, EntityID parent) { } protected: World* m_World; diff --git a/Escape-the-Dawn/Systems/InputSystem.cpp b/Escape-the-Dawn/Systems/InputSystem.cpp index c0d6822..278a2d0 100644 --- a/Escape-the-Dawn/Systems/InputSystem.cpp +++ b/Escape-the-Dawn/Systems/InputSystem.cpp @@ -1,18 +1,40 @@ #include "InputSystem.h" #include "World.h" +void Systems::InputSystem::Update(double dt) +{ + m_LastKeyState = m_CurrentKeyState; + m_LastMouseState = m_CurrentMouseState; + + // Keyboard input + for (int i = 0; i <= GLFW_KEY_LAST; ++i) { + m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i); + } + + // Mouse buttons + for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) { + m_CurrentKeyState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i); + } + + // Cursor position + double xpos, ypos; + glfwGetCursorPos(m_Renderer->GetWindow(), &xpos, &ypos); + m_CurrentMouseDeltaX = xpos - m_LastMouseX; + m_CurrentMouseDeltaY = ypos - m_LastMouseY; + m_LastMouseX = xpos; + m_LastMouseY = ypos; +} + void Systems::InputSystem::Update(double dt, EntityID entity, EntityID parent) { auto input = m_World->GetComponent(entity, "Input"); if (input == nullptr) return; - for (int i = 0; i <= GLFW_KEY_LAST; ++i) { - input->KeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i); - } - - for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) { - input->MouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i); - } + input->KeyState = m_CurrentKeyState; + input->LastKeyState = m_LastKeyState; + input->MouseState = m_CurrentMouseState; + input->LastMouseState = m_LastMouseState; + input->dX = m_CurrentMouseDeltaX; + input->dY = m_CurrentMouseDeltaY; } - diff --git a/Escape-the-Dawn/Systems/InputSystem.h b/Escape-the-Dawn/Systems/InputSystem.h index ff51ec2..2c0e521 100644 --- a/Escape-the-Dawn/Systems/InputSystem.h +++ b/Escape-the-Dawn/Systems/InputSystem.h @@ -1,6 +1,8 @@ #ifndef InputSystem_h__ #define InputSystem_h__ +#include + #include "System.h" #include "Renderer.h" #include "Components/Input.h" @@ -14,10 +16,17 @@ public: InputSystem(World* world, Renderer* renderer) : System(world), m_Renderer(renderer) { } + void Update(double dt) override; void Update(double dt, EntityID entity, EntityID parent) override; private: Renderer* m_Renderer; + + std::array m_CurrentKeyState; + std::array m_LastKeyState; + std::array m_CurrentMouseState; + std::array m_LastMouseState; + float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY; float m_LastMouseX, m_LastMouseY; }; diff --git a/Escape-the-Dawn/World.cpp b/Escape-the-Dawn/World.cpp index 43dd8d8..ae5a7d8 100644 --- a/Escape-the-Dawn/World.cpp +++ b/Escape-the-Dawn/World.cpp @@ -32,6 +32,7 @@ void World::RecursiveUpdate(std::shared_ptr system, double dt, EntityID void World::Update(double dt) { for (auto system : m_Systems) { + system->Update(dt); RecursiveUpdate(system, dt, 0); } } diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index 5ff403d..874cee4 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -64,8 +64,8 @@ public: void AddSystem(System* system); - // Recursively update through the scene graph void Update(double dt); + // Recursively update through the scene graph void RecursiveUpdate(std::shared_ptr system, double dt, EntityID parentEntity); private: From 1f48279bc6cf94dba885e23c03e747b87a54be96 Mon Sep 17 00:00:00 2001 From: Tobias Dahl Date: Fri, 7 Mar 2014 19:07:07 +0100 Subject: [PATCH 4/4] WIP (Really really broken tho) --- Escape-the-Dawn/Engine.h | 49 +++----- Escape-the-Dawn/Escape-the-Dawn.vcxproj | 3 +- .../Escape-the-Dawn.vcxproj.filters | 3 +- Escape-the-Dawn/Renderer.cpp | 107 ++++++++++-------- Escape-the-Dawn/Renderer.h | 13 ++- Escape-the-Dawn/Renderer/Renderer.cpp | 38 ------- Escape-the-Dawn/ShaderProgram.cpp | 4 +- 7 files changed, 88 insertions(+), 129 deletions(-) delete mode 100644 Escape-the-Dawn/Renderer/Renderer.cpp diff --git a/Escape-the-Dawn/Engine.h b/Escape-the-Dawn/Engine.h index a93eb0f..99f037a 100644 --- a/Escape-the-Dawn/Engine.h +++ b/Escape-the-Dawn/Engine.h @@ -11,53 +11,32 @@ #include "World.h" + + class Engine { public: Engine(int argc, char* argv[]) { - // Initialize GLFW - if (!glfwInit()) { - LOG_ERROR("GLFW: Initialization failed"); - exit(EXIT_FAILURE); - } - - // Create a window - m_Window = glfwCreateWindow(1280, 720, "OpenGL", nullptr, nullptr); - if (!m_Window) { - LOG_ERROR("GLFW: Failed to create window"); - exit(EXIT_FAILURE); - } - glfwMakeContextCurrent(m_Window); - - // GL version info - glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]); - glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]); - glVendor = (GLchar*)glGetString(GL_VENDOR); - std::stringstream ss; - ss << glVendor << " OpenGL " << glVersion[0] << "." << glVersion[1]; -#ifdef DEBUG - ss << " DEBUG"; -#endif - LOG_INFO(ss.str().c_str()); - glfwSetWindowTitle(m_Window, ss.str().c_str()); - - // Initialize GLEW - if (glewInit() != GLEW_OK) { - LOG_ERROR("GLEW: Initialization failed"); - exit(EXIT_FAILURE); - } + renderer.Initialize(); + m_LastTime = glfwGetTime(); } - bool Running() const { return !glfwWindowShouldClose(m_Window); } + bool Running() const { return !glfwWindowShouldClose(renderer.GetWindow()); } void Tick() { + double currentTime = glfwGetTime(); + double dt = currentTime - m_LastTime; + m_LastTime = currentTime; + + //World.Update(dt); + renderer.Draw(dt); + glfwPollEvents(); } private: - GLFWwindow* m_Window; - GLint glVersion[2]; - GLchar* glVendor; + Renderer renderer; + double m_LastTime; }; \ No newline at end of file diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 6dd87d1..d38ea9e 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -99,7 +99,8 @@ - + + diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters index a27572c..93547f2 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters @@ -1,7 +1,6 @@  - Systems @@ -26,6 +25,8 @@ + + diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index aa59a22..985bf2c 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -1,64 +1,77 @@ -#include "Renderer/Renderer.h" +#include "Renderer.h" Renderer::Renderer() { - } -void Renderer::Draw() +void Renderer::Initialize() { + // Initialize GLFW + if (!glfwInit()) { + LOG_ERROR("GLFW: Initialization failed"); + exit(EXIT_FAILURE); + } + // Create a window + m_Window = glfwCreateWindow(1280, 720, "OpenGL", nullptr, nullptr); + if (!m_Window) { + LOG_ERROR("GLFW: Failed to create window"); + exit(EXIT_FAILURE); + } + glfwMakeContextCurrent(m_Window); + + // GL version info + glGetIntegerv(GL_MAJOR_VERSION, &m_glVersion[0]); + glGetIntegerv(GL_MINOR_VERSION, &m_glVersion[1]); + m_glVendor = (GLchar*)glGetString(GL_VENDOR); + std::stringstream ss; + ss << m_glVendor << " OpenGL " << m_glVersion[0] << "." << m_glVersion[1]; +#ifdef DEBUG + ss << " DEBUG"; +#endif + LOG_INFO(ss.str().c_str()); + glfwSetWindowTitle(m_Window, ss.str().c_str()); + + // Initialize GLEW + if (glewInit() != GLEW_OK) { + LOG_ERROR("GLEW: Initialization failed"); + exit(EXIT_FAILURE); + } + glEnable(GL_DEPTH_TEST); + + LoadContent(); +} + +void Renderer::Draw(double _dt) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClearColor(1.0f, 1.0f, 0.0f, 1.0f); + + //m_ShaderProgram->Bind(); + + glfwSwapBuffers(m_Window); } void Renderer::DrawText() { + //DrawShitInTextForm +} +void Renderer::AddTextToDraw() +{ + //Add to draw shit vector +} + +void Renderer::AddModelToDraw(Model* _model) +{ + ModelsToRender.push_back(_model); } //Fixa med shaders, lägga in alla verts osv. - -GLuint Renderer::CompileShader(GLenum shaderType, std::string fileName) +void Renderer::LoadContent() { - std::string shaderFile; - std::ifstream in(fileName, std::ios::in); - - if(!in) - { - LOG_ERROR("Error: Failed to open shader file %s", fileName); - return 0; - } - in.seekg(0, std::ios::end); - shaderFile.resize((int)in.tellg()); - in.seekg(0, std::ios::beg); - in.read(&shaderFile[0], shaderFile.size()); - in.close(); - - GLuint shader = glCreateShader(shaderType); //GL_VERTEX_SHADER, GL_FRAGMENT_SHADER - if(GLERROR("glCreateShader")) - return 0; - - const GLchar* shaderFiles = shaderFile.c_str(); - const GLint length = shaderFile.length(); - glShaderSource(shader, 1, &shaderFiles, &length); - if(GLERROR("glShaderSource")) - return 0; - - glCompileShader(shader); - GLint compileStatus; - glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus); - if(compileStatus != GL_TRUE) - { - GLsizei infoLogLength; - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); - GLchar* infolog = new GLchar[infoLogLength]; - glGetShaderInfoLog(shader, infoLogLength, &infoLogLength, infolog); - std::cerr << infolog << std::endl; - delete[] infolog; - } - - if(GLERROR("glCompileShader")) - return 0; - - return shader; -} \ No newline at end of file + m_ShaderProgram->AddShader(std::unique_ptr(new VertexShader("Shaders/Vertex.glsl"))); + m_ShaderProgram->AddShader(std::unique_ptr(new FragmentShader("Shaders/Fragment.glsl"))); + m_ShaderProgram->Link(); +} diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index 3280816..c7547a6 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -20,23 +20,22 @@ #include "glerror.h" #include "ShaderProgram.h" +#include "Model.h" class Renderer { public: std::shared_ptr m_ShaderProgram; - GLuint VAO; - std::vector> Vertices; - std::vector> Normals; - std::vector> TextureCoords; + std::vector ModelsToRender; Renderer(); - void Draw(); + void Initialize(); + void Draw(double dt); void DrawText(); - void AddObjectToDraw(); + void AddModelToDraw(Model*); void AddTextToDraw(); void LoadContent(); @@ -45,6 +44,8 @@ public: private: GLFWwindow* m_Window; + GLint m_glVersion[2]; + GLchar* m_glVendor; }; #endif // Renderer_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Renderer/Renderer.cpp b/Escape-the-Dawn/Renderer/Renderer.cpp deleted file mode 100644 index 2821bb5..0000000 --- a/Escape-the-Dawn/Renderer/Renderer.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "Renderer.h" - -Renderer::Renderer() -{ - glEnable(GL_DEPTH_TEST); -} - -void Renderer::Draw() -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glClearColor(1.0f, 1.0f, 0.0f, 1.0f); - - m_ShaderProgram->Bind(); -} - -void Renderer::DrawText() -{ - //DrawShitInTextForm -} - -void Renderer::AddTextToDraw() -{ - //Add to draw shit vector -} - -void Renderer::AddObjectToDraw() -{ - -} - -//Fixa med shaders, lägga in alla verts osv. - -void Renderer::LoadContent() -{ - m_ShaderProgram->AddShader(std::unique_ptr(new VertexShader("Shaders/Vertex.glsl"))); - m_ShaderProgram->AddShader(std::unique_ptr(new FragmentShader("Shaders/Fragment.glsl"))); - m_ShaderProgram->Link(); -} diff --git a/Escape-the-Dawn/ShaderProgram.cpp b/Escape-the-Dawn/ShaderProgram.cpp index 2aee783..f913f12 100644 --- a/Escape-the-Dawn/ShaderProgram.cpp +++ b/Escape-the-Dawn/ShaderProgram.cpp @@ -105,7 +105,9 @@ ShaderProgram::~ShaderProgram() void ShaderProgram::AddShader(std::unique_ptr shader) { if (!shader->IsCompiled()) { - shader->Compile(); + if (shader->Compile() == 0) { + return; + } } m_Shaders.push_back(std::move(shader));