summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorzlago2024-10-11 16:52:05 +0200
committerzlago2024-10-11 16:52:05 +0200
commit2e0f24e0344ee2bd5bd6e6269f39e804c50d05e0 (patch)
treea272507341766d315975aa558efbe597b737e0f5 /src
parenta1b7736a6b1dae8fcdf36e12857b61506342d71d (diff)
change the janky vector implementation
Diffstat (limited to 'src')
-rw-r--r--src/loader.c64
-rw-r--r--src/loader.h5
-rw-r--r--src/main.c1
3 files changed, 19 insertions, 51 deletions
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);