Palette loading done.

This commit is contained in:
2025-08-27 19:59:55 -05:00
parent 7a90d2d38f
commit 6c11096fd2
11 changed files with 110 additions and 27 deletions

View File

@@ -55,7 +55,11 @@ errorret_t assetInit(void) {
errorOk();
}
void assetLoad(const char_t *filename) {
void assetLoad(
const char_t *filename,
void (*callback)(void* data),
void *data
) {
assertNotNull(filename, "Filename cannot be NULL.");
assertTrue(strlen(filename) < ASSET_FILENAME_MAX, "Filename too long.");
assertTrue(strlen(filename) > 0, "Filename cannot be empty.");
@@ -74,6 +78,8 @@ void assetLoad(const char_t *filename) {
assertNotNull(expected->extension, "Unknown asset type.");
// Pass off to the thread to begin loading.
ASSET.callback = callback;
ASSET.callbackData = data;
ASSET.errorState = ERROR_STATE_INIT;
ASSET.state = ASSET_STATE_LOADING;
stringCopy(ASSET.filename, filename, ASSET_FILENAME_MAX);
@@ -88,6 +94,7 @@ void assetLoad(const char_t *filename) {
"Failed to open asset: %s",
filename
);
if(ASSET.callback) ASSET.callback(ASSET.callbackData);
return;
}
@@ -101,11 +108,13 @@ void assetLoad(const char_t *filename) {
"Failed to stat asset: %s",
filename
);
if(ASSET.callback) ASSET.callback(ASSET.callbackData);
return;
}
if(st.size > sizeof(ASSET.data.palette)) {
zip_fclose(file);
ASSET.state = ASSET_STATE_ERROR;
ASSET.error = errorCreate(
&ASSET.errorState,
"Asset size mismatch: %s (got %d, expected %d)",
@@ -113,11 +122,12 @@ void assetLoad(const char_t *filename) {
(int)st.size,
(int)sizeof(ASSET.data.palette)
);
if(ASSET.callback) ASSET.callback(ASSET.callbackData);
return;
}
// Read entire file into the asset data.
zip_fread(file, &ASSET.data, st.size);
zip_fread(file, &ASSET.data, st.size);
zip_fclose(file);
ASSET.state = ASSET_STATE_LOADED;
@@ -136,10 +146,16 @@ void assetLoad(const char_t *filename) {
expected->header[1],
expected->header[2]
);
if(ASSET.callback) ASSET.callback(ASSET.callbackData);
return;
}
// Parse the asset.
assertNotNull(expected->parser, "Asset parser cannot be NULL.");
expected->parser();
printf("Loaded asset: %s\n", filename);
if(ASSET.callback) ASSET.callback(ASSET.callbackData);
}
void assetDispose(void) {