diff options
-rwxr-xr-x | build.sh | 4 | ||||
-rw-r--r-- | src/loader.c | 64 | ||||
-rw-r--r-- | src/loader.h | 5 | ||||
-rw-r--r-- | src/main.c | 1 |
4 files changed, 21 insertions, 53 deletions
diff --git a/build.sh b/build.sh index aeb3c3d..60ad14c 100755 --- a/build.sh +++ b/build.sh @@ -8,10 +8,10 @@ case $1 in NS=w EXTENSION=exe CFLAGS='-g1 -Os -mwindows' CC='x86_64-w64-mingw32-cc' make ;; leak) - NS=leak CC='cc -fsanitize=leak' CFLAGS='-g -O0' make + NS=leak CFLAGS='-g -Og -fsanitize=leak -llsan' make ;; ub) - NS=ub CC='cc -fsanitize=undefined' CFLAGS='-g -O0' make + NS=ub CFLAGS='-g -Og -fsanitize=undefined -lubsan' make ;; emscripten) NS=em EXTENSION=html CC='/usr/lib/emscripten/emcc' CFLAGS='-g2 -O1 -sUSE_SDL=2 -sUSE_ZLIB' LDFLAGS='-sASYNCIFY --embed-file out/assets.res@./assets.res' make 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) { diff --git a/src/loader.h b/src/loader.h index b16fd28..800ef0b 100644 --- a/src/loader.h +++ b/src/loader.h @@ -16,17 +16,12 @@ struct funs { int (*setfun)(void *const restrict self, char const *const restrict key, char const *const restrict value); }; -void res_init(void); - -void res_init_texture(void); void res_free_texture(void); struct blob res_get_texture(name_T const name); -void res_init_map(void); void res_free_map(void); struct blob res_get_map(name_T const name); -void res_init_collision(void); void res_free_collision(void); struct blob res_get_collision(name_T const name); diff --git a/src/main.c b/src/main.c index 5cb3960..85d0917 100644 --- a/src/main.c +++ b/src/main.c @@ -332,7 +332,6 @@ int main(int const argc, char *const *const argv) { framebuffer = SDL_CreateTexture(renderer, SDL_PIXELTYPE_UNKNOWN, SDL_TEXTUREACCESS_TARGET, WINDOW_WIDTH, WINDOW_HEIGHT); { - res_init(); funs_init(); if (optind == argc) { void *a = util_executableRelativePath("assets.res", *argv, 0); |