Skip to content

Commit

Permalink
RTDCmptTraits -> EntityMngr
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 14, 2020
1 parent 794c296 commit ba65258
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 35 deletions.
2 changes: 2 additions & 0 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace Ubpa::UECS {
// - API with Entity require Entity exist (throw std::invalid_argument)
class EntityMngr {
public:
RTDCmptTraits cmptTraits;

template<typename... Cmpts>
std::tuple<Entity, Cmpts*...> Create();

Expand Down
3 changes: 1 addition & 2 deletions include/UECS/RTDCmptTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace Ubpa::UECS {
public:
static constexpr size_t default_alignment = alignof(std::max_align_t);

static RTDCmptTraits& Instance() noexcept;

RTDCmptTraits& Clear();

// neccessary
Expand Down Expand Up @@ -69,6 +67,7 @@ namespace Ubpa::UECS {
friend class RTSCmptTraits;
friend class Archetype;
friend class EntityMngr;
friend class World;

RTDCmptTraits() = default;

Expand Down
5 changes: 0 additions & 5 deletions include/UECS/detail/RTDCmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
#include "../RTDCmptTraits.h"

namespace Ubpa::UECS {
inline RTDCmptTraits& RTDCmptTraits::Instance() noexcept {
static RTDCmptTraits instance;
return instance;
}

inline RTDCmptTraits& RTDCmptTraits::RegisterSize(CmptType type, size_t size) {
sizeofs.emplace(type, size);
return *this;
Expand Down
3 changes: 2 additions & 1 deletion include/UECS/detail/RTSCmptTraits.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../CmptType.h"
#include "../RTDCmptTraits.h"

#include <unordered_map>
#include <functional>
Expand All @@ -20,7 +21,7 @@ namespace Ubpa::UECS {
void Register();

// use RTDCmptTraits
void Register(CmptType type);
void Register(const RTDCmptTraits& rtdCmptTraits, CmptType type);

template<typename Cmpt>
void Deregister();
Expand Down
5 changes: 1 addition & 4 deletions include/UECS/detail/RTSCmptTraits.inl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include "../RTDCmptTraits.h"

#include <stdexcept>

namespace Ubpa::UECS {
Expand Down Expand Up @@ -99,8 +97,7 @@ namespace Ubpa::UECS {
move_assignments.erase(type);
}

inline void RTSCmptTraits::Register(CmptType type) {
const auto& rtdct = RTDCmptTraits().Instance();
inline void RTSCmptTraits::Register(const RTDCmptTraits& rtdct, CmptType type) {
auto size_target = rtdct.sizeofs.find(type);
if (size_target == rtdct.sizeofs.end())
throw std::logic_error("RTSCmptTraits::Register: RTDCmptTraits hasn't registered <CmptType>");
Expand Down
6 changes: 3 additions & 3 deletions src/core/Archetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Archetype* Archetype::New(EntityMngr* entityMngr, const CmptType* types, size_t
rst->types.data.insert(CmptType::Of<Entity>);
rst->cmptTraits.Register<Entity>();
for (size_t i = 0; i < num; i++)
rst->cmptTraits.Register(types[i]);
rst->cmptTraits.Register(entityMngr->cmptTraits, types[i]);
rst->SetLayout();
return rst;
}
Expand All @@ -73,7 +73,7 @@ Archetype* Archetype::Add(const Archetype* from, const CmptType* types, size_t n
rst->cmptTraits = from->cmptTraits;
rst->types.Insert(types, num);
for (size_t i = 0; i < num; i++)
rst->cmptTraits.Register(types[i]);
rst->cmptTraits.Register(rst->entityMngr->cmptTraits, types[i]);

rst->SetLayout();

Expand Down Expand Up @@ -102,7 +102,7 @@ size_t Archetype::Create(Entity e) {
size_t idxInChunk = idx % chunkCapacity;
byte* buffer = chunks[idx / chunkCapacity]->Data();

const auto& rtdct = RTDCmptTraits::Instance();
const auto& rtdct = entityMngr->cmptTraits;
for (const auto& type : types.data) {
if (type.Is<Entity>()) {
constexpr size_t size = sizeof(Entity);
Expand Down
5 changes: 2 additions & 3 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,13 @@ Archetype* EntityMngr::AttachWithoutInit(Entity e, const CmptType* types, size_t
void EntityMngr::Attach(Entity e, const CmptType* types, size_t num) {
auto origArchetype = AttachWithoutInit(e, types, num);
const auto& info = entityTable[e.Idx()];
const auto& rtdct = RTDCmptTraits::Instance();
for (size_t i = 0; i < num; i++) {
const auto& type = types[i];
if (origArchetype->GetCmptTypeSet().Contains(type))
continue;

auto target = rtdct.default_constructors.find(type);
if (target == rtdct.default_constructors.end())
auto target = cmptTraits.default_constructors.find(type);
if (target == cmptTraits.default_constructors.end())
continue;

target->second(info.archetype->At(type, info.idxInArchetype));
Expand Down
4 changes: 2 additions & 2 deletions src/core/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const {
unordered_map<CmptType, size_t> cmptType2idx;
unordered_map<size_t, size_t> sysFuncHashcode2idx;

auto queryCmptName = [](CmptType type) -> string {
auto cmptName = RTDCmptTraits::Instance().Nameof(type);
auto queryCmptName = [this](CmptType type) -> string {
auto cmptName = entityMngr.cmptTraits.Nameof(type);
return cmptName.empty() ? std::to_string(type.HashCode()) : string{ cmptName };
};

Expand Down
8 changes: 4 additions & 4 deletions src/test/02_order/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class DataSystem : public System {
};

int main() {
RTDCmptTraits::Instance().Register<
World w;
w.systemMngr.Register<DataSystem>();

w.entityMngr.cmptTraits.Register<
Data1,
Data2
>();

World w;
w.systemMngr.Register<DataSystem>();

w.entityMngr.Create<Data1, Data2>();

w.Update();
Expand Down
7 changes: 4 additions & 3 deletions src/test/11_runtime_cmpt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ int main() {
// LuaCmpt {
// number value;
// }
RTDCmptTraits::Instance().RegisterSize(type, 8);
RTDCmptTraits::Instance().RegisterDefaultConstructor(type, [](void*) { cout << "construct" << endl;});
RTDCmptTraits::Instance().RegisterDestructor(type, [](void*) { cout << "destruct" << endl; });

World w;
w.systemMngr.Register<RTDSystem>();
w.entityMngr.cmptTraits
.RegisterSize(type, 8)
.RegisterDefaultConstructor(type, [](void*) { cout << "construct" << endl; })
.RegisterDestructor(type, [](void*) { cout << "destruct" << endl; });

auto [e] = w.entityMngr.Create();
w.entityMngr.Attach(e, &type, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/test/12_framegraph/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main() {
World w;
w.systemMngr.Register<MySystem>();

RTDCmptTraits::Instance()
w.entityMngr.cmptTraits
.RegisterName(CmptType::Of<A>, "A")
.RegisterName(CmptType::Of<B>, "B")
.RegisterName(CmptType::Of<C>, "C")
Expand Down
6 changes: 4 additions & 2 deletions src/test/14_serialize/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Dumper : public IListener {
bool firstSystem{ true };
bool firstEntity{ true };
bool firstCmpt{ true };
const World* w;

void PrintIndent() {
for (size_t i = 0; i < indent; i++)
Expand All @@ -26,6 +27,7 @@ class Dumper : public IListener {
indent++;
PrintIndent();
cout << "\"type\" : \"World\"," << endl;
this->w = w;
}

virtual void ExistWorld(const World* w) override {
Expand Down Expand Up @@ -128,7 +130,7 @@ class Dumper : public IListener {
cout << "{" << endl;
indent++;
PrintIndent();
cout << "\"type\" : \"" << RTDCmptTraits::Instance().Nameof(cmpt->Type()) << "\"";
cout << "\"type\" : \"" << w->entityMngr.cmptTraits.Nameof(cmpt->Type()) << "\"";
if (cmpt->Type().Is<Velocity>()) {
auto v = cmpt->As<Velocity>();
cout << "," << endl;
Expand Down Expand Up @@ -165,7 +167,7 @@ class MoverSystem : public System {

int main() {
World w;
RTDCmptTraits::Instance().Register<Position, Velocity>();
w.entityMngr.cmptTraits.Register<Position, Velocity>();
w.systemMngr.Register<MoverSystem>();
w.entityMngr.Create<Position, Velocity>();
w.entityMngr.Create<Position>();
Expand Down
4 changes: 2 additions & 2 deletions src/test/15_chunk_job/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class SAB_System : public System {
};

int main() {
RTDCmptTraits::Instance().Register<S, A, B>();

World w;
w.systemMngr.Register<SAB_System>();

w.entityMngr.cmptTraits.Register<S, A, B>();

w.entityMngr.Create<S>();
w.entityMngr.Create<S, A>();
w.entityMngr.Create<S, B>();
Expand Down
5 changes: 2 additions & 3 deletions src/test/16_singleton/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ class MoverSystem : public System {
};

int main() {
RTDCmptTraits::Instance().Register
<Timer, Velocity, Position>();

World w;
w.systemMngr.Register<MoverSystem>();
w.entityMngr.Create<Position, Velocity>();
w.entityMngr.Create<Timer>();
w.entityMngr.cmptTraits.Register
<Timer, Velocity, Position>();

w.Update();
std::cout << w.DumpUpdateJobGraph() << std::endl;
Expand Down

0 comments on commit ba65258

Please sign in to comment.