From da57b43f45546cefb28a4b485eb7d5ca0d4e1d42 Mon Sep 17 00:00:00 2001
From: AnonymusRaccoon
Date: Fri, 6 Mar 2020 17:34:29 +0100
Subject: [PATCH] Making entity destruction better
---
include/entity.h | 2 +-
include/list.h | 4 +++-
src/entity/entity.c | 6 +++++-
src/scene/scene_destroy.c | 2 +-
src/utility/list.c | 16 ++++++++++++++++
5 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/include/entity.h b/include/entity.h
index 27ed8c3..5f8cfa6 100644
--- a/include/entity.h
+++ b/include/entity.h
@@ -23,7 +23,7 @@ struct gc_entity
void (*remove_component)(const gc_scene *scene, const gc_entity *entity, \
const char *name);
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);
diff --git a/include/list.h b/include/list.h
index 7ebfba5..a3a13de 100644
--- a/include/list.h
+++ b/include/list.h
@@ -16,5 +16,7 @@ struct gc_list
};
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))
\ No newline at end of file
+#define LISTADD(list, obj) (list = list_add(list, obj))
+#define LISTREM(list, obj) (list = list_remove(list, obj))
\ No newline at end of file
diff --git a/src/entity/entity.c b/src/entity/entity.c
index 18fac9a..0e8a1ce 100644
--- a/src/entity/entity.c
+++ b/src/entity/entity.c
@@ -53,11 +53,15 @@ char *entity_serialize(gc_entity *entity, int fd)
return (NULL);
}
-static void destroy(gc_entity *entity)
+static void destroy(gc_entity *entity, gc_scene *scene)
{
gc_component *next = NULL;
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;
cmp->destroy(cmp);
}
diff --git a/src/scene/scene_destroy.c b/src/scene/scene_destroy.c
index 7ef7f9d..da26520 100644
--- a/src/scene/scene_destroy.c
+++ b/src/scene/scene_destroy.c
@@ -32,7 +32,7 @@ void scene_destroy(gc_scene *scene)
for (gc_list *entity = scene->entities; entity; entity = next) {
next = entity->next;
- ((gc_entity *)entity->data)->destroy(entity->data);
+ ((gc_entity *)entity->data)->destroy(entity->data, scene);
free(entity);
}
free_data(scene);
diff --git a/src/utility/list.c b/src/utility/list.c
index da5154a..fd2f7fb 100644
--- a/src/utility/list.c
+++ b/src/utility/list.c
@@ -26,4 +26,20 @@ gc_list *list_add(gc_list *list, void *obj)
list->data = obj;
list->next = NULL;
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);
}
\ No newline at end of file