FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
fplbase::AssetManager Class Reference

Central place to own game assets loaded from disk. More...

#include <asset_manager.h>

Detailed Description

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.
 
ShaderFindShader (const char *basename)
 Returns a previously loaded shader object. More...
 
ShaderLoadShader (const char *basename, bool async=false, const char *alias=nullptr)
 Loads and returns a shader object. More...
 
ShaderLoadShader (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...
 
ShaderLoadShaderDef (const char *filename)
 Load a shader built by shader_pipeline. More...
 
void UnloadShader (const char *filename)
 Deletes the previously loaded shader. More...
 
TextureFindTexture (const char *filename)
 Returns a previously created texture. More...
 
TextureLoadTexture (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...
 
MaterialFindMaterial (const char *filename)
 Returns a previously loaded material. More...
 
MaterialLoadMaterial (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...
 
MeshFindMesh (const char *filename)
 Returns a previously loaded mesh. More...
 
MeshLoadMesh (const char *filename, bool async=false)
 Loads and returns a mesh object. More...
 
void UnloadMesh (const char *filename)
 Deletes the previously loaded mesh. More...
 
TextureAtlasFindTextureAtlas (const char *filename)
 Look up a previously loaded texture atlas. More...
 
TextureAtlasLoadTextureAtlas (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...
 
FileAssetFindFileAsset (const char *filename)
 Look up a previously loaded file asset. More...
 
FileAssetLoadFileAsset (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...
 
Rendererrenderer ()
 Handy accessor for the renderer. More...
 
const Rendererrenderer () 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.
 

Constructor & Destructor Documentation

fplbase::AssetManager::AssetManager ( Renderer renderer)

AssetManager constructor.

Parameters
[in]rendererA reference to the Renderer to use with the AssetManager.

Member Function Documentation

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.

Parameters
filenameName of the file asset file to look up.
Returns
Pointer to the file asset if found, nullptr otherwise.
Material* fplbase::AssetManager::FindMaterial ( const char *  filename)

Returns a previously loaded material.

Parameters
filenameThe name of the material.
Returns
Returns the material, or nullptr if not previously loaded.
Mesh* fplbase::AssetManager::FindMesh ( const char *  filename)

Returns a previously loaded mesh.

Parameters
filenameThe name of the mesh.
Returns
Returns the mesh, or nullptr if not previously loaded.
Shader* fplbase::AssetManager::FindShader ( const char *  basename)

Returns a previously loaded shader object.

Parameters
basenameThe name of the shader.
Returns
Returns the shader, or nullptr if not previously loaded.
Texture* fplbase::AssetManager::FindTexture ( const char *  filename)

Returns a previously created texture.

Parameters
filenameThe name of the texture.
Returns
Returns the texture, or nullptr if not previously loaded.
TextureAtlas* fplbase::AssetManager::FindTextureAtlas ( const char *  filename)

Look up a previously loaded texture atlas.

Parameters
filenameName of the texture atlas file to look up.
Returns
Pointer to the texture atlas if found, nullptr otherwise.
FileAsset* fplbase::AssetManager::LoadFileAsset ( const char *  filename)

Loads a file asset.

Returns
nullptr on error.
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().

Parameters
filenameThe name of the material.
Returns
Returns the loaded material, or nullptr if there was an error.
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().

Parameters
filenameThe name of the mesh.
Returns
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().

Parameters
basenameThe name of the shader.
asyncA boolean to indicate whether to load asynchronously or not.
Returns
Returns the loaded shader, or nullptr if there was an error.
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.

Parameters
basenameThe name of the shader.
definesA vector of defines.
asyncA boolean to indicate whether to load asynchronously or not.
Note
An example of how to call this function: const std::vector<std::string> defines[] = { USE_SHADOWS, USE_THE_FORCE, USE_SOMEBODY, nullptr }; LoadShader("source_file", defines, false, nullptr);
Returns
Returns the loaded shader, or nullptr if there was an error.
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().

Parameters
filenameName of the shader file to load.
Returns
Returns the loaded shader, or nullptr if there was an error.
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.

Parameters
filenameThe name of the texture to load.
formatThe texture format, defaults to kFormatAuto.
flagsThe texture flags, by default loads textures async.
Returns
Returns an unloaded texture object. If not async, may also return null to signal and error.
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.

Parameters
filenameName of the texture atlas file to load.
formatThe texture format, defaults to kFormatAuto.
flagsTexture flags: by default load async.
Returns
If this returns nullptr, the error can be found in Renderer::last_error().
Renderer& fplbase::AssetManager::renderer ( )
inline

Handy accessor for the renderer.

Returns
Returns the renderer.
const Renderer& fplbase::AssetManager::renderer ( ) const
inline

Handy accessor for the renderer.

Returns
Returns 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.

void fplbase::AssetManager::SetTextureScale ( const mathfu::vec2 &  scale)
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.

Returns
Returns true when all resources have been loaded & finalized.
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.

Parameters
filenameThe 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.

Parameters
filenameThe 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.

Parameters
filenameThe 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.

Parameters
filenameThe 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.


The documentation for this class was generated from the following file: