Merge branch 'master' into OctTree
# Conflicts: # include/Game/Game.h # include/Game/HardcodedTestWorld.h # src/Game/Game.cpp
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
#define ComponentWrapper_h__
|
||||
|
||||
#include "../Common.h"
|
||||
#include "EntityWrapper.h"
|
||||
#include "Entity.h"
|
||||
#include "ComponentInfo.h"
|
||||
#include "Util/Any.h"
|
||||
|
||||
@@ -11,7 +11,7 @@ struct ComponentWrapper
|
||||
ComponentWrapper(const ComponentInfo& componentInfo, char* data)
|
||||
: Info(componentInfo)
|
||||
, EntityID(*reinterpret_cast<::EntityID*>(data))
|
||||
, Data(data + sizeof(EntityID))
|
||||
, Data(data + sizeof(::EntityID))
|
||||
{ }
|
||||
|
||||
const ComponentInfo& Info;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef Entity_h__
|
||||
#define Entity_h__
|
||||
|
||||
typedef unsigned int EntityID;
|
||||
|
||||
#endif
|
||||
@@ -1,12 +0,0 @@
|
||||
#include "ResourceManager.h"
|
||||
|
||||
class EntityFile : public Resource
|
||||
{
|
||||
friend class ResourceManager;
|
||||
|
||||
private:
|
||||
EntityFile(std::string path);
|
||||
|
||||
public:
|
||||
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
#ifndef Entity_h__
|
||||
#define Entity_h__
|
||||
|
||||
typedef unsigned int EntityID;
|
||||
|
||||
struct EntityWrapper
|
||||
{
|
||||
EntityWrapper(EntityID entityID)
|
||||
: ID(entityID)
|
||||
{ }
|
||||
|
||||
EntityID ID;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,141 @@
|
||||
#ifndef EntityXMLFile_h__
|
||||
#define EntityXMLFile_h__
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "../Common.h"
|
||||
#include "../GLM.h"
|
||||
#include <xercesc/dom/DOM.hpp>
|
||||
#include <xercesc/dom/DOMLSInput.hpp>
|
||||
#include <xercesc/dom/DOMElement.hpp>
|
||||
#include <xercesc/framework/Wrapper4InputSource.hpp>
|
||||
#include <xercesc/framework/LocalFileInputSource.hpp>
|
||||
#include <xercesc/framework/LocalFileFormatTarget.hpp>
|
||||
#include <xercesc/framework/MemBufFormatTarget.hpp>
|
||||
#include <xercesc/framework/MemBufInputSource.hpp>
|
||||
#include <xercesc/sax/HandlerBase.hpp>
|
||||
#include <xercesc/util/PlatformUtils.hpp>
|
||||
#include <xercesc/util/XMLFloat.hpp>
|
||||
#include <xercesc/framework/XMLGrammarPoolImpl.hpp>
|
||||
#include <xercesc/parsers/XercesDOMParser.hpp>
|
||||
#include <xercesc/framework/psvi/XSElementDeclaration.hpp>
|
||||
#include <xercesc/framework/psvi/XSComplexTypeDefinition.hpp>
|
||||
#include <xercesc/framework/psvi/XSParticle.hpp>
|
||||
#include <xercesc/framework/psvi/XSModelGroup.hpp>
|
||||
#include <xercesc/framework/psvi/XSAnnotation.hpp>
|
||||
#include <xercesc/framework/psvi/XSValue.hpp>
|
||||
#include <xercesc/framework/XMLValidator.hpp>
|
||||
|
||||
#include "ResourceManager.h"
|
||||
#include "Entity.h"
|
||||
#include "ComponentInfo.h"
|
||||
class World;
|
||||
|
||||
class EntityPreprocessorXMLErrorHandler : public xercesc::DOMErrorHandler
|
||||
{
|
||||
public:
|
||||
bool handleError(const xercesc::DOMError &e) override
|
||||
{
|
||||
char* message = xercesc::XMLString::transcode(e.getMessage());
|
||||
std::cerr << "Preprocessor DOMError: " << message << std::endl;
|
||||
xercesc::XMLString::release(&message);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class EntityParserXMLErrorHandler : public xercesc::ErrorHandler
|
||||
{
|
||||
public:
|
||||
void warning(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("Warning", e);
|
||||
}
|
||||
void error(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("Error", e);
|
||||
}
|
||||
void fatalError(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("FATAL ERROR", e);
|
||||
}
|
||||
void resetErrors() override { }
|
||||
|
||||
private:
|
||||
void reportParseException(std::string type, const xercesc::SAXParseException& e)
|
||||
{
|
||||
char* message = xercesc::XMLString::transcode(e.getMessage());
|
||||
char* systemID = xercesc::XMLString::transcode(e.getSystemId());
|
||||
std::cerr << systemID << ":" << e.getLineNumber() << ":" << e.getColumnNumber() << std::endl;
|
||||
std::cerr << type << ": " << message << std::endl;
|
||||
xercesc::XMLString::release(&systemID);
|
||||
xercesc::XMLString::release(&message);
|
||||
}
|
||||
};
|
||||
|
||||
class XSTR
|
||||
{
|
||||
public:
|
||||
XSTR(const XMLCh* const xmlString)
|
||||
{
|
||||
m_AsChar = xercesc::XMLString::transcode(xmlString);
|
||||
}
|
||||
|
||||
XSTR(const char* normalString)
|
||||
{
|
||||
m_AsXMLCh = xercesc::XMLString::transcode(normalString);
|
||||
}
|
||||
|
||||
~XSTR()
|
||||
{
|
||||
if (m_AsChar != nullptr) {
|
||||
xercesc::XMLString::release(&m_AsChar);
|
||||
}
|
||||
if (m_AsXMLCh != nullptr) {
|
||||
xercesc::XMLString::release(&m_AsXMLCh);
|
||||
}
|
||||
}
|
||||
|
||||
operator const char*() const { return m_AsChar; }
|
||||
operator const XMLCh*() const { return m_AsXMLCh; }
|
||||
|
||||
private:
|
||||
char* m_AsChar = nullptr;
|
||||
XMLCh* m_AsXMLCh = nullptr;
|
||||
};
|
||||
|
||||
class EntityXMLFile : public Resource
|
||||
{
|
||||
friend class ResourceManager;
|
||||
|
||||
private:
|
||||
EntityXMLFile(std::string path);
|
||||
|
||||
public:
|
||||
~EntityXMLFile();
|
||||
|
||||
void PopulateWorld(World* world);
|
||||
|
||||
private:
|
||||
static unsigned int InstanceCount;
|
||||
|
||||
std::string m_EntityFile;
|
||||
xercesc::XMLGrammarPool* m_GrammarPool = nullptr;
|
||||
EntityParserXMLErrorHandler* m_ErrorHandler = nullptr;
|
||||
xercesc::XercesDOMParser* m_DOMParser = nullptr;
|
||||
xercesc::DOMDocument* m_DOMDocument = nullptr;
|
||||
std::map<std::string, ComponentInfo> m_ComponentInfo;
|
||||
|
||||
// Preprocesses the entity file to insert include-by-copy child entities
|
||||
// TODO: Make this work in memory instead of saving to file
|
||||
void preprocess(std::string inPath, std::string outPath);
|
||||
|
||||
void parseComponentInfo();
|
||||
void parseDefaults();
|
||||
void predictComponentAllocation();
|
||||
void parseEntityGraph(World* world, xercesc::DOMElement* parent, EntityID parentEntity);
|
||||
std::size_t getTypeStride(std::string typeName);
|
||||
float getFloatAttribute(const xercesc::DOMElement* element, const char* attribute) const;
|
||||
void writeData(const xercesc::DOMElement* element, std::string typeName, char* outData);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -81,6 +81,8 @@ public:
|
||||
@param resourceName Fully qualified name of the resource to reload.
|
||||
*/
|
||||
static void Reload(std::string resourceName);
|
||||
|
||||
static void Release(std::string resourceType, std::string resourceName);
|
||||
|
||||
static void Update();
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef System_h__
|
||||
#define System_h__
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "World.h"
|
||||
#include "ComponentWrapper.h"
|
||||
|
||||
class System
|
||||
{
|
||||
friend class SystemPipeline;
|
||||
|
||||
public:
|
||||
System(const EventBroker* eventBroker, std::string componentType)
|
||||
: m_EventBroker(eventBroker)
|
||||
, m_ComponentType(componentType)
|
||||
{ }
|
||||
|
||||
virtual void Update(World* world, ComponentWrapper& component, double dt) = 0;
|
||||
|
||||
private:
|
||||
const EventBroker* m_EventBroker;
|
||||
std::string m_ComponentType;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
#ifndef SystemPipeline_h__
|
||||
#define SystemPipeline_h__
|
||||
|
||||
#include "../Common.h"
|
||||
#include "EventBroker.h"
|
||||
#include "System.h"
|
||||
#include "World.h"
|
||||
|
||||
class SystemPipeline
|
||||
{
|
||||
public:
|
||||
SystemPipeline(const EventBroker* eventBroker)
|
||||
: m_EventBroker(eventBroker)
|
||||
{ }
|
||||
~SystemPipeline()
|
||||
{
|
||||
for (auto& pair : m_Systems) {
|
||||
for (auto& system : pair.second) {
|
||||
delete system;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename... Arguments>
|
||||
void AddSystem(Arguments... args)
|
||||
{
|
||||
System* system = new T(m_EventBroker, args...);
|
||||
if (!system->m_ComponentType.empty()) {
|
||||
m_Systems[system->m_ComponentType].push_back(system);
|
||||
} else {
|
||||
LOG_ERROR("Failed to add system \"%s\": Missing component type!", typeid(T).name());
|
||||
delete system;
|
||||
}
|
||||
}
|
||||
|
||||
void Update(World* world, double dt)
|
||||
{
|
||||
for (auto& pair : m_Systems) {
|
||||
const std::string& componentName = pair.first;
|
||||
auto& systems = pair.second;
|
||||
const ComponentPool* pool = world->GetComponents(componentName);
|
||||
if (pool == nullptr) {
|
||||
continue;
|
||||
}
|
||||
for (auto& component : *pool) {
|
||||
for (auto& system : systems) {
|
||||
system->Update(world, component, dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const EventBroker* m_EventBroker;
|
||||
std::unordered_map<std::string, std::vector<System*>> m_Systems;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -2,7 +2,7 @@
|
||||
#define World_h__
|
||||
|
||||
#include "../Common.h"
|
||||
#include "EntityWrapper.h"
|
||||
#include "Entity.h"
|
||||
#include "ObjectPool.h"
|
||||
#include "ComponentPool.h"
|
||||
|
||||
@@ -12,6 +12,7 @@ public:
|
||||
World() = default;
|
||||
~World();
|
||||
|
||||
// Create empty entity
|
||||
EntityID CreateEntity(EntityID parent = 0);
|
||||
|
||||
// Register a component type and allocate space for it
|
||||
@@ -21,10 +22,12 @@ public:
|
||||
// Get a component of an entity
|
||||
ComponentWrapper GetComponent(EntityID entity, std::string componentType);
|
||||
// Get all components of the specified type
|
||||
const ComponentPool& GetComponents(std::string componentType);
|
||||
const ComponentPool* GetComponents(std::string componentType);
|
||||
// Get entity parent
|
||||
EntityID GetParent(EntityID entity);
|
||||
|
||||
private:
|
||||
EntityID m_CurrentEntityID = 0;
|
||||
EntityID m_CurrentEntityID = 1;
|
||||
|
||||
std::unordered_map<EntityID, EntityID> m_EntityParents;
|
||||
std::unordered_multimap<EntityID, EntityID> m_EntityChildren;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "../Common.h"
|
||||
#include "../GLM.h"
|
||||
#include "../Core/Util/Rectangle.h"
|
||||
#include "../Core/EntityWrapper.h"
|
||||
#include "../Core/Entity.h"
|
||||
|
||||
class Model;
|
||||
class Skeleton;
|
||||
|
||||
@@ -22,7 +22,10 @@ private:
|
||||
void FillLights(World* world, RenderQueue* renderQueue);
|
||||
|
||||
glm::mat4 ModelMatrix(World* world, EntityID entity);
|
||||
glm::vec3 GetAbsolutePosition(World* world, ComponentWrapper transformComponent);
|
||||
|
||||
glm::vec3 AbsolutePosition(World* world, EntityID entity);
|
||||
glm::quat AbsoluteOrientation(World* world, EntityID entity);
|
||||
glm::vec3 AbsoluteScale(World* world, EntityID entity);
|
||||
};
|
||||
|
||||
#endif
|
||||
+11
-3
@@ -9,8 +9,10 @@
|
||||
#include "GUI/Frame.h"
|
||||
#include "Core/World.h"
|
||||
#include "Rendering/RenderQueueFactory.h"
|
||||
|
||||
class OctTree;
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
#include "RaptorCopterSystem.h"
|
||||
|
||||
class Game
|
||||
{
|
||||
@@ -29,8 +31,14 @@ private:
|
||||
InputManager* m_InputManager;
|
||||
GUI::Frame* m_FrameStack;
|
||||
World* m_World;
|
||||
SystemPipeline* m_SystemPipeline;
|
||||
RenderQueueFactory* m_RenderQueueFactory;
|
||||
OctTree* m_OctTree;
|
||||
|
||||
EventRelay<Game, Events::KeyUp> m_EKeyUp;
|
||||
bool testOnKeyUp(const Events::KeyUp& e);
|
||||
|
||||
void testIntialize();
|
||||
void testTick(double dt);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,149 +0,0 @@
|
||||
#include <list>
|
||||
#include <tuple>
|
||||
#include <boost/any.hpp>
|
||||
#include "GLM.h"
|
||||
#include "Core/World.h"
|
||||
#include "Core/Util/Any.h"
|
||||
|
||||
class HardcodedTestWorld : public World
|
||||
{
|
||||
public:
|
||||
HardcodedTestWorld()
|
||||
: World()
|
||||
{
|
||||
registerTestComponents();
|
||||
createTestEntities();
|
||||
}
|
||||
|
||||
private:
|
||||
void registerTestComponents()
|
||||
{
|
||||
ComponentWrapperFactory f;
|
||||
|
||||
|
||||
f = ComponentWrapperFactory("Test");
|
||||
f.AddProperty("TestInteger", 1337);
|
||||
f.AddProperty("TestFloat", 13.37f);
|
||||
f.AddProperty("TestString", std::string("Carlito"));
|
||||
RegisterComponent(f);
|
||||
|
||||
f = ComponentWrapperFactory("Debug");
|
||||
f.AddProperty("Name", std::string("Unnamed"));
|
||||
RegisterComponent(f);
|
||||
|
||||
f = ComponentWrapperFactory("Transform");
|
||||
f.AddProperty("Position", glm::vec3(0.f, 0.f, 0.f));
|
||||
f.AddProperty("Orientation", glm::quat());
|
||||
f.AddProperty("Scale", glm::vec3(1.f, 1.f, 1.f));
|
||||
RegisterComponent(f);
|
||||
|
||||
f = ComponentWrapperFactory("Model");
|
||||
f.AddProperty("Resource", std::string());
|
||||
f.AddProperty("Color", glm::vec4(1.f, 1.f, 1.f, 1.f));
|
||||
f.AddProperty("Visible", true);
|
||||
RegisterComponent(f);
|
||||
|
||||
f = ComponentWrapperFactory("Collision");
|
||||
f.AddProperty("BoxCenter", glm::vec3(0.f, 0.f, 0.f));
|
||||
f.AddProperty("BoxSize", glm::vec3(1.f, 1.f, 1.f));
|
||||
RegisterComponent(f);
|
||||
}
|
||||
|
||||
void createTestEntities()
|
||||
{
|
||||
World& world = *this;
|
||||
|
||||
// Create an entity
|
||||
EntityID e = world.CreateEntity();
|
||||
|
||||
// Attach a Debug component
|
||||
ComponentWrapper debug = world.AttachComponent(e, "Debug");
|
||||
// Set the Name field of the Debug component using subscript operator
|
||||
debug["Name"] = "Carlito";
|
||||
|
||||
// Attach a Transform component
|
||||
world.AttachComponent(e, "Transform");
|
||||
// Fetch the component based on EntityID and component type
|
||||
ComponentWrapper transform = world.GetComponent(e, "Transform");
|
||||
// Set the fields of the Transform component
|
||||
transform["Position"] = glm::vec3(0.f, 0.f, 0.f);
|
||||
transform["Scale"] = glm::vec3(1.f, 1.f, 1.f);
|
||||
|
||||
// Move on the X axis by fetching field as reference
|
||||
((glm::vec3&)transform["Position"]).x += 10.f;
|
||||
// Shrink by a factor of 100
|
||||
((glm::vec3&)transform["Scale"]) /= 100.f;
|
||||
|
||||
// Loop through all Transform components and print them
|
||||
for (auto& transform : world.GetComponents("Transform")) {
|
||||
glm::vec3 pos = transform["Position"];
|
||||
std::cout << "Position: " << pos.x << " " << pos.y << " " << pos.z << std::endl;
|
||||
glm::vec3 scale = transform["Scale"];
|
||||
std::cout << "Scale: " << scale.x << " " << scale.y << " " << scale.z << std::endl;
|
||||
|
||||
// Fetch the Debug component also present in this entity
|
||||
ComponentWrapper debug = world.GetComponent(transform.EntityID, "Debug");
|
||||
std::cout << "Name: " << (std::string)debug["Name"] << std::endl;
|
||||
}
|
||||
|
||||
//Create some test widgets
|
||||
{
|
||||
EntityID entityScaleWidget = world.CreateEntity();
|
||||
ComponentWrapper transform = world.AttachComponent(entityScaleWidget, "Transform");
|
||||
transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f);
|
||||
ComponentWrapper model = world.AttachComponent(entityScaleWidget, "Model");
|
||||
model["Resource"] = "Models/ScaleWidget.obj";
|
||||
}
|
||||
{
|
||||
EntityID entityRotationWidget = world.CreateEntity();
|
||||
ComponentWrapper transform = world.AttachComponent(entityRotationWidget, "Transform");
|
||||
transform["Position"] = glm::vec3(1.5f, 0.f, 0.f);
|
||||
ComponentWrapper model = world.AttachComponent(entityRotationWidget, "Model");
|
||||
model["Resource"] = "Models/RotationWidget.obj";
|
||||
}
|
||||
{
|
||||
EntityID entityDummyScene = world.CreateEntity();
|
||||
ComponentWrapper transform = world.AttachComponent(entityDummyScene, "Transform");
|
||||
transform["Position"] = glm::vec3(0, 0.f, 0.f);
|
||||
ComponentWrapper model = world.AttachComponent(entityDummyScene, "Model");
|
||||
model["Resource"] = "Models/DummyScene.obj";
|
||||
}
|
||||
for (int i = 0; i < 16; ++i)
|
||||
{
|
||||
EntityID entityCollisionBox = world.CreateEntity();
|
||||
ComponentWrapper transform = world.AttachComponent(entityCollisionBox, "Transform");
|
||||
transform["Position"] = glm::vec3(rand()%11-5, rand() % 11 - 5, rand() % 11 - 5);
|
||||
ComponentWrapper model = world.AttachComponent(entityCollisionBox, "Model");
|
||||
model["Resource"] = "Models/Core/UnitBox.obj";
|
||||
float sc = 0.20f;
|
||||
transform["Scale"] = glm::vec3(sc, sc, sc);
|
||||
|
||||
ComponentWrapper collision = world.AttachComponent(entityCollisionBox, "Collision");
|
||||
glm::vec3 pos = transform["Position"];
|
||||
glm::vec3 scale = transform["Scale"];
|
||||
glm::quat ori = transform["Orientation"];
|
||||
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]);
|
||||
|
||||
//WTODO: This only works for objects that never moves/scales/rotates since modelMatrix don't change.
|
||||
//For dynamic objects, should save a collision box in modelspace and transform box with modelMatrix per collision check.
|
||||
glm::mat4 modelMatrix = modelRes->m_Matrix * glm::translate(glm::mat4(), pos) * glm::toMat4(ori) * glm::scale(scale);
|
||||
|
||||
glm::vec3 mini = glm::vec3(INFINITY, INFINITY, INFINITY);
|
||||
glm::vec3 maxi = glm::vec3(-INFINITY, -INFINITY, -INFINITY);
|
||||
for (const auto& v : modelRes->m_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);
|
||||
maxi.z = std::max(wPos.z, maxi.z);
|
||||
mini.x = std::min(wPos.x, mini.x);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "Common.h"
|
||||
#include "Core/System.h"
|
||||
|
||||
class RaptorCopterSystem : public System
|
||||
{
|
||||
public:
|
||||
RaptorCopterSystem(const EventBroker* eventBroker)
|
||||
: System(eventBroker, "RaptorCopter")
|
||||
{ }
|
||||
|
||||
virtual void Update(World* world, ComponentWrapper& raptorCopter, double dt) override
|
||||
{
|
||||
ComponentWrapper& transform = world->GetComponent(raptorCopter.EntityID, "Transform");
|
||||
(glm::vec3&)transform["Orientation"] += (float)(double)raptorCopter["Speed"] * (float)dt * (glm::vec3)raptorCopter["Axis"];
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user