Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
Flint::AssetManager Class Reference

Central resource cache and hot-reload coordinator. More...

#include <flint/resource/assets-manager.h>

+ Collaboration diagram for Flint::AssetManager:

Classes

class  WatchHandle
 RAII handle returned by watch(). More...
 

Public Types

using ReloadCallback = std::function<void(const std::string &)>
 Callback type invoked when a watched asset is reloaded.
 

Public Member Functions

 AssetManager (IAssetLoader &loader, SDL_GPUDevice *gpuDevice, bool hotReloadEnable)
 Construct an AssetManager.
 
 ~AssetManager ()
 Destroy the AssetManager.
 
FLINT_API GPUTexture & getTexture (const std::string &path)
 Return a GPU texture for path, uploading it on first access.
 
FLINT_API WatchHandle watch (const std::string &path, ReloadCallback callback)
 Register a callback to be invoked whenever path is reloaded.
 
FLINT_API void update ()
 Poll for hot-reload changes and fire pending callbacks.
 
IAssetLoader & getLoader ()
 Return a reference to the underlying asset loader.
 
void setHotReloadEnabled (bool enabled)
 Enable or disable hot-reload polling.
 
bool isHotReloadEnabled () const
 Return whether hot-reload polling is currently active.
 

Detailed Description

Central resource cache and hot-reload coordinator.

AssetManager sits between game code and the raw byte-loading layer (IAssetLoader). It caches GPU textures so repeated calls to getTexture() never re-upload the same asset, and—when hot-reload is enabled—polls the loader for revision bumps every HOT_RELOAD_INTERVAL milliseconds, invoking registered callbacks so the game can react without restarting.

Ownership and lifetime

AssetManager is non-copyable. Callbacks registered via watch() are automatically cancelled when their WatchHandle is destroyed, which prevents use-after-free if a system outlives the manager.

Note
SDL_GPUDevice must outlive this object; the manager does not take ownership of the device pointer.

Definition at line 38 of file assets-manager.h.

Member Typedef Documentation

◆ ReloadCallback

using Flint::AssetManager::ReloadCallback = std::function<void(const std::string &)>

Callback type invoked when a watched asset is reloaded.

The argument is the asset path that changed.

Definition at line 76 of file assets-manager.h.

Constructor & Destructor Documentation

◆ AssetManager()

Flint::AssetManager::AssetManager ( IAssetLoader & loader,
SDL_GPUDevice * gpuDevice,
bool hotReloadEnable )
inline

Construct an AssetManager.

Parameters
loaderByte-level asset loader used for all reads and revision queries.
gpuDeviceSDL GPU device used to upload textures.
hotReloadEnableWhether to poll for asset changes in update().

Definition at line 53 of file assets-manager.h.

◆ ~AssetManager()

Flint::AssetManager::~AssetManager ( )
inline

Destroy the AssetManager.

Releases the shared lifetime token so that all outstanding WatchHandle objects become inactive immediately, preventing any post-destruction unwatch() calls.

Definition at line 67 of file assets-manager.h.

Member Function Documentation

◆ getLoader()

IAssetLoader & Flint::AssetManager::getLoader ( )
inline

Return a reference to the underlying asset loader.

Useful for one-off raw asset reads that the manager does not cache.

Returns
Reference to the IAssetLoader provided at construction.

Definition at line 243 of file assets-manager.h.

◆ getTexture()

FLINT_API GPUTexture & Flint::AssetManager::getTexture ( const std::string & path)

Return a GPU texture for path, uploading it on first access.

Subsequent calls with the same path return the cached texture without re-uploading.

Warning
The referenced texture is reassigned in place when hot-reload replaces the asset, so a reference held across frames may point at different GPU resources than when it was obtained. Re-call getTexture() each frame rather than caching the reference.
Parameters
pathAsset path relative to the loader's root.
Returns
Reference to the cached GPUTexture.
Exceptions
std::runtime_errorif the asset cannot be retrieved or uploaded.

◆ isHotReloadEnabled()

bool Flint::AssetManager::isHotReloadEnabled ( ) const
inlinenodiscard

Return whether hot-reload polling is currently active.

Returns
true if update() will poll for asset changes.

Definition at line 265 of file assets-manager.h.

◆ setHotReloadEnabled()

void Flint::AssetManager::setHotReloadEnabled ( bool enabled)
inline

Enable or disable hot-reload polling.

When set to false, update() becomes a no-op and no callbacks are fired. Existing watch registrations are preserved and will resume firing if hot-reload is re-enabled.

Parameters
enabledtrue to enable hot-reload, false to disable.

Definition at line 256 of file assets-manager.h.

◆ update()

FLINT_API void Flint::AssetManager::update ( )

Poll for hot-reload changes and fire pending callbacks.

Must be called once per frame (or at any other regular interval). When hot-reload is disabled, this is a no-op. Internally, the method checks elapsed time against HOT_RELOAD_INTERVAL and PENDING_RELOAD_INTERVAL to debounce rapid file-save events.

A texture whose reload fails to read or decode keeps its previous contents and is retried on the next revision change. An asset whose revision drops to 0 is logged once as missing, and its cached copy is kept until it reappears.

◆ watch()

FLINT_API WatchHandle Flint::AssetManager::watch ( const std::string & path,
ReloadCallback callback )
nodiscard

Register a callback to be invoked whenever path is reloaded.

The callback fires during update() after the hot-reload interval elapses and the loader reports a new revision for path. The watch is automatically cancelled when the returned WatchHandle is destroyed.

A change is delivered at most once per HOT_RELOAD_INTERVAL and only after the revision has been stable for PENDING_RELOAD_INTERVAL, so a callback can lag the file write by roughly 400 ms.

Exceptions thrown from the callback are caught and logged; the change is treated as delivered and will not be retried until the asset changes again. Callbacks run after the watch map has been scanned, so calling watch() or destroying a WatchHandle from inside one is safe.

Parameters
pathAsset path to monitor.
callbackFunction called with path when the asset changes.
Returns
A move-only handle whose lifetime controls the watch registration.

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