Skip to content

Commit

Permalink
EntityMngr += Get...Array
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 20, 2020
1 parent 3833dac commit 49f80df
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(UECS VERSION 0.11.6)
project(UECS VERSION 0.11.7)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
10 changes: 8 additions & 2 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ namespace Ubpa::UECS {
template<typename Cmpt>
Cmpt* GetSingleton() const { return GetSingleton(CmptType::Of<Cmpt>).As<Cmpt>(); }

// filter's all contains cmpt
template<typename Cmpt>
std::vector<Cmpt*> GetCmptArray(const ArchetypeFilter&) const;
std::vector<CmptPtr> GetCmptArray(const ArchetypeFilter&, CmptType) const;
std::vector<Entity> GetEntityArray(const ArchetypeFilter&) const;

void Accept(IListener* listener) const;

private:
Expand All @@ -98,7 +104,7 @@ namespace Ubpa::UECS {
Archetype* AttachWithoutInit(Entity);
Archetype* AttachWithoutInit(Entity, const CmptType* types, size_t num);

const std::set<Archetype*>& QueryArchetypes(const EntityQuery& query) const;
const std::set<Archetype*>& QueryArchetypes(const EntityQuery&) const;
mutable std::unordered_map<EntityQuery, std::set<Archetype*>> queryCache;

void GenEntityJob(World*, Job*, SystemFunc*) const;
Expand All @@ -113,7 +119,7 @@ namespace Ubpa::UECS {
std::vector<EntityInfo> entityTable;
std::vector<size_t> entityTableFreeEntry;
size_t RequestEntityFreeEntry();
void RecycleEntityEntry(Entity e);
void RecycleEntityEntry(Entity);

std::unordered_map<CmptTypeSet, std::unique_ptr<Archetype>> ts2a; // archetype's CmptTypeSet to archetype
};
Expand Down
33 changes: 33 additions & 0 deletions include/UECS/detail/EntityMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,37 @@ namespace Ubpa::UECS {
inline bool EntityMngr::Exist(Entity e) const {
return e.Idx() < entityTable.size() && e.Version() == entityTable[e.Idx()].version;
}

template<typename Cmpt>
std::vector<Cmpt*> EntityMngr::GetCmptArray(const ArchetypeFilter& filter) const {
constexpr auto type = CmptType::Of<Cmpt>;
assert(filter.all.find(type) != filter.all.end());

std::vector<Cmpt*> rst;

const auto& archetypes = QueryArchetypes(filter);
size_t num = 0;
for (const auto& archetype : archetypes)
num += archetype->EntityNum();

rst.resize(num);
size_t idx = 0;
for (auto archetype : archetypes) {
/*for (size_t i = 0; i < archetype->EntityNum(); i++)
rst[idx++] = archetype->At<CmptType>(i);*/

// speed up

size_t offset = archetype->Offsetof(type);
for (size_t c = 0; c < archetype->chunks.size(); c++) {
auto buffer = archetype->chunks[c]->Data();
auto beg = buffer + offset;
size_t chunkSize = archetype->EntityNumOfChunk(c);
for (size_t i = 0; i < chunkSize; i++)
rst[idx++] = reinterpret_cast<Cmpt*>(beg + i * sizeof(Cmpt));
}
}

return rst;
}
}
61 changes: 61 additions & 0 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,64 @@ CmptPtr EntityMngr::GetSingleton(CmptType t) const {

return { t, archetype->At(t, 0) };
}

std::vector<CmptPtr> EntityMngr::GetCmptArray(const ArchetypeFilter& filter, CmptType type) const {
assert(filter.all.find(type) != filter.all.end());

std::vector<CmptPtr> rst;

const auto& archetypes = QueryArchetypes(filter);

size_t num = 0;
for (const auto& archetype : archetypes)
num += archetype->EntityNum();

rst.reserve(num);
for (auto archetype : archetypes) {
/*for (size_t i = 0; i < archetype->EntityNum(); i++)
rst[idx++] = *archetype->At(type, i);*/

// speed up

size_t size = archetype->cmptTraits.Sizeof(type);
size_t offset = archetype->Offsetof(type);
for (size_t c = 0; c < archetype->chunks.size(); c++) {
auto buffer = archetype->chunks[c]->Data();
auto beg = buffer + offset;
size_t chunkSize = archetype->EntityNumOfChunk(c);
for (size_t i = 0; i < chunkSize; i++)
rst.emplace_back(type, beg + i * size);
}
}

return rst;
}

std::vector<Entity> EntityMngr::GetEntityArray(const ArchetypeFilter& filter) const {
std::vector<Entity> rst;

const auto& archetypes = QueryArchetypes(filter);

size_t num = 0;
for (const auto& archetype : archetypes)
num += archetype->EntityNum();

rst.reserve(num);
for (auto archetype : archetypes) {
/*for (size_t i = 0; i < archetype->EntityNum(); i++)
rst[idx++] = *archetype->At<Entity>(i);*/

// speed up

size_t offset = archetype->Offsetof(CmptType::Of<Entity>);
for (size_t c = 0; c < archetype->chunks.size(); c++) {
auto buffer = archetype->chunks[c]->Data();
auto beg = buffer + offset;
size_t chunkSize = archetype->EntityNumOfChunk(c);
for (size_t i = 0; i < chunkSize; i++)
rst.push_back(*reinterpret_cast<Entity*>(beg + i * sizeof(Entity)));
}
}

return rst;
}

0 comments on commit 49f80df

Please sign in to comment.