/** * 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 "util/reflist.h" #include #include "asset/type/assetpaletteimage.h" #include "asset/type/assetalphaimage.h" #define ASSET_HEADER_SIZE 3 #define ASSET_REFERENCE_COUNT_MAX 8 typedef struct { void (*load)(); } assetcallback_t; typedef enum { ASSET_STATE_NOT_LOADED, ASSET_STATE_LOADING, ASSET_STATE_LOADED, ASSET_STATE_ERROR, } assetstate_t; typedef enum { ASSET_TYPE_UNKNOWN, ASSET_TYPE_PALETTE_IMAGE, ASSET_TYPE_ALPHA_IMAGE, ASSET_TYPE_COUNT } assettype_t; typedef struct asset_s { char_t filename[FILENAME_MAX]; ref_t refListArray[ASSET_REFERENCE_COUNT_MAX]; reflist_t refList; assetstate_t state; assettype_t type; zip_file_t *file; union { assetpaletteimage_t paletteImage; assetalphaimage_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]; /** * Initializes an asset structure. This should be called by the asset manager * only. * * @param asset The asset structure to initialize. * @param filename The filename of the asset. * @return An error code. */ errorret_t assetInit(asset_t *asset, const char_t *filename); /** * Requests a lock on the given asset. This will increase the reference count * of the asset, and prevent it from being unloaded until all locks are * released. * * @param asset The asset to lock. * @return A unique reference ID for the lock. */ ref_t assetLock(asset_t *asset); /** * Releases a lock on the given asset. This will decrease the reference count * of the asset, and allow it to be unloaded if there are no more locks. * * @param asset The asset to unlock. * @param ref The reference ID of the lock to release. */ void assetUnlock(asset_t *asset, const ref_t ref); /** * Permission has been granted to load the asset data from disk. This should * only be called by the asset manager. * * @param asset The asset to load. * @return An error code. */ errorret_t assetLoad(asset_t *asset); /** * Disposes of the asset, freeing any allocated memory and closing any open * file handles. This should only be called by the asset manager. * * @param asset The asset to dispose of. */ errorret_t assetDispose(asset_t *asset);