Mostly nuking old system
This commit is contained in:
@@ -232,6 +232,20 @@ errorret_t assetLoad(const char_t *filename, void *output) {
|
||||
assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
|
||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||
|
||||
// Determine the asset type by reading the extension
|
||||
const assettypedef_t *def = NULL;
|
||||
for(uint_fast8_t i = 0; i < ASSET_TYPE_COUNT; i++) {
|
||||
const assettypedef_t *cmp = &ASSET_TYPE_DEFINITIONS[i];
|
||||
assertNotNull(cmp, "Asset type definition cannot be NULL.");
|
||||
assertNotNull(cmp->extension, "Asset type definition has NULL extension.");
|
||||
if(!stringEndsWithCaseInsensitive(filename, cmp->extension)) continue;
|
||||
def = cmp;
|
||||
break;
|
||||
}
|
||||
if(def == NULL) {
|
||||
errorThrow("Unknown asset type for file: %s", filename);
|
||||
}
|
||||
|
||||
// Get file size of the asset.
|
||||
zip_stat_t st;
|
||||
zip_stat_init(&st);
|
||||
@@ -241,8 +255,8 @@ errorret_t assetLoad(const char_t *filename, void *output) {
|
||||
|
||||
// Minimum file size.
|
||||
zip_int64_t fileSize = (zip_int64_t)st.size;
|
||||
if(fileSize < sizeof(assetheader_t)) {
|
||||
errorThrow("Asset file too small to contain header: %s", filename);
|
||||
if(fileSize <= 0) {
|
||||
errorThrow("Asset file is empty: %s", filename);
|
||||
}
|
||||
|
||||
// Try to open the file
|
||||
@@ -250,61 +264,19 @@ errorret_t assetLoad(const char_t *filename, void *output) {
|
||||
if(file == NULL) {
|
||||
errorThrow("Failed to open asset file: %s", filename);
|
||||
}
|
||||
|
||||
// Read the header.
|
||||
zip_int64_t bytesRemaining = fileSize;
|
||||
assetheader_t header;
|
||||
memoryZero(&header, sizeof(assetheader_t));
|
||||
zip_int64_t bytesRead = zip_fread(
|
||||
file,
|
||||
&header,
|
||||
(zip_uint64_t)sizeof(assetheader_t)
|
||||
);
|
||||
if((size_t)bytesRead != sizeof(assetheader_t)) {
|
||||
zip_fclose(file);
|
||||
errorThrow("Failed to read asset header for: %s", filename);
|
||||
}
|
||||
bytesRemaining -= (zip_uint64_t)bytesRead;
|
||||
|
||||
assertTrue(sizeof(assetheader_t) == ASSET_HEADER_SIZE, "Asset header size mismatch.");
|
||||
assertTrue(bytesRead == ASSET_HEADER_SIZE, "Asset header read size mismatch.");
|
||||
|
||||
// Find the asset type based on the header
|
||||
const assettypedef_t *def = NULL;
|
||||
for(uint_fast8_t i = 0; i < ASSET_TYPE_COUNT; i++) {
|
||||
const assettypedef_t *cmp = &ASSET_TYPE_DEFINITIONS[i];
|
||||
if(cmp->header == NULL) continue;
|
||||
|
||||
// strcmp didn't work because it's a fixed char_t[3] I think, or maybe
|
||||
// because of the packed struct?
|
||||
bool_t match = true;
|
||||
for(size_t h = 0; h < ASSET_HEADER_SIZE; h++) {
|
||||
if(header.header[h] == cmp->header[h]) continue;
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
if(!match) continue;
|
||||
|
||||
def = cmp;
|
||||
break;
|
||||
}
|
||||
if(def == NULL) {
|
||||
zip_fclose(file);
|
||||
errorThrow("Unknown asset type for file: %s", filename);
|
||||
}
|
||||
|
||||
// We found the asset type, now load the asset data
|
||||
// Load the asset data
|
||||
switch(def->loadStrategy) {
|
||||
case ASSET_LOAD_STRAT_ENTIRE:
|
||||
assertNotNull(def->entire, "Asset load function cannot be NULL.");
|
||||
|
||||
// Must have more to read
|
||||
if(bytesRemaining <= 0) {
|
||||
if(fileSize <= 0) {
|
||||
zip_fclose(file);
|
||||
errorThrow("No data remaining to read for asset: %s", filename);
|
||||
}
|
||||
|
||||
if(bytesRemaining > def->dataSize) {
|
||||
if(fileSize > def->dataSize) {
|
||||
zip_fclose(file);
|
||||
errorThrow(
|
||||
"Asset file has too much data remaining after header: %s",
|
||||
@@ -313,20 +285,20 @@ errorret_t assetLoad(const char_t *filename, void *output) {
|
||||
}
|
||||
|
||||
// Create space to read the entire asset data
|
||||
void *data = memoryAllocate(bytesRemaining);
|
||||
void *data = memoryAllocate(fileSize);
|
||||
if(!data) {
|
||||
zip_fclose(file);
|
||||
errorThrow("Failed to allocate memory for asset data of file: %s", filename);
|
||||
}
|
||||
|
||||
// Read in the asset data.
|
||||
bytesRead = zip_fread(file, data, bytesRemaining);
|
||||
if(bytesRead == 0 || bytesRead > bytesRemaining) {
|
||||
zip_int64_t bytesRead = zip_fread(file, data, fileSize);
|
||||
if(bytesRead == 0 || bytesRead > fileSize) {
|
||||
memoryFree(data);
|
||||
zip_fclose(file);
|
||||
errorThrow("Failed to read asset data for file: %s", filename);
|
||||
}
|
||||
bytesRemaining -= bytesRead;
|
||||
fileSize -= bytesRead;
|
||||
|
||||
// Close the file now we have the data
|
||||
zip_fclose(file);
|
||||
|
||||
@@ -72,12 +72,6 @@ static const char_t *ASSET_SEARCH_PATHS[] = {
|
||||
NULL
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
char_t header[ASSET_HEADER_SIZE];
|
||||
} assetheader_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
typedef struct {
|
||||
zip_t *zip;
|
||||
char_t systemPath[FILENAME_MAX];
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "type/assetpaletteimage.h"
|
||||
#include "type/assetalphaimage.h"
|
||||
#include "type/assettexture.h"
|
||||
#include "type/assetlanguage.h"
|
||||
#include "type/assetscript.h"
|
||||
#include "type/assetmap.h"
|
||||
@@ -17,8 +16,7 @@
|
||||
typedef enum {
|
||||
ASSET_TYPE_NULL,
|
||||
|
||||
ASSET_TYPE_PALETTE_IMAGE,
|
||||
ASSET_TYPE_ALPHA_IMAGE,
|
||||
ASSET_TYPE_TEXTURE,
|
||||
ASSET_TYPE_LANGUAGE,
|
||||
ASSET_TYPE_SCRIPT,
|
||||
ASSET_TYPE_MAP,
|
||||
@@ -38,7 +36,7 @@ typedef struct assetcustom_s {
|
||||
} assetcustom_t;
|
||||
|
||||
typedef struct {
|
||||
const char_t *header;
|
||||
const char_t *extension;
|
||||
const size_t dataSize;
|
||||
const assetloadstrat_t loadStrategy;
|
||||
union {
|
||||
@@ -52,41 +50,34 @@ static const assettypedef_t ASSET_TYPE_DEFINITIONS[ASSET_TYPE_COUNT] = {
|
||||
0
|
||||
},
|
||||
|
||||
[ASSET_TYPE_PALETTE_IMAGE] = {
|
||||
.header = "DPI",
|
||||
[ASSET_TYPE_TEXTURE] = {
|
||||
.extension = "dpt",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_ENTIRE,
|
||||
.dataSize = sizeof(assetpaletteimage_t),
|
||||
.entire = assetPaletteImageLoad
|
||||
},
|
||||
|
||||
[ASSET_TYPE_ALPHA_IMAGE] = {
|
||||
.header = "DAI",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_ENTIRE,
|
||||
.dataSize = sizeof(assetalphaimage_t),
|
||||
.entire = assetAlphaImageLoad
|
||||
.dataSize = sizeof(assettexture_t),
|
||||
.entire = assetTextureLoad
|
||||
},
|
||||
|
||||
[ASSET_TYPE_LANGUAGE] = {
|
||||
.header = "DLF",
|
||||
.extension = "DLF",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
.custom = assetLanguageHandler
|
||||
},
|
||||
|
||||
[ASSET_TYPE_SCRIPT] = {
|
||||
.header = "DSF",
|
||||
.extension = "lua",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
.custom = assetScriptHandler
|
||||
},
|
||||
|
||||
[ASSET_TYPE_MAP] = {
|
||||
.header = "DMF",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
.custom = assetMapHandler
|
||||
},
|
||||
// [ASSET_TYPE_MAP] = {
|
||||
// .extension = "DMF",
|
||||
// .loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
// .custom = assetMapHandler
|
||||
// },
|
||||
|
||||
[ASSET_TYPE_MAP_CHUNK] = {
|
||||
.header = "DMC",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
.custom = assetMapChunkHandler
|
||||
},
|
||||
// [ASSET_TYPE_MAP_CHUNK] = {
|
||||
// .extension = "DMC",
|
||||
// .loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
// .custom = assetMapChunkHandler
|
||||
// },
|
||||
};
|
||||
@@ -6,8 +6,7 @@
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
assetalphaimage.c
|
||||
assetpaletteimage.c
|
||||
assettexture.c
|
||||
assetlanguage.c
|
||||
assetscript.c
|
||||
assetmap.c
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assetalphaimage.h"
|
||||
#include "assert/assert.h"
|
||||
#include "display/texture.h"
|
||||
|
||||
#include "debug/debug.h"
|
||||
|
||||
errorret_t assetAlphaImageLoad(void *data, void *output) {
|
||||
assertNotNull(data, "Data pointer cannot be NULL.");
|
||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||
|
||||
assetalphaimage_t *dataPtr = (assetalphaimage_t *)data;
|
||||
texture_t *outputPtr = (texture_t *)output;
|
||||
|
||||
// Fix endian
|
||||
dataPtr->width = le32toh(dataPtr->width);
|
||||
dataPtr->height = le32toh(dataPtr->height);
|
||||
|
||||
textureInit(
|
||||
outputPtr,
|
||||
dataPtr->width,
|
||||
dataPtr->height,
|
||||
TEXTURE_FORMAT_ALPHA,
|
||||
(texturedata_t){ .alpha = { .data = dataPtr->pixels } }
|
||||
);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
@@ -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 "error/error.h"
|
||||
|
||||
#define ASSET_ALPHA_IMAGE_WIDTH_MAX 256
|
||||
#define ASSET_ALPHA_IMAGE_HEIGHT_MAX 256
|
||||
#define ASSET_ALPHA_IMAGE_SIZE_MAX ( \
|
||||
ASSET_ALPHA_IMAGE_WIDTH_MAX * ASSET_ALPHA_IMAGE_HEIGHT_MAX \
|
||||
)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint8_t pixels[ASSET_ALPHA_IMAGE_SIZE_MAX];
|
||||
} assetalphaimage_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
/**
|
||||
* Loads an alpha image asset from the given asset structure. The asset must
|
||||
* be of type ASSET_TYPE_ALPHA_IMAGE and must be loaded.
|
||||
*
|
||||
* @param asset The asset to load the alpha image from.
|
||||
* @return An error code.
|
||||
*/
|
||||
errorret_t assetAlphaImageLoad(void *data, void *output);
|
||||
@@ -6,7 +6,6 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "locale/language/keys.h"
|
||||
#include "error/error.h"
|
||||
#include "duskdefs.h"
|
||||
#include <zip.h>
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
#define ASSET_PALETTE_IMAGE_WIDTH_MAX 256
|
||||
#define ASSET_PALETTE_IMAGE_HEIGHT_MAX 256
|
||||
#define ASSET_PALETTE_IMAGE_SIZE_MAX ( \
|
||||
ASSET_PALETTE_IMAGE_WIDTH_MAX * ASSET_PALETTE_IMAGE_HEIGHT_MAX \
|
||||
)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint8_t paletteIndex;
|
||||
uint8_t palette[ASSET_PALETTE_IMAGE_SIZE_MAX];
|
||||
} assetpaletteimage_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
/**
|
||||
* Loads a palette image asset from the given data pointer into the output
|
||||
* texture.
|
||||
*
|
||||
* @param data Pointer to the raw assetpaletteimage_t data.
|
||||
* @param output Pointer to the texture_t to load the image into.
|
||||
* @return An error code.
|
||||
*/
|
||||
errorret_t assetPaletteImageLoad(void *data, void *output);
|
||||
@@ -5,37 +5,38 @@
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assetpaletteimage.h"
|
||||
#include "assettexture.h"
|
||||
#include "assert/assert.h"
|
||||
#include "display/texture.h"
|
||||
#include "display/palette/palettelist.h"
|
||||
|
||||
errorret_t assetPaletteImageLoad(void *data, void *output) {
|
||||
assertNotNull(data, "Data pointer cannot be NULL.");
|
||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||
|
||||
assetpaletteimage_t *assetData = (assetpaletteimage_t *)data;
|
||||
assettexture_t *assetData = (assettexture_t *)data;
|
||||
texture_t *texture = (texture_t *)output;
|
||||
|
||||
// Fix endian
|
||||
assetData->width = le32toh(assetData->width);
|
||||
assetData->height = le32toh(assetData->height);
|
||||
|
||||
const palette_t *pal = PALETTE_LIST[assetData->paletteIndex];
|
||||
assertNotNull(pal, "Palette index is out of bounds");
|
||||
|
||||
textureInit(
|
||||
texture,
|
||||
assetData->width,
|
||||
assetData->height,
|
||||
TEXTURE_FORMAT_PALETTE,
|
||||
(texturedata_t){
|
||||
.palette = {
|
||||
.palette = pal,
|
||||
.data = assetData->palette
|
||||
}
|
||||
}
|
||||
);
|
||||
errorThrow("Hello World\n");
|
||||
|
||||
errorOk();
|
||||
// const palette_t *pal = PALETTE_LIST[assetData->paletteIndex];
|
||||
// assertNotNull(pal, "Palette index is out of bounds");
|
||||
|
||||
// textureInit(
|
||||
// texture,
|
||||
// assetData->width,
|
||||
// assetData->height,
|
||||
// TEXTURE_FORMAT_PALETTE,
|
||||
// (texturedata_t){
|
||||
// .palette = {
|
||||
// .palette = pal,
|
||||
// .data = assetData->palette
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
|
||||
// errorOk();
|
||||
}
|
||||
34
src/asset/type/assettexture.h
Normal file
34
src/asset/type/assettexture.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
#define ASSET_TEXTURE_WIDTH_MAX 256
|
||||
#define ASSET_TEXTURE_HEIGHT_MAX 256
|
||||
#define ASSET_TEXTURE_SIZE_MAX ( \
|
||||
ASSET_TEXTURE_WIDTH_MAX * ASSET_TEXTURE_HEIGHT_MAX \
|
||||
)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint8_t paletteIndex;
|
||||
uint8_t palette[ASSET_TEXTURE_SIZE_MAX];
|
||||
} 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(void *data, void *output);
|
||||
@@ -6,4 +6,5 @@
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
palette.c
|
||||
)
|
||||
8
src/display/palette/palette.c
Normal file
8
src/display/palette/palette.c
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "palette.h"
|
||||
@@ -8,7 +8,11 @@
|
||||
#pragma once
|
||||
#include "display/color.h"
|
||||
|
||||
#define PALETTE_COUNT 4
|
||||
|
||||
typedef struct {
|
||||
const uint8_t colorCount;
|
||||
const color_t *colors;
|
||||
} palette_t;
|
||||
} palette_t;
|
||||
|
||||
extern palette_t PALETTES[PALETTE_COUNT];
|
||||
@@ -12,14 +12,15 @@
|
||||
#include "asset/asset.h"
|
||||
|
||||
texture_t DEFAULT_FONT_TEXTURE;
|
||||
tileset_t DEFAULT_FONT_TILESET;
|
||||
|
||||
errorret_t textInit(void) {
|
||||
errorChain(assetLoad(DEFAULT_FONT_TILESET.image, &DEFAULT_FONT_TEXTURE));
|
||||
// errorChain(assetLoad(DEFAULT_FONT_TILESET.image, &DEFAULT_FONT_TEXTURE));
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void textDispose(void) {
|
||||
textureDispose(&DEFAULT_FONT_TEXTURE);
|
||||
// textureDispose(&DEFAULT_FONT_TEXTURE);
|
||||
}
|
||||
|
||||
void textDrawChar(
|
||||
|
||||
@@ -9,12 +9,11 @@
|
||||
#include "asset/asset.h"
|
||||
#include "display/texture.h"
|
||||
#include "display/tileset/tileset.h"
|
||||
#include "display/tileset/tileset_minogram.h"
|
||||
|
||||
#define TEXT_CHAR_START '!'
|
||||
|
||||
extern texture_t DEFAULT_FONT_TEXTURE;
|
||||
#define DEFAULT_FONT_TILESET TILESET_MINOGRAM
|
||||
// extern texture_t DEFAULT_FONT_TEXTURE;
|
||||
// #define DEFAULT_FONT_TILESET TILESET_MINOGRAM
|
||||
|
||||
/**
|
||||
* Initializes the text system.
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "util/memory.h"
|
||||
#include "util/math.h"
|
||||
#include "util/string.h"
|
||||
#include "display/palette/palettelist.h"
|
||||
#include "debug/debug.h"
|
||||
|
||||
const texture_t *TEXTURE_BOUND = NULL;
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
*/
|
||||
|
||||
#include "tileset.h"
|
||||
#include "display/tileset/tilesetlist.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/string.h"
|
||||
|
||||
@@ -34,13 +33,4 @@ void tilesetPositionGetUV(
|
||||
outUV[1] = ((float_t)row) * tileset->uv[1];
|
||||
outUV[2] = outUV[0] + tileset->uv[0];
|
||||
outUV[3] = outUV[1] + tileset->uv[1];
|
||||
}
|
||||
|
||||
const tileset_t * tilesetGetByName(const char_t *name) {
|
||||
assertStrLenMin(name, 1, "Tileset name cannot be empty");
|
||||
for(uint32_t i = 0; i < TILESET_LIST_COUNT; i++) {
|
||||
if(stringCompare(TILESET_LIST[i]->name, name) != 0) continue;
|
||||
return TILESET_LIST[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -45,12 +45,4 @@ void tilesetPositionGetUV(
|
||||
const uint16_t column,
|
||||
const uint16_t row,
|
||||
vec4 outUV
|
||||
);
|
||||
|
||||
/**
|
||||
* Gets a tileset by its name.
|
||||
*
|
||||
* @param name The name of the tileset to get.
|
||||
* @return The tileset with the given name, or NULL if not found.
|
||||
*/
|
||||
const tileset_t* tilesetGetByName(const char_t *name);
|
||||
);
|
||||
13
src/locale/locale.h
Normal file
13
src/locale/locale.h
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
typedef struct {
|
||||
void *nothing;
|
||||
} dusklocale_t;
|
||||
Reference in New Issue
Block a user