Loading Assets
Content management in the context of games relates to how a game loads, manages and unloads its assets such as images, sounds, fonts and shaders.
cerlib makes this process as easy as possible, while being equally as efficient. The assets of a game are stored in the assets
folder.
There is no special asset loading function in cerlib. Any asset type such as cer::Image
, cer::Font
and cer::Sound
is loadable directly using its constructor that takes a single string argument. When the asset was not loaded, for example due to a wrong name or non-existent file, a cer::Error
is thrown.
Caching
Loaded assets are cached, which means that when you load a specific asset multiple times, it always refers to the same object in memory.
Consider this example:
In this case, both image1
and image2
refer to the same internal image object. The asset is therefore guaranteed to be only loaded once.
All cerlib objects are automatically reference counted using RAII. Copying an object simply increments the internal object’s reference count. When an object goes out of scope (i.e. its destructor is called), the reference count is decremented.
This ensures that an object’s memory, and therefore an assets memory, is automatically managed for you. You don’t have to unload assets explicitly; just let them go out of scope.
In the example above, the "MyImage.png"
asset would have a reference count of 2. When both objects would go out of scope, the asset would automatically be unloaded.
This behavior is similar to that of std::shared_ptr.
Custom assets
Sometimes, a game might want to load assets in the form of custom data types.
More information will follow soon