Asset refactor

This commit is contained in:
2026-04-03 12:56:04 -05:00
parent 0885da8d44
commit da1a5a3f1b
59 changed files with 1032 additions and 726 deletions

View File

@@ -0,0 +1,13 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
assettexture.c
assettileset.c
assetlanguage.c
assetscript.c
)

View File

@@ -0,0 +1,45 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
#include "assert/assert.h"
#include "locale/localemanager.h"
errorret_t assetLanguageHandler(assetcustom_t custom) {
assertNotNull(custom.zipFile, "Custom asset zip file cannot be NULL");
assertNotNull(custom.output, "Custom asset output cannot be NULL");
assetlanguage_t *lang = (assetlanguage_t *)custom.output;
errorChain(assetLanguageInit(lang, custom.zipFile));
errorOk();
}
errorret_t assetLanguageInit(
assetlanguage_t *lang,
zip_file_t *zipFile
) {
errorThrow("Language asset initialization is not yet implemented.");
}
errorret_t assetLanguageRead(
assetlanguage_t *lang,
const uint32_t key,
char_t *buffer,
const uint32_t bufferSize,
uint32_t *outLength
) {
errorThrow("Language string reading is not yet implemented.");
}
void assetLanguageDispose(assetlanguage_t *lang) {
assertNotNull(lang, "Language asset cannot be NULL");
if(lang->zip) {
zip_fclose(lang->zip);
}
}

View File

@@ -0,0 +1,61 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "duskdefs.h"
#include <zip.h>
typedef struct {
zip_file_t *zip;
zip_int64_t chunksOffset;
} assetlanguage_t;
typedef struct assetcustom_s assetcustom_t;
/**
* Receiving function from the asset manager to handle language assets.
*
* @param custom Custom asset loading data.
* @return Error code.
*/
errorret_t assetLanguageHandler(assetcustom_t custom);
/**
* Initializes a language asset and loads the header data into memory.
*
* @param lang Language asset to initialize.
* @param zipFile Zip file handle for the language asset.
* @return Error code.
*/
errorret_t assetLanguageInit(assetlanguage_t *lang, zip_file_t *zipFile);
/**
* Reads a string from the language asset into the provided buffer.
*
* @param lang Language asset to read from.
* @param key Language key to read.
* @param buffer Buffer to read the string into.
* @param bufferSize Size of the provided buffer.
* @param outLength Pointer to store the length of the string read.
* @return Error code.
*/
errorret_t assetLanguageRead(
assetlanguage_t *lang,
const uint32_t key,
char_t *buffer,
const uint32_t bufferSize,
uint32_t *outLength
);
/**
* Disposes of language asset resources.
*
* @param custom Custom asset loading data.
* @return Error code.
*/
void assetLanguageDispose(assetlanguage_t *lang);

View File

@@ -0,0 +1,58 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
#include "assert/assert.h"
errorret_t assetScriptHandler(assetcustom_t custom) {
assertNotNull(custom.zipFile, "Custom asset zip file cannot be NULL");
assertNotNull(custom.output, "Custom asset output cannot be NULL");
assetscript_t *script = (assetscript_t *)custom.output;
errorChain(assetScriptInit(script, custom.zipFile));
errorOk();
}
errorret_t assetScriptInit(
assetscript_t *script,
zip_file_t *zipFile
) {
assertNotNull(script, "Script asset cannot be NULL");
assertNotNull(zipFile, "Zip file cannot be NULL");
// We now own the zip file handle.
script->zip = zipFile;
errorOk();
}
const char_t * assetScriptReader(lua_State* lState, void* data, size_t* size) {
assetscript_t *script = (assetscript_t *)data;
zip_int64_t bytesRead = zip_fread(
script->zip, script->buffer, sizeof(script->buffer)
);
if(bytesRead < 0) {
*size = 0;
return NULL;
}
*size = (size_t)bytesRead;
return script->buffer;
}
errorret_t assetScriptDispose(assetscript_t *script) {
assertNotNull(script, "Script asset cannot be NULL");
if(script->zip != NULL) {
zip_fclose(script->zip);
script->zip = NULL;
}
errorOk();
}

View File

@@ -0,0 +1,57 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "duskdefs.h"
#include <zip.h>
#include "script/scriptcontext.h"
#define ASSET_SCRIPT_BUFFER_SIZE 1024
typedef struct assetscript_s {
zip_file_t *zip;
char_t buffer[ASSET_SCRIPT_BUFFER_SIZE];
} assetscript_t;
typedef struct assetcustom_s assetcustom_t;
/**
* Receiving function from the asset manager to handle script assets.
*
* @param custom Custom asset loading data.
* @return Error code.
*/
errorret_t assetScriptHandler(assetcustom_t custom);
/**
* Initializes a script asset.
*
* @param script Script asset to initialize.
* @param zipFile Zip file handle for the script asset.
* @return Error code.
*/
errorret_t assetScriptInit(assetscript_t *script, zip_file_t *zipFile);
/**
* Reader function for Lua to read script data from the asset.
*
* @param L Lua state.
* @param data Pointer to the assetscript_t structure.
* @param size Pointer to store the size of the read data.
* @return Pointer to the read data buffer.
*/
const char_t * assetScriptReader(lua_State* L, void* data, size_t* size);
/**
* Disposes of a script asset, freeing any allocated resources.
*
* @param script Script asset to dispose of.
* @return Error code.
*/
errorret_t assetScriptDispose(assetscript_t *script);

View File

