commit asset prog

This commit is contained in:
2025-08-26 16:23:08 -05:00
parent 8af2f044ed
commit a543bc7c00
11 changed files with 244 additions and 126 deletions

View File

@@ -7,7 +7,6 @@
target_sources(${DUSK_TARGET_NAME}
PRIVATE
asset.c
assetsystem.c
)
# Compile definitions

View File

@@ -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;
}
}

View File

@@ -7,19 +7,75 @@
#pragma once
#include "dusk.h"
#include "assetpalette.h"
#include "assettileset.h"
#include "error/error.h"
#include <zip.h>
#define ASSET_FILENAME_MAX 256
#define ASSET_COUNT_MAX 128
#define ASSET_FILENAME_MAX 128
typedef struct asset_s {
#if ASSET_TYPE == wad
#else
#error "Unsupported ASSET_TYPE"
#endif
typedef struct {
char_t header[3];
union {
assetpalette_t palette;
assettileset_t tileset;
};
} assetdata_t;
typedef enum {
ASSET_STATE_NONE,
ASSET_STATE_LOADING,
ASSET_STATE_LOADED,
ASSET_STATE_ERROR
} assetstate_t;
typedef struct {
int32_t nothing;
zip_t *zip;
// thread_t thread;
const char_t *filename;
zip_file_t *fileHandle;
errorstate_t errorState;
errorret_t error;
assetstate_t state;
char_t filename[ASSET_FILENAME_MAX];
assetdata_t data;
} asset_t;
void assetInit(
const char_t *filename,
);
static const char_t ASSET_SEARCH_PATHS[][FILENAME_MAX] = {
"%s/%s",
"./%s",
"../%s",
"../../%s",
"data/%s",
"../data/%s",
};
#define ASSET_SEARCH_PATHS_COUNT (\
sizeof(ASSET_SEARCH_PATHS) / FILENAME_MAX\
)
extern asset_t ASSET;
/**
* Initializes the asset system.
*/
errorret_t assetInit(void);
/**
* Gets an asset by filename, does not load it.
*
* @param filename The filename of the asset to get.
* @return The asset.
*/
void assetLoad(const char_t *filename);
/**
* Disposes/cleans up the asset system.
*/
void assetDispose(void);

View File

@@ -12,7 +12,6 @@
#define ASSET_PALETTE_COLOR_COUNT_MAX 256
typedef struct {
char_t header[3];
int32_t colorCount;
color4b_t colors[ASSET_PALETTE_COLOR_COUNT_MAX];
} assetpalette_t;

View File

@@ -1,74 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "assetsystem.h"
#include "util/memory.h"
#include "console/console.h"
#define ASSET_SYSTEM_ASSET_FILE "dusk.dsk"
const char_t ASSET_SYSTEM_SEARCH_PATHS[][FILENAME_MAX] = {
"%s/%s",
"./%s",
"../%s",
"../../%s",
"data/%s",
"../data/%s",
};
#define ASSET_SYSTEM_SEARCH_PATHS_COUNT (\
sizeof(ASSET_SYSTEM_SEARCH_PATHS) / FILENAME_MAX\
)
assetsystem_t ASSET_SYSTEM;
errorret_t assetSystemInit(void) {
memoryZero(&ASSET_SYSTEM, sizeof(assetsystem_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_SYSTEM_SEARCH_PATHS_COUNT; i++) {
sprintf(
searchPath,
ASSET_SYSTEM_SEARCH_PATHS[i],
sysPath,
ASSET_SYSTEM_ASSET_FILE
);
// Try open
ASSET_SYSTEM.zip = zip_open(searchPath, ZIP_RDONLY, NULL);
if(ASSET_SYSTEM.zip == NULL) continue;
consolePrint("Opened asset file: %s", searchPath);
break;
}
// Did we open the asset?
if(ASSET_SYSTEM.zip == NULL) errorThrow("Failed to open asset file.");
// Get "test.palette.dpf" file.
zip_file_t *file = zip_fopen(ASSET_SYSTEM.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 assetSystemDispose(void) {
if(ASSET_SYSTEM.zip != NULL) {
zip_close(ASSET_SYSTEM.zip);
ASSET_SYSTEM.zip = NULL;
}
}

View File

@@ -1,32 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "asset.h"
#include "error/error.h"
#if ASSET_TYPE == wad
#else
#error "Unsupported ASSET_TYPE"
#endif
typedef struct {
int32_t nothing;
zip_t *zip;
} assetsystem_t;
extern assetsystem_t ASSET_SYSTEM;
/**
* Initializes the asset system.
*/
errorret_t assetSystemInit(void);
/**
* Disposes/cleans up the asset system.
*/
void assetSystemDispose(void);

View File

@@ -11,7 +11,6 @@
typedef struct assetimage_s assetimage_t;
typedef struct {
char_t header[3];
int32_t tileWidth;
int32_t tileHeight;
int32_t tileCount;