1#include "Services/Variables.h"
2#include "SwathPathMeshManager.h"
3#include "Components/Core/MeshComponent.h"
4#include "Resources/Mesh.h"
7#include "Foundation/ComponentModel/Entity.h"
12void Cogs::Core::EchoSounder::SwathPathMeshManager::discardEntity(WeakEntityPtr entity)
14 if (entity.expired())
return;
16 auto e = entity.lock();
17 auto meshComp = e->getComponent<MeshComponent>();
19 meshComp->setChanged();
20 entityPool.push_back(entity);
23bool Cogs::Core::EchoSounder::SwathPathMeshManager::ready() {
24 if(persistent->running != 0)
26 for(
auto &tmp : persistent->toBuild)
27 if(tmp.second && !tmp.second->isActive())
32bool Cogs::Core::EchoSounder::SwathPathMeshManager::update(Context* context,
33 SwathPathChunks& chunks,
34 CreateBuildFunc& createBuildFunc,
35 CreateEntity & createEntity,
36 ActivateEntity & activateEntity)
38 assert(persistent->running == 0);
40 if (chunkGen != chunks.getGeneration()) {
41 chunkGen = chunks.getGeneration();
48 for (
auto & i : current) {
49 discardEntity(i.second.entity);
57 for (
auto & i : toDiscard) {
58 discardEntity(i.second.entity);
59 current.erase(i.first);
62 for (
auto & i : persistent->toBuild) {
67 if (!entityPool.empty()) {
68 if (!entityPool.back().expired()) {
69 entity = entityPool.back().lock();
71 entityPool.pop_back();
74 if (!entity) entity = createEntity();
76 auto meshComp = entity->getComponent<MeshComponent>();
77 meshComp->meshHandle = i.second;
78 meshComp->setChanged();
80 activateEntity(entity, i.first.index);
82 i.first.entity = entity;
85 current.insert(std::make_pair(i.first.index, i.first));
89 persistent->toBuild.clear();
92 for (
auto & i : current) {
93 i.second.dirty =
true;
95 update_current =
false;
99 for (
auto & i : current) {
100 if (!chunks.getChunks().validIndex(i.first)
101 || !(chunks.getChunks()[i.first].support == i.second.chunk.support)
102 || !(chunks.getChunks()[i.first].extent == i.second.chunk.extent)
103 || !(chunks.getChunks()[i.first].flags == i.second.chunk.flags))
105 toDiscard.insert(std::make_pair(i.first, i.second));
110 auto maxNewChunks =
static_cast<uint32_t
>(std::max(1, context->variables->get(
"echo.maxNewChunks", 0)));
112 for (uint32_t i = chunks.getChunks().count(); (0 < i) && ((persistent->toBuild.size() < maxNewChunks)); i--) {
113 const auto index = chunks.getChunks().frontIndex() + i - 1;
114 const auto & c = chunks.getChunks()[index];
116 auto it = current.find(index);
117 if (it == current.end()
119 || !(c.extent == it->second.chunk.extent)
120 || !(c.support == it->second.chunk.support)
121 || !(c.flags == it->second.chunk.flags)
125 if (it != current.end()) {
126 toDiscard.insert(std::make_pair(it->first, it->second));
131 if (!persistent->toBuild.empty()) {
132 persistent->running = 1;
134 auto group = context->taskManager->createGroup();
135 std::vector<TaskId> tasks;
137 for (
auto & i : persistent->toBuild) {
138 tasks.push_back(context->taskManager->createChild(group, createBuildFunc(i.second, i.first.index)));
141 context->taskManager->enqueue(context->taskManager->GlobalQueue, [p=persistent, tasks, context, group]()
143 for (
auto task : tasks) {
144 context->taskManager->enqueue(task);
146 context->taskManager->wait(group);
152 return !persistent->toBuild.empty() || !toDiscard.empty();
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
std::weak_ptr< ComponentModel::Entity > WeakEntityPtr
Weak Smart pointer for Entity access.
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.