Skip to content

Commit

Permalink
Abstracted away img loading and unloading (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed May 27, 2024
1 parent 61b176b commit 9a0e194
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
4 changes: 4 additions & 0 deletions tools/skap/include/skap_idx_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 10 additions & 24 deletions tools/skap/src/skap.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,50 @@
*/


#include <assert.h>
#include <skap_file.h>
#include <skap_header.h>
#include <skap_idx_image.h>

#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);
if (!skap_idx_image_append(fd, &img_idxs[i])) {
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;
}
14 changes: 14 additions & 0 deletions tools/skap/src/skap_idx_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@
*/


#include <assert.h>
#include <skap_idx_image.h>

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) {
Expand Down

0 comments on commit 9a0e194

Please sign in to comment.