Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
SceneManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <vector>
6#include <mutex>
7#include <atomic>
8#include <functional>
9
10class Scene;
11class Registry;
12class VulkanRenderer;
14
20 int id = -1;
21 std::string name;
22 std::string path;
23 std::unique_ptr<Scene> scene;
24};
25
31 std::string path;
34 std::shared_ptr<AsyncOperation> asyncOp;
35};
36
42 std::string path;
43 std::string jsonContent;
45 std::shared_ptr<AsyncOperation> asyncOp;
46};
47
55public:
56
57 // -------------------------------------------------------------------------
58 // Synchronous (deferred to next frame)
59 // -------------------------------------------------------------------------
60
67 void loadScene(const std::string& path,
68 LoadSceneMode mode,
69 bool discardPendingAsync = true);
70
77 void loadScene(int sceneId,
78 LoadSceneMode mode,
79 bool discardPendingAsync = true);
80
81 // -------------------------------------------------------------------------
82 // Async load
83 // -------------------------------------------------------------------------
84
93 std::shared_ptr<AsyncOperation> loadSceneAsync(const std::string& path,
94 LoadSceneMode mode,
95 bool discardPendingAsync = true);
96
97 // -------------------------------------------------------------------------
98 // Unload
99 // -------------------------------------------------------------------------
100
104 void unloadScene(const std::string& path);
105
109 void unloadScene(int sceneId);
110
111 // -------------------------------------------------------------------------
112 // Legacy / backward-compatible API
113 // -------------------------------------------------------------------------
114
119 void changeScene(std::unique_ptr<Scene> nextScene);
120
121 // -------------------------------------------------------------------------
122 // Update & Context
123 // -------------------------------------------------------------------------
124
129 void update(float dt);
130
134 void setContext(Registry* reg, VulkanRenderer* rend);
135
136 // -------------------------------------------------------------------------
137 // Events — set by SceneManagement
138 // -------------------------------------------------------------------------
139
140 std::function<void(const SceneInfo&)> onSceneLoadedCallback;
141 std::function<void(const SceneInfo&)> onSceneUnloadedCallback;
142
143 // -------------------------------------------------------------------------
144 // Query
145 // -------------------------------------------------------------------------
146
148 Scene* getCurrentScene() const;
149
151 std::vector<SceneInfo> getLoadedSceneInfos() const;
152
154 SceneInfo getSceneInfo(int id) const;
155
157 SceneInfo getSceneInfo(const std::string& nameOrPath) const;
158
160 bool hasActiveAsyncLoads() const;
161
162private:
163 // --- Internals ---
164 Registry* registry = nullptr;
166
167 std::atomic<int> nextSceneId{0};
168
170 std::vector<LoadedSceneEntry> loadedScenes;
171
172 // Pending operations queued from loadScene() calls (main thread write, main thread flush)
173 std::vector<PendingLoad> pendingLoads;
174 std::vector<std::string> pendingUnloadPaths;
175
176 // Async merge queue — written by background threads, read on main thread
177 std::vector<PendingAsyncMerge> pendingAsyncMerges;
178 mutable std::mutex asyncMergeMutex;
179
180 // Track in-flight async ops so we can cancel them
181 std::vector<std::shared_ptr<AsyncOperation>> activeAsyncOps;
182 mutable std::mutex activeAsyncMutex;
183
184 // --- Helpers ---
185 std::string resolvePath(const std::string& nameOrPath) const;
186 std::string pathToName(const std::string& path) const;
187 int findSceneIdByPath(const std::string& path) const;
188
189 void flushPendingUnloads();
190 void flushPendingLoads();
192 void unloadSceneByPath(const std::string& path);
193 void unloadAllScenes();
194
196 SceneInfo spawnScene(const std::string& path, LoadSceneMode mode);
197
199 SceneInfo spawnSceneFromContent(const std::string& path,
200 const std::string& jsonContent,
201 LoadSceneMode mode);
202
203 void fireSceneLoaded(const SceneInfo& info);
204 void fireSceneUnloaded(const SceneInfo& info);
205 void cancelAllAsyncOps();
206};
LoadSceneMode
Determines whether loading a scene replaces or adds to the current set.
Definition SceneManagement.hpp:15
Coordinates entities and components, providing creation, removal, lookup, and views.
Definition Registry.hpp:30
Abstract base class representing a game scene, managing entity lifecycle and persistence.
Definition Scene.hpp:14
Manages a set of simultaneously loaded Scenes. Supports Single and Additive loading,...
Definition SceneManager.hpp:54
void flushPendingAsyncMerges()
Definition SceneManager.cpp:327
std::vector< PendingAsyncMerge > pendingAsyncMerges
Definition SceneManager.hpp:177
void changeScene(std::unique_ptr< Scene > nextScene)
Direct scene swap (used by editor and legacy code). Unloads all current scenes and activates nextScen...
Definition SceneManager.cpp:297
void unloadAllScenes()
Definition SceneManager.cpp:127
Registry * registry
Definition SceneManager.hpp:164
std::mutex activeAsyncMutex
Definition SceneManager.hpp:182
void setContext(Registry *reg, VulkanRenderer *rend)
Provide registry and renderer pointers needed for scene construction.
Definition SceneManager.cpp:58
void cancelAllAsyncOps()
Definition SceneManager.cpp:81
std::vector< PendingLoad > pendingLoads
Definition SceneManager.hpp:173
std::vector< std::shared_ptr< AsyncOperation > > activeAsyncOps
Definition SceneManager.hpp:181
Scene * getCurrentScene() const
Returns the first loaded scene (oldest), or nullptr. Backward-compatible.
Definition SceneManager.cpp:376
VulkanRenderer * renderer
Definition SceneManager.hpp:165
std::function< void(const SceneInfo &)> onSceneUnloadedCallback
Definition SceneManager.hpp:141
void fireSceneLoaded(const SceneInfo &info)
Definition SceneManager.cpp:67
std::mutex asyncMergeMutex
Definition SceneManager.hpp:178
bool hasActiveAsyncLoads() const
True if any async loads are currently in flight.
Definition SceneManager.cpp:97
std::string pathToName(const std::string &path) const
Definition SceneManager.cpp:40
std::vector< SceneInfo > getLoadedSceneInfos() const
Returns all currently loaded scene entries.
Definition SceneManager.cpp:381
SceneInfo spawnSceneFromContent(const std::string &path, const std::string &jsonContent, LoadSceneMode mode)
Definition SceneManager.cpp:163
std::shared_ptr< AsyncOperation > loadSceneAsync(const std::string &path, LoadSceneMode mode, bool discardPendingAsync=true)
Start an async scene load. File I/O runs on a JobSystem worker; entity spawning is merged on the main...
Definition SceneManager.cpp:226
void unloadScene(const std::string &path)
Immediately queue an unload for the scene with the given path.
Definition SceneManager.cpp:279
std::function< void(const SceneInfo &)> onSceneLoadedCallback
Definition SceneManager.hpp:140
SceneInfo spawnScene(const std::string &path, LoadSceneMode mode)
Definition SceneManager.cpp:141
std::vector< LoadedSceneEntry > loadedScenes
Definition SceneManager.hpp:170
void unloadSceneByPath(const std::string &path)
Definition SceneManager.cpp:109
SceneInfo getSceneInfo(int id) const
Returns info for a scene by id, or invalid SceneInfo (id=-1) if not found.
Definition SceneManager.cpp:389
void fireSceneUnloaded(const SceneInfo &info)
Definition SceneManager.cpp:72
void flushPendingUnloads()
Definition SceneManager.cpp:313
void flushPendingLoads()
Definition SceneManager.cpp:320
std::vector< std::string > pendingUnloadPaths
Definition SceneManager.hpp:174
std::atomic< int > nextSceneId
Definition SceneManager.hpp:167
std::string resolvePath(const std::string &nameOrPath) const
Resolves a name-or-path string to a canonical scene file path. "level1" -> "assets/scenes/level1....
Definition SceneManager.cpp:30
void update(float dt)
Called each frame. Flushes all pending loads, unloads, and async merges.
Definition SceneManager.cpp:362
int findSceneIdByPath(const std::string &path) const
Definition SceneManager.cpp:44
void loadScene(const std::string &path, LoadSceneMode mode, bool discardPendingAsync=true)
Queue a scene to load next frame by path.
Definition SceneManager.cpp:200
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.
Definition VulkanRenderer.hpp:127
Internal record linking a Scene instance with its metadata.
Definition SceneManager.hpp:19
std::string path
Definition SceneManager.hpp:22
std::unique_ptr< Scene > scene
Definition SceneManager.hpp:23
std::string name
Definition SceneManager.hpp:21
A completed async I/O load ready to be merged onto the main thread.
Definition SceneManager.hpp:41
std::string path
Definition SceneManager.hpp:42
std::shared_ptr< AsyncOperation > asyncOp
Definition SceneManager.hpp:45
std::string jsonContent
Raw JSON read from disk on background thread.
Definition SceneManager.hpp:43
LoadSceneMode mode
Definition SceneManager.hpp:44
A queued synchronous or async scene load request.
Definition SceneManager.hpp:30
std::string path
Definition SceneManager.hpp:31
std::shared_ptr< AsyncOperation > asyncOp
Non-null for async loads.
Definition SceneManager.hpp:34
bool discardPendingAsync
Definition SceneManager.hpp:33
LoadSceneMode mode
Definition SceneManager.hpp:32
Lightweight descriptor for a known or loaded scene.
Definition SceneManagement.hpp:24