5#include "flint/core/asset-loader.h"
6#include "flint/graphics/gpu/gpu-texture.h"
7#include "flint/time/time.h"
13#include <unordered_map>
38 struct ManagerLifetime {};
54 m_gpuDevice(gpuDevice),
55 m_hotReloadEnabled(hotReloadEnable) {
56 m_hotReloadClock.
start();
67 m_managerLifetime.reset();
107 std::weak_ptr<ManagerLifetime> managerLifetime)
108 : m_manager(manager),
110 m_managerLifetime(std::move(managerLifetime)) {
120 : m_manager(other.m_manager),
122 m_managerLifetime(std::move(other.m_managerLifetime)) {
123 other.m_manager =
nullptr;
133 if (
this != &other) {
136 m_manager = other.m_manager;
138 m_managerLifetime = std::move(other.m_managerLifetime);
140 other.m_manager =
nullptr;
157 return m_manager !=
nullptr && !m_managerLifetime.expired();
166 if (m_manager && !m_managerLifetime.expired()) {
167 m_manager->unwatch(m_id);
171 m_managerLifetime.reset();
178 std::weak_ptr<ManagerLifetime> m_managerLifetime;
256 m_hotReloadEnabled = enabled;
265 return m_hotReloadEnabled;
269 std::shared_ptr<ManagerLifetime> m_managerLifetime = std::make_shared<ManagerLifetime>();
272 void unwatch(uint64_t
id);
275 struct CachedTexture {
277 uint64_t revision = 0;
278 bool missing =
false;
282 struct WatchedAsset {
284 uint64_t revision = 0;
285 uint64_t pendingRevision = 0;
286 uint64_t pendingSince = 0;
288 bool missing =
false;
291 IAssetLoader &m_loader;
292 SDL_GPUDevice *m_gpuDevice =
nullptr;
294 bool m_hotReloadEnabled =
false;
295 Clock m_hotReloadClock;
297 static constexpr uint64_t HOT_RELOAD_INTERVAL = 250;
299 static constexpr uint64_t PENDING_RELOAD_INTERVAL = 150;
301 std::unordered_map<std::string, CachedTexture> m_textures;
302 std::unordered_map<uint64_t, WatchedAsset> m_watched;
303 uint64_t m_nextWatchId = 1;
bool isActive() const
Return whether this handle is still registered.
WatchHandle(WatchHandle &&other) noexcept
Move constructor; transfers ownership and nulls the source.
void reset()
Cancel the watch and release the handle.
WatchHandle(AssetManager *manager, uint64_t id, std::weak_ptr< ManagerLifetime > managerLifetime)
Construct an active handle tied to a specific watch entry.
WatchHandle & operator=(WatchHandle &&other) noexcept
Move-assignment operator.
~WatchHandle()
Destructor; calls reset() to cancel the watch if still active.
RAII handle returned by watch().
WatchHandle()=default
Construct an empty (inactive) handle.
AssetManager(IAssetLoader &loader, SDL_GPUDevice *gpuDevice, bool hotReloadEnable)
Construct an AssetManager.
IAssetLoader & getLoader()
Return a reference to the underlying asset loader.
WatchHandle watch(const std::string &path, ReloadCallback callback)
Register a callback to be invoked whenever path is reloaded.
~AssetManager()
Destroy the AssetManager.
std::function< void(const std::string &)> ReloadCallback
Callback type invoked when a watched asset is reloaded.
void update()
Poll for hot-reload changes and fire pending callbacks.
bool isHotReloadEnabled() const
Return whether hot-reload polling is currently active.
GPUTexture & getTexture(const std::string &path)
Return a GPU texture for path, uploading it on first access.
void setHotReloadEnabled(bool enabled)
Enable or disable hot-reload polling.
Central resource cache and hot-reload coordinator.
void start()
Start the clock if it is currently stopped.
Owns an SDL GPU texture and its associated sampler.
Abstract interface for loading raw asset data by path.