Making entity destruction better

This commit is contained in:
AnonymusRaccoon
2020-03-06 17:34:29 +01:00
parent 76101c2992
commit da57b43f45
5 changed files with 26 additions and 4 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ struct gc_entity
void (*remove_component)(const gc_scene *scene, const gc_entity *entity, \ void (*remove_component)(const gc_scene *scene, const gc_entity *entity, \
const char *name); const char *name);
char *(*serialize)(gc_entity *entity, int fd); char *(*serialize)(gc_entity *entity, int fd);
void (*destroy)(gc_entity *entity); void (*destroy)(gc_entity *entity, gc_scene *scene);
}; };
gc_entity *entity_create(void); gc_entity *entity_create(void);
+3 -1
View File
@@ -16,5 +16,7 @@ struct gc_list
}; };
gc_list *list_add(gc_list *list, void *obj); gc_list *list_add(gc_list *list, void *obj);
gc_list *list_remove(gc_list *list, void *obj);
#define LISTADD(list, obj) (list = list_add(list, obj)) #define LISTADD(list, obj) (list = list_add(list, obj))
#define LISTREM(list, obj) (list = list_remove(list, obj))
+5 -1
View File
@@ -53,11 +53,15 @@ char *entity_serialize(gc_entity *entity, int fd)
return (NULL); return (NULL);
} }
static void destroy(gc_entity *entity) static void destroy(gc_entity *entity, gc_scene *scene)
{ {
gc_component *next = NULL; gc_component *next = NULL;
for (gc_component *cmp = entity->components; cmp; cmp = next) { for (gc_component *cmp = entity->components; cmp; cmp = next) {
for (gc_tupple *tup = scene->entities_by_cmp; tup; tup = tup->next)
if (!my_strcmp(tup->name, cmp->name))
tup_remove(tup, entity->id);
LISTREM(scene->entities, entity);
next = cmp->next; next = cmp->next;
cmp->destroy(cmp); cmp->destroy(cmp);
} }
+1 -1
View File
@@ -32,7 +32,7 @@ void scene_destroy(gc_scene *scene)
for (gc_list *entity = scene->entities; entity; entity = next) { for (gc_list *entity = scene->entities; entity; entity = next) {
next = entity->next; next = entity->next;
((gc_entity *)entity->data)->destroy(entity->data); ((gc_entity *)entity->data)->destroy(entity->data, scene);
free(entity); free(entity);
} }
free_data(scene); free_data(scene);
+16
View File
@@ -26,4 +26,20 @@ gc_list *list_add(gc_list *list, void *obj)
list->data = obj; list->data = obj;
list->next = NULL; list->next = NULL;
return (listconst); return (listconst);
}
gc_list *list_remove(gc_list *list, void *obj)
{
gc_list *listconst = list;
if (!list)
return (NULL);
if (list->data == obj)
return (list->next);
while (list->next && list->next->data != obj)
list = list->next;
if (!list->next)
return (listconst);
list->next = list->next->next;
return (listconst);
} }