Render text working.

This commit is contained in:
2025-09-02 22:47:07 -05:00
parent 87f18d0e13
commit 1af2b8f47b
19 changed files with 340 additions and 157 deletions

View File

@@ -12,7 +12,12 @@
#include "console/console.h"
assetdef_t ASSET_DEFINITIONS[ASSET_TYPE_COUNT] = {
[ASSET_TYPE_PALETTE_IMAGE] = { "DPI", assetPaletteImageLoad },
[ASSET_TYPE_PALETTE_IMAGE] = {
"DPI", assetPaletteImageLoad, assetPaletteImageDispose
},
[ASSET_TYPE_ALPHA_IMAGE] = {
"DAI", assetAlphaImageLoad, assetAlphaImageDispose
},
};
errorret_t assetInit(asset_t *asset, const char_t *filename) {
@@ -50,7 +55,6 @@ ref_t assetLock(asset_t *asset) {
void assetUnlock(asset_t *asset, const ref_t ref) {
assertNotNull(asset, "Asset cannot be NULL.");
assertTrue(ref > 0, "Reference must be greater than 0.");
// Just unlock the reference in the reference list.
refListUnlock(&asset->refList, ref);
@@ -121,9 +125,16 @@ errorret_t assetLoad(asset_t *asset) {
errorOk();
}
void assetDispose(asset_t *asset) {
errorret_t assetDispose(asset_t *asset) {
assertNotNull(asset, "Asset cannot be NULL.");
if(asset->state == ASSET_STATE_LOADED) {
// Dispose of the asset based on its type.
assetdef_t *def = &ASSET_DEFINITIONS[asset->type];
if(def->dispose) errorChain(def->dispose(asset));
asset->state = ASSET_STATE_NOT_LOADED;
}
if(asset->file) {
zip_fclose(asset->file);
asset->file = NULL;

View File

@@ -11,6 +11,7 @@
#include <zip.h>
#include "asset/type/assetpaletteimage.h"
#include "asset/type/assetalphaimage.h"
#define ASSET_HEADER_SIZE 3
#define ASSET_REFERENCE_COUNT_MAX 8
@@ -29,6 +30,7 @@ typedef enum {
typedef enum {
ASSET_TYPE_UNKNOWN,
ASSET_TYPE_PALETTE_IMAGE,
ASSET_TYPE_ALPHA_IMAGE,
ASSET_TYPE_COUNT
} assettype_t;
@@ -43,12 +45,14 @@ typedef struct asset_s {
union {
assetpaletteimage_t paletteImage;
assetalphaimager_t alphaImage;
};
} asset_t;
typedef struct {
const char_t header[ASSET_HEADER_SIZE + 1];
errorret_t (*load)(asset_t *asset);
errorret_t (*dispose)(asset_t *asset);
} assetdef_t;
extern assetdef_t ASSET_DEFINITIONS[ASSET_TYPE_COUNT];
@@ -97,4 +101,4 @@ errorret_t assetLoad(asset_t *asset);
*
* @param asset The asset to dispose of.
*/
void assetDispose(asset_t *asset);
errorret_t assetDispose(asset_t *asset);

View File

@@ -87,7 +87,26 @@ errorret_t assetManagerGetAsset(const char_t *filename, asset_t **outAsset) {
errorOk();
}
errorret_t assetManagerLoadAsset(
const char_t *filename,
asset_t **outAsset,
ref_t *outRef
) {
assertNotNull(outRef, "Output reference pointer cannot be null.");
errorChain(assetManagerGetAsset(filename, outAsset));
ref_t ref = assetLock(*outAsset);
errorChain(assetLoad(*outAsset));
*outRef = ref;
errorOk();
}
void assetManagerDispose(void) {
asset_t *asset = ASSET_MANAGER.assets;
while(asset < &ASSET_MANAGER.assets[ASSET_MANAGER.assetCount]) {
assetDispose(asset);
++asset;
}
if(ASSET_MANAGER.zip != NULL) {
zip_close(ASSET_MANAGER.zip);
ASSET_MANAGER.zip = NULL;

View File

@@ -6,7 +6,6 @@
*/
#pragma once
#include "display/texture/texture.h"
#include "asset.h"
#define ASSET_MANAGER_ASSET_COUNT_MAX 256
@@ -59,6 +58,21 @@ void assetManagerUpdate(void);
*/
errorret_t assetManagerGetAsset(const char_t *filename, asset_t **outAsset);
/**
* Gets, locks and loads an asset. This is all blocking so only use if you
* really need to.
*
* @param filename The filename of the asset to get.
* @param outAsset The output asset pointer.
* @param outRef The output asset reference pointer.
* @return An error code if something goes wrong.
*/
errorret_t assetManagerLoadAsset(
const char_t *filename,
asset_t **outAsset,
ref_t *outRef
);
/**
* Disposes/cleans up the asset system.
*/

View File

@@ -6,5 +6,6 @@
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
assetalphaimage.c
assetpaletteimage.c
)

View File

@@ -0,0 +1,55 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
errorret_t assetAlphaImageLoad(asset_t *asset) {
// Read entire alpha image.
assetalphaimageraw_t raw;
zip_int64_t bytesRead = zip_fread(asset->file, &raw, sizeof(raw));
if(bytesRead < sizeof(raw.width) + sizeof(raw.height)) {
errorThrow("Failed to read alpha image dimensions.");
}
if(raw.width <= 0 || raw.width > ASSET_ALPHA_IMAGE_WIDTH_MAX) {
errorThrow("Invalid alpha image width.");
}
if(raw.height <= 0 || raw.height > ASSET_ALPHA_IMAGE_HEIGHT_MAX) {
errorThrow("Invalid alpha image height.");
}
printf("Alpha image dimensions: %ux%u\n", raw.width, raw.height);
zip_int64_t expecting = (
sizeof(raw.width) +
sizeof(raw.height) +
(raw.width * raw.height * sizeof(uint8_t))
);
if(bytesRead != expecting) {
errorThrow("Incorrect alpha filesize.");
}
textureInit(
&asset->alphaImage.texture,
(int32_t)raw.width,
(int32_t)raw.height,
TEXTURE_FORMAT_ALPHA,
(texturedata_t){
.alpha = {
.data = raw.pixels
}
}
);
errorOk();
}
errorret_t assetAlphaImageDispose(asset_t *asset) {
textureDispose(&asset->alphaImage.texture);
errorOk();
}

View File

@@ -0,0 +1,47 @@
/**
* 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/texture/texture.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 \
)
typedef struct asset_s asset_t;
#pragma pack(push, 1)
typedef struct {
uint32_t width;
uint32_t height;
uint8_t pixels[ASSET_ALPHA_IMAGE_SIZE_MAX];
} assetalphaimageraw_t;
#pragma pack(pop)
typedef struct {
texture_t texture;
} assetalphaimager_t;
/**
* 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(asset_t *asset);
/**
* Disposes of an alpha image asset, freeing any allocated resources.
*
* @param asset The asset to dispose of.
* @return An error code.
*/
errorret_t assetAlphaImageDispose(asset_t *asset);

View File

@@ -53,5 +53,10 @@ errorret_t assetPaletteImageLoad(asset_t *asset) {
}
);
errorOk();
}
errorret_t assetPaletteImageDispose(asset_t *asset) {
textureDispose(&asset->paletteImage.texture);
errorOk();
}

View File

@@ -37,4 +37,12 @@ typedef struct {
* @param asset The asset to load the palette image from.
* @return An error code.
*/
errorret_t assetPaletteImageLoad(asset_t *asset);
errorret_t assetPaletteImageLoad(asset_t *asset);
/**
* Disposes of a palette image asset, freeing any allocated resources.
*
* @param asset The asset to dispose of.
* @return An error code.
*/
errorret_t assetPaletteImageDispose(asset_t *asset);