|
Flint Engine
Modular C++20 2D game engine powered by SDL3
|
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. | |
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.
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.
Definition at line 38 of file assets-manager.h.
| 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.
|
inline |
Construct an AssetManager.
| loader | Byte-level asset loader used for all reads and revision queries. |
| gpuDevice | SDL GPU device used to upload textures. |
| hotReloadEnable | Whether to poll for asset changes in update(). |
Definition at line 53 of file assets-manager.h.
|
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.
|
inline |
Return a reference to the underlying asset loader.
Useful for one-off raw asset reads that the manager does not cache.
Definition at line 243 of file assets-manager.h.
| 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.
| path | Asset path relative to the loader's root. |
| std::runtime_error | if the asset cannot be retrieved or uploaded. |
|
inlinenodiscard |
Return whether hot-reload polling is currently active.
true if update() will poll for asset changes. Definition at line 265 of file assets-manager.h.
|
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.
| enabled | true to enable hot-reload, false to disable. |
Definition at line 256 of file assets-manager.h.
| 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.
|
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.
| path | Asset path to monitor. |
| callback | Function called with path when the asset changes. |