diff options
Diffstat (limited to 'src/ecs.cpp')
| -rw-r--r-- | src/ecs.cpp | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/ecs.cpp b/src/ecs.cpp index dd521ce..b4391dc 100644 --- a/src/ecs.cpp +++ b/src/ecs.cpp @@ -15,6 +15,7 @@ typedef pk_bkt_arr_t<Entity_Base *> type_bkt_arr_entities ; struct ECS { struct pk_membucket *bkt = nullptr; + pk_ev_mgr_id_T ev_mgr_id; struct ECSBucketContainers { pk_bkt_arr_t<Entity_Base> generics{}; type_bkt_arr_entities entityPtrs{}; @@ -28,6 +29,15 @@ struct ECS { * Entities that have been marked for removal by calling ECS_MarkForRemoval * * Used to build the other "removal" lists. + * + * Each ECS module is REQUIRED to manage the lifetimes of its objects. + * Each module MUST wait for an entity to be flagged "IsMarkedForRemoval". + * Even though the module itself is sometimes the source of reporting the + * entity to be cleaned up, it must wait to free resources until marked. + * Do note that this means that it can take several ticks for all resources + * of a level to get freed. + * Ex: box with text; text isn't marked for removal until after box. + * (This happens when deserializing, the FontRender doesn't know about its parent") */ pk_arr_t<Entity_Base *> entitiesMarkedForRemoval{}; /* @@ -68,7 +78,7 @@ void ECS_Init() { pk_arr_reserve(&entitiesMarkedForRemoval, 16); pk_arr_reserve(&entitiesYetToBeRemoved, 16); pk_arr_reserve(&EntitiesWithExcessInstances, 16); - pk_ev_create_mgr(); + ecs.ev_mgr_id = pk_ev_create_mgr(); } Entity_Base *ECS_CreateGenericEntity() { @@ -105,8 +115,8 @@ Entity_Base *ECS_GetEntityByUUID(pk_uuid uuid) { auto entity_ptr_find_cb = [](void *user_data, const void *arr_user_data, const void *arr_obj_data) { (void)user_data; const pk_uuid &uuid = *reinterpret_cast<const pk_uuid*>(arr_user_data);; - const Entity_Base &ent = **reinterpret_cast<const Entity_Base*const*>(arr_obj_data); - return (ent.uuid == uuid); + const Entity_Base *ent = *reinterpret_cast<const Entity_Base*const*>(arr_obj_data); + return (ent->uuid == uuid); }; EntityHandle handle { pk_bkt_arr_find_first_handle(&ecs.bc.entityPtrs, entity_ptr_find_cb, NULL, &uuid) }; if (handle == EntityHandle_MAX) return nullptr; @@ -203,8 +213,7 @@ void ECS_Tick_Early(double delta) { while (b == true) { Entity_Base *ent = ecs.bc.entityPtrs[iter_comp_ev->entity_handle]; if (ent->isMarkedForRemoval) { - pk_ev_destroy_mgr(iter_comp_ev->ev_mgr_id); - pk_bkt_arr_free_handle(&ecs.bc.ev_mgrs, iter_comp_ev->entity_handle); + pk_bkt_arr_free_handle(&ecs.bc.ev_mgrs, iter_comp_ev->pke_event_handle); new (&(*iter_comp_ev)) pke_component_event{}; } b = pk_bkt_arr_iter_increment(&ecs.bc.ev_mgrs, &iter_comp_ev); @@ -565,9 +574,8 @@ CompInstance *ECS_GetInstance(InstanceHandle instanceHandle ) { if (instanceHandle == InstanceHandle_MAX) return nullptr; assert(pk_bkt_arr_handle_validate(&ecs.bc.instances, instanceHandle) == PK_BKT_ARR_HANDLE_VALIDATION_VALID); - auto *inst = &ecs.bc.instances[instanceHandle]; - return inst; + return &ecs.bc.instances[instanceHandle]; } void ECS_GetInstances(Entity_Base *entity, pk_arr_t<CompInstance *> &arr) { @@ -657,6 +665,7 @@ pk_bkt_arr *ECS_GetEvs() { } void ECS_Teardown() { + pk_ev_destroy_mgr(ecs.ev_mgr_id); pk_arr_reset(&EntitiesWithExcessInstances); pk_arr_reset(&entitiesYetToBeRemoved); pk_arr_reset(&entitiesMarkedForRemoval); |
