Started asset refact

This commit is contained in:
2025-11-04 10:15:19 -06:00
parent 7d46b98310
commit 7c11a7e5bc
25 changed files with 362 additions and 208 deletions

View File

@@ -0,0 +1,11 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
assetalphaimage.c
assetpaletteimage.c
)

View File

@@ -0,0 +1,54 @@
/**
* 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.");
}
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.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;
} assetalphaimage_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

@@ -0,0 +1,61 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
#include "display/palette/palettelist.h"
errorret_t assetPaletteImageLoad(asset_t *asset) {
// Read entire palettized image.
assetpaletteimageraw_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 palette image dimensions.");
}
if(raw.width <= 0 || raw.width > ASSET_PALETTE_IMAGE_WIDTH_MAX) {
errorThrow("Invalid palette image width.");
}
if(raw.height <= 0 || raw.height > ASSET_PALETTE_IMAGE_HEIGHT_MAX) {
errorThrow("Invalid palette image height.");
}
if(raw.paletteIndex >= PALETTE_LIST_COUNT) {
errorThrow("Invalid palette index.");
}
zip_int64_t expecting = (
sizeof(raw.width) +
sizeof(raw.height) +
sizeof(raw.paletteIndex) +
(raw.width * raw.height * sizeof(uint8_t))
);
if(bytesRead != expecting) {
errorThrow("Incorrect palette filesize.");
}
textureInit(
&asset->paletteImage.texture,
(int32_t)raw.width,
(int32_t)raw.height,
TEXTURE_FORMAT_PALETTE,
(texturedata_t){
.palette = {
.palette = raw.paletteIndex,
.data = raw.palette
}
}
);
errorOk();
}
errorret_t assetPaletteImageDispose(asset_t *asset) {
textureDispose(&asset->paletteImage.texture);
errorOk();
}

View File

@@ -0,0 +1,48 @@
/**
* 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.h"
#define ASSET_PALETTE_IMAGE_WIDTH_MAX 128
#define ASSET_PALETTE_IMAGE_HEIGHT_MAX 128
#define ASSET_PALETTE_IMAGE_SIZE_MAX ( \
ASSET_PALETTE_IMAGE_WIDTH_MAX * ASSET_PALETTE_IMAGE_HEIGHT_MAX \
)
typedef struct asset_s asset_t;
#pragma pack(push, 1)
typedef struct {
uint32_t width;
uint32_t height;
uint8_t paletteIndex;
uint8_t palette[ASSET_PALETTE_IMAGE_SIZE_MAX];
} assetpaletteimageraw_t;
#pragma pack(pop)
typedef struct {
texture_t texture;
} assetpaletteimage_t;
/**
* Loads a palette image asset from the given asset structure. The asset must
* be of type ASSET_TYPE_PALETTE_IMAGE and must be loaded.
*
* @param asset The asset to load the palette image from.
* @return An error code.
*/
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);