217 lines
5.7 KiB
C
217 lines
5.7 KiB
C
/**
|
|
* Copyright (c) 2021 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "asset.h"
|
|
#include "../util/flags.h"
|
|
#include "../engine/thread.h"
|
|
#include "../display/shader.h"
|
|
#include "../display/texture.h"
|
|
#include "../display/scaledtexture.h"
|
|
#include "../display/font.h"
|
|
#include "../script/scripter.h"
|
|
#include "xml.h"
|
|
|
|
#define ASSET_MANAGER_ITEMS_MAX 64
|
|
|
|
#define ASSET_MANAGER_STATE_PENDING 0x00
|
|
#define ASSET_MANAGER_STATE_ASYNC_LOADING 0x01
|
|
#define ASSET_MANAGER_STATE_ASYNC_ERROR 0x02
|
|
#define ASSET_MANAGER_STATE_ASYNC_DONE 0x03
|
|
#define ASSET_MANAGER_STATE_SYNC_LOADING 0x04
|
|
#define ASSET_MANAGER_STATE_SYNC_ERROR 0x05
|
|
#define ASSET_MANAGER_STATE_SYNC_DONE 0x06
|
|
|
|
#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
|
|
|
|
// Types
|
|
typedef struct {
|
|
texture_t *texture;
|
|
char *fileName;
|
|
int32_t width, height;
|
|
pixel_t *data;
|
|
} assetmanagertexture_t;
|
|
|
|
typedef struct {
|
|
shader_t *shader;
|
|
char *fileVert;
|
|
char *fileFrag;
|
|
char *dataVert;
|
|
char *dataFrag;
|
|
} assetmanagershader_t;
|
|
|
|
typedef struct {
|
|
font_t *font;
|
|
char *fileName;
|
|
char *data;
|
|
} assetmanagerfont_t;
|
|
|
|
typedef struct {
|
|
scaledtexture_t *scaledTexture;
|
|
} assetmanagerscaledtexture_t;
|
|
|
|
typedef struct {
|
|
scaledtexture_t *scaledTexture;
|
|
texture_t *texture;
|
|
uint8_t scale;
|
|
pixel_t *data;
|
|
} assetmanagerscaletexture_t;
|
|
|
|
|
|
// Item
|
|
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;
|
|
assetmanagerassetdata_t data;
|
|
} assetmanageritem_t;
|
|
|
|
|
|
// Loader
|
|
typedef bool assetmanagerloader_t(assetmanageritem_t *item);
|
|
|
|
typedef struct {
|
|
assetmanagerloader_t *loadAsync;
|
|
assetmanagerloader_t *loadSync;
|
|
} assetmanagerloaderdefinition_t;
|
|
|
|
|
|
// Manager
|
|
typedef struct {
|
|
thread_t thread;
|
|
assetmanageritem_t items[ASSET_MANAGER_ITEMS_MAX];
|
|
uint8_t itemCount;
|
|
bool finished;
|
|
} assetmanager_t;
|
|
|
|
extern assetmanagerloaderdefinition_t ASSET_MANAGER_LOADERS[];
|
|
|
|
|
|
/**
|
|
* Initialize the asset manager
|
|
*
|
|
* @param manager Manager to initialize.
|
|
*/
|
|
void assetManagerInit(assetmanager_t *manager);
|
|
|
|
/**
|
|
* Begin asynchronously loading all of the assets
|
|
*
|
|
* @param manager Manager to begin async loading.
|
|
*/
|
|
void assetManagerStart(assetmanager_t *manager);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* Returns whether or not the given item is finished.
|
|
*
|
|
* @param item Item to check state of.
|
|
* @return True if finished, otherwise false.
|
|
*/
|
|
bool assetManagerIsItemFinished(assetmanageritem_t *item);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/** Private Thread that is executed asynchronously */
|
|
int32_t _assetManagerThread(thread_t *thread);
|
|
|
|
/**
|
|
* Private method, simply adds an item to the manager and resets the state.
|
|
*
|
|
* @param manager Manager to add to.
|
|
* @return The added and reset item.
|
|
*/
|
|
assetmanageritem_t * assetManagerItemAdd(assetmanager_t *manager);
|
|
|
|
/**
|
|
* Queue a texture load onto the asset manager buffer.
|
|
*
|
|
* @param manager Manager to queue on to.
|
|
* @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, texture_t *texture, char *fileName
|
|
);
|
|
|
|
bool _assetManagerLoaderTextureAsync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderTextureSync(assetmanageritem_t *item);
|
|
|
|
/**
|
|
* Queue a font load onto the asset manager buffer.
|
|
*
|
|
* @param manager Manager to queue on to.
|
|
* @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, font_t *font, char *fileName
|
|
);
|
|
|
|
bool _assetManagerLoaderFontAsync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderFontSync(assetmanageritem_t *item);
|
|
|
|
/**
|
|
* Queues a shader load onto the asset manager buffer.
|
|
*
|
|
* @param manager Manager to queue on to.
|
|
* @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, shader_t *shader, char *fileVert, char *fileFrag
|
|
);
|
|
|
|
bool _assetManagerLoaderShaderAsync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderShaderSync(assetmanageritem_t *item);
|
|
|
|
|
|
assetmanageritem_t * assetManagerLoadScaledTexture(
|
|
assetmanager_t *manager, scaledtexture_t *mt, char *path, char *file
|
|
);
|
|
|
|
bool _assetManagerLoaderScaledTextureAsync(assetmanageritem_t *item);
|
|
|
|
|
|
|
|
|
|
assetmanageritem_t * assetManagerLoadTextureScale(
|
|
assetmanager_t *manager, scaledtexture_t *st, texture_t *text, uint8_t scale
|
|
);
|
|
|
|
bool _assetManagerLoaderTextureScaleAsync(assetmanageritem_t *item);
|
|
bool _assetManagerLoaderTextureScaleSync(assetmanageritem_t *item); |