diff --git a/CMakeLists.txt b/CMakeLists.txt index 4162a9c..f984af0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -project(UECS VERSION 0.13.3) +project(UECS VERSION 0.14.0) message(STATUS "[Project] ${PROJECT_NAME}") include(cmake/InitUCMake.cmake) diff --git a/README.md b/README.md index 37ab750..562cc10 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ struct MoverSystem { int main() { World w; - auto move = w.systemMngr.Register(); + auto move = w.systemMngr.systemTraits.Register(); w.entityMngr.Create(); w.systemMngr.Activate(move); w.Update(); diff --git a/doc/todo.md b/doc/todo.md index 87d0869..c26e217 100644 --- a/doc/todo.md +++ b/doc/todo.md @@ -29,6 +29,8 @@ - [x] system base -> `System` - [x] singleton - [ ] doxygen +- [ ] lock ? +- [x] system traits ### maybe support in future @@ -44,7 +46,7 @@ ### maybe not support in future - [ ] shared component -- [ ] system group +- [ ] system group ? ## tool diff --git a/include/UECS/CmptType.h b/include/UECS/CmptType.h index cac2277..82556ef 100644 --- a/include/UECS/CmptType.h +++ b/include/UECS/CmptType.h @@ -26,7 +26,8 @@ namespace Ubpa::UECS { constexpr bool Valid() const noexcept { return hashcode == static_cast(-1); } template - constexpr bool Is() const noexcept; + constexpr bool Is() const noexcept { return operator==(Of); } + constexpr bool Is(std::string_view type_name) const noexcept { return operator==(CmptType{ type_name }); } constexpr bool operator< (const CmptType& rhs) const noexcept { return hashcode < rhs.hashcode; } constexpr bool operator<=(const CmptType& rhs) const noexcept { return hashcode <= rhs.hashcode; } diff --git a/include/UECS/EntityMngr.h b/include/UECS/EntityMngr.h index bfe1d44..a25bf77 100644 --- a/include/UECS/EntityMngr.h +++ b/include/UECS/EntityMngr.h @@ -26,6 +26,7 @@ namespace Ubpa::UECS { public: EntityMngr(); EntityMngr(const EntityMngr& em); + EntityMngr(EntityMngr&&) noexcept = default; ~EntityMngr(); RTDCmptTraits cmptTraits; diff --git a/include/UECS/SystemMngr.h b/include/UECS/SystemMngr.h index f8e194e..1f45987 100644 --- a/include/UECS/SystemMngr.h +++ b/include/UECS/SystemMngr.h @@ -1,55 +1,53 @@ #pragma once -#include -#include -#include -#include -#include +#include "SystemTraits.h" + +#include namespace Ubpa::UECS { class Schedule; class SystemMngr { public: - SystemMngr() = default; - SystemMngr(const SystemMngr&); - - using Func = std::function; - struct SystemInfo { - Func func; - std::string name; - }; - const std::vector& GetSystems() const noexcept { return systems; } - const std::set& GetActiveSystemIndices() const noexcept { return activeSystemIndices; } - const std::map& GetNameToIndexMap() const noexcept { return name2idx; } - - // if unregister, return static_cast(-1) - size_t GetIndex(std::string_view name) const; - // name: nameof::nameof_type - template - size_t GetIndex() const; - - void Clear() noexcept; - - size_t Register(std::string name, Func); - // name: nameof::nameof_type - // func: static void System::OnUpdate(Schedule&); - template - std::array Register(); - void Unregister(size_t idx); - void Unregister(std::string_view name); - // name: nameof::nameof_type - template - void Unregister(); - - void Activate(size_t index); - void Deactivate(size_t index) noexcept; + SystemTraits systemTraits; + + SystemMngr(World* w) : w{w}{} + SystemMngr(const SystemMngr& mngr, World* w) : systemTraits{ mngr.systemTraits }, w{ w } {} + SystemMngr(SystemMngr&& mngr, World* w) noexcept : systemTraits{ std::move(mngr.systemTraits) }, w{ w } {} + ~SystemMngr(); + + // not alive -> create + void Create(size_t systemID); + + // 1. not alive create -> create and activate + // 2. not active -> then activate + void Activate(size_t systemID); + + // active -> deactavate + void Deactivate(size_t systemID); + + // 1. active -> deactavite + // 2. alive -> destroy + void Destroy(size_t systemID); + + bool IsAlive(size_t systemID) const; + bool IsActive(size_t systemID) const; + + const auto& GetAliveSystemIDs() const noexcept { return aliveSystemIDs; } + const auto& GetActiveSystemsIDs() const noexcept { return activeSystemIDs; } + + SystemMngr(const SystemMngr&) = delete; + SystemMngr(SystemMngr&&) noexcept = delete; + SystemMngr& operator=(const SystemMngr&) = delete; + SystemMngr& operator=(SystemMngr&&) noexcept = delete; + private: - std::vector systems; - std::vector frees; - std::map name2idx; - std::set activeSystemIndices; + friend class World; + World* w; + void Update(size_t systemID, Schedule&) const; + void Clear(); + + std::unordered_set aliveSystemIDs; + std::unordered_set activeSystemIDs; }; } - -#include "detail/SystemMngr.inl" diff --git a/include/UECS/SystemTraits.h b/include/UECS/SystemTraits.h new file mode 100644 index 0000000..2ee4f90 --- /dev/null +++ b/include/UECS/SystemTraits.h @@ -0,0 +1,78 @@ +#pragma once + +#include +#include +#include + +namespace Ubpa::UECS { + class World; + class Schedule; + + class SystemTraits { + public: + // [life cycle] + // OnCreate + // | + // v + // OnActivate <--- + // | | + // v | + // --> OnUpdate | + // | | | + // --------x | + // | | + // v | + // OnDeactivate | + // | | + // x---------- + // | + // v + // OnDestroy + using OnCreate = std::function; + using OnActivate = std::function; + using OnUpdate = std::function; + using OnDeactivate = std::function; + using OnDestroy = std::function; + + SystemTraits() = default; + SystemTraits(const SystemTraits&); + SystemTraits(SystemTraits&&) noexcept = default; + + size_t Register(std::string name); + void RegisterOnCreate(size_t ID, OnCreate); + void RegisterOnActivate(size_t ID, OnActivate); + void RegisterOnUpdate(size_t ID, OnUpdate); + void RegisterOnDeactivate(size_t ID, OnDeactivate); + void RegisterOnDestroy(size_t ID, OnDestroy); + + bool IsRegistered(size_t ID) const noexcept; + size_t GetID(std::string_view name) const; + std::string_view Nameof(size_t ID) const noexcept; + const auto& GetNameIDMap() const noexcept { return name2id; } + + template + std::array Register(); + template + static std::string_view StaticNameof() noexcept; + + private: + friend class SystemMngr; + + void Create(size_t ID, World*) const; + void Activate(size_t ID, World*) const; + void Update(size_t ID, Schedule&) const; + void Deactivate(size_t ID, World*) const; + void Destroy(size_t ID, World*) const; + + std::vector names; + std::unordered_map name2id; + + std::unordered_map createMap; + std::unordered_map activateMap; + std::unordered_map updateMap; + std::unordered_map deactivateMap; + std::unordered_map destroyMap; + }; +} + +#include "detail/SystemTraits.inl" diff --git a/include/UECS/World.h b/include/UECS/World.h index 166c99f..18b503a 100644 --- a/include/UECS/World.h +++ b/include/UECS/World.h @@ -15,9 +15,11 @@ namespace Ubpa::UECS { // SystemMngr + EntityMngr class World { public: - World() = default; + World() : systemMngr{ this }{} // not copy schedule, so you can't use DumpUpdateJobGraph() and GenUpdateFrameGraph() before Update() World(const World&); + World(World&&) noexcept; + ~World(); SystemMngr systemMngr; EntityMngr entityMngr; diff --git a/include/UECS/detail/CmptType.inl b/include/UECS/detail/CmptType.inl index f1e6eda..4607c4c 100644 --- a/include/UECS/detail/CmptType.inl +++ b/include/UECS/detail/CmptType.inl @@ -1,28 +1,20 @@ #pragma once -namespace Ubpa::UECS { - template - constexpr bool CmptType::Is() const noexcept { - static_assert(!IsTaggedCmpt_v); - return hashcode == TypeID; - } -} - namespace std { template struct hash; +} - template<> - struct hash { - constexpr size_t operator()(const Ubpa::UECS::CmptType& t) const noexcept { - return t.HashCode(); - } - }; +template<> +struct std::hash { + constexpr size_t operator()(const Ubpa::UECS::CmptType& t) const noexcept { + return t.HashCode(); + } +}; - template<> - struct hash { - constexpr size_t operator()(const Ubpa::UECS::CmptAccessType& t) const noexcept { - return t.HashCode(); - } - }; -} +template<> +struct std::hash { + constexpr size_t operator()(const Ubpa::UECS::CmptAccessType& t) const noexcept { + return t.HashCode(); + } +}; \ No newline at end of file diff --git a/include/UECS/detail/SystemMngr.inl b/include/UECS/detail/SystemMngr.inl deleted file mode 100644 index 7f4731a..0000000 --- a/include/UECS/detail/SystemMngr.inl +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - -namespace Ubpa::UECS { - template - size_t SystemMngr::GetIndex() const { - return GetIndex(nameof::nameof_type()); - } - - template - std::array SystemMngr::Register() { - return { Register(std::string{ nameof::nameof_type() }, &Systems::OnUpdate) ... }; - } - - template - void SystemMngr::Unregister() { - Unregister(nameof::nameof_type()); - } -} diff --git a/include/UECS/detail/SystemTraits.inl b/include/UECS/detail/SystemTraits.inl new file mode 100644 index 0000000..50dd6ee --- /dev/null +++ b/include/UECS/detail/SystemTraits.inl @@ -0,0 +1,46 @@ +#pragma once + +#include + +#include + +namespace Ubpa::UECS::detail { + template + Concept(HaveOnCreate, static_cast(&System::OnCreate)); + template + Concept(HaveOnActivate, static_cast(&System::OnActivate)); + template + Concept(HaveOnUpdate, static_cast(&System::OnUpdate)); + template + Concept(HaveOnDeactivate, static_cast(&System::OnDeactivate)); + template + Concept(HaveOnDestroy, static_cast(&System::OnDestroy)); + + template + size_t Register(SystemTraits& traits) { + size_t ID = traits.Register(std::string{ SystemTraits::StaticNameof() }); + if constexpr (Require) + traits.RegisterOnCreate(ID, std::function{ static_cast(&System::OnCreate) }); + if constexpr (Require) + traits.RegisterOnActivate(ID, std::function{ static_cast(&System::OnActivate) }); + if constexpr (Require) + traits.RegisterOnUpdate(ID, static_cast(&System::OnUpdate)); + if constexpr (Require) + traits.RegisterOnDeactivate(ID, std::function{ static_cast(&System::OnDeactivate) }); + if constexpr (Require) + traits.RegisterOnDestroy(ID, std::function{ static_cast(&System::OnDestroy) }); + return ID; + } +} + +namespace Ubpa::UECS { + template + std::array SystemTraits::Register() { + return { detail::Register(*this)... }; + } + + template + std::string_view SystemTraits::StaticNameof() noexcept { + return nameof::nameof_type(); + } +} diff --git a/src/core/EntityMngr.cpp b/src/core/EntityMngr.cpp index c219517..72ad2b8 100644 --- a/src/core/EntityMngr.cpp +++ b/src/core/EntityMngr.cpp @@ -1,7 +1,6 @@ #include #include -#include #include using namespace Ubpa::UECS; diff --git a/src/core/SystemMngr.cpp b/src/core/SystemMngr.cpp index 23a53c2..faa473e 100644 --- a/src/core/SystemMngr.cpp +++ b/src/core/SystemMngr.cpp @@ -4,86 +4,59 @@ using namespace Ubpa::UECS; -SystemMngr::SystemMngr(const SystemMngr& mngr) - : - systems{mngr.systems}, - frees{mngr.frees}, - activeSystemIndices{mngr.activeSystemIndices} -{ - for (size_t i = 0; i < systems.size(); i++) { - const auto& system = systems[i]; - - if (system.name.empty()) - continue; - - name2idx.emplace(std::string_view{ system.name }, i); - } +SystemMngr::~SystemMngr() { + assert(activeSystemIDs.empty()); + assert(aliveSystemIDs.empty()); } -void SystemMngr::Clear() noexcept { - frees.clear(); - activeSystemIndices.clear(); - name2idx.clear(); - systems.clear(); +void SystemMngr::Clear() { + for (auto ID : activeSystemIDs) + systemTraits.Deactivate(ID, w); + for (auto ID : aliveSystemIDs) + systemTraits.Destroy(ID, w); + activeSystemIDs.clear(); + aliveSystemIDs.clear(); } -size_t SystemMngr::Register(std::string name, Func func) { - assert(!name.empty()); - - size_t idx; - if (!frees.empty()) { - idx = frees.back(); - frees.pop_back(); - } - else { - idx = systems.size(); - systems.emplace_back(); - } - - name2idx.emplace(std::string_view(name), idx); - systems[idx].func = std::move(func); - systems[idx].name = std::move(name); - - return idx; -} - -void SystemMngr::Unregister(size_t idx) { - assert(idx < systems.size()); - auto& system = systems[idx]; - if (system.name.empty()) +void SystemMngr::Create(size_t ID) { + if (IsAlive(ID)) return; - activeSystemIndices.erase(idx); - name2idx.erase(system.name); - system.func = nullptr; - system.name.clear(); - frees.push_back(idx); + systemTraits.Create(ID, w); + aliveSystemIDs.insert(ID); } -void SystemMngr::Unregister(std::string_view name) { - auto target = name2idx.find(name); - if (target == name2idx.end()) +void SystemMngr::Activate(size_t ID) { + Create(ID); + if (IsActive(ID)) return; + systemTraits.Activate(ID, w); + activeSystemIDs.insert(ID); +} - size_t idx = target->second; +void SystemMngr::Update(size_t ID, Schedule& schedule) const { + assert(IsActive(ID)); + systemTraits.Update(ID, schedule); +} - activeSystemIndices.erase(idx); - name2idx.erase(target); - systems[idx].func = nullptr; - systems[idx].name.clear(); - frees.push_back(idx); +void SystemMngr::Deactivate(size_t ID) { + if (!IsAlive(ID) || !IsActive(ID)) + return; + systemTraits.Deactivate(ID, w); + activeSystemIDs.erase(ID); } -void SystemMngr::Activate(size_t index) { - assert(index < systems.size()); - activeSystemIndices.insert(index); +void SystemMngr::Destroy(size_t ID) { + if (!IsAlive(ID)) + return; + Deactivate(ID); + systemTraits.Destroy(ID, w); + aliveSystemIDs.erase(ID); } -void SystemMngr::Deactivate(size_t index) noexcept { - assert(index < systems.size()); - activeSystemIndices.erase(index); +bool SystemMngr::IsAlive(size_t ID) const { + return aliveSystemIDs.find(ID) != aliveSystemIDs.end(); } -size_t SystemMngr::GetIndex(std::string_view name) const { - auto target = name2idx.find(name); - return target != name2idx.end() ? target->second : static_cast(-1); +bool SystemMngr::IsActive(size_t ID) const { + return activeSystemIDs.find(ID) != activeSystemIDs.end(); } diff --git a/src/core/SystemTraits.cpp b/src/core/SystemTraits.cpp new file mode 100644 index 0000000..8ce76a9 --- /dev/null +++ b/src/core/SystemTraits.cpp @@ -0,0 +1,107 @@ +#include +#include + +using namespace Ubpa::UECS; + +SystemTraits::SystemTraits(const SystemTraits& traits) + : + names{ traits.names }, + createMap{ traits.createMap }, + activateMap{ traits.activateMap }, + updateMap{ traits.updateMap }, + deactivateMap{ traits.deactivateMap }, + destroyMap{ traits.destroyMap } +{ + for (size_t i = 0; i < names.size(); i++) { + const auto& name = names[i]; + name2id.emplace(std::string_view{ name }, i); + } +} + +size_t SystemTraits::Register(std::string name) { + assert(names.empty()); + + auto target = name2id.find(name); + if (target != name2id.end()) + return target->second; + + size_t idx = names.size(); + names.push_back(std::move(name)); + name2id.emplace_hint(target, std::pair{ std::string_view{names[idx]}, idx }); + + return idx; +} + +bool SystemTraits::IsRegistered(size_t ID) const noexcept { + return ID < names.size(); +} + +size_t SystemTraits::GetID(std::string_view name) const { + auto target = name2id.find(name); + return target == name2id.end() ? static_cast(-1) : target->second; +} + +void SystemTraits::RegisterOnCreate(size_t ID, OnCreate func) { + assert(IsRegistered(ID)); + createMap.emplace(ID, std::move(func)); +} + +void SystemTraits::RegisterOnActivate(size_t ID, OnActivate func) { + assert(IsRegistered(ID)); + activateMap.emplace(ID, std::move(func)); +} + +void SystemTraits::RegisterOnUpdate(size_t ID, OnUpdate func) { + assert(IsRegistered(ID)); + updateMap.emplace(ID, std::move(func)); +} + +void SystemTraits::RegisterOnDeactivate(size_t ID, OnDeactivate func) { + assert(IsRegistered(ID)); + deactivateMap.emplace(ID, std::move(func)); +} + +void SystemTraits::RegisterOnDestroy(size_t ID, OnDestroy func) { + assert(IsRegistered(ID)); + destroyMap.emplace(ID, std::move(func)); +} + +std::string_view SystemTraits::Nameof(size_t ID) const noexcept { + assert(ID < names.size()); + return names[ID]; +} + +void SystemTraits::Create(size_t ID, World* w) const { + assert(IsRegistered(ID)); + auto target = createMap.find(ID); + if (target != createMap.end()) + target->second(w); +} + +void SystemTraits::Activate(size_t ID, World* w) const { + assert(IsRegistered(ID)); + auto target = activateMap.find(ID); + if (target != activateMap.end()) + target->second(w); +} + +void SystemTraits::Update(size_t ID, Schedule& schedule) const { + assert(IsRegistered(ID)); + auto target = updateMap.find(ID); + if (target != updateMap.end()) + target->second(schedule); +} + +void SystemTraits::Deactivate(size_t ID, World* w) const { + assert(IsRegistered(ID)); + auto target = deactivateMap.find(ID); + if (target != deactivateMap.end()) + target->second(w); +} + +void SystemTraits::Destroy(size_t ID, World* w) const { + assert(IsRegistered(ID)); + auto target = destroyMap.find(ID); + if (target != destroyMap.end()) + target->second(w); +} diff --git a/src/core/World.cpp b/src/core/World.cpp index 850498d..cec5931 100644 --- a/src/core/World.cpp +++ b/src/core/World.cpp @@ -9,46 +9,58 @@ using namespace Ubpa; using namespace std; World::World(const World& w) - : entityMngr{ w.entityMngr }, systemMngr{ w.systemMngr } + : + systemMngr{ w.systemMngr, this }, + entityMngr{ w.entityMngr } {} +World::World(World&& w) noexcept + : + systemMngr{ std::move(w.systemMngr), this }, + entityMngr{ std::move(w.entityMngr) } +{} + +World::~World() { + systemMngr.Clear(); +} + void World::Update() { inRunningJobGraph = true; schedule.Clear(); - for (auto job : jobs) + for (auto* job : jobs) jobPool.Recycle(job); jobs.clear(); jobGraph.clear(); - for (auto id : systemMngr.GetActiveSystemIndices()) - systemMngr.GetSystems().at(id).func(schedule); + for (auto id : systemMngr.GetActiveSystemsIDs()) + systemMngr.Update(id, schedule); for (auto& [layer, scheduleCommands] : schedule.commandBuffer) { auto& worldCommands = commandBuffer[layer]; for (auto& command : scheduleCommands) { - worldCommands.push_back([this, command = std::move(command)](){ + worldCommands.emplace_back([this, command = std::move(command)](){ command(this); }); } } schedule.commandBuffer.clear(); - auto graph = schedule.GenSysFuncGraph(); + const auto graph = schedule.GenSysFuncGraph(); unordered_map table; for (const auto& [func, adjVs] : graph.GetAdjList()) { - auto job = jobPool.Request(func->Name()); + auto* job = jobPool.Request(func->Name()); jobs.push_back(job); entityMngr.AutoGen(this, job, func); - table[func] = jobGraph.composed_of(*job); + table.emplace(func, jobGraph.composed_of(*job)); } for (const auto& [v, adjVs] : graph.GetAdjList()) { - auto vJob = table[v]; - for (auto adjV : adjVs) - vJob.precede(table[adjV]); + auto vJob = table.at(v); + for (auto* adjV : adjVs) + vJob.precede(table.at(adjV)); } executor.run(jobGraph).wait(); @@ -171,15 +183,15 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { size_t edgeIdx; switch (cmptType.GetAccessMode()) { - case Ubpa::UECS::AccessMode::LAST_FRAME: + case AccessMode::LAST_FRAME: edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); subgraph_lastframe.AddEdge(edgeIdx); break; - case Ubpa::UECS::AccessMode::WRITE: + case AccessMode::WRITE: edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]); subgraph_write.AddEdge(edgeIdx); break; - case Ubpa::UECS::AccessMode::LATEST: + case AccessMode::LATEST: edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); subgraph_latest.AddEdge(edgeIdx); break; @@ -197,15 +209,15 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { size_t edgeIdx; switch (cmptType.GetAccessMode()) { - case Ubpa::UECS::AccessMode::LAST_FRAME: + case AccessMode::LAST_FRAME: edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); subgraph_lastframe.AddEdge(edgeIdx); break; - case Ubpa::UECS::AccessMode::WRITE: + case AccessMode::WRITE: edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]); subgraph_write.AddEdge(edgeIdx); break; - case Ubpa::UECS::AccessMode::LATEST: + case AccessMode::LATEST: edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); subgraph_latest.AddEdge(edgeIdx); break; @@ -219,15 +231,15 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { size_t edgeIdx; switch (cmptType.GetAccessMode()) { - case Ubpa::UECS::AccessMode::LAST_FRAME: + case AccessMode::LAST_FRAME: edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); subgraph_lastframe.AddEdge(edgeIdx); break; - case Ubpa::UECS::AccessMode::WRITE: + case AccessMode::WRITE: edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]); subgraph_write.AddEdge(edgeIdx); break; - case Ubpa::UECS::AccessMode::LATEST: + case AccessMode::LATEST: edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); subgraph_latest.AddEdge(edgeIdx); break; @@ -265,15 +277,15 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { size_t edgeIdx; switch (singleton.GetAccessMode()) { - case Ubpa::UECS::AccessMode::LAST_FRAME_SINGLETON: + case AccessMode::LAST_FRAME_SINGLETON: edgeIdx = registry.RegisterEdge(cmptType2idx[singleton], sysIdx); subgraph_lastframe.AddEdge(edgeIdx); break; - case Ubpa::UECS::AccessMode::WRITE_SINGLETON: + case AccessMode::WRITE_SINGLETON: edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[singleton]); subgraph_write.AddEdge(edgeIdx); break; - case Ubpa::UECS::AccessMode::LATEST_SINGLETON: + case AccessMode::LATEST_SINGLETON: edgeIdx = registry.RegisterEdge(cmptType2idx[singleton], sysIdx); subgraph_latest.AddEdge(edgeIdx); break; diff --git a/src/test/00_basic/main.cpp b/src/test/00_basic/main.cpp index 0d4c6d9..0612c60 100644 --- a/src/test/00_basic/main.cpp +++ b/src/test/00_basic/main.cpp @@ -18,7 +18,7 @@ struct MoverSystem { int main() { World w; - auto [move] = w.systemMngr.Register(); + auto [move] = w.systemMngr.systemTraits.Register(); w.entityMngr.Create(); w.systemMngr.Activate(move); w.Update(); diff --git a/src/test/01_tag/main.cpp b/src/test/01_tag/main.cpp index c648c0e..e1a015c 100644 --- a/src/test/01_tag/main.cpp +++ b/src/test/01_tag/main.cpp @@ -21,7 +21,7 @@ struct DataSystem { int main() { World w; - auto [dataSystem] = w.systemMngr.Register(); + auto [dataSystem] = w.systemMngr.systemTraits.Register(); w.entityMngr.Create(); diff --git a/src/test/02_order/main.cpp b/src/test/02_order/main.cpp index a6a0f47..17325d7 100644 --- a/src/test/02_order/main.cpp +++ b/src/test/02_order/main.cpp @@ -23,7 +23,7 @@ struct DataSystem { int main() { World w; - auto [dataSystem] = w.systemMngr.Register(); + auto [dataSystem] = w.systemMngr.systemTraits.Register(); w.entityMngr.cmptTraits.Register< Data1, diff --git a/src/test/03_query_entity/main.cpp b/src/test/03_query_entity/main.cpp index 5450a5b..53a89a9 100644 --- a/src/test/03_query_entity/main.cpp +++ b/src/test/03_query_entity/main.cpp @@ -42,7 +42,7 @@ struct MySystem { int main() { World w; - auto [mySystem] = w.systemMngr.Register(); + auto [mySystem] = w.systemMngr.systemTraits.Register(); w.entityMngr.Create(); diff --git a/src/test/04_filter/main.cpp b/src/test/04_filter/main.cpp index d9ac4cf..11aeca4 100644 --- a/src/test/04_filter/main.cpp +++ b/src/test/04_filter/main.cpp @@ -32,7 +32,7 @@ struct MySystem { int main() { World w; - auto [mySystem] = w.systemMngr.Register(); + auto [mySystem] = w.systemMngr.systemTraits.Register(); auto [e0, b0 ] = w.entityMngr.Create< B >(); // x auto [e1, a1 ] = w.entityMngr.Create(); // x diff --git a/src/test/06_none_parallel/main.cpp b/src/test/06_none_parallel/main.cpp index 9f43f63..3005f82 100644 --- a/src/test/06_none_parallel/main.cpp +++ b/src/test/06_none_parallel/main.cpp @@ -21,7 +21,7 @@ struct MySystem { int main() { World w; - auto [mySystem] = w.systemMngr.Register(); + auto [mySystem] = w.systemMngr.systemTraits.Register(); w.entityMngr.Create<>(); w.entityMngr.Create(); w.entityMngr.Create(); diff --git a/src/test/07_overload/main.cpp b/src/test/07_overload/main.cpp index a9e366c..5ee717a 100644 --- a/src/test/07_overload/main.cpp +++ b/src/test/07_overload/main.cpp @@ -24,7 +24,7 @@ struct AVP_System { int main() { World w; - auto systemIndices = w.systemMngr.Register(); + auto systemIndices = w.systemMngr.systemTraits.Register(); for (auto idx : systemIndices) w.systemMngr.Activate(idx); diff --git a/src/test/08_job/main.cpp b/src/test/08_job/main.cpp index 1dd64d7..152458a 100644 --- a/src/test/08_job/main.cpp +++ b/src/test/08_job/main.cpp @@ -39,7 +39,7 @@ struct MySystem { int main() { World w; - auto [mySystem] = w.systemMngr.Register(); + auto [mySystem] = w.systemMngr.systemTraits.Register(); for (size_t i = 1; i <= 100; i++) { auto [e] = w.entityMngr.Create(); diff --git a/src/test/10_instantiate/main.cpp b/src/test/10_instantiate/main.cpp index 8e7797b..20fe1e4 100644 --- a/src/test/10_instantiate/main.cpp +++ b/src/test/10_instantiate/main.cpp @@ -17,7 +17,7 @@ struct MySystem { int main() { World w; - auto [mySystem] = w.systemMngr.Register(); + auto [mySystem] = w.systemMngr.systemTraits.Register(); auto [e] = w.entityMngr.Create<>(); w.entityMngr.Emplace(e, 1.f); diff --git a/src/test/11_runtime_cmpt/main.cpp b/src/test/11_runtime_cmpt/main.cpp index 2d7ad01..e2c58bc 100644 --- a/src/test/11_runtime_cmpt/main.cpp +++ b/src/test/11_runtime_cmpt/main.cpp @@ -53,7 +53,7 @@ int main() { // } World w; - auto [rtdSystem] = w.systemMngr.Register(); + auto [rtdSystem] = w.systemMngr.systemTraits.Register(); w.entityMngr.cmptTraits .RegisterSize(type, 8) .RegisterDefaultConstructor(type, [](void*) { cout << "construct" << endl; }) diff --git a/src/test/12_framegraph/main.cpp b/src/test/12_framegraph/main.cpp index 4ee6e90..8909607 100644 --- a/src/test/12_framegraph/main.cpp +++ b/src/test/12_framegraph/main.cpp @@ -31,7 +31,7 @@ struct MySystem { int main() { World w; - auto [mySystem] = w.systemMngr.Register(); + auto [mySystem] = w.systemMngr.systemTraits.Register(); w.entityMngr.cmptTraits .RegisterName(CmptType::Of, "A") diff --git a/src/test/13_performance/main.cpp b/src/test/13_performance/main.cpp index a9a7948..e46af84 100644 --- a/src/test/13_performance/main.cpp +++ b/src/test/13_performance/main.cpp @@ -23,7 +23,7 @@ int main() { size_t numEntities = 65536; size_t numUpdate = 144 * 10; World w; - auto [testSystem] = w.systemMngr.Register(); + auto [testSystem] = w.systemMngr.systemTraits.Register(); w.systemMngr.Activate(testSystem); auto t0 = std::chrono::steady_clock::now(); diff --git a/src/test/14_serialize/main.cpp b/src/test/14_serialize/main.cpp index e3d49b9..40203eb 100644 --- a/src/test/14_serialize/main.cpp +++ b/src/test/14_serialize/main.cpp @@ -129,7 +129,7 @@ struct MoverSystem { int main() { World w; w.entityMngr.cmptTraits.Register(); - auto [moverSystem] = w.systemMngr.Register(); + auto [moverSystem] = w.systemMngr.systemTraits.Register(); w.systemMngr.Activate(moverSystem); w.entityMngr.Create(); w.entityMngr.Create(); diff --git a/src/test/15_chunk_job/main.cpp b/src/test/15_chunk_job/main.cpp index 4d8e8ce..f591b8c 100644 --- a/src/test/15_chunk_job/main.cpp +++ b/src/test/15_chunk_job/main.cpp @@ -50,7 +50,7 @@ struct SAB_System { int main() { World w; - auto [sabSystem] = w.systemMngr.Register(); + auto [sabSystem] = w.systemMngr.systemTraits.Register(); w.systemMngr.Activate(sabSystem); diff --git a/src/test/16_singleton/main.cpp b/src/test/16_singleton/main.cpp index 92affe1..6c662d6 100644 --- a/src/test/16_singleton/main.cpp +++ b/src/test/16_singleton/main.cpp @@ -27,7 +27,7 @@ struct MoverSystem { int main() { World w; - auto [moverSystem] = w.systemMngr.Register(); + auto [moverSystem] = w.systemMngr.systemTraits.Register(); w.systemMngr.Activate(moverSystem); w.entityMngr.Create(); w.entityMngr.Create(); diff --git a/src/test/17_serial/main.cpp b/src/test/17_serial/main.cpp index 8ed2dba..abd6b11 100644 --- a/src/test/17_serial/main.cpp +++ b/src/test/17_serial/main.cpp @@ -26,7 +26,7 @@ struct PrintASystem { int main() { World w; - auto [printSystem] = w.systemMngr.Register(); + auto [printSystem] = w.systemMngr.systemTraits.Register(); w.systemMngr.Activate(printSystem); w.entityMngr.Create(); w.entityMngr.Create(); diff --git a/src/test/18_copy/main.cpp b/src/test/18_copy/main.cpp index e12ad15..0033efa 100644 --- a/src/test/18_copy/main.cpp +++ b/src/test/18_copy/main.cpp @@ -27,7 +27,7 @@ struct MoverSystem { int main() { World w; - auto [moverSystem] = w.systemMngr.Register(); + auto [moverSystem] = w.systemMngr.systemTraits.Register(); w.systemMngr.Activate(moverSystem); w.entityMngr.Create(); diff --git a/src/test/19_direct_run/main.cpp b/src/test/19_direct_run/main.cpp index 3d425dc..9da7a13 100644 --- a/src/test/19_direct_run/main.cpp +++ b/src/test/19_direct_run/main.cpp @@ -44,7 +44,7 @@ void print1(const World& w) { int main() { World w; - auto [move] = w.systemMngr.Register(); + auto [move] = w.systemMngr.systemTraits.Register(); w.entityMngr.Create(); w.systemMngr.Activate(move); w.Update(); diff --git a/src/test/20_system_lifecycle/CMakeLists.txt b/src/test/20_system_lifecycle/CMakeLists.txt new file mode 100644 index 0000000..ec4a795 --- /dev/null +++ b/src/test/20_system_lifecycle/CMakeLists.txt @@ -0,0 +1,6 @@ +Ubpa_GetTargetName(core "${PROJECT_SOURCE_DIR}/src/core") +Ubpa_AddTarget( + TEST + MODE EXE + LIB ${core} +) diff --git a/src/test/20_system_lifecycle/main.cpp b/src/test/20_system_lifecycle/main.cpp new file mode 100644 index 0000000..085d11a --- /dev/null +++ b/src/test/20_system_lifecycle/main.cpp @@ -0,0 +1,38 @@ +#include + +using namespace Ubpa::UECS; + +struct Position { float val; }; +struct Velocity { float val; }; + +struct MoverSystem { + static void OnCreate(World* w) { + std::cout << "OnCreate" << std::endl; + } + static void OnActivate(World* w) { + std::cout << "OnActivate" << std::endl; + } + static void OnUpdate(Schedule& schedule) { + std::cout << "OnUpdate" << std::endl; + schedule.RegisterEntityJob( + [](const Velocity* v, Position* p) { + p->val += v->val; + }, + "Mover" + ); + } + static void OnDeactivate(World* w) { + std::cout << "OnDeactivate" << std::endl; + } + static void OnDestroy(World* w) { + std::cout << "OnDestroy" << std::endl; + } +}; + +int main() { + World w; + auto [move] = w.systemMngr.systemTraits.Register(); + w.entityMngr.Create(); + w.systemMngr.Activate(move); + w.Update(); +}