mirror of
https://github.com/zoriya/Gamacon.git
synced 2026-06-09 22:50:53 +00:00
Making entity destruction better
This commit is contained in:
+1
-1
@@ -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
@@ -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
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user