From 2e0f24e0344ee2bd5bd6e6269f39e804c50d05e0 Mon Sep 17 00:00:00 2001 From: zlago Date: Fri, 11 Oct 2024 16:52:05 +0200 Subject: change the janky vector implementation --- src/loader.c | 64 ++++++++++++++++++------------------------------------------ 1 file changed, 19 insertions(+), 45 deletions(-) (limited to 'src/loader.c') diff --git a/src/loader.c b/src/loader.c index 2382246..54425d9 100644 --- a/src/loader.c +++ b/src/loader.c @@ -36,8 +36,8 @@ struct blob_collection { struct blob_item { struct blob blob; name_T name; - } items[]; -} *textures = NULL, *maps = NULL, *collisions = NULL; + } *items; +} textures = {0, NULL}, maps = {0, NULL}, collisions = {0, NULL}; struct fun_collection { size_t count; @@ -81,37 +81,13 @@ void *bsearchinsertposition(const void *key, void *base, size_t nmemb, size_t si } } -static struct blob_collection *res_init_blobs(void) { - struct blob_collection *collection = malloc(sizeof (struct blob_collection)); - collection->count = 0; - return collection; -} - -void res_init_texture(void) { - textures = res_init_blobs(); -} - -void res_init_map(void) { - maps = res_init_blobs(); -} - -void res_init_collision(void) { - collisions = res_init_blobs(); -} - -void res_init(void) { - res_init_texture(); - res_init_map(); - res_init_collision(); -} - void res_free_texture(void) { - for (size_t i = 0; i < textures->count; i++) { - SDL_DestroyTexture(textures->items[i].blob.data); - free(textures->items[i].name); + for (size_t i = 0; i < textures.count; i++) { + SDL_DestroyTexture(textures.items[i].blob.data); + free(textures.items[i].name); } - free(textures); - textures = NULL; + free(textures.items); + textures.count = 0, textures.items = NULL; } static void res_free_blobs(struct blob_collection *collection) { @@ -119,17 +95,16 @@ static void res_free_blobs(struct blob_collection *collection) { free(collection->items[i].blob.data); free(collection->items[i].name); } - free(collection); + free(collection->items); + collection->count = 0, collection->items = NULL; } void res_free_map(void) { - res_free_blobs(maps); - maps = NULL; + res_free_blobs(&maps); } void res_free_collision(void) { - res_free_blobs(maps); - collisions = NULL; + res_free_blobs(&collisions); } void res_free_fun(void) { @@ -160,15 +135,15 @@ static struct blob res_get_blob(name_T const name, struct blob_collection *colle } struct blob res_get_texture(name_T const name) { - return res_get_blob(name, textures); + return res_get_blob(name, &textures); } struct blob res_get_map(name_T const name) { - return res_get_blob(name, maps); + return res_get_blob(name, &maps); } struct blob res_get_collision(name_T const name) { - return res_get_blob(name, collisions); + return res_get_blob(name, &collisions); } struct funs res_get_fun(name_T const name) { @@ -180,7 +155,7 @@ struct funs res_get_fun(name_T const name) { return dst->funs; } -static struct blob_collection *res_push_blob(struct blob *blob, name_T name, struct blob_collection *collection, void (*freeFunc)(void *)) { +static void res_push_blob(struct blob *blob, name_T name, struct blob_collection *collection, void (*freeFunc)(void *)) { int found; struct blob_item new = {.blob = *blob, .name = name}; struct blob_item *dst = bsearchinsertposition(&new, collection->items, collection->count, sizeof (struct blob_item), res_compare_blobs, &found); @@ -191,7 +166,7 @@ static struct blob_collection *res_push_blob(struct blob *blob, name_T name, str } else { size_t index = dst - collection->items; // hack collection->count++; - collection = realloc(collection, sizeof (struct blob_collection) + sizeof (struct blob_item) * collection->count); + collection->items = realloc(collection->items, sizeof (struct blob_item) * collection->count); dst = collection->items + index; // hack for the struct having been reallocated #if 0 fprintf(stderr, "info: no collision: %s\n", new.name); @@ -199,7 +174,6 @@ static struct blob_collection *res_push_blob(struct blob *blob, name_T name, str memmove(dst + 1, dst, sizeof (struct blob_item) * (collection->count - 1 - (dst - collection->items))); memcpy(dst, &new, sizeof (struct blob_item)); } - return collection; } static void texture_free_func(void *ptr) { @@ -209,7 +183,7 @@ static void texture_free_func(void *ptr) { } void res_push_texture(struct blob *blob, name_T name) { - textures = res_push_blob(blob, name, textures, texture_free_func); + res_push_blob(blob, name, &textures, texture_free_func); } static void blob_free_func(void *ptr) { @@ -219,11 +193,11 @@ static void blob_free_func(void *ptr) { } void res_push_map(struct blob *blob, name_T name) { - maps = res_push_blob(blob, name, maps, blob_free_func); + res_push_blob(blob, name, &maps, blob_free_func); } void res_push_collision(struct blob *blob, name_T name) { - collisions = res_push_blob(blob, name, collisions, blob_free_func); + res_push_blob(blob, name, &collisions, blob_free_func); } void res_push_fun(void *(*newfun)(struct entities *entities), int (*setfun)(void *const restrict self, char const *const restrict key, char const *const restrict value), name_T name) { -- cgit 1.4.1-2-gfad0