288 lines
8.5 KiB
C
288 lines
8.5 KiB
C
/**
|
|
* Copyright (c) 2021 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "../libs.h"
|
|
#include "../display/font.h"
|
|
#include "../display/texture.h"
|
|
#include "../display/scaledtexture.h"
|
|
#include "../display/shader/shader.h"
|
|
#include "../engine/thread.h"
|
|
#include "../util/array.h"
|
|
#include "asset.h"
|
|
#include "xml.h"
|
|
|
|
#define ASSET_MANAGER_ITEMS_MAX 64
|
|
#define ASSET_MANAGER_ITEM_NAME_MAX 96
|
|
|
|
#define ASSET_MANAGER_HOLDERS_MAX 8
|
|
|
|
#define ASSET_MANAGER_STATE_NOT_READY 0x00
|
|
#define ASSET_MANAGER_STATE_PENDING 0x01
|
|
#define ASSET_MANAGER_STATE_ASYNC_LOADING 0x02
|
|
#define ASSET_MANAGER_STATE_ASYNC_ERROR 0x03
|
|
#define ASSET_MANAGER_STATE_ASYNC_DONE 0x04
|
|
#define ASSET_MANAGER_STATE_SYNC_LOADING 0x05
|
|
#define ASSET_MANAGER_STATE_SYNC_ERROR 0x06
|
|
#define ASSET_MANAGER_STATE_SYNC_DONE 0x07
|
|
|
|
#define ASSET_MANAGER_TYPE_TEXTURE 0x00
|
|
#define ASSET_MANAGER_TYPE_FONT 0x01
|
|
#define ASSET_MANAGER_TYPE_SHADER 0x02
|
|
#define ASSET_MANAGER_TYPE_SCALED_TEXTURE 0x03
|
|
#define ASSET_MANAGER_TYPE_SCALE_TEXTURE 0x04
|
|
|
|
// Loader Types
|
|
typedef struct {
|
|
font_t *font;
|
|
char *fileName;
|
|
char *data;
|
|
} assetmanagerfont_t;
|
|
|
|
typedef struct {
|
|
texture_t *texture;
|
|
char *fileName;
|
|
int32_t width, height;
|
|
pixel_t *data;
|
|
} assetmanagertexture_t;
|
|
|
|
typedef struct {
|
|
scaledtexture_t *scaledTexture;
|
|
} assetmanagerscaledtexture_t;
|
|
|
|
typedef struct {
|
|
scaledtexture_t *scaledTexture;
|
|
texture_t *texture;
|
|
uint8_t scale;
|
|
pixel_t *data;
|
|
} assetmanagerscaletexture_t;
|
|
|
|
typedef struct {
|
|
shader_t *shader;
|
|
char *fileVert;
|
|
char *fileFrag;
|
|
char *dataVert;
|
|
char *dataFrag;
|
|
} assetmanagershader_t;
|
|
|
|
// Item
|
|
typedef uint8_t assetmanagerowner_t;
|
|
|
|
typedef union {
|
|
assetmanagertexture_t texture;
|
|
assetmanagershader_t shader;
|
|
assetmanagerfont_t font;
|
|
assetmanagerscaledtexture_t scaledTexture;
|
|
assetmanagerscaletexture_t scaleTexture;
|
|
} assetmanagerassetdata_t;
|
|
|
|
typedef struct {
|
|
uint8_t type;
|
|
uint8_t state;
|
|
char key[ASSET_MANAGER_ITEM_NAME_MAX];
|
|
assetmanagerassetdata_t data;
|
|
assetmanagerowner_t holders[ASSET_MANAGER_HOLDERS_MAX];
|
|
uint8_t holderCount;
|
|
} assetmanageritem_t;
|
|
|
|
// Loader
|
|
typedef bool assetmanagerloader_t(assetmanageritem_t *item);
|
|
|
|
typedef struct {
|
|
assetmanagerloader_t *loadAsync;
|
|
assetmanagerloader_t *loadSync;
|
|
assetmanagerloader_t *dispose;
|
|
} assetmanagerloaderdefinition_t;
|
|
|
|
// Manager
|
|
typedef struct {
|
|
thread_t thread;
|
|
bool finished;
|
|
assetmanageritem_t items[ASSET_MANAGER_ITEMS_MAX];
|
|
uint8_t itemCount;
|
|
assetmanagerowner_t holders[ASSET_MANAGER_HOLDERS_MAX];
|
|
uint8_t holderCount;
|
|
bool running;
|
|
} assetmanager_t;
|
|
|
|
// Constants
|
|
extern assetmanagerloaderdefinition_t ASSET_MANAGER_LOADERS[];
|
|
|
|
/**
|
|
* Initialize the asset manager
|
|
*
|
|
* @param manager Manager to initialize.
|
|
*/
|
|
void assetManagerInit(assetmanager_t *manager);
|
|
|
|
/**
|
|
* Gets the progress of the asset manager as a representation of 0-1 as a % that
|
|
* has loaded.
|
|
*
|
|
* @param manager Manager to check the state of.
|
|
* @return The progress percent as a representation of 0-1
|
|
*/
|
|
float assetManagerProgressGet(assetmanager_t *manager);
|
|
|
|
assetmanagerowner_t assetManagerHolderCreate(assetmanager_t *man);
|
|
void assetManagerHolderRelease(assetmanager_t *man, uint8_t hold);
|
|
void assetManagerDisposeReleased(assetmanager_t *man);
|
|
void assetManagerDispose(assetmanager_t *man);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* Begin asynchronously loading all of the assets
|
|
*
|
|
* @param manager Manager to begin async loading.
|
|
*/
|
|
void assetManagerStart(assetmanager_t *manager);
|
|
|
|
/** Private Thread that is executed asynchronously */
|
|
int32_t _assetManagerThread(thread_t *thread);
|
|
|
|
/**
|
|
* Synchronously tick the asset manager. Some assets require some form of sync
|
|
* operations, such as buffering to the GPU, so make sure this is called at a
|
|
* good time for that task, such as right at the end of a frame.
|
|
*
|
|
* @param manager Manager to synchronously tick.
|
|
*/
|
|
void assetManagerUpdate(assetmanager_t *manager);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* Retreive an exisitng asset manager item by its key.
|
|
*
|
|
* @param man Manager to get from
|
|
* @param key Key to search for.
|
|
* @return The matching asset manager item, or NULL if not found.
|
|
*/
|
|
assetmanageritem_t * assetManagerItemGet(assetmanager_t *man, char *key);
|
|
|
|
/**
|
|
* Private method, simply adds an item to the manager and resets the state.
|
|
*
|
|
* @param manager Manager to add to.
|
|
* @param key Key to use when adding.
|
|
* @return The added and reset item.
|
|
*/
|
|
assetmanageritem_t * assetManagerItemAdd(assetmanager_t *manager, char *key);
|
|
|
|
/**
|
|
* Add or get the index that a given holder has as a manager item.
|
|
*
|
|
* @param i Asset Item to check.
|
|
* @param o Owner to get/add.
|
|
* @return The index within the item that the owner is at.
|
|
*/
|
|
uint8_t assetManagerItemGetOrAddHolder(
|
|
assetmanageritem_t *i, assetmanagerowner_t o
|
|
);
|
|
|
|
/**
|
|
* Checks if a given asset item is finished or not.
|
|
*
|
|
* @param item Item to check.
|
|
* @return True if finished, otherwise false.
|
|
*/
|
|
bool assetManagerItemIsFinished(assetmanageritem_t *item);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* Queue a font load onto the asset manager buffer.
|
|
*
|
|
* @param manager Manager to queue on to.
|
|
* @param owner Owner ID requesting to load this resource.
|
|
* @param font Font to push the result in to.
|
|
* @param fileName Filename of the asset to load.
|
|
* @return A pointer to the asset manager item for tracking.
|
|
*/
|
|
assetmanageritem_t * assetManagerLoadFont(
|
|
assetmanager_t *manager, assetmanagerowner_t owner,
|
|
font_t *font, char *fileName
|
|
);
|
|
|
|
bool _assetManagerLoaderFontAsync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderFontSync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderFontDispose(assetmanageritem_t *item);
|
|
|
|
/**
|
|
* Queue a texture load onto the asset manager buffer.
|
|
*
|
|
* @param manager Manager to queue on to.
|
|
* @param owner Owner ID requesting to load this resource.
|
|
* @param texture Texture to push the result in to.
|
|
* @param fileName Texture filename to load.
|
|
* @return A pointer to the asset manager item for tracking.
|
|
*/
|
|
assetmanageritem_t * assetManagerLoadTexture(
|
|
assetmanager_t *manager, assetmanagerowner_t owner,
|
|
texture_t *texture, char *fileName
|
|
);
|
|
|
|
bool _assetManagerLoaderTextureAsync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderTextureSync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderTextureDispose(assetmanageritem_t *item);
|
|
|
|
/**
|
|
* Queue a scaled texture load asset to the asset manager buffer. This will not
|
|
* actually load the texture, just the scaled texture information.
|
|
*
|
|
* @param manager Manager to queue on to.
|
|
* @param owner Owner ID requesting to load this resource.
|
|
* @param mt Scaled Texture to load in to.
|
|
* @param path Path of the texture files
|
|
* @param file File name of the texture sets.
|
|
* @return A pointer to the asset manager item for tracking.
|
|
*/
|
|
assetmanageritem_t * assetManagerLoadScaledTexture(
|
|
assetmanager_t *manager, assetmanagerowner_t owner,
|
|
scaledtexture_t *st, char *path, char *file
|
|
);
|
|
|
|
bool _assetManagerLoaderScaledTextureAsync(assetmanageritem_t *item);
|
|
|
|
/**
|
|
* Load the given texture scale for a scaled texture.
|
|
*
|
|
* @param manager Manager to queue on to.
|
|
* @param owner Owner ID requesting to load this resource.
|
|
* @param st Scaled Texture to load in to.
|
|
* @param text Texture to load the scale in to.
|
|
* @param scale Scale to load.
|
|
* @return A pointer to the asset manager item for tracking.
|
|
*/
|
|
assetmanageritem_t * assetManagerLoadTextureScale(
|
|
assetmanager_t *manager, assetmanagerowner_t owner,
|
|
scaledtexture_t *st, texture_t *text, uint8_t scale
|
|
);
|
|
|
|
bool _assetManagerLoaderTextureScaleAsync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderTextureScaleSync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderTextureScaleDispose(assetmanageritem_t *item);
|
|
|
|
/**
|
|
* Queues a shader load onto the asset manager buffer.
|
|
*
|
|
* @param manager Manager to queue on to.
|
|
* @param owner Owner ID requesting to load this resource.
|
|
* @param shader Shader to push the result in to.
|
|
* @param fileVert Vertex file in question to load.
|
|
* @param fileFrag Fragment file in question to load.
|
|
* @return A pointer to the asset manager item for tracking.
|
|
*/
|
|
assetmanageritem_t * assetManagerLoadShader(
|
|
assetmanager_t *manager, assetmanagerowner_t owner,
|
|
shader_t *shader, char *fileVert, char *fileFrag
|
|
);
|
|
|
|
bool _assetManagerLoaderShaderAsync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderShaderSync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderShaderDispose(assetmanageritem_t *item); |