Fixed an error bug
This commit is contained in:
@@ -6,5 +6,6 @@
|
|||||||
# Sources
|
# Sources
|
||||||
target_sources(${DUSK_TARGET_NAME}
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
|
asset.c
|
||||||
assetmanager.c
|
assetmanager.c
|
||||||
)
|
)
|
36
src/asset/asset.c
Normal file
36
src/asset/asset.c
Normal 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();
|
||||||
|
}
|
@@ -7,7 +7,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "assetpaletteimage.h"
|
#include "assetpaletteimage.h"
|
||||||
|
#include "error/error.h"
|
||||||
#include "util/reflist.h"
|
#include "util/reflist.h"
|
||||||
|
#include <zip.h>
|
||||||
|
|
||||||
#define ASSET_HEADER_SIZE 3
|
#define ASSET_HEADER_SIZE 3
|
||||||
#define ASSET_REFERENCE_COUNT_MAX 8
|
#define ASSET_REFERENCE_COUNT_MAX 8
|
||||||
@@ -31,4 +33,15 @@ typedef struct {
|
|||||||
assetstate_t state;
|
assetstate_t state;
|
||||||
assettype_t type;
|
assettype_t type;
|
||||||
void *data;
|
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);
|
@@ -43,6 +43,38 @@ errorret_t assetManagerInit(void) {
|
|||||||
errorOk();
|
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) {
|
void assetManagerDispose(void) {
|
||||||
if(ASSET_MANAGER.zip != NULL) {
|
if(ASSET_MANAGER.zip != NULL) {
|
||||||
zip_close(ASSET_MANAGER.zip);
|
zip_close(ASSET_MANAGER.zip);
|
||||||
|
@@ -6,8 +6,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.h"
|
|
||||||
#include <zip.h>
|
|
||||||
#include "display/texture/texture.h"
|
#include "display/texture/texture.h"
|
||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
|
|
||||||
@@ -46,22 +44,19 @@ extern assetmanager_t ASSET_MANAGER;
|
|||||||
errorret_t assetManagerInit(void);
|
errorret_t assetManagerInit(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets and requests a lock on a given asset. Locking an asset will prevent it
|
* Update the asset manager. This should be called once per frame.
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
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 filename The filename of the asset to get.
|
||||||
* @param ref The reference ID to unlock.
|
* @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.
|
* Disposes/cleans up the asset system.
|
||||||
|
@@ -94,10 +94,9 @@ consolevar_t * consoleRegVar(
|
|||||||
consolevar_t * consoleVarGet(const char_t *name);
|
consolevar_t * consoleVarGet(const char_t *name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the value of a console variable.
|
* Prints a message to the console.
|
||||||
*
|
*
|
||||||
* @param name The name of the variable.
|
* @param message The message to print.
|
||||||
* @param value The new value of the variable.
|
|
||||||
*/
|
*/
|
||||||
void consolePrint(
|
void consolePrint(
|
||||||
const char_t *message,
|
const char_t *message,
|
||||||
|
@@ -15,6 +15,8 @@
|
|||||||
|
|
||||||
engine_t ENGINE;
|
engine_t ENGINE;
|
||||||
|
|
||||||
|
asset_t *testAsset;
|
||||||
|
|
||||||
errorret_t engineInit(void) {
|
errorret_t engineInit(void) {
|
||||||
memoryZero(&ENGINE, sizeof(engine_t));
|
memoryZero(&ENGINE, sizeof(engine_t));
|
||||||
ENGINE.running = true;
|
ENGINE.running = true;
|
||||||
@@ -26,12 +28,15 @@ errorret_t engineInit(void) {
|
|||||||
errorChain(displayInit());
|
errorChain(displayInit());
|
||||||
rpgInit();
|
rpgInit();
|
||||||
|
|
||||||
|
errorChain(assetManagerGetAsset("entities.dpi", &testAsset));
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t engineUpdate(void) {
|
errorret_t engineUpdate(void) {
|
||||||
timeUpdate();
|
timeUpdate();
|
||||||
consoleUpdate();
|
consoleUpdate();
|
||||||
|
assetManagerUpdate();
|
||||||
errorChain(displayUpdate());
|
errorChain(displayUpdate());
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
|
@@ -151,10 +151,13 @@ errorret_t errorPrint(const errorret_t retval);
|
|||||||
* @param retval The return value containing the error state.
|
* @param retval The return value containing the error state.
|
||||||
* @return The error code if an error occurred, otherwise continues execution.
|
* @return The error code if an error occurred, otherwise continues execution.
|
||||||
*/
|
*/
|
||||||
#define errorChain(retval) \
|
#define errorChain(retval) { \
|
||||||
if ((retval).code != ERROR_OK) { \
|
errorret_t errorChainRetval = (retval); \
|
||||||
return errorChainImpl(retval, __FILE__, __func__, __LINE__); \
|
if (errorChainRetval.code != ERROR_OK) { \
|
||||||
}
|
return errorChainImpl(errorChainRetval, __FILE__, __func__, __LINE__); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns without an error.
|
* Returns without an error.
|
||||||
|
@@ -15,14 +15,17 @@ int main(int argc, char **argv) {
|
|||||||
errorret_t ret;
|
errorret_t ret;
|
||||||
ret = engineInit();
|
ret = engineInit();
|
||||||
|
|
||||||
// Set console variable
|
|
||||||
if(argc > 0) consoleRegVar("sys_path", argv[0], NULL);
|
|
||||||
|
|
||||||
if(ret.code != ERROR_OK) {
|
if(ret.code != ERROR_OK) {
|
||||||
errorCatch(errorPrint(ret));
|
errorCatch(errorPrint(ret));
|
||||||
return ret.code;
|
return ret.code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set console variable. This is commented out because at the moment;
|
||||||
|
// Engine init happens, which needs to happen to init console
|
||||||
|
// It also inits asset manager, and the problem is that asset manager
|
||||||
|
// needs to know where the sys_path is to find the asset file.
|
||||||
|
// if(argc > 0) consoleRegVar("sys_path", argv[0], NULL);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
ret = engineUpdate();
|
ret = engineUpdate();
|
||||||
if(ret.code != ERROR_OK) {
|
if(ret.code != ERROR_OK) {
|
||||||
|
Reference in New Issue
Block a user