Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
assets-manager.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
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"
9
10#include <cstdint>
11#include <functional>
12#include <memory>
13#include <string>
14#include <unordered_map>
15#include <utility>
16
17namespace Flint {
18
39 struct ManagerLifetime {};
40
41 AssetManager(const AssetManager &) = delete;
42 AssetManager &operator=(const AssetManager &) = delete;
43
44 public:
53 AssetManager(IAssetLoader &loader, SDL_GPUDevice *gpuDevice, bool hotReloadEnable)
54 : m_loader(loader),
55 m_gpuDevice(gpuDevice),
56 m_hotReloadEnabled(hotReloadEnable) {
57 m_hotReloadClock.start();
58 }
59
68 m_managerLifetime.reset();
69 }
70
76 using ReloadCallback = std::function<void(const std::string &)>;
77
96 public:
98 WatchHandle() = default;
99
107 WatchHandle(AssetManager *manager, uint64_t id,
108 std::weak_ptr<ManagerLifetime> managerLifetime)
109 : m_manager(manager),
110 m_id(id),
111 m_managerLifetime(std::move(managerLifetime)) {
112 }
113
116 reset();
117 }
118
120 WatchHandle(WatchHandle &&other) noexcept
121 : m_manager(other.m_manager),
122 m_id(other.m_id),
123 m_managerLifetime(std::move(other.m_managerLifetime)) {
124 other.m_manager = nullptr;
125 }
126
133 WatchHandle &operator=(WatchHandle &&other) noexcept {
134 if (this != &other) {
135 reset();
136
137 m_manager = other.m_manager;
138 m_id = other.m_id;
139 m_managerLifetime = std::move(other.m_managerLifetime);
140
141 other.m_manager = nullptr;
142 }
143 return *this;
144 }
145
146 WatchHandle(const WatchHandle &) = delete;
147 WatchHandle &operator=(const WatchHandle &) = delete;
148
157 [[nodiscard]] bool isActive() const {
158 return m_manager != nullptr && !m_managerLifetime.expired();
159 }
160
166 void reset() {
167 if (m_manager && !m_managerLifetime.expired()) {
168 m_manager->unwatch(m_id);
169 }
170
171 m_manager = nullptr;
172 m_managerLifetime.reset();
173 }
174
175 private:
176 AssetManager *m_manager = nullptr;
177 uint64_t m_id = 0;
178
179 std::weak_ptr<ManagerLifetime> m_managerLifetime;
180 };
181
197 FLINT_API GPUTexture &getTexture(const std::string &path);
198
219 [[nodiscard]] FLINT_API WatchHandle watch(const std::string &path, ReloadCallback callback);
220
234 FLINT_API void update();
235
244 return m_loader;
245 }
246
256 void setHotReloadEnabled(bool enabled) {
257 m_hotReloadEnabled = enabled;
258 }
259
265 [[nodiscard]] bool isHotReloadEnabled() const {
266 return m_hotReloadEnabled;
267 }
268
269 private:
270 std::shared_ptr<ManagerLifetime> m_managerLifetime = std::make_shared<ManagerLifetime>();
271
273 FLINT_API void unwatch(uint64_t id);
274
276 struct CachedTexture {
277 GPUTexture texture;
278 uint64_t revision = 0;
279 bool missing = false;
280 };
281
283 struct WatchedAsset {
284 std::string path;
285 uint64_t revision = 0;
286 uint64_t pendingRevision = 0;
287 uint64_t pendingSince = 0;
288 ReloadCallback callback;
289 bool missing = false;
290 };
291
292 IAssetLoader &m_loader;
293 SDL_GPUDevice *m_gpuDevice = nullptr;
294
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;
301
302 std::unordered_map<std::string, CachedTexture> m_textures;
303 std::unordered_map<uint64_t, WatchedAsset> m_watched;
304 uint64_t m_nextWatchId = 1;
305 };
306}
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.
Definition gpu-texture.h:21
Abstract interface for loading raw asset data by path.