Skip to content

Commit

Permalink
Implemented image assets archiving to skap (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed May 24, 2024
1 parent f39bce8 commit 5d1e2a1
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ docs/www/node_modules/

sparky
sparky_debug
assets.skap
assets.skap.old
19 changes: 13 additions & 6 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ define MCONF_CPPFLAGS
-I $(TOOLS_DIR)/$(MCONF_NAME)/$(HDR_DIR)
endef
define SKAP_CPPFLAGS
$(DISABLE_ASSERTS_OPTS) \
$(DISABLE_ASSERTS_OPTS) \
-isystem $(RAYLIB_SRC_DIR) \
-I $(TOOLS_DIR)/$(SKAP_NAME)/$(HDR_DIR)
endef
define RAYLIB_CFLAGS
Expand Down Expand Up @@ -276,7 +277,10 @@ endef
define SKAP_LDFLAGS
$(BUILDID_OPTS) \
$(STRIP_OPTS) \
$(RELEASE_OPTS)
$(RELEASE_OPTS) \
-L $(BUILD_DIR) \
-lraylib \
-lm
endef

# Build output
Expand All @@ -292,7 +296,8 @@ TEST_OUT = $(TEST_BUILD_DIR)/$(TEST_ENGINE_NAME)
EDITOR_OUT = $(EXTRAS_BUILD_DIR)/$(EDITOR_NAME)
MCONF_OUT = $(MCONF_BUILD_DIR)/$(MCONF_NAME)
ifndef D
SKAP_OUT = $(SKAP_BUILD_DIR)/$(SKAP_NAME)
SKAP_OUT = $(SKAP_BUILD_DIR)/$(SKAP_NAME)
SKAP_ASSETPACK_OUT = $(ASSETS_DIR).$(SKAP_NAME)
endif


Expand Down Expand Up @@ -367,7 +372,7 @@ $(MCONF_BUILD_DIR)/%.o: $(TOOLS_DIR)/$(MCONF_NAME)/$(SRC_DIR)/%.c
@echo " $(PPO_HOSTCC) $@"
$(Q)$(CC) $(MCONF_CPPFLAGS) $(MCONF_CFLAGS) -c -MD $< -o $@

game: $(OUT) $(SKAP_OUT)
game: $(OUT) $(SKAP_ASSETPACK_OUT)
@printf "INFO: \033[1;35m$(OUT) is ready ($(FULL_VERSION))\033[0m\n"

$(OUT): $(RAYLIB_OUT) $(LUA_OUT) $(LAUNCHER_OUT) $(OBJS)
Expand Down Expand Up @@ -403,10 +408,12 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@echo " $(PPO_CC) $@"
$(Q)$(CC) $(CPPFLAGS) $(CFLAGS) -c -MD $< -o $@

$(SKAP_OUT): $(SKAP_OBJS) $(SKAP_ASSETS)
$(SKAP_ASSETPACK_OUT): $(SKAP_OUT) $(SKAP_ASSETS)
$(Q)./$<

$(SKAP_OUT): $(SKAP_OBJS)
@echo " $(PPO_HOSTLD) $@"
$(Q)$(CC) $(SKAP_OBJS) $(SKAP_LDFLAGS) -o $@
$(Q)./$@

$(SKAP_BUILD_DIR)/%.o: $(TOOLS_DIR)/$(SKAP_NAME)/$(SRC_DIR)/%.c
@echo " $(PPO_HOSTCC) $@"
Expand Down
5 changes: 5 additions & 0 deletions tools/skap/include/skap_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#pragma once

#include <stdint.h>
#include <stddef.h>

#define SKAP_FILENAME "assets.skap"

typedef uint8_t u8;
typedef uint8_t u32;
typedef uint64_t u64;
typedef size_t usz;
28 changes: 28 additions & 0 deletions tools/skap/include/skap_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <stdio.h>

FILE *skap_file_create(void);

void skap_file_destroy(FILE *fd);
2 changes: 2 additions & 0 deletions tools/skap/include/skap_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ typedef struct {
} skap_header;

skap_header skap_header_create(void);

u8 skap_header_append(FILE *fd, skap_header *h);
46 changes: 46 additions & 0 deletions tools/skap/include/skap_idx_image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <stdio.h>
#include <raylib.h>
#include <skap_defines.h>

typedef struct {
const char *name;
u32 width;
u32 height;
u32 mipmaps;
u8 format;
} skap_idx_image_md;

typedef struct {
skap_idx_image_md metadata;
usz blob_offset;
usz blob_size;
} skap_idx_image;

skap_idx_image skap_idx_image_create(const char *name, Image *img);

u8 skap_idx_image_append(FILE *fd, skap_idx_image *i);

void skap_idx_image_link_blob(skap_idx_image *i, usz blob_offset, usz blob_size);
56 changes: 52 additions & 4 deletions tools/skap/src/skap.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,61 @@
*/


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

#define ASSETPATH(s) "assets/" s
#define ARRLEN(x) (sizeof((x)) / sizeof((x)[0]))

static const char *imgs_paths[] = {
ASSETPATH("icon.png")
};
static Image imgs[ARRLEN(imgs_paths)] = {0};
static skap_idx_image img_idxs[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]);
}
}

int main(void) {
load_imgs();
FILE *fd = skap_file_create();
skap_header header = skap_header_create();
printf("header.signature => %s\n", header.signature);
printf("header.fmt_ver => %hhu\n", header.fmt_ver);
printf("header.build_ver => %ld\n", header.build_ver);
if (!skap_header_append(fd, &header)) {
skap_file_destroy(fd);
unload_imgs();
return 1;
}
for (usz i = 0; i < ARRLEN(imgs_paths); ++i) {
img_idxs[i] = skap_idx_image_create(imgs_paths[i], &imgs[i]);
if (!skap_idx_image_append(fd, &img_idxs[i])) {
skap_file_destroy(fd);
unload_imgs();
return 1;
}
}
for (usz i = 0; i < ARRLEN(imgs_paths); ++i) {
skap_idx_image_link_blob(&img_idxs[i], ftell(fd), imgs[i].width * imgs[i].height);
printf(" WRITE %s >> " SKAP_FILENAME "\n", imgs_paths[i]);
if (fwrite(imgs[i].data, sizeof(Color), imgs[i].width * imgs[i].height, fd) != (usz) (imgs[i].width * imgs[i].height)) {
fprintf(stderr, "ERROR: unable to write to file\n");
skap_file_destroy(fd);
unload_imgs();
return 1;
}
}
skap_file_destroy(fd);
unload_imgs();
return 0;
}
46 changes: 46 additions & 0 deletions tools/skap/src/skap_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <skap_file.h>
#include <skap_defines.h>

FILE *skap_file_create(void) {
if (!access(SKAP_FILENAME, F_OK)) {
printf(" RENAME " SKAP_FILENAME " -> " SKAP_FILENAME ".old\n");
if (rename(SKAP_FILENAME, SKAP_FILENAME ".old") == -1) {
fprintf(stderr, "ERROR: rename(2) unable to rename the file (%s)\n", strerror(errno));
return 0;
}
}
else printf(" TOUCH " SKAP_FILENAME "\n");
return fopen(SKAP_FILENAME, "ab");
}

void skap_file_destroy(FILE *fd) {
if (!fd) {
printf("WARNING: skap_file_destroy :: `fd` is not a valid pointer, skipping destruction\n");
return;
}
fclose(fd);
}
14 changes: 14 additions & 0 deletions tools/skap/src/skap_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


#include <time.h>
#include <stdio.h>
#include <skap_header.h>

static u64 concat_num(u64 x, u64 y) {
Expand Down Expand Up @@ -52,3 +53,16 @@ skap_header skap_header_create(void) {
.build_ver = compute_build_ver()
};
}

u8 skap_header_append(FILE *fd, skap_header *h) {
if (!fd || !h) {
fprintf(stderr, "ERROR: skap_header_append :: `fd` and `h` need to be valid pointers\n");
return 0;
}
printf(" WRITE skap_header >> " SKAP_FILENAME "\n");
if (fwrite(h, sizeof(skap_header), 1, fd) != 1) {
fprintf(stderr, "ERROR: skap_header_append :: unable to write to file\n");
return 0;
}
return 1;
}
59 changes: 59 additions & 0 deletions tools/skap/src/skap_idx_image.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <skap_idx_image.h>

skap_idx_image skap_idx_image_create(const char *name, Image *img) {
return (skap_idx_image) {
.metadata = (skap_idx_image_md) {
.name = name,
.width = img->width,
.height = img->height,
.mipmaps = img->mipmaps,
.format = img->format
},
.blob_offset = 0,
.blob_size = 0
};
}

u8 skap_idx_image_append(FILE *fd, skap_idx_image *i) {
if (!fd || !i) {
fprintf(stderr, "ERROR: skap_idx_image_append :: `fd` and `i` need to be valid pointers\n");
return 0;
}
printf(" WRITE skap_idx_image(%s) >> " SKAP_FILENAME "\n", i->metadata.name);
if (fwrite(i, sizeof(skap_idx_image), 1, fd) != 1) {
fprintf(stderr, "ERROR: skap_idx_image_append :: unable to write to file\n");
return 0;
}
return 1;
}

void skap_idx_image_link_blob(skap_idx_image *i, usz blob_offset, usz blob_size) {
if (!i || !blob_offset || !blob_size) {
printf("WARNING: skap_idx_image_link_blob :: args need to be valid, skipping linkage\n");
return;
}
printf(" LINK skap_idx_image(%s) -> {%zu, %zu}\n", i->metadata.name, blob_offset, blob_size);
i->blob_offset = blob_offset;
i->blob_size = blob_size;
}

0 comments on commit 5d1e2a1

Please sign in to comment.