ongoing refactor of serialization and various cleanup
This commit is contained in:
+13
-16
@@ -2,24 +2,21 @@
|
||||
|
||||
#include <Core/Common/serialization.h>
|
||||
|
||||
DEFINE_ENTITY(Inert);
|
||||
DEFINE_CLASS_VERSIONED(Entity, 1, nullptr, nullptr)
|
||||
|
||||
void serialize(archive& ar, NonNullPtr<Entity> entity)
|
||||
DEFINE_ENTITY_VERSIONED(Inert, 1, nullptr)
|
||||
|
||||
void serialize(Archive& ar, NonNullPtr<Entity> entity)
|
||||
{
|
||||
serialize_elem(ar, entity->ID);
|
||||
// Entity fields
|
||||
serialize(ar, Entity::kind, entity.Get());
|
||||
|
||||
if (ar.loading)
|
||||
{
|
||||
serialize_elem(ar, entity->Kind->kind);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8 kind;
|
||||
serialize_elem(ar, kind);
|
||||
entity->Kind = kEntity_type_class_ptr[kind];
|
||||
}
|
||||
SERIALIZE(ar, id, entity->ID);
|
||||
SERIALIZE(ar, position, entity->position);
|
||||
|
||||
serialize_elem(ar, entity->X);
|
||||
serialize_elem(ar, entity->Y);
|
||||
serialize_elem(ar, entity->Z);
|
||||
// Derived fields
|
||||
if (entity->derived_kind != nullptr && entity->derived != nullptr)
|
||||
{
|
||||
serialize(ar, entity->derived_kind, entity->derived);
|
||||
}
|
||||
}
|
||||
|
||||
+24
-28
@@ -2,24 +2,19 @@
|
||||
|
||||
#include <Core/Common/CoreUtils.h>
|
||||
#include <Core/Common/EnumUtils.h>
|
||||
#include <Core/Math/Vector.h>
|
||||
#include <Core/Memory/Allocator.h>
|
||||
#include <Core/Memory/MemoryArena.h>
|
||||
#include <Engine/Class.h>
|
||||
|
||||
#define DECLARE_ENTITY() \
|
||||
Entity* Base; \
|
||||
static Class* Kind;
|
||||
Entity* base; \
|
||||
static Class* kind;
|
||||
|
||||
// Will register the class globally at launch
|
||||
#define DEFINE_ENTITY(entity) \
|
||||
Class entityKind##entity = \
|
||||
MakeClass(ConstString(#entity), (uint8)Entity_Type::entity, sizeof(entity), alignof(entity), nullptr); \
|
||||
Class* entity::Kind = &entityKind##entity;
|
||||
|
||||
#define DEFINE_ENTITY_SERIALIZED(entity, serialize_fct) \
|
||||
Class entityKind##entity = \
|
||||
MakeClass(ConstString(#entity), (uint8)Entity_Type::entity, sizeof(entity), alignof(entity), serialize_fct); \
|
||||
Class* entity::Kind = &entityKind##entity;
|
||||
#define DEFINE_ENTITY_VERSIONED(entity, version, serialize_fct) \
|
||||
constexpr Class entityKind##entity = MakeClass(ConstString(#entity), (uint8)Entity_Type::entity, (version), \
|
||||
&classKindEntity, sizeof(entity), alignof(entity), (serialize_fct)); \
|
||||
Class* entity::kind = const_cast<Class*>(&entityKind##entity);
|
||||
|
||||
struct EntityManager;
|
||||
using DerivedType = void*;
|
||||
@@ -27,12 +22,12 @@ using EntityID = uint64_t;
|
||||
|
||||
struct Entity final
|
||||
{
|
||||
EntityID ID = 0;
|
||||
Class* Kind = nullptr;
|
||||
DerivedType Derived = nullptr;
|
||||
float X = 0.0f;
|
||||
float Y = 0.0f;
|
||||
float Z = 0.0f;
|
||||
static Class* kind;
|
||||
|
||||
EntityID ID = 0;
|
||||
Class* derived_kind = nullptr;
|
||||
DerivedType derived = nullptr;
|
||||
Vector4 position = {};
|
||||
};
|
||||
|
||||
struct Inert
|
||||
@@ -60,7 +55,7 @@ inline const char* kEntity_type_names[] = {
|
||||
};
|
||||
#undef AS_STR
|
||||
|
||||
#define AS_CLASS(name) name::Kind
|
||||
#define AS_CLASS(name) name::kind
|
||||
inline Class* kEntity_type_class_ptr[]
|
||||
{
|
||||
ENTITY_TYPE_LIST(AS_CLASS)
|
||||
@@ -80,8 +75,8 @@ struct entity_template
|
||||
//
|
||||
template <typename EntityType>
|
||||
concept EntityConcept = requires(EntityType entity) {
|
||||
{ EntityType::Kind } -> std::convertible_to<const Class*>;
|
||||
requires std::same_as<decltype(entity.Base), Entity*>;
|
||||
{ EntityType::kind } -> std::convertible_to<const Class*>;
|
||||
requires std::same_as<decltype(entity.base), Entity*>;
|
||||
};
|
||||
|
||||
template <typename EntityType>
|
||||
@@ -89,7 +84,7 @@ template <typename EntityType>
|
||||
[[nodiscard]] bool IsA(const Entity* entity)
|
||||
{
|
||||
Assert(entity != nullptr);
|
||||
return entity->Kind == EntityType::Kind;
|
||||
return entity->derived_kind == EntityType::kind;
|
||||
}
|
||||
|
||||
template <typename EntityType>
|
||||
@@ -98,10 +93,11 @@ template <typename EntityType>
|
||||
{
|
||||
EntityType result;
|
||||
Entity base;
|
||||
base.X = x;
|
||||
base.Y = y;
|
||||
base.Z = z;
|
||||
base.Kind = EntityType::Kind;
|
||||
base.position.x = x;
|
||||
base.position.y = y;
|
||||
base.position.z = z;
|
||||
base.position.w = 1.0f;
|
||||
base.derived_kind = EntityType::kind;
|
||||
|
||||
return (EntityType*)RegisterEntity(manager, &base, &result);
|
||||
}
|
||||
@@ -112,7 +108,7 @@ template <typename EntityType>
|
||||
{
|
||||
Assert(entity != nullptr);
|
||||
Assert(IsA<EntityType>(entity));
|
||||
return static_cast<EntityType*>(entity->Derived);
|
||||
return static_cast<EntityType*>(entity->derived);
|
||||
}
|
||||
|
||||
void serialize(archive& ar, NonNullPtr<Entity> entity);
|
||||
void serialize(Archive& ar, NonNullPtr<Entity> entity);
|
||||
|
||||
@@ -46,30 +46,26 @@ EntityManager& GetEntityManager()
|
||||
entity_template* RegisterEntity(EntityManager& manager, Entity* base, DerivedType entity)
|
||||
{
|
||||
base->ID = EntityManager::ID++;
|
||||
base->Derived = entity;
|
||||
base->derived = entity;
|
||||
|
||||
manager.Entities.PushBack(*base);
|
||||
|
||||
auto* ptr = (entity_template*)ArenaPushSize(manager.by_type[base->Kind->kind].arena, base->Kind->size_of, base->Kind->alignment,
|
||||
false JULIET_DEBUG_PARAM(kEntity_type_names[base->Kind->kind]));
|
||||
MemCopy(ptr, entity, base->Kind->size_of);
|
||||
manager.by_type[base->Kind->kind].count += 1;
|
||||
auto* ptr = (entity_template*)ArenaPushSize(manager.by_type[base->derived_kind->kind].arena,
|
||||
base->derived_kind->size_of, base->derived_kind->alignment,
|
||||
false JULIET_DEBUG_PARAM(kEntity_type_names[base->derived_kind->kind]));
|
||||
MemCopy(ptr, entity, base->derived_kind->size_of);
|
||||
manager.by_type[base->derived_kind->kind].count += 1;
|
||||
|
||||
ptr->Base = manager.Entities.Back();
|
||||
ptr->base = manager.Entities.Back();
|
||||
|
||||
if (manager.by_type[base->Kind->kind].array == nullptr)
|
||||
if (manager.by_type[base->derived_kind->kind].array == nullptr)
|
||||
{
|
||||
manager.by_type[base->Kind->kind].array = ptr;
|
||||
manager.by_type[base->derived_kind->kind].array = ptr;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void RegisterBaseEntity(EntityManager& manager, Entity&& base)
|
||||
{
|
||||
GetEntityManager().Entities.PushBack(std::move(base));
|
||||
}
|
||||
|
||||
void UpdateEntityManager(EntityManager& manager)
|
||||
{
|
||||
// Todo : inert by definition dont move, but this is for test
|
||||
@@ -79,7 +75,9 @@ void UpdateEntityManager(EntityManager& manager)
|
||||
Inert* inert = reinterpret_cast<Inert*>(by_type.array) + i;
|
||||
if (inert->MeshInstance != indexMax)
|
||||
{
|
||||
SetMeshInstanceTransform(inert->MeshInstance, MatrixTranslation(inert->Base->X, inert->Base->Y, inert->Base->Z));
|
||||
SetMeshInstanceTransform(inert->MeshInstance,
|
||||
MatrixTranslation(inert->base->position.x, inert->base->position.y,
|
||||
inert->base->position.z));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <Core/Common/CoreTypes.h>
|
||||
#include <Core/Container/Vector.h>
|
||||
#include <Entity/Entity.h>
|
||||
|
||||
@@ -28,5 +27,4 @@ void InitEntityManager(NonNullPtr<World> world);
|
||||
void ShutdownEntityManager();
|
||||
EntityManager& GetEntityManager();
|
||||
entity_template* RegisterEntity(EntityManager& manager, Entity* base, DerivedType entity);
|
||||
void RegisterBaseEntity(EntityManager& manager, Entity&& base);
|
||||
void UpdateEntityManager(EntityManager& manager);
|
||||
|
||||
Reference in New Issue
Block a user