Fixed an error bug

This commit is contained in:
2025-09-02 09:23:46 -05:00
parent f915a4208b
commit 71080682cc
9 changed files with 111 additions and 24 deletions

View File

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

36
src/asset/asset.c Normal file
View File

@@ -0,0 +1,36 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset.h"
#include "assetmanager.h"
#include "util/memory.h"
#include "assert/assert.h"
#include "console/console.h"
errorret_t assetInit(asset_t *asset, const char_t *filename) {
assertNotNull(asset, "Asset cannot be NULL.");
assertNotNull(filename, "Filename cannot be NULL.");
assertTrue(strlen(filename) < FILENAME_MAX, "Filename too long.");
assertTrue(strlen(filename) > 0, "Filename cannot be empty.");
memoryZero(asset, sizeof(asset_t));
memoryCopy(asset->filename, filename, strlen(filename) + 1);
// Initialze the reference list.
refListInit(
&asset->refList,
asset->refListArray,
ASSET_REFERENCE_COUNT_MAX
);
asset->file = zip_fopen(ASSET_MANAGER.zip, filename, 0);
if(asset->file == NULL) errorThrow("Failed to open asset file: %s", filename);
consolePrint("Initialized asset: %s", filename);
errorOk();
}

View File

@@ -7,7 +7,9 @@
#pragma once
#include "assetpaletteimage.h"
#include "error/error.h"
#include "util/reflist.h"
#include <zip.h>
#define ASSET_HEADER_SIZE 3
#define ASSET_REFERENCE_COUNT_MAX 8
@@ -31,4 +33,15 @@ typedef struct {
assetstate_t state;
assettype_t type;
void *data;
} asset_t;
zip_file_t *file;
} asset_t;
/**
* 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);

View File

@@ -43,6 +43,38 @@ errorret_t assetManagerInit(void) {
errorOk();
}
void assetManagerUpdate(void) {
}
errorret_t assetManagerGetAsset(const char_t *filename, asset_t **outAsset) {
assertNotNull(outAsset, "Output asset pointer cannot be null.");
assertNotNull(filename, "Filename cannot be null.");
assertStrLenMin(filename, 1, "Filename cannot be empty.");
assertStrLenMax(filename, FILENAME_MAX - 1, "Filename is too long.");
// Is this asset already in memory?
asset_t *asset = ASSET_MANAGER.assets;
while(asset < &ASSET_MANAGER.assets[ASSET_MANAGER.assetCount]) {
if(stringCompare(asset->filename, filename) == 0) {
*outAsset = asset;
errorOk();
}
++asset;
}
// Asset not in memory, can we load it?
if(ASSET_MANAGER.assetCount >= ASSET_MANAGER_ASSET_COUNT_MAX) {
*outAsset = NULL;
errorThrow("Asset limit reached.");
}
// Pop an asset off the struct
asset = &ASSET_MANAGER.assets[ASSET_MANAGER.assetCount++];
errorChain(assetInit(asset, filename));
errorOk();
}
void assetManagerDispose(void) {
if(ASSET_MANAGER.zip != NULL) {
zip_close(ASSET_MANAGER.zip);

View File

@@ -6,8 +6,6 @@
*/
#pragma once
#include "error/error.h"
#include <zip.h>
#include "display/texture/texture.h"
#include "asset.h"
@@ -46,22 +44,19 @@ extern assetmanager_t ASSET_MANAGER;
errorret_t assetManagerInit(void);
/**
* Gets and requests a lock on a given asset. Locking an asset will prevent it
* from being unloaded until it is unlocked.
*
* @param filename The filename of the asset to lock.
* @param outRef A pointer to store the locked reference ID in.
* @return A pointer to the locked asset, or NULL if the asset could not be
* Update the asset manager. This should be called once per frame.
*/
asset_t * assetManagerLock(const char_t *filename, ref_t *outRef);
void assetManagerUpdate(void);
/**
* Unlocks a previously locked asset reference.
* Get an asset by filename. This will return NULL if the asset does not exist.
* This will not lock the asset, you must do that separately.
*
* @param asset The asset to unlock the reference from.
* @param ref The reference ID to unlock.
* @param filename The filename of the asset to get.
* @param outAsset The output asset pointer.
* @return An error code.
*/
void assetManagerUnlock(const ref_t ref);
errorret_t assetManagerGetAsset(const char_t *filename, asset_t **outAsset);
/**
* Disposes/cleans up the asset system.