Only run update and check_timer functions on components that need it

This commit is contained in:
Yousef Khadadeh 2025-07-21 13:57:48 +03:00
parent f82e626686
commit 1c1f23d68d
2 changed files with 27 additions and 4 deletions

View file

@ -57,9 +57,14 @@ DrawingProgram::DrawingProgram(World& initWorld):
};
components.clientInsertCallback = [&](const CollabListType::ObjectInfoPtr& c) {
compCache.add_component(c);
DrawComponentType t = c->obj->get_type();
if(t == DRAWCOMPONENT_IMAGE || t == DRAWCOMPONENT_TEXTBOX)
updateableComponents.emplace(c->obj);
};
components.clientEraseCallback = [&](const CollabListType::ObjectInfoPtr& c) {
compCache.erase_component(c);
delayedUpdateTransformComponents.erase(c->obj);
updateableComponents.erase(c->obj);
};
components.clientServerLastPosShiftCallback = [&](uint64_t lastShiftPos) {
compCache.invalidate_cache_before_pos(lastShiftPos);
@ -87,6 +92,7 @@ void DrawingProgram::init_client_callbacks() {
else {
comp->delayedUpdatePtr = DrawComponent::allocate_comp_type(comp->get_type());
message(*comp->delayedUpdatePtr);
delayedUpdateTransformComponents.emplace(comp);
}
});
world.con.client_add_recv_callback(CLIENT_TRANSFORM_COMPONENT, [&](cereal::PortableBinaryInputArchive& message) {
@ -107,6 +113,7 @@ void DrawingProgram::init_client_callbacks() {
else {
comp->delayedCoordinateSpace = std::make_shared<CoordSpaceHelper>();
message(*comp->delayedCoordinateSpace);
delayedUpdateTransformComponents.emplace(comp);
}
});
world.con.client_add_recv_callback(CLIENT_PLACE_COMPONENT, [&](cereal::PortableBinaryInputArchive& message) {
@ -127,6 +134,18 @@ void DrawingProgram::init_client_callbacks() {
});
}
void DrawingProgram::check_delayed_update_transform_timers() {
std::erase_if(delayedUpdateTransformComponents, [&](auto& comp) {
comp->check_timers(*this);
return !comp->delayedUpdatePtr && !comp->delayedCoordinateSpace;
});
}
void DrawingProgram::check_updateable_components() {
for(auto& comp : updateableComponents)
comp->update(*this);
}
void DrawingProgram::free_collider_memory() {
for(auto& c : components.client_list())
c->obj->free_collider();
@ -354,10 +373,8 @@ void DrawingProgram::update() {
if(controls.leftClickReleased)
controls.leftClickReleased = false;
for(auto& c : components.client_list()) {
c->obj->check_timers(*this);
c->obj->update(*this);
}
check_delayed_update_transform_timers();
check_updateable_components();
compCache.update();
}

View file

@ -55,9 +55,15 @@ class DrawingProgram {
CollabListType components;
std::unordered_set<std::shared_ptr<DrawComponent>> updateableComponents;
std::unordered_set<std::shared_ptr<DrawComponent>> delayedUpdateTransformComponents;
Vector4f* get_foreground_color_ptr();
private:
void check_delayed_update_transform_timers();
void check_updateable_components();
std::atomic<bool> addFileInNextFrame = false;
std::pair<std::string, Vector2f> addFileInfo;