From be992919ec1d59d6c18f5a93f888654c70dc0b77 Mon Sep 17 00:00:00 2001 From: Ubpa Date: Mon, 5 Oct 2020 15:38:37 +0800 Subject: [PATCH] World::Run*Job const --- CMakeLists.txt | 14 +-- cmake/InitUCMake.cmake | 4 +- include/UECS/ArchetypeFilter.h | 4 +- include/UECS/CmptLocator.h | 2 + include/UECS/CmptTag.h | 101 ---------------------- include/UECS/Schedule.h | 6 +- include/UECS/World.h | 40 +++++++-- include/UECS/detail/CmptTag.inl | 118 +++++++++++++++++++++++++- include/UECS/detail/SystemFunc.h | 5 +- include/UECS/detail/SystemFunc.inl | 21 +++-- include/UECS/detail/World.inl | 45 ++++++++-- src/app/CmptHashCode/CMakeLists.txt | 1 + src/core/ArchetypeFilter.cpp | 20 ++++- src/core/Chunk.cpp | 2 - src/core/CmptLocator.cpp | 15 +++- src/core/EntityMngr.cpp | 17 +++- src/core/SingletonLocator.cpp | 6 +- src/core/SystemFunc.cpp | 4 +- src/test/19_direct_run/CMakeLists.txt | 6 ++ src/test/19_direct_run/main.cpp | 53 ++++++++++++ 20 files changed, 335 insertions(+), 149 deletions(-) create mode 100644 src/test/19_direct_run/CMakeLists.txt create mode 100644 src/test/19_direct_run/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 100d4b1..7dcc94d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -project(UECS VERSION 0.13.1) +project(UECS VERSION 0.13.2) message(STATUS "[Project] ${PROJECT_NAME}") include(cmake/InitUCMake.cmake) @@ -8,11 +8,15 @@ Ubpa_InitUCMake() Ubpa_InitProject() -Ubpa_AddDep(UContainer 0.0.6) -Ubpa_AddDep(UTemplate 0.4.7) -Ubpa_AddDep(UGraphviz 0.1.4) +Ubpa_AddDep(UContainer 0.0.7) +Ubpa_AddDep(UTemplate 0.4.8) +Ubpa_AddDep(UGraphviz 0.1.5) Ubpa_AddSubDirsRec(include) Ubpa_AddSubDirsRec(src) -Ubpa_Export(DIRECTORIES "include") +Ubpa_Export( + TARGET + DIRECTORIES + "include" +) diff --git a/cmake/InitUCMake.cmake b/cmake/InitUCMake.cmake index 559d3a6..fab41e3 100644 --- a/cmake/InitUCMake.cmake +++ b/cmake/InitUCMake.cmake @@ -1,8 +1,8 @@ macro(Ubpa_InitUCMake) include(FetchContent) - set(UCMake_VERSION 0.6.1) + set(UCMake_VERSION 0.6.2) message(STATUS "find package: UCMake ${UCMake_VERSION}") - find_package(UCMake ${UCMake_VERSION} QUIET) + find_package(UCMake ${UCMake_VERSION} EXACT QUIET) if(NOT UCMake_FOUND) message(STATUS "UCMake ${UCMake_VERSION} not found") set(_address "https://github.com/Ubpa/UCMake") diff --git a/include/UECS/ArchetypeFilter.h b/include/UECS/ArchetypeFilter.h index 657d1d5..2a6181d 100644 --- a/include/UECS/ArchetypeFilter.h +++ b/include/UECS/ArchetypeFilter.h @@ -2,8 +2,6 @@ #include "CmptType.h" -#include - #include namespace Ubpa::UECS { @@ -15,6 +13,8 @@ namespace Ubpa::UECS { size_t HashCode() const noexcept; + bool HaveWriteCmptType() const noexcept; + bool operator==(const ArchetypeFilter& rhs) const; }; } diff --git a/include/UECS/CmptLocator.h b/include/UECS/CmptLocator.h index 2fed2f3..1a53457 100644 --- a/include/UECS/CmptLocator.h +++ b/include/UECS/CmptLocator.h @@ -26,6 +26,8 @@ namespace Ubpa::UECS { const CmptAccessTypeSet& CmptAccessTypes() const noexcept { return cmptTypes; } bool operator==(const CmptLocator& rhs) const; + + bool HasWriteCmptType() const noexcept; private: size_t GenHashCode() const noexcept; diff --git a/include/UECS/CmptTag.h b/include/UECS/CmptTag.h index 6a51ec1..47e83db 100644 --- a/include/UECS/CmptTag.h +++ b/include/UECS/CmptTag.h @@ -1,9 +1,6 @@ #pragma once -#include - namespace Ubpa::UECS { - // read/write tag : LastFrame -> Write -> Latest // singleton tag : Singleton // ==== @@ -110,104 +107,6 @@ namespace Ubpa::UECS { private: const Cmpt* cmpt; }; - - template - void* CastToVoidPointer(TaggedCmpt p) { - return const_cast(reinterpret_cast(p)); - } - - // (without read/write and singleton tag) - template - struct RemoveTag; - template - using RemoveTag_t = typename RemoveTag::type; - - // / Singleton - template - struct RemoveRWTag; - template - using RemoveRWTag_t = typename RemoveRWTag::type; - - // LastFrame - // Write / Cmpt* - // Latest / const Cmpt* - template - struct RemoveSingletonTag; - template - using RemoveSingletonTag_t = typename RemoveSingletonTag::type; - - // * - template - struct DecayTag; - template - using DecayTag_t = typename DecayTag::type; - - template - struct IsLastFrame; - template - static constexpr bool IsLastFrame_v = IsLastFrame::value; - - template - struct IsWrite; - template - static constexpr bool IsWrite_v = IsWrite::value; - - template - struct IsLatest; - template - static constexpr bool IsLatest_v = IsLatest::value; - - template - struct IsLastFrameSingleton; - template - static constexpr bool IsLastFrameSingleton_v = IsLastFrameSingleton::value; - - template - struct IsWriteSingleton; - template - static constexpr bool IsWriteSingleton_v = IsWriteSingleton::value; - - template - struct IsLatestSingleton; - template - static constexpr bool IsLatestSingleton_v = IsLatestSingleton::value; - - template - struct IsSingleton : IValue - || IsWriteSingleton_v - || IsLatestSingleton_v - > {}; - template - static constexpr bool IsSingleton_v = IsSingleton::value; - - template - struct IsNonSingleton : IValue - || IsWrite_v - || IsLatest_v - > {}; - template - static constexpr bool IsNonSingleton_v = IsNonSingleton::value; - - template - struct IsTaggedCmpt : IValue || IsSingleton_v> {}; - template - static constexpr bool IsTaggedCmpt_v = IsTaggedCmpt::value; - - template - static constexpr AccessMode AccessModeOf_default = - IsLastFrame_v ? AccessMode::LAST_FRAME : ( - IsWrite_v ? AccessMode::WRITE : ( - IsLatest_v ? AccessMode::LATEST : ( - IsLastFrameSingleton_v ? AccessMode::LAST_FRAME_SINGLETON : ( - IsWriteSingleton_v ? AccessMode::WRITE_SINGLETON : ( - IsLatestSingleton_v ? AccessMode::LATEST_SINGLETON : - defaultMode - ))))); - - template - static constexpr AccessMode AccessModeOf = AccessModeOf_default; } #include "detail/CmptTag.inl" diff --git a/include/UECS/Schedule.h b/include/UECS/Schedule.h index 55164c7..3bb601f 100644 --- a/include/UECS/Schedule.h +++ b/include/UECS/Schedule.h @@ -26,7 +26,7 @@ namespace Ubpa::UECS { class Schedule { public: // Func's argument list: - // World* + // [const] World* // {LastFrame|Latest}> // SingletonsView // Entity @@ -44,7 +44,7 @@ namespace Ubpa::UECS { ); // Func's argument list: - // World* + // [const] World* // {LastFrame|Latest}> // SingletonsView // ChunkView (necessary) @@ -58,7 +58,7 @@ namespace Ubpa::UECS { ); // Func's argument list: - // World* + // [const] World* // {LastFrame|Write|Latest}> // SingletonsView template diff --git a/include/UECS/World.h b/include/UECS/World.h index 5d63b90..166c99f 100644 --- a/include/UECS/World.h +++ b/include/UECS/World.h @@ -41,10 +41,9 @@ namespace Ubpa::UECS { void Accept(IListener*) const; // you can't run several parallel jobs in parallel because there is only an executor - // you can't run parallel jobs in runing job graph // Func's argument list: - // World* + // [const] World* // {LastFrame|Latest}> // SingletonsView // Entity @@ -61,9 +60,29 @@ namespace Ubpa::UECS { ); // Func's argument list: - // World* + // const World* // {LastFrame|Latest}> // SingletonsView + // Entity + // size_t indexInQuery + // : const *... + // CmptsView + // -- + // CmptLocator's Cmpt AccessMode can't be WRITE + template + void RunEntityJob( + Func&&, + bool isParallel = true, + ArchetypeFilter = {}, + CmptLocator = {}, + SingletonLocator = {} + ) const; + + // Func's argument list: + // [const] World* + // {LastFrame|Latest}> + // SingletonsView + // size_t entityBeginIndexInQuery // ChunkView (necessary) template void RunChunkJob( @@ -74,15 +93,20 @@ namespace Ubpa::UECS { ); // Func's argument list: - // World* - // {LastFrame|Write|Latest}> + // const World* + // {LastFrame|Latest}> // SingletonsView + // size_t entityBeginIndexInQuery + // ChunkView (necessary) + // -- + // ArchetypeFilter's Cmpt AccessMode can't be WRITE template - void RunJob( + void RunChunkJob( Func&&, + ArchetypeFilter = {}, + bool isParallel = true, SingletonLocator = {} - ); - + ) const; private: bool inRunningJobGraph{ false }; diff --git a/include/UECS/detail/CmptTag.inl b/include/UECS/detail/CmptTag.inl index 879c637..dd144bb 100644 --- a/include/UECS/detail/CmptTag.inl +++ b/include/UECS/detail/CmptTag.inl @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace Ubpa::UECS { class Entity; @@ -9,6 +9,114 @@ namespace Ubpa::UECS { } namespace Ubpa::UECS { + template + void* CastToVoidPointer(TaggedCmpt p) { + return const_cast(reinterpret_cast(p)); + } + + // (without read/write and singleton tag) + template + struct RemoveTag; + template + using RemoveTag_t = typename RemoveTag::type; + + // / Singleton + template + struct RemoveRWTag; + template + using RemoveRWTag_t = typename RemoveRWTag::type; + + // LastFrame + // Write / Cmpt* + // Latest / const Cmpt* + template + struct RemoveSingletonTag; + template + using RemoveSingletonTag_t = typename RemoveSingletonTag::type; + + // * + template + struct DecayTag; + template + using DecayTag_t = typename DecayTag::type; + + // *, World* + template + struct DecayArg; + template + using DecayArg_t = typename DecayArg::type; + + template + struct IsLastFrame; + template + static constexpr bool IsLastFrame_v = IsLastFrame::value; + + template + struct IsWrite; + template + static constexpr bool IsWrite_v = IsWrite::value; + + template + struct IsLatest; + template + static constexpr bool IsLatest_v = IsLatest::value; + + template + struct IsLastFrameSingleton; + template + static constexpr bool IsLastFrameSingleton_v = IsLastFrameSingleton::value; + + template + struct IsWriteSingleton; + template + static constexpr bool IsWriteSingleton_v = IsWriteSingleton::value; + + template + struct IsLatestSingleton; + template + static constexpr bool IsLatestSingleton_v = IsLatestSingleton::value; + + template + struct IsSingleton : IValue + || IsWriteSingleton_v + || IsLatestSingleton_v + > {}; + template + static constexpr bool IsSingleton_v = IsSingleton::value; + + template + struct IsNonSingleton : IValue + || IsWrite_v + || IsLatest_v + > {}; + template + static constexpr bool IsNonSingleton_v = IsNonSingleton::value; + + template + struct IsTaggedCmpt : IValue || IsSingleton_v> {}; + template + static constexpr bool IsTaggedCmpt_v = IsTaggedCmpt::value; + + template + static constexpr AccessMode AccessModeOf_default = + IsLastFrame_v ? AccessMode::LAST_FRAME : ( + IsWrite_v ? AccessMode::WRITE : ( + IsLatest_v ? AccessMode::LATEST : ( + IsLastFrameSingleton_v ? AccessMode::LAST_FRAME_SINGLETON : ( + IsWriteSingleton_v ? AccessMode::WRITE_SINGLETON : ( + IsLatestSingleton_v ? AccessMode::LATEST_SINGLETON : + defaultMode + ) + ) + ) + ) + ); + + template + static constexpr AccessMode AccessModeOf = AccessModeOf_default; + template struct RemoveTag : IType {}; // default template struct RemoveTag> : IType {}; @@ -63,6 +171,13 @@ namespace Ubpa::UECS { template struct DecayTag> : IType {}; template struct DecayTag> : IType {}; + // ==== + + template + struct DecayArg : DecayTag {}; + template<> + struct DecayArg : IType {}; + // ==== template struct IsLastFrame : std::false_type {}; @@ -80,6 +195,7 @@ namespace Ubpa::UECS { template struct IsLatest> : std::true_type {}; template struct IsLatest>> : std::false_type {}; template struct IsLatest : std::true_type {}; + template<> struct IsLatest : std::false_type {}; template struct IsLastFrameSingleton : std::false_type {}; template struct IsLastFrameSingleton>> : std::true_type {}; diff --git a/include/UECS/detail/SystemFunc.h b/include/UECS/detail/SystemFunc.h index 0ee9522..9e87ec9 100644 --- a/include/UECS/detail/SystemFunc.h +++ b/include/UECS/detail/SystemFunc.h @@ -16,13 +16,14 @@ namespace Ubpa::UECS { // name('s hashcode) must be unique in global // query.filter.none can be change dynamically by other with // [- system function kind] (distinguish by argument list) - // common arguments : World*, SingletonsView, {LastFrame|Latest}> + // common arguments : [const] World*, SingletonsView, {LastFrame|Latest}> // 1. Mode::Entity: per entity function // * Entity // * size_t indexInQuery // * : {LastFrame|Write|Latest}... // * CmptsView // 2. Mode::Chunk + // * size_t entityBeginIndexInQuery // * ChunkView (necessary) // 3. Mode::Job // * Write> (only job can write singletons) @@ -56,7 +57,7 @@ namespace Ubpa::UECS { size_t HashCode() const noexcept { return hashCode; } void operator()(World*, SingletonsView, Entity, size_t entityIndexInQuery, CmptsView); - void operator()(World*, SingletonsView, ChunkView); + void operator()(World*, SingletonsView, size_t entityBeginIndexInQuery, ChunkView); void operator()(World*, SingletonsView); Mode GetMode() const noexcept { return mode; } diff --git a/include/UECS/detail/SystemFunc.inl b/include/UECS/detail/SystemFunc.inl index 42f7ca9..f12eb58 100644 --- a/include/UECS/detail/SystemFunc.inl +++ b/include/UECS/detail/SystemFunc.inl @@ -59,14 +59,20 @@ namespace Ubpa::UECS { using ArgList = FuncTraits_ArgList>; static_assert(Length_v> == 0, - "(Mode::Chunk) SystemFunc can't write singletons, use {Latest|LastFrame}Singleton instead"); + "(Mode::Chunk) SystemFunc can't write singletons, use {Latest|LastFrame}Singleton"); static_assert(Contain_v); + static_assert(!Contain_v, + "(Mode::Chunk) SystemFunc can't use Entity directly, use ChunkView::GetEntityArray()"); + + static_assert(!Contain_v, + "(Mode::Chunk) SystemFunc's argument list cann't have CmptsView"); + static_assert(Length_v> == 0, "(Mode::Chunk) SystemFunc can't directly access entities' components"); - assert("(Mode::Chunk) SystemFunc can't write singletons, use {Latest|LastFrame}Singleton instead" + assert("(Mode::Chunk) SystemFunc can't write singletons, use {Latest|LastFrame}Singleton" && !singletonLocator.HasWriteSingletonType()); } @@ -86,8 +92,13 @@ namespace Ubpa::UECS { static_assert(Length_v> == 0, "(Mode::Job) SystemFunc can't access entities' components"); - static_assert(!Contain_v && !Contain_v && !Contain_v, - "(Mode::Job) SystemFunc's argument list cann't have Entity, indexInQuery or ChunkView"); + static_assert( + !Contain_v + && !Contain_v + && !Contain_v + && !Contain_v, + "(Mode::Job) SystemFunc's argument list cann't have Entity, indexInQuery CmptsView or ChunkView" + ); } } @@ -128,7 +139,7 @@ namespace Ubpa::UECS::detail { auto Pack(Func&& func) noexcept { using ArgList = FuncTraits_ArgList; - using DecayedArgList = Transform_t; + using DecayedArgList = Transform_t; static_assert(IsSet_v, "detail::System_::Pack: 's argument types must be a set"); using TaggedCmptList = Filter_t; diff --git a/include/UECS/detail/World.inl b/include/UECS/detail/World.inl index 28f4836..9b5861d 100644 --- a/include/UECS/detail/World.inl +++ b/include/UECS/detail/World.inl @@ -20,6 +20,31 @@ namespace Ubpa::UECS { Run(&sys); } + template + void World::RunEntityJob( + Func&& func, + bool isParallel, + ArchetypeFilter filter, + CmptLocator cmptLocator, + SingletonLocator singletonLocator + ) const { + using ArgList = FuncTraits_ArgList; + static_assert(Contain_v == 0, + "const RunEntityJob should use const World*"); + static_assert(Length_v> == 0, + "const RunEntityJob can't write cmpt"); + assert("const RunEntityJob can't write cmpt" + && !cmptLocator.HasWriteCmptType()); + + const_cast(this)->RunEntityJob( + std::forward(func), + isParallel, + std::move(filter), + std::move(cmptLocator), + std::move(singletonLocator) + ); + } + template void World::RunChunkJob( Func&& func, @@ -38,15 +63,23 @@ namespace Ubpa::UECS { } template - void World::RunJob( + void World::RunChunkJob( Func&& func, + ArchetypeFilter filter, + bool isParallel, SingletonLocator singletonLocator - ) { - SystemFunc sys{ + ) const { + using ArgList = FuncTraits_ArgList; + static_assert(Contain_v == 0, + "const RunChunkJob should use const World*"); + assert("const RunChunkJob can't write cmpt" + && !filter.HaveWriteCmptType()); + + const_cast(this)->RunChunkJob( std::forward(func), - "", + std::move(filter), + isParallel, std::move(singletonLocator) - }; - Run(&sys); + ); } } diff --git a/src/app/CmptHashCode/CMakeLists.txt b/src/app/CmptHashCode/CMakeLists.txt index 0cd4121..66acc83 100644 --- a/src/app/CmptHashCode/CMakeLists.txt +++ b/src/app/CmptHashCode/CMakeLists.txt @@ -1,6 +1,7 @@ Ubpa_GetTargetName(core "${PROJECT_SOURCE_DIR}/src/core") Ubpa_AddTarget( MODE EXE + OUTPUT_NAME CmptHashCodeGenerator LIB Ubpa::UTemplate_core INC_PRIVATE diff --git a/src/core/ArchetypeFilter.cpp b/src/core/ArchetypeFilter.cpp index 74499f4..944fa62 100644 --- a/src/core/ArchetypeFilter.cpp +++ b/src/core/ArchetypeFilter.cpp @@ -6,15 +6,31 @@ using namespace Ubpa::UECS; size_t ArchetypeFilter::HashCode() const noexcept { size_t rst = TypeID; - for (const auto& type : all) + for (const auto& type : all) { + assert(!AccessMode_IsSingleton(type.GetAccessMode())); rst = hash_combine(rst, type.HashCode()); - for (const auto& type : any) + } + for (const auto& type : any) { + assert(!AccessMode_IsSingleton(type.GetAccessMode())); rst = hash_combine(rst, type.HashCode()); + } for (const auto& type : none) rst = hash_combine(rst, type.HashCode()); return rst; } +bool ArchetypeFilter::HaveWriteCmptType() const noexcept { + for (const auto& type : all) { + if (type.GetAccessMode() == AccessMode::WRITE) + return true; + } + for (const auto& type : any) { + if (type.GetAccessMode() == AccessMode::WRITE) + return true; + } + return false; +} + bool ArchetypeFilter::operator==(const ArchetypeFilter& rhs) const { return all == rhs.all && any == rhs.any diff --git a/src/core/Chunk.cpp b/src/core/Chunk.cpp index 8d95eaf..3f73e81 100644 --- a/src/core/Chunk.cpp +++ b/src/core/Chunk.cpp @@ -1,5 +1,3 @@ -#pragma once - #include #include diff --git a/src/core/CmptLocator.cpp b/src/core/CmptLocator.cpp index f9eedd1..9a28109 100644 --- a/src/core/CmptLocator.cpp +++ b/src/core/CmptLocator.cpp @@ -2,15 +2,14 @@ #include -#include - using namespace Ubpa::UECS; -using namespace std; CmptLocator::CmptLocator(const CmptAccessType* types, size_t num) { assert(types || num == 0); - for (size_t i = 0; i < num; i++) + for (size_t i = 0; i < num; i++) { + assert(!AccessMode_IsSingleton(types[i].GetAccessMode())); cmptTypes.insert(types[i]); + } hashCode = GenHashCode(); } @@ -25,6 +24,14 @@ size_t CmptLocator::GenHashCode() const noexcept { return rst; } +bool CmptLocator::HasWriteCmptType() const noexcept { + for (const auto& type : cmptTypes) { + if (type.GetAccessMode() == AccessMode::WRITE) + return true; + } + return false; +} + bool CmptLocator::operator==(const CmptLocator& rhs) const { return cmptTypes == rhs.cmptTypes; } diff --git a/src/core/EntityMngr.cpp b/src/core/EntityMngr.cpp index b88ac3a..5ddd0b5 100644 --- a/src/core/EntityMngr.cpp +++ b/src/core/EntityMngr.cpp @@ -391,30 +391,45 @@ void EntityMngr::GenChunkJob(World* w, Job* job, SystemFunc* sys) const { if (sys->IsParallel()) { assert(job != nullptr); + size_t indexOffsetInQuery = 0; for (Archetype* archetype : QueryArchetypes(sys->entityQuery)) { + size_t num = archetype->EntityNum(); size_t chunkNum = archetype->ChunkNum(); + size_t chunkCapacity = archetype->ChunkCapacity(); for (size_t i = 0; i < chunkNum; i++) { + size_t idxOffsetInChunk = i * chunkCapacity; + size_t indexOffsetInQueryChunk = indexOffsetInQuery + idxOffsetInChunk; job->emplace([=, singletons = singletons]() { (*sys)( w, SingletonsView{ singletons.data(), singletons.size() }, + indexOffsetInQueryChunk, ChunkView{ archetype, i } ); }); } + + indexOffsetInQuery += num; } } else { auto work = [this, w, sys, singletons = std::move(singletons)]() { + SingletonsView singletonsView{ singletons.data(), singletons.size() }; + + size_t indexOffsetInQuery = 0; for (Archetype* archetype : QueryArchetypes(sys->entityQuery)) { + size_t num = archetype->EntityNum(); size_t chunkNum = archetype->ChunkNum(); - SingletonsView singletonsView{ singletons.data(), singletons.size() }; + size_t chunkCapacity = archetype->ChunkCapacity(); for (size_t i = 0; i < chunkNum; i++) { + size_t idxOffsetInChunk = i * chunkCapacity; + size_t indexOffsetInQueryChunk = indexOffsetInQuery + idxOffsetInChunk; (*sys)( w, singletonsView, + indexOffsetInQueryChunk, ChunkView{ archetype, i } ); } diff --git a/src/core/SingletonLocator.cpp b/src/core/SingletonLocator.cpp index 8203f9a..cfc2738 100644 --- a/src/core/SingletonLocator.cpp +++ b/src/core/SingletonLocator.cpp @@ -2,15 +2,15 @@ #include -#include - using namespace Ubpa::UECS; using namespace std; SingletonLocator::SingletonLocator(const CmptAccessType* types, size_t num) { assert(types || num == 0); - for (size_t i = 0; i < num; i++) + for (size_t i = 0; i < num; i++) { + assert(AccessMode_IsSingleton(types[i].GetAccessMode())); singletonTypes.insert(types[i]); + } } bool SingletonLocator::HasWriteSingletonType() const noexcept { diff --git a/src/core/SystemFunc.cpp b/src/core/SystemFunc.cpp index 341bd3f..31a3498 100644 --- a/src/core/SystemFunc.cpp +++ b/src/core/SystemFunc.cpp @@ -14,13 +14,13 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView, Entity e, s ); } -void SystemFunc::operator()(World* w, SingletonsView singletonsView, ChunkView chunkView) { +void SystemFunc::operator()(World* w, SingletonsView singletonsView, size_t entityBeginIndexInQuery, ChunkView chunkView) { assert(mode == Mode::Chunk); return func( w, singletonsView, Entity::Invalid(), - static_cast(-1), + entityBeginIndexInQuery, CmptsView{ nullptr, 0 }, chunkView ); diff --git a/src/test/19_direct_run/CMakeLists.txt b/src/test/19_direct_run/CMakeLists.txt new file mode 100644 index 0000000..ec4a795 --- /dev/null +++ b/src/test/19_direct_run/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/19_direct_run/main.cpp b/src/test/19_direct_run/main.cpp new file mode 100644 index 0000000..3d425dc --- /dev/null +++ b/src/test/19_direct_run/main.cpp @@ -0,0 +1,53 @@ +#include + +#include + +using namespace Ubpa::UECS; +using namespace std; + +struct Position { float val; }; +struct Velocity { float val; }; + +struct MoverSystem { + static void OnUpdate(Schedule& schedule) { + schedule.RegisterEntityJob( + [](const Velocity* v, Position* p) { + p->val += v->val; + }, + "Mover" + ); + } +}; + +void print0(const World& w) { + w.RunEntityJob([](const World* w, const Velocity* v, const Position* p) { + cout << v->val << ", " << p->val << endl; + }, false); +} + +void print1(const World& w) { + ArchetypeFilter filter; + filter.all = { CmptAccessType::Of>, CmptAccessType::Of> }; + w.RunChunkJob( + [](const World* w, ChunkView chunk) { + auto velocities = chunk.GetCmptArray(); + auto positions = chunk.GetCmptArray(); + size_t N = chunk.EntityNum(); + for (size_t i = 0; i < N; i++) { + cout << velocities[i].val << ", " << positions[i].val << endl; + } + }, + filter, + false + ); +} + +int main() { + World w; + auto [move] = w.systemMngr.Register(); + w.entityMngr.Create(); + w.systemMngr.Activate(move); + w.Update(); + print0(w); + print1(w); +}