Added ComponentInfo::Field_t::Index

This commit is contained in:
2016-03-08 15:46:14 +01:00
parent 3436ba6656
commit ecc8d998cf
4 changed files with 21 additions and 6 deletions
+12
View File
@@ -2,6 +2,7 @@
#define ComponentInfo_h__ #define ComponentInfo_h__
#include "../Common.h" #include "../Common.h"
#include "Entity.h"
#include <boost/shared_array.hpp> #include <boost/shared_array.hpp>
struct ComponentInfo struct ComponentInfo
@@ -21,6 +22,7 @@ struct ComponentInfo
{ {
std::string Name; std::string Name;
std::string Type; std::string Type;
unsigned char Index;
unsigned int Offset; unsigned int Offset;
unsigned int Stride; unsigned int Stride;
}; };
@@ -32,6 +34,16 @@ struct ComponentInfo
unsigned int Stride = 0; unsigned int Stride = 0;
boost::shared_array<char> Defaults = nullptr; boost::shared_array<char> Defaults = nullptr;
std::shared_ptr<Meta_t> Meta = nullptr; std::shared_ptr<Meta_t> Meta = nullptr;
std::size_t GetHeaderSize() const
{
std::size_t size = 0;
// A component block starts with an entity ID
size += sizeof(EntityID);
return size;
}
}; };
template<> template<>
+1 -1
View File
@@ -44,7 +44,7 @@ public:
ComponentPool(const ::ComponentInfo& ci) ComponentPool(const ::ComponentInfo& ci)
: m_ComponentInfo(ci) : m_ComponentInfo(ci)
, m_Pool(ci.Meta->Allocation, sizeof(EntityID) + ci.Stride) , m_Pool(ci.Meta->Allocation, ci.GetHeaderSize() + ci.Stride)
{ } { }
~ComponentPool(); ~ComponentPool();
ComponentPool(const ComponentPool& other); ComponentPool(const ComponentPool& other);
+5 -5
View File
@@ -30,7 +30,7 @@ struct ComponentWrapper
ComponentWrapper(const ComponentInfo& componentInfo, char* data) ComponentWrapper(const ComponentInfo& componentInfo, char* data)
: Info(componentInfo) : Info(componentInfo)
, EntityID(*reinterpret_cast<::EntityID*>(data)) , EntityID(*reinterpret_cast<::EntityID*>(data))
, Data(data + sizeof(::EntityID)) , Data(data + componentInfo.GetHeaderSize())
{ } { }
const ComponentInfo& Info; const ComponentInfo& Info;
@@ -43,7 +43,7 @@ struct ComponentWrapper
} }
template <typename T> template <typename T>
T& Field(std::string name) T& Field(const std::string& name)
{ {
const ComponentInfo::Field_t& field = Info.Fields.at(name); const ComponentInfo::Field_t& field = Info.Fields.at(name);
if (sizeof(T) > field.Stride) { if (sizeof(T) > field.Stride) {
@@ -55,13 +55,13 @@ struct ComponentWrapper
} }
template <typename T> template <typename T>
void SetField(std::string name, const T value) { Field<T>(name) = value; } void SetField(const std::string& name, const T value) { Field<T>(name) = value; }
//template <typename T> //template <typename T>
//void SetField(std::string name, T& value) { Field<T>(name) = value; } //void SetField(std::string name, T& value) { Field<T>(name) = value; }
// Specialization for string literals // Specialization for string literals
template <std::size_t N> template <std::size_t N>
void SetField(std::string name, const char(&value)[N]) { Field<std::string>(name) = std::string(value); } void SetField(const std::string& name, const char(&value)[N]) { Field<std::string>(name) = std::string(value); }
void Copy(ComponentWrapper& destination) void Copy(ComponentWrapper& destination)
{ {
@@ -121,7 +121,7 @@ struct ComponentWrapper
template <std::size_t N> template <std::size_t N>
void operator=(const char(&val)[N]) { m_Component->SetField<N>(m_PropertyName, val); } void operator=(const char(&val)[N]) { m_Component->SetField<N>(m_PropertyName, val); }
}; };
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); } SubscriptProxy operator[](const std::string& propertyName) { return SubscriptProxy(this, propertyName); }
}; };
// A component wrapper that "owns" its data through a shared pointer // A component wrapper that "owns" its data through a shared pointer
@@ -125,6 +125,7 @@ void EntityXMLFilePreprocessor::parseComponentInfo()
auto modelGroup = modelGroupParticle->getModelGroupTerm(); auto modelGroup = modelGroupParticle->getModelGroupTerm();
// <xs:element... // <xs:element...
unsigned char fieldIndex = 0;
unsigned int fieldOffset = 0; unsigned int fieldOffset = 0;
auto particles = modelGroup->getParticles(); auto particles = modelGroup->getParticles();
for (unsigned int i = 0; i < particles->size(); ++i) { for (unsigned int i = 0; i < particles->size(); ++i) {
@@ -182,12 +183,14 @@ void EntityXMLFilePreprocessor::parseComponentInfo()
auto& field = compInfo.Fields[name]; auto& field = compInfo.Fields[name];
field.Name = name; field.Name = name;
field.Type = effectiveType; field.Type = effectiveType;
field.Index = fieldIndex;
field.Offset = fieldOffset; field.Offset = fieldOffset;
field.Stride = stride; field.Stride = stride;
compInfo.FieldsInOrder.push_back(name); compInfo.FieldsInOrder.push_back(name);
if (field.Type == "string") { if (field.Type == "string") {
compInfo.StringFields.push_back(name); compInfo.StringFields.push_back(name);
} }
fieldIndex += 1;
fieldOffset += stride; fieldOffset += stride;
} }