Refactored factory and component system.

This commit is contained in:
2014-03-07 01:08:54 +01:00
parent 18197bf21d
commit e65db077d5
30 changed files with 313 additions and 284 deletions
+3
View File
@@ -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
-46
View File
@@ -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 < )
}
+7 -15
View File
@@ -1,23 +1,15 @@
#ifndef AABB_h__
#define AABB_h__
#include <GL/glew.h>
//#define GLFW_INCLUDE_GLU
//#include <GLFW/glfw3.h>
//#include <glext.h>
#include <glm/common.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/glm.hpp>
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__
+3 -1
View File
@@ -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__
-51
View File
@@ -1,51 +0,0 @@
#ifndef ComponentFactory_h__
#define ComponentFactory_h__
#include <string>
#include <memory>
#include <functional>
#include <map>
struct Component;
#define REGISTER_COMPONENT(NAME, TYPE) \
static ComponentRegistrar<TYPE> registrar(NAME);
template <typename T>
class ComponentRegistrar
{
public:
ComponentRegistrar(std::string name)
{
ComponentFactory::Instance()->Register(name, [](void) -> std::shared_ptr<Component> { return std::shared_ptr<Component>(new T()); });
}
};
class ComponentFactory
{
public:
static ComponentFactory* Instance()
{
static ComponentFactory factory;
return &factory;
}
void Register(std::string name, std::function<std::shared_ptr<Component>(void)> factoryFunction)
{
m_FactoryFunctions[name] = factoryFunction;
}
std::shared_ptr<Component> Create(std::string name)
{
auto it = m_FactoryFunctions.find(name);
if (it != m_FactoryFunctions.end())
return it->second();
else
return nullptr;
}
private:
std::map<std::string, std::function<std::shared_ptr<Component>(void)>> m_FactoryFunctions;
};
#endif // ComponentFactory_h__
+7 -8
View File
@@ -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__
#endif // !Components_Bounds_h__
+9 -10
View File
@@ -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__
#endif // !Components_Camera_h__
+9 -9
View File
@@ -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 <vector>
namespace Components
{
struct Collision : Component
{
bool Phantom;
std::vector<int> CollidingEntities;
};
REGISTER_COMPONENT("Collision", Collision);
struct Collision : Component
{
bool Phantom;
std::vector<EntityID> CollidingEntities;
};
}
#endif // !Collision_h__
#endif // !Components_Collision_h__
+10 -11
View File
@@ -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__
#endif // !Components_DirectionalLight_h__
+10 -10
View File
@@ -1,5 +1,5 @@
#ifndef Input_h__
#define Input_h__
#ifndef Components_Input_h__
#define Components_Input_h__
#include "Component.h"
#include <GLFW/glfw3.h>
@@ -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__
#endif // !Components_Input_h__
+10 -9
View File
@@ -1,5 +1,7 @@
#ifndef Model_h__
#define Model_h__
#ifndef Components_Model_h__
#define Components_Model_h__
#include <string>
#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__
#endif // !Components_Model_h__
+15 -16
View File
@@ -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<Color> ColorSpectrum;
std::vector<float> ScaleSpectrum;
float SpreadAngle;
float LifeTime;
std::vector<float[3]> VelocitySpectrum;
std::vector<float[3]> AngularVelocitySpectrum;
};
REGISTER_COMPONENT("ParticleEmitter", ParticleEmitter);
struct ParticleEmitter : Component
{
int ParticleTemplate;
float SpawnFrequency;
int SpawnCount;
std::vector<Color> ColorSpectrum;
std::vector<float> ScaleSpectrum;
float SpreadAngle;
float LifeTime;
std::vector<float[3]> VelocitySpectrum;
std::vector<float[3]> AngularVelocitySpectrum;
};
}
#endif // !ParticleEmitter_h__
#endif // !Components_ParticleEmitter_h__
+10 -11
View File
@@ -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__
#endif // !Components_PointLight_h__
+7 -8
View File
@@ -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__
#endif // !Components_PowerUp_h__
+11 -10
View File
@@ -1,18 +1,19 @@
#ifndef SoundEmitter_h__
#define SoundEmitter_h__
#ifndef Components_SoundEmitter_h__
#define Components_SoundEmitter_h__
#include <string>
#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__
#endif // !Components_SoundEmitter_h__
+10 -9
View File
@@ -1,5 +1,7 @@
#ifndef Sprite_h__
#define Sprite_h__
#ifndef Components_Sprite_h__
#define Components_Sprite_h__
#include <string>
#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__
#endif // !Components_Sprite_h__
+8 -9
View File
@@ -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__
#endif // !Components_Stat_h__
+4 -5
View File
@@ -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__
#endif // !Components_Template_h__
+19
View File
@@ -0,0 +1,19 @@
#ifndef Components_Transform_h__
#define Components_Transform_h__
#include "Component.h"
#include <glm/gtc/quaternion.hpp>
namespace Components
{
struct Transform : Component
{
glm::vec3 Position;
glm::quat Orientation;
glm::vec3 Velocity;
};
}
#endif // Components_Transform_h__
@@ -1,19 +0,0 @@
#ifndef Transform_h__
#define Transform_h__
#include "Component.h"
#include <glm/gtc/quaternion.hpp>
namespace Components
{
struct Transform : Component
{
float Position[3];
glm::fquat qtrn; //Quaternion with floats
float Velocity[3];
};
REGISTER_COMPONENT("Transform", Transform);
}
#endif // Transform_h__
+2 -4
View File
@@ -98,8 +98,6 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Engine.h" />
<ClCompile Include="AABB.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Model.cpp" />
<ClCompile Include="Renderer\Renderer.cpp" />
<ClCompile Include="Systems\CollisionSystem.cpp" />
@@ -115,9 +113,9 @@
<ClInclude Include="AABB.h" />
<ClInclude Include="Color.h" />
<ClInclude Include="Component.h" />
<ClInclude Include="ComponentFactory.h" />
<ClInclude Include="Components\Bounds.h" />
<ClInclude Include="Components\Camera.h" />
<ClInclude Include="Factory.h" />
<ClInclude Include="Components\Bounds.h" />
<ClInclude Include="Components\Collision.h" />
<ClInclude Include="Components\DirectionalLight.h" />
<ClInclude Include="Components\Input.h" />
+22 -17
View File
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="Renderer\Renderer.cpp" />
<ClCompile Include="World.cpp" />
<ClCompile Include="Systems\CollisionSystem.cpp">
@@ -27,14 +26,10 @@
</ClCompile>
<ClCompile Include="Model.cpp" />
<ClCompile Include="Engine.h" />
<ClCompile Include="AABB.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Component.h" />
<ClInclude Include="ComponentFactory.h" />
<ClInclude Include="Entity.h" />
<ClInclude Include="logging.h" />
<ClInclude Include="glerror.h" />
<ClInclude Include="Renderer\Renderer.h" />
<ClInclude Include="System.h" />
<ClInclude Include="World.h" />
@@ -64,18 +59,6 @@
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="ShaderProgram.h" />
<ClInclude Include="Components\Bounds.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="Components\Camera.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="Components\Collision.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="Components\DirectionalLight.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="Components\Input.h">
<Filter>Components</Filter>
</ClInclude>
@@ -105,6 +88,25 @@
</ClInclude>
<ClInclude Include="AABB.h" />
<ClInclude Include="Color.h" />
<ClInclude Include="Factory.h" />
<ClInclude Include="Components\DirectionalLight.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="Components\Collision.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="Components\Camera.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="Components\Bounds.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="glerror.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="logging.h">
<Filter>Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="Shaders\Fragment.glsl">
@@ -124,5 +126,8 @@
<Filter Include="Shaders">
<UniqueIdentifier>{54f1eb6f-dc00-4e97-a3ef-ac255d755e2e}</UniqueIdentifier>
</Filter>
<Filter Include="Util">
<UniqueIdentifier>{d985d7a7-c041-46fe-ae03-aa40295a7aad}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
+35
View File
@@ -0,0 +1,35 @@
#ifndef ComponentFactory_h__
#define ComponentFactory_h__
#include <string>
#include <memory>
#include <functional>
#include <map>
template <typename T>
class Factory
{
public:
void Register(std::string name, std::function<T(void)> 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<std::string, std::function<T(void)>> m_FactoryFunctions;
};
struct Component;
class ComponentFactory : public Factory<Component*> { };
#endif // ComponentFactory_h__
+1 -2
View File
@@ -4,8 +4,7 @@
#include "System.h"
#include "logging.h"
#include "Components/TransformComponent.h"
#include "Components/Transform.h"
namespace Systems
{
+12
View File
@@ -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<Components::Input>(entity, "Input");
if (input == nullptr)
return;
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef InputSystem_h__
#define InputSystem_h__
#include <GLFW/glfw3.h>
#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__
+16 -1
View File
@@ -84,4 +84,19 @@ World::World()
m_LastEntityID = 0;
m_Systems.push_back(std::make_shared<Systems::Collision>(this));
}
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()
{
}
+20 -2
View File
@@ -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 <class T>
std::shared_ptr<T> AddComponent(EntityID entity, std::string componentType)
{
std::shared_ptr<T> component = std::static_pointer_cast<T>(ComponentFactory::Instance()->Create(componentType));
std::shared_ptr<T> component = std::shared_ptr<T>(static_cast<T*>(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<Component> AddComponent(EntityID entity, std::string componentType)
{
return AddComponent<Component>(entity, componentType);
}
template <class T>
std::shared_ptr<T> 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<EntityID, EntityID> m_EntityParents;
ComponentFactory m_ComponentFactory;
std::unordered_map<std::string, std::vector<std::shared_ptr<Component>>> m_ComponentsOfType;
std::unordered_map<EntityID, std::map<std::string, std::shared_ptr<Component>>> m_EntityComponents;
std::vector<std::shared_ptr<System>> m_Systems;
+14
View File
@@ -63,6 +63,13 @@
</Link>
<PostBuildEvent />
<PostBuildEvent />
<PreBuildEvent />
<PostBuildEvent>
<Command>$(OutDir)$(TargetName)$(TargetExt) --log_level=test_suite</Command>
</PostBuildEvent>
<PostBuildEvent>
<Message>Running tests</Message>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
@@ -78,6 +85,13 @@
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>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)</AdditionalDependencies>
</Link>
<PreBuildEvent />
<PostBuildEvent>
<Command>$(OutDir)$(TargetName)$(TargetExt) --log_level=test_suite</Command>
</PostBuildEvent>
<PostBuildEvent>
<Message>Running tests</Message>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
+12 -1
View File
@@ -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);
}