FPLBase
An open source project by
FPL.
|
Central place to own game assets loaded from disk. More...
#include <asset_manager.h>
Central place to own game assets loaded from disk.
Loads of some assets, such as textures, can be batched in a bit asynchronous load. This allows you to continue setting up your game as assets are loaded, in the background.
Loading assets such as meshes will trigger the load of dependent assets such as textures.
Public Member Functions | |
AssetManager (Renderer &renderer) | |
AssetManager constructor. More... | |
~AssetManager () | |
AssetManager destructor that purges all assets. | |
Shader * | FindShader (const char *basename) |
Returns a previously loaded shader object. More... | |
Shader * | LoadShader (const char *basename, bool async=false, const char *alias=nullptr) |
Loads and returns a shader object. More... | |
Shader * | LoadShader (const char *basename, const std::vector< std::string > &defines, bool async=false, const char *alias=nullptr) |
Loads and returns a shader object with pre-defined identifiers. More... | |
Shader * | LoadShaderDef (const char *filename) |
Load a shader built by shader_pipeline. More... | |
void | UnloadShader (const char *filename) |
Deletes the previously loaded shader. More... | |
Texture * | FindTexture (const char *filename) |
Returns a previously created texture. More... | |
Texture * | LoadTexture (const char *filename, TextureFormat format=kFormatAuto, TextureFlags flags=kTextureFlagsUseMipMaps|kTextureFlagsLoadAsync) |
Queue loading a texture if it hasn't been loaded already. More... | |
void | StartLoadingTextures () |
Start loading all previously queued textures. More... | |
void | StopLoadingTextures () |
Stop loading previously queued textures. More... | |
bool | TryFinalize () |
Check for the status of async loading resources. More... | |
void | UnloadTexture (const char *filename) |
Deletes the previously loaded texture. More... | |
Material * | FindMaterial (const char *filename) |
Returns a previously loaded material. More... | |
Material * | LoadMaterial (const char *filename, bool async_resources=false) |
Loads and returns a material object. More... | |
void | UnloadMaterial (const char *filename) |
Deletes the previously loaded material. More... | |
Mesh * | FindMesh (const char *filename) |
Returns a previously loaded mesh. More... | |
Mesh * | LoadMesh (const char *filename, bool async=false) |
Loads and returns a mesh object. More... | |
void | UnloadMesh (const char *filename) |
Deletes the previously loaded mesh. More... | |
TextureAtlas * | FindTextureAtlas (const char *filename) |
Look up a previously loaded texture atlas. More... | |
TextureAtlas * | LoadTextureAtlas (const char *filename, TextureFormat format=kFormatAuto, TextureFlags flags=kTextureFlagsUseMipMaps|kTextureFlagsLoadAsync) |
Loads a texture atlas. More... | |
void | UnloadTextureAtlas (const char *filename) |
Delete a texture atlas and remove it from the asset manager. More... | |
FileAsset * | FindFileAsset (const char *filename) |
Look up a previously loaded file asset. More... | |
FileAsset * | LoadFileAsset (const char *filename) |
Loads a file asset. More... | |
void | UnloadFileAsset (const char *filename) |
Delete a file asset and remove it from the asset manager. More... | |
Renderer & | renderer () |
Handy accessor for the renderer. More... | |
const Renderer & | renderer () const |
Handy accessor for the renderer. More... | |
void | ClearAllAssets () |
Removes and destructs all assets held by the AssetManager. More... | |
void | SetTextureScale (const mathfu::vec2 &scale) |
Set a scaling factor to apply when loading texture materials. More... | |
void | ResetGlobalShaderDefines (const std::vector< std::string > &defines_to_add, const std::vector< std::string > &defines_to_omit) |
Reset global defines and set dirty flags of all shaders. More... | |
void | ForEachShaderWithDefine (const char *define, const std::function< void(Shader *)> &func) |
Foreach shader with a specific define. | |
fplbase::AssetManager::AssetManager | ( | Renderer & | renderer | ) |
AssetManager constructor.
[in] | renderer | A reference to the Renderer to use with the AssetManager. |
void fplbase::AssetManager::ClearAllAssets | ( | ) |
Removes and destructs all assets held by the AssetManager.
Will be called automatically by the destructor, but can also be called manually beforehand if necessary since destructing assets requires the OpenGL context to still be alive.
FileAsset* fplbase::AssetManager::FindFileAsset | ( | const char * | filename | ) |
Look up a previously loaded file asset.
filename | Name of the file asset file to look up. |
Material* fplbase::AssetManager::FindMaterial | ( | const char * | filename | ) |
Returns a previously loaded material.
filename | The name of the material. |
Mesh* fplbase::AssetManager::FindMesh | ( | const char * | filename | ) |
Returns a previously loaded mesh.
filename | The name of the mesh. |
Shader* fplbase::AssetManager::FindShader | ( | const char * | basename | ) |
Returns a previously loaded shader object.
basename | The name of the shader. |
Texture* fplbase::AssetManager::FindTexture | ( | const char * | filename | ) |
Returns a previously created texture.
filename | The name of the texture. |
TextureAtlas* fplbase::AssetManager::FindTextureAtlas | ( | const char * | filename | ) |
Look up a previously loaded texture atlas.
filename | Name of the texture atlas file to look up. |
FileAsset* fplbase::AssetManager::LoadFileAsset | ( | const char * | filename | ) |
Loads a file asset.
Material* fplbase::AssetManager::LoadMaterial | ( | const char * | filename, |
bool | async_resources = false |
||
) |
Loads and returns a material object.
Loads a material, which is a compiled FlatBuffer file with root Material. This loads all resources contained there-in. Optionally, you can Q up any contained resources for async loading. If this returns nullptr, the error can be found in Renderer::last_error().
filename | The name of the material. |
Mesh* fplbase::AssetManager::LoadMesh | ( | const char * | filename, |
bool | async = false |
||
) |
Loads and returns a mesh object.
Loads a mesh, which is a compiled FlatBuffer file with root Mesh. If this returns nullptr, the error can be found in Renderer::last_error().
filename | The name of the mesh. |
Shader* fplbase::AssetManager::LoadShader | ( | const char * | basename, |
bool | async = false , |
||
const char * | alias = nullptr |
||
) |
Loads and returns a shader object.
Loads a shader if it hasn't been loaded already, by appending .glslv and .glslf to the basename, compiling and linking them. If this returns nullptr, the error can be found in Renderer::last_error().
basename | The name of the shader. |
async | A boolean to indicate whether to load asynchronously or not. |
Shader* fplbase::AssetManager::LoadShader | ( | const char * | basename, |
const std::vector< std::string > & | defines, | ||
bool | async = false , |
||
const char * | alias = nullptr |
||
) |
Loads and returns a shader object with pre-defined identifiers.
Works like LoadShader (above), but takes in a set of #define variables.
basename | The name of the shader. |
defines | A vector of defines. |
async | A boolean to indicate whether to load asynchronously or not. |
Shader* fplbase::AssetManager::LoadShaderDef | ( | const char * | filename | ) |
Load a shader built by shader_pipeline.
Loads a shader built by the shader_pipeline if it hasn't been loaded. already. If this returns nullptr, the error can be found in Renderer::last_error().
filename | Name of the shader file to load. |
Texture* fplbase::AssetManager::LoadTexture | ( | const char * | filename, |
TextureFormat | format = kFormatAuto , |
||
TextureFlags | flags = kTextureFlagsUseMipMaps|kTextureFlagsLoadAsync |
||
) |
Queue loading a texture if it hasn't been loaded already.
If async, queues a texture for loading if it hasn't been loaded already, otherwise loads it directly. Currently only supports TGA/WebP format files. If async, the returned texture isn't usable until TryFinalize() succeeds and the id is non-zero.
filename | The name of the texture to load. |
format | The texture format, defaults to kFormatAuto. |
flags | The texture flags, by default loads textures async. |
TextureAtlas* fplbase::AssetManager::LoadTextureAtlas | ( | const char * | filename, |
TextureFormat | format = kFormatAuto , |
||
TextureFlags | flags = kTextureFlagsUseMipMaps|kTextureFlagsLoadAsync |
||
) |
Loads a texture atlas.
Loads a texture atlas, which is a compiled FlatBuffer file containing a texture path and subtexture rectangles.
filename | Name of the texture atlas file to load. |
format | The texture format, defaults to kFormatAuto. |
flags | Texture flags: by default load async. |
|
inline |
Handy accessor for the renderer.
|
inline |
Handy accessor for the renderer.
void fplbase::AssetManager::ResetGlobalShaderDefines | ( | const std::vector< std::string > & | defines_to_add, |
const std::vector< std::string > & | defines_to_omit | ||
) |
Reset global defines and set dirty flags of all shaders.
This will cause all shaders be reloaded in the next frame it is being used.
|
inline |
Set a scaling factor to apply when loading texture materials.
By setting the scaling factor, an application save a memory footprint on low RAM devices.
void fplbase::AssetManager::StartLoadingTextures | ( | ) |
Start loading all previously queued textures.
LoadTextures doesn't actually load anything, this will start the async loading of all files, and decompression.
void fplbase::AssetManager::StopLoadingTextures | ( | ) |
Stop loading previously queued textures.
This method will block until the currently loading textures have finished loading. You can resume loading queued textures by calling StartLoadingTextures.
bool fplbase::AssetManager::TryFinalize | ( | ) |
Check for the status of async loading resources.
Call this repeatedly until it returns true, which signals all resources will have loaded, and turned into OpenGL resources. Call IsValid() on a resource to see if there were problems in loading / finalizing.
void fplbase::AssetManager::UnloadFileAsset | ( | const char * | filename | ) |
Delete a file asset and remove it from the asset manager.
If its reference count was >1, it will be decreased instead of unloaded.
void fplbase::AssetManager::UnloadMaterial | ( | const char * | filename | ) |
Deletes the previously loaded material.
Deletes all OpenGL textures contained in this material, and removes the textures and the material from material manager. Any subsequent requests for these textures through Load*() will cause them to be loaded anew. If its reference count was >1, it will be decreased instead of unloaded.
filename | The name of the material to unload. |
void fplbase::AssetManager::UnloadMesh | ( | const char * | filename | ) |
Deletes the previously loaded mesh.
Deletes the mesh and removes it from the material manager. Any subsequent requests for this mesh through Load*() will cause them to be loaded anew. If its reference count was >1, it will be decreased instead of unloaded.
filename | The name of the mesh to unload. |
void fplbase::AssetManager::UnloadShader | ( | const char * | filename | ) |
Deletes the previously loaded shader.
Deletes the shader and removes it from the material manager. Any subsequent requests for this shader through Load*() will cause them to be loaded anew. If its reference count was >1, it will be decreased instead of unloaded.
filename | The name of the shader to unload. |
void fplbase::AssetManager::UnloadTexture | ( | const char * | filename | ) |
Deletes the previously loaded texture.
Deletes the texture and removes it from the material manager. Any subsequent requests for this mesh through Load*() will cause them to be loaded anew. If its reference count was >1, it will be decreased instead of unloaded.
filename | The name of the texture to unload. |
void fplbase::AssetManager::UnloadTextureAtlas | ( | const char * | filename | ) |
Delete a texture atlas and remove it from the asset manager.
This will cause any Texture objects this atlas has issued to no longer refer to a valid texture. Any subsequent requests for this texture atlas through Load*() will cause it to be loaded anew. If its reference count was >1, it will be decreased instead of unloaded.