![]() |
ImFusion SDK 4.3
|
Resource system to retrieve at runtime binary data embedded into the application/library. More...
Resource system to retrieve at runtime binary data embedded into the application/library.
The ImFusion Resource system is a light-weight platform-independent mechanism to embed binary data in a library/executable. It is somewhat similar to the Qt Resource System, however does not have any third-party dependencies except for ImFusionCore. It consists of two layers/steps: The embedding mechanism of the data at compilation time, and the retrieval of the data at runtime.
Use the ImFusionResourceCompiler
executable to generate a C++ file to build and link into your library/application. The resulting file will contain the specified input files as well as the code needed to implement a Repository and automatically register it with the ImFusion::Resource system.
A minimal example to embed two files could look like this:
This will create the file MyRepo.cpp
containing code that registers a Repository with name MyRepo
providing the contents of the files data/mesh.obj
and data/texture.png
under the names mesh.obj
and texture.png
. Finally, you need to include this file into your build system so that it gets compiled and linked into the target library/executable. The target library/executable needs to link against ImFusionCore.
The ImFusion CMake infrastructure provides a convenience function to perform the above steps automatically with a single function call. Additionally, the option EXTERNAL_STORAGE
is provided, which allows to store files in an external archive:
For instance, all you need to do to create the resource repository from the above example for your library MyLibrary
is the following:
${CMAKE_CURRENT_BINARY_DIR}/${RepositoryName}.cpp
as default output file name. Thus, if you want to have multiple repositories of the same name in a single target you will need to specify individual output file names for each of them. The option EXTERNAL_STORAGE
allows to store files in an external zip archive and avoid embedding them as binary. This option is for internal use and can only be used via cmake. Therefore it is not available directly via the ImFusionResourceCompiler
options.Use the functions in the ImFusion::Resource namespace to retrieve compiled resources at runtime. Resource::query() will search all registered repositories for a resource with the given name and return the first one it finds (the search order is undefined). You can provide an optional repository name to filter and avoid ambiguities.
A Resource::Repository must be registered with the resource system before it can be used. Manual registration of custom repositories is done using the Resource::addRepository() function.
Repositories created with the ImFusionResourceCompiler
as described above are registered automatically by default using the static initialization pattern. Therefore, you normally do not need to do anything else in addition to define the resource repository via CMake.
You can opt-out of this feature by passing the no-auto-register
flag. In this case the resource compiler will output a header file next to the cpp file providing the declaration of the created repository. You can then include this header file, instantiate an instance of the repository, and register it with the resource system as needed.
During development it can be convenient to not always embed all resources on every compilation but instead load them dynamically from the filesystem at runtime. The FilesystemRepository enables you to do this while maintaining the same retrieval API through Resource::query(). For instance, you could setup your build system to use the ImFusionResourceCompiler
only for production builds and setup a FilesystemRepository with the exact same semantics for development builds.
Namespaces | |
namespace | ImFusion::Resource |
ImFusion Resource System to store binary data in the executable/library and retrieve it at runtime. | |
Classes | |
class | FilesystemRepository |
Repository for loading resources from the local filesystem. More... | |
class | Repository |
Base interface for a ImFusion resource repository. More... | |
Functions | |
void | addRepository (const Repository *rep) |
Add the given Repository to the set of registered repositories. | |
void | removeRepository (const Repository *rep) |
Remove the given Repository from the set of registered repositories. | |
const Repository * | locate (const std::string_view &repositoryName, const std::string_view &resourceName) |
Searches the registered repositories if they provide a resource identified by the given name. | |
std::optional< ByteBuffer > | query (const std::string_view &repositoryName, const std::string_view &resourceName) |
Queries the registered repositories for the resource identified by the given name. | |
const Repository * locate | ( | const std::string_view & | repositoryName, |
const std::string_view & | resourceName ) |
#include <ImFusion/Core/Resource/Resource.h>
Searches the registered repositories if they provide a resource identified by the given name.
You can use this function to check if a resource is available without actually retrieving it. If there are multiple repositories matching the given query it is undefined which one is returned.
repositoryName | Optional filter to restrict the search to repositories of the given name. Will search all registered repositories if empty. |
resourceName | Name of the resource to search for |
repository->query(resourceName)
returns a valid ByteBuffer, nullptr
otherwise. std::optional< ByteBuffer > query | ( | const std::string_view & | repositoryName, |
const std::string_view & | resourceName ) |
#include <ImFusion/Core/Resource/Resource.h>
Queries the registered repositories for the resource identified by the given name.
If there are multiple resources matching the given query it is undefined which one is returned. Will return an invalid Optional if no resource was found.
repositoryName | Optional filter to restrict the search to repositories of the given name. Will search all registered repositories if empty. |
resourceName | Name of the resource to search for |