5#include "flint/core/api.h"
6#include "flint/core/asset-loader.h"
7#include "flint/graphics/gpu/gpu-texture.h"
8#include "flint/time/time.h"
14#include <unordered_map>
39 struct ManagerLifetime {};
55 m_gpuDevice(gpuDevice),
56 m_hotReloadEnabled(hotReloadEnable) {
57 m_hotReloadClock.
start();
68 m_managerLifetime.reset();
108 std::weak_ptr<ManagerLifetime> managerLifetime)
109 : m_manager(manager),
111 m_managerLifetime(std::move(managerLifetime)) {
121 : m_manager(other.m_manager),
123 m_managerLifetime(std::move(other.m_managerLifetime)) {
124 other.m_manager =
nullptr;
134 if (
this != &other) {
137 m_manager = other.m_manager;
139 m_managerLifetime = std::move(other.m_managerLifetime);
141 other.m_manager =
nullptr;
158 return m_manager !=
nullptr && !m_managerLifetime.expired();
167 if (m_manager && !m_managerLifetime.expired()) {
168 m_manager->unwatch(m_id);
172 m_managerLifetime.reset();
179 std::weak_ptr<ManagerLifetime> m_managerLifetime;
257 m_hotReloadEnabled = enabled;
266 return m_hotReloadEnabled;
270 std::shared_ptr<ManagerLifetime> m_managerLifetime = std::make_shared<ManagerLifetime>();
273 FLINT_API
void unwatch(uint64_t
id);
276 struct CachedTexture {
278 uint64_t revision = 0;
279 bool missing =
false;
283 struct WatchedAsset {
285 uint64_t revision = 0;
286 uint64_t pendingRevision = 0;
287 uint64_t pendingSince = 0;
289 bool missing =
false;
292 IAssetLoader &m_loader;
293 SDL_GPUDevice *m_gpuDevice =
nullptr;
295 bool m_hotReloadEnabled =
false;
296 Clock m_hotReloadClock;
298 static constexpr uint64_t HOT_RELOAD_INTERVAL = 250;
300 static constexpr uint64_t PENDING_RELOAD_INTERVAL = 150;
302 std::unordered_map<std::string, CachedTexture> m_textures;
303 std::unordered_map<uint64_t, WatchedAsset> m_watched;
304 uint64_t m_nextWatchId = 1;
RAII handle returned by watch().
bool isActive() const
Return whether this handle is still registered.
WatchHandle()=default
Construct an empty (inactive) handle.
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.
Central resource cache and hot-reload coordinator.
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.
AssetManager(IAssetLoader &loader, SDL_GPUDevice *gpuDevice, bool hotReloadEnable)
Construct an AssetManager.
IAssetLoader & getLoader()
Return a reference to the underlying asset loader.
~AssetManager()
Destroy the AssetManager.
std::function< void(const std::string &)> ReloadCallback
Callback type invoked when a watched asset is reloaded.
bool isHotReloadEnabled() const
Return whether hot-reload polling is currently active.
void setHotReloadEnabled(bool enabled)
Enable or disable hot-reload polling.
FLINT_API void update()
Poll for hot-reload changes and fire pending callbacks.
FLINT_API 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.