@@ -0,0 +1,78 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "assettexture.h"
#include "asset/assettype.h"
#include "assert/assert.h"
#include "display/texture/texture.h"
#include "util/endian.h"
errorret_t assetTextureLoad(assetentire_t entire) {
assertNotNull(entire.data, "Data pointer cannot be NULL.");
assertNotNull(entire.output, "Output pointer cannot be NULL.");
assettexture_t *assetData = (assettexture_t *)entire.data;
texture_t *texture = (texture_t *)entire.output;
// Read header and version (first 4 bytes)
if(
assetData->header[0] != 'D' ||
assetData->header[1] != 'T' ||
assetData->header[2] != 'X'
) {
errorThrow("Invalid texture header");
}
// Version (can only be 1 atm)
if(assetData->version != 0x01) {
errorThrow("Unsupported texture version");
}
// Fix endian
assetData->width = endianLittleToHost32(assetData->width);
assetData->height = endianLittleToHost32(assetData->height);
// Check dimensions.
if(
assetData->width == 0 || assetData->width > ASSET_TEXTURE_WIDTH_MAX ||
assetData->height == 0 || assetData->height > ASSET_TEXTURE_HEIGHT_MAX
) {
errorThrow("Invalid texture dimensions");
}
// Validate format
textureformat_t format;
texturedata_t data;
switch(assetData->type) {
case 0x00: // RGBA8888
format = TEXTURE_FORMAT_RGBA;
data.rgbaColors = (color_t *)assetData->data;
break;
// case 0x01:
// format = TEXTURE_FORMAT_RGB;
// break;
// case 0x02:
// format = TEXTURE_FORMAT_RGB565;
// break;
// case 0x03:
// format = TEXTURE_FORMAT_RGB5A3;
// break;
default:
errorThrow("Unsupported texture format");
}
errorChain(textureInit(
texture, assetData->width, assetData->height, format, data
));
errorOk();
}

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "display/color.h"
#define ASSET_TEXTURE_WIDTH_MAX 2048
#define ASSET_TEXTURE_HEIGHT_MAX 2048
#define ASSET_TEXTURE_SIZE_MAX ( \
ASSET_TEXTURE_WIDTH_MAX * ASSET_TEXTURE_HEIGHT_MAX \
)
typedef struct assetentire_s assetentire_t;
#pragma pack(push, 1)
typedef struct {
char_t header[3];
uint8_t version;
uint8_t type;
uint32_t width;
uint32_t height;
uint8_t data[ASSET_TEXTURE_SIZE_MAX * sizeof(color4b_t)];
} assettexture_t;
#pragma pack(pop)
/**
* Loads a palettized texture from the given data pointer into the output
* texture.
*
* @param data Pointer to the raw assettexture_t data.
* @param output Pointer to the texture_t to load the image into.
* @return An error code.
*/
errorret_t assetTextureLoad(assetentire_t entire);

View File

@@ -0,0 +1,70 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
#include "assert/assert.h"
#include "display/texture/tileset.h"
#include "util/memory.h"
#include "util/endian.h"
errorret_t assetTilesetLoad(assetentire_t entire) {
assertNotNull(entire.data, "Asset data cannot be null");
assertNotNull(entire.output, "Asset output cannot be null");
assettileset_t *tilesetData = (assettileset_t *)entire.data;
tileset_t *tileset = (tileset_t *)entire.output;
if(
tilesetData->header[0] != 'D' ||
tilesetData->header[1] != 'T' ||
tilesetData->header[2] != 'F'
) {
errorThrow("Invalid tileset header");
}
if(tilesetData->version != 0x00) {
errorThrow("Unsupported tileset version");
}
// Fix endianness
tilesetData->tileWidth = endianLittleToHost16(tilesetData->tileWidth);
tilesetData->tileHeight = endianLittleToHost16(tilesetData->tileHeight);
tilesetData->columnCount = endianLittleToHost16(tilesetData->columnCount);
tilesetData->rowCount = endianLittleToHost16(tilesetData->rowCount);
tilesetData->right = endianLittleToHost16(tilesetData->right);
tilesetData->bottom = endianLittleToHost16(tilesetData->bottom);
if(tilesetData->tileWidth == 0) {
errorThrow("Tile width cannot be 0");
}
if(tilesetData->tileHeight == 0) {
errorThrow("Tile height cannot be 0");
}
if(tilesetData->columnCount == 0) {
errorThrow("Column count cannot be 0");
}
if(tilesetData->rowCount == 0) {
errorThrow("Row count cannot be 0");
}
tilesetData->u0 = endianLittleToHostFloat(tilesetData->u0);
tilesetData->v0 = endianLittleToHostFloat(tilesetData->v0);
if(tilesetData->v0 < 0.0f || tilesetData->v0 > 1.0f) {
errorThrow("Invalid v0 value in tileset");
}
// Setup tileset
tileset->tileWidth = tilesetData->tileWidth;
tileset->tileHeight = tilesetData->tileHeight;
tileset->tileCount = tilesetData->columnCount * tilesetData->rowCount;
tileset->columns = tilesetData->columnCount;
tileset->rows = tilesetData->rowCount;
tileset->uv[0] = tilesetData->u0;
tileset->uv[1] = tilesetData->v0;
errorOk();
}

View File

@@ -0,0 +1,32 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#pragma pack(push, 1)
typedef struct {
char_t header[3];
uint8_t version;
uint16_t tileWidth;
uint16_t tileHeight;
uint16_t columnCount;
uint16_t rowCount;
uint16_t right;
uint16_t bottom;
float_t u0;
float_t v0;
} assettileset_t;
#pragma pack(pop)
/**
* Loads a tileset from the given data pointer into the output tileset.
*
* @param entire Data received from the asset loader system.
* @return An error code.
*/
errorret_t assetTilesetLoad(assetentire_t entire);