From 9a0e1945a13a44f6fcbb08a9c962a462a42ce3a7 Mon Sep 17 00:00:00 2001 From: iWas-Coder Date: Mon, 27 May 2024 19:17:05 +0200 Subject: [PATCH] Abstracted away img loading and unloading (#31) --- tools/skap/include/skap_idx_image.h | 4 ++++ tools/skap/src/skap.c | 34 +++++++++-------------------- tools/skap/src/skap_idx_image.c | 14 ++++++++++++ 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/tools/skap/include/skap_idx_image.h b/tools/skap/include/skap_idx_image.h index b6f368d..f173ee3 100644 --- a/tools/skap/include/skap_idx_image.h +++ b/tools/skap/include/skap_idx_image.h @@ -39,6 +39,10 @@ typedef struct { usz blob_size; } skap_idx_image; +void skap_idx_image_loadall(Image *imgs, const char **img_paths, usz size); + +void skap_idx_image_unloadall(Image *imgs, usz size); + skap_idx_image skap_idx_image_create(const char *name, Image *img); u8 skap_idx_image_append(FILE *fd, skap_idx_image *i); diff --git a/tools/skap/src/skap.c b/tools/skap/src/skap.c index 583c844..583eb07 100644 --- a/tools/skap/src/skap.c +++ b/tools/skap/src/skap.c @@ -19,7 +19,6 @@ */ -#include #include #include #include @@ -27,43 +26,30 @@ #define ARRLEN(x) (sizeof((x)) / sizeof((x)[0])) #define return_defer(x) do { result = 1; goto defer; } while(0) -static const char *imgs_paths[] = { +static const char *img_paths[] = { "assets/icon.png", "assets/images/loading-controls.png" }; -static Image imgs[ARRLEN(imgs_paths)] = {0}; -static skap_idx_image img_idxs[ARRLEN(imgs_paths)] = {0}; -static usz img_idx_locs[ARRLEN(imgs_paths)] = {0}; - -static void load_imgs(void) { - for (usz i = 0; i < ARRLEN(imgs_paths); ++i) { - imgs[i] = LoadImage(imgs_paths[i]); - assert(imgs[i].data); - } -} - -static void unload_imgs(void) { - for (usz i = 0; i < ARRLEN(imgs_paths); ++i) { - UnloadImage(imgs[i]); - } -} +static Image imgs[ARRLEN(img_paths)] = {0}; +static skap_idx_image img_idxs[ARRLEN(img_paths)] = {0}; +static usz img_idx_locs[ARRLEN(img_paths)] = {0}; int main(void) { int result = 0; - load_imgs(); + skap_idx_image_loadall(imgs, img_paths, ARRLEN(img_paths)); FILE *fd = skap_file_create(); skap_header header = skap_header_create(); if (!skap_header_append(fd, &header)) { return_defer(1); } - for (usz i = 0; i < ARRLEN(imgs_paths); ++i) { - img_idxs[i] = skap_idx_image_create(imgs_paths[i], &imgs[i]); + for (usz i = 0; i < ARRLEN(img_paths); ++i) { + img_idxs[i] = skap_idx_image_create(img_paths[i], &imgs[i]); img_idx_locs[i] = ftell(fd); if (!skap_idx_image_append(fd, &img_idxs[i])) { return_defer(1); } } - for (usz i = 0; i < ARRLEN(imgs_paths); ++i) { + for (usz i = 0; i < ARRLEN(img_paths); ++i) { usz blob_loc = ftell(fd); skap_idx_image_link_blob(&img_idxs[i], blob_loc, (usz) imgs[i].width * imgs[i].height); fseek(fd, img_idx_locs[i], SEEK_SET); @@ -71,12 +57,12 @@ int main(void) { return_defer(1); } fseek(fd, blob_loc, SEEK_SET); - if (!skap_idx_image_blob_append(fd, imgs_paths[i], &imgs[i])) { + if (!skap_idx_image_blob_append(fd, img_paths[i], &imgs[i])) { return_defer(1); } } defer: skap_file_destroy(fd); - unload_imgs(); + skap_idx_image_unloadall(imgs, ARRLEN(img_paths)); return result; } diff --git a/tools/skap/src/skap_idx_image.c b/tools/skap/src/skap_idx_image.c index c4c3ddb..ebee9eb 100644 --- a/tools/skap/src/skap_idx_image.c +++ b/tools/skap/src/skap_idx_image.c @@ -19,8 +19,22 @@ */ +#include #include +void skap_idx_image_loadall(Image *imgs, const char **img_paths, usz size) { + for (usz i = 0; i < size; ++i) { + imgs[i] = LoadImage(img_paths[i]); + assert(imgs[i].data); + } +} + +void skap_idx_image_unloadall(Image *imgs, usz size) { + for (usz i = 0; i < size; ++i) { + UnloadImage(imgs[i]); + } +} + skap_idx_image skap_idx_image_create(const char *name, Image *img) { return (skap_idx_image) { .metadata = (skap_idx_image_md) {