This commit is contained in:
2025-08-27 07:46:11 -05:00
parent a543bc7c00
commit 7a90d2d38f
3 changed files with 43 additions and 13 deletions

View File

@@ -64,6 +64,14 @@ void assetLoad(const char_t *filename) {
ASSET.state != ASSET_STATE_LOADING,
"Asset system is already loading an asset."
);
// Determine the asset map type we are expecting based on the extension.
const assetmap_t *expected = &ASSET_MAP[0];
do {
if(stringEndsWith(filename, expected->extension)) break;
expected++;
} while(expected->extension);
assertNotNull(expected->extension, "Unknown asset type.");
// Pass off to the thread to begin loading.
ASSET.errorState = ERROR_STATE_INIT;
@@ -71,8 +79,6 @@ void assetLoad(const char_t *filename) {
stringCopy(ASSET.filename, filename, ASSET_FILENAME_MAX);
memoryZero(&ASSET.data, sizeof(ASSET.data));
consolePrint("Loading asset: %s", filename);
// For the sake of testing I'm going to do blocking load, fun stuff.
zip_file_t *file = zip_fopen(ASSET.zip, filename, 0);
if(file == NULL) {
@@ -85,8 +91,6 @@ void assetLoad(const char_t *filename) {
return;
}
consolePrint("Opened asset: %s", filename);
// Determine length of the file (uncompressed)
struct zip_stat st;
if(zip_stat(ASSET.zip, filename, 0, &st) != 0) {
@@ -100,7 +104,6 @@ void assetLoad(const char_t *filename) {
return;
}
printf("Asset size: %d\n", (int)st.size);
if(st.size > sizeof(ASSET.data.palette)) {
zip_fclose(file);
ASSET.error = errorCreate(
@@ -117,11 +120,26 @@ void assetLoad(const char_t *filename) {
zip_fread(file, &ASSET.data, st.size);
zip_fclose(file);
ASSET.state = ASSET_STATE_LOADED;
consolePrint("First 3 bytes: %c%c%c\n",
ASSET.data.header[0],
ASSET.data.header[1],
ASSET.data.header[2]
);
if(memoryCompare(
ASSET.data.header, expected->header, ASSET_HEADER_LENGTH
) != 0) {
ASSET.state = ASSET_STATE_ERROR;
ASSET.error = errorCreate(
&ASSET.errorState,
"Asset header mismatch: %s (got %c%c%c, expected %c%c%c)",
filename,
ASSET.data.header[0],
ASSET.data.header[1],
ASSET.data.header[2],
expected->header[0],
expected->header[1],
expected->header[2]
);
return;
}
printf("Loaded asset: %s\n", filename);
}
void assetDispose(void) {

View File

@@ -14,6 +14,7 @@
#define ASSET_COUNT_MAX 128
#define ASSET_FILENAME_MAX 128
#define ASSET_HEADER_LENGTH 3
#if ASSET_TYPE == wad
#else
@@ -21,7 +22,7 @@
#endif
typedef struct {
char_t header[3];
char_t header[ASSET_HEADER_LENGTH];
union {
assetpalette_t palette;
assettileset_t tileset;
@@ -47,6 +48,11 @@ typedef struct {
assetdata_t data;
} asset_t;
typedef struct {
const char_t *header;
const char_t *extension;
} assetmap_t;
static const char_t ASSET_SEARCH_PATHS[][FILENAME_MAX] = {
"%s/%s",
"./%s",
@@ -56,6 +62,12 @@ static const char_t ASSET_SEARCH_PATHS[][FILENAME_MAX] = {
"../data/%s",
};
static const assetmap_t ASSET_MAP[] = {
{ "DPF", "test.palette.dpf" },
{ "DPT", "test.tileset.dpt" },
{ NULL, NULL }
};
#define ASSET_SEARCH_PATHS_COUNT (\
sizeof(ASSET_SEARCH_PATHS) / FILENAME_MAX\
)

View File

@@ -29,8 +29,8 @@ errorret_t engineInit(void) {
errorChain(assetInit());
errorChain(displayInit());
assetLoad("test.palette.dp123123");
// assetLoad("test.palette.dpf");
// assetLoad("test.palette.dp123123");
assetLoad("test.palette.dpf");
if(ASSET.state == ASSET_STATE_ERROR) errorChain(ASSET.error);
sceneTestAdd();