commit asset prog
This commit is contained in:
@@ -6,7 +6,127 @@
|
||||
*/
|
||||
|
||||
#include "asset.h"
|
||||
#include "util/memory.h"
|
||||
#include "console/console.h"
|
||||
#include "util/string.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void assetInit() {
|
||||
#define ASSET_ASSET_FILE "dusk.dsk"
|
||||
|
||||
asset_t ASSET;
|
||||
|
||||
errorret_t assetInit(void) {
|
||||
memoryZero(&ASSET, sizeof(asset_t));
|
||||
|
||||
// Open zip file
|
||||
char_t searchPath[FILENAME_MAX];
|
||||
consolevar_t *var = consoleVarGet("sys_path");
|
||||
const char_t *sysPath = var ? var->value : ".";
|
||||
for(int32_t i = 0; i < ASSET_SEARCH_PATHS_COUNT; i++) {
|
||||
sprintf(
|
||||
searchPath,
|
||||
ASSET_SEARCH_PATHS[i],
|
||||
sysPath,
|
||||
ASSET_ASSET_FILE
|
||||
);
|
||||
|
||||
// Try open
|
||||
ASSET.zip = zip_open(searchPath, ZIP_RDONLY, NULL);
|
||||
if(ASSET.zip == NULL) continue;
|
||||
consolePrint("Opened asset file: %s", searchPath);
|
||||
break;
|
||||
}
|
||||
|
||||
// Did we open the asset?
|
||||
if(ASSET.zip == NULL) errorThrow("Failed to open asset file.");
|
||||
|
||||
// Get "test.palette.dpf" file.
|
||||
zip_file_t *file = zip_fopen(ASSET.zip, "test.palette.dpf", 0);
|
||||
if(file == NULL) errorThrow("Failed to open test.palette.dpf in asset file.");
|
||||
|
||||
// Read it
|
||||
char_t buffer[256];
|
||||
zip_int64_t n = zip_fread(file, buffer, 256);
|
||||
if(n < 0) {
|
||||
zip_fclose(file);
|
||||
errorThrow("Failed to read test.palette.dpf in asset file.");
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void assetLoad(const char_t *filename) {
|
||||
assertNotNull(filename, "Filename cannot be NULL.");
|
||||
assertTrue(strlen(filename) < ASSET_FILENAME_MAX, "Filename too long.");
|
||||
assertTrue(strlen(filename) > 0, "Filename cannot be empty.");
|
||||
|
||||
assertTrue(
|
||||
ASSET.state != ASSET_STATE_LOADING,
|
||||
"Asset system is already loading an asset."
|
||||
);
|
||||
|
||||
// Pass off to the thread to begin loading.
|
||||
ASSET.errorState = ERROR_STATE_INIT;
|
||||
ASSET.state = ASSET_STATE_LOADING;
|
||||
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) {
|
||||
ASSET.state = ASSET_STATE_ERROR;
|
||||
ASSET.error = errorCreate(
|
||||
&ASSET.errorState,
|
||||
"Failed to open asset: %s",
|
||||
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) {
|
||||
zip_fclose(file);
|
||||
ASSET.state = ASSET_STATE_ERROR;
|
||||
ASSET.error = errorCreate(
|
||||
&ASSET.errorState,
|
||||
"Failed to stat asset: %s",
|
||||
filename
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Asset size: %d\n", (int)st.size);
|
||||
if(st.size > sizeof(ASSET.data.palette)) {
|
||||
zip_fclose(file);
|
||||
ASSET.error = errorCreate(
|
||||
&ASSET.errorState,
|
||||
"Asset size mismatch: %s (got %d, expected %d)",
|
||||
filename,
|
||||
(int)st.size,
|
||||
(int)sizeof(ASSET.data.palette)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Read entire file into the asset data.
|
||||
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]
|
||||
);
|
||||
}
|
||||
|
||||
void assetDispose(void) {
|
||||
if(ASSET.zip != NULL) {
|
||||
zip_close(ASSET.zip);
|
||||
ASSET.zip = NULL;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user