From 54ff18bd32c932b47aa77497bc8a6fb6344894b6 Mon Sep 17 00:00:00 2001 From: zlago Date: Thu, 3 Oct 2024 15:50:44 +0200 Subject: refactor entities --- src/loader.c | 100 ++++++++++++++++------------------------------------------- src/loader.h | 15 ++++----- src/main.c | 8 ++--- 3 files changed, 38 insertions(+), 85 deletions(-) diff --git a/src/loader.c b/src/loader.c index 3b39bca..f132b7a 100644 --- a/src/loader.c +++ b/src/loader.c @@ -39,21 +39,13 @@ struct blob_collection { } items[]; } *textures = NULL, *maps = NULL, *collisions = NULL; -struct newfun_collection { +struct fun_collection { size_t count; - struct newfun_item { - struct entity *(*fun)(void); + struct fun_item { + struct funs funs; name_T name; } *items; -} newfuns = {0, NULL}; - -struct setfun_collection { - size_t count; - struct setfun_item { - int (*fun)(struct entity *const restrict self, char const *const restrict key, char const *const restrict value); - name_T name; - } *items; -} setfuns = {0, NULL}; +} funs = {0, NULL}; static int name_compare(name_T a, name_T b) { return strcmp(a, b); @@ -140,20 +132,12 @@ void res_free_collision(void) { collisions = NULL; } -void res_free_newfun(void) { - for (size_t i = 0; i < newfuns.count; i++) { - free(newfuns.items[i].name); - } - free(newfuns.items); - newfuns.count = 0; -} - -void res_free_setfun(void) { - for (size_t i = 0; i < setfuns.count; i++) { - free(setfuns.items[i].name); +void res_free_fun(void) { + for (size_t i = 0; i < funs.count; i++) { + free(funs.items[i].name); } - free(setfuns.items); - setfuns.count = 0; + free(funs.items); + funs.count = 0; } static int res_compare_blobs(void const *a, void const *b) { @@ -161,13 +145,8 @@ static int res_compare_blobs(void const *a, void const *b) { return name_compare(aa->name, bb->name); } -static int res_compare_newfuns(void const *a, void const *b) { - struct newfun_item const *aa = a, *bb = b; - return name_compare(aa->name, bb->name); -} - -static int res_compare_setfuns(void const *a, void const *b) { - struct setfun_item const *aa = a, *bb = b; +static int res_compare_funs(void const *a, void const *b) { + struct fun_item const *aa = a, *bb = b; return name_compare(aa->name, bb->name); } @@ -192,22 +171,13 @@ struct blob res_get_collision(name_T const name) { return res_get_blob(name, collisions); } -struct entity *(*res_get_newfun(name_T const name))(void) { - struct newfun_item new = {.name = name}; - struct newfun_item *dst = bsearch(&new, newfuns.items, newfuns.count, sizeof (struct newfun_item), res_compare_newfuns); - if (dst == NULL) { - return NULL; - } - return dst->fun; -} - -int (*res_get_setfun(name_T const name))(struct entity *const restrict self, char const *const restrict key, char const *const restrict value) { - struct setfun_item new = {.name = name}; - struct setfun_item *dst = bsearch(&new, setfuns.items, setfuns.count, sizeof (struct setfun_item), res_compare_setfuns); +struct funs res_get_fun(name_T const name) { + struct fun_item new = {.name = name}; + struct fun_item *dst = bsearch(&new, funs.items, funs.count, sizeof (struct fun_item), res_compare_funs); if (dst == NULL) { - return NULL; + return (struct funs) {.newfun = NULL, .setfun = NULL}; } - return dst->fun; + return dst->funs; } static struct blob_collection *res_push_blob(struct blob *blob, name_T name, struct blob_collection *collection, void (*freeFunc)(void *)) { @@ -256,39 +226,21 @@ void res_push_collision(struct blob *blob, name_T name) { collisions = res_push_blob(blob, name, collisions, blob_free_func); } -void res_push_newfun(struct entity *(*newfun)(void), name_T name) { - int found; - struct newfun_item new = {.fun = newfun, .name = name}; - struct newfun_item *dst = bsearchinsertposition(&new, newfuns.items, newfuns.count, sizeof (struct newfun_item), res_compare_newfuns, &found); - if (found) { - fprintf(stderr, "warn: name collision: %s\n", new.name); - free(dst->name); - memcpy(dst, &new, sizeof (struct newfun_item)); - } else { - size_t index = dst - newfuns.items; // hack - newfuns.count++; - newfuns.items = realloc(newfuns.items, sizeof (struct newfun_item) * newfuns.count); - dst = newfuns.items + index; // hack for the struct having been reallocated - memmove(dst + 1, dst, sizeof (struct newfun_item) * (newfuns.count - 1 - (dst - newfuns.items))); - memcpy(dst, &new, sizeof (struct newfun_item)); - } -} - -void res_push_setfun(int (*setfun)(struct entity *const restrict self, char const *const restrict key, char const *const restrict value), name_T name) { +void res_push_fun(struct entity *(*newfun)(void), int (*setfun)(struct entity *const restrict self, char const *const restrict key, char const *const restrict value), name_T name) { int found; - struct setfun_item new = {.fun = setfun, .name = name}; - struct setfun_item *dst = bsearchinsertposition(&new, setfuns.items, setfuns.count, sizeof (struct setfun_item), res_compare_setfuns, &found); + struct fun_item new = {.funs = {.newfun = newfun, .setfun = setfun}, .name = name}; + struct fun_item *dst = bsearchinsertposition(&new, funs.items, funs.count, sizeof (struct fun_item), res_compare_funs, &found); if (found) { fprintf(stderr, "warn: name collision: %s\n", new.name); free(dst->name); - memcpy(dst, &new, sizeof (struct setfun_item)); + memcpy(dst, &new, sizeof (struct fun_item)); } else { - size_t index = dst - setfuns.items; // hack - setfuns.count++; - setfuns.items = realloc(setfuns.items, sizeof (struct setfun_item) * setfuns.count); - dst = setfuns.items + index; // hack for the struct having been reallocated - memmove(dst + 1, dst, sizeof (struct setfun_item) * (setfuns.count - 1 - (dst - setfuns.items))); - memcpy(dst, &new, sizeof (struct setfun_item)); + size_t index = dst - funs.items; // hack + funs.count++; + funs.items = realloc(funs.items, sizeof (struct fun_item) * funs.count); + dst = funs.items + index; // hack for the struct having been reallocated + memmove(dst + 1, dst, sizeof (struct fun_item) * (funs.count - 1 - (dst - funs.items))); + memcpy(dst, &new, sizeof (struct fun_item)); } } diff --git a/src/loader.h b/src/loader.h index 016b8b2..61f9e3c 100644 --- a/src/loader.h +++ b/src/loader.h @@ -10,6 +10,11 @@ struct blob { size_t size; }; +struct funs { + struct entity *(*newfun)(void); + int (*setfun)(struct entity *const restrict self, char const *const restrict key, char const *const restrict value); +}; + void res_init(void); void res_init_texture(void); @@ -24,12 +29,8 @@ void res_init_collision(void); void res_free_collision(void); struct blob res_get_collision(name_T const name); -void res_free_newfun(void); -void res_push_newfun(struct entity *(*newfun)(void), name_T name); -struct entity *(*res_get_newfun(name_T const name))(void); - -void res_free_setfun(void); -void res_push_setfun(int (*setfun)(struct entity *const restrict self, char const *const restrict key, char const *const restrict value), name_T name); -int (*res_get_setfun(name_T const name))(struct entity *const restrict self, char const *const restrict key, char const *const restrict value); +void res_free_fun(void); +void res_push_fun(struct entity *(*newfun)(void), int (*setfun)(struct entity *const restrict self, char const *const restrict key, char const *const restrict value), name_T name); +struct funs res_get_fun(name_T const name); int loadResources(char *filename); diff --git a/src/main.c b/src/main.c index 8fc400f..2d3e89d 100644 --- a/src/main.c +++ b/src/main.c @@ -187,11 +187,11 @@ int main(int const argc, char *const *const argv) { } char *name = a; puts(name); - struct entity *(*fun)(void) = res_get_newfun(name); + struct funs fun = res_get_fun(name); a += len + 1; map.size -= len + 1; - if (fun) { - struct entity *entity = fun(); + if (fun.newfun) { + struct entity *entity = fun.newfun(); while (1) { if ((len = strnlen(a, map.size)) == map.size) { return 1; // hack @@ -209,7 +209,7 @@ int main(int const argc, char *const *const argv) { char *value = a; a += len + 1; map.size -= len + 1; - if (res_get_setfun(name)(entity, key, value)) { + if (fun.setfun(entity, key, value)) { return 1; // hack } } -- cgit 1.4.1-2-gfad0