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 Class Reference

Manages a set of simultaneously loaded Scenes. Supports Single and Additive loading, async loads via JobSystem, explicit per-scene unloading, and deferred scene transitions. More...

#include <SceneManager.hpp>

Public Member Functions

void loadScene (const std::string &path, LoadSceneMode mode, bool discardPendingAsync=true)
 Queue a scene to load next frame by path.
void loadScene (int sceneId, LoadSceneMode mode, bool discardPendingAsync=true)
 Queue a scene to load next frame by its existing id.
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 thread next frame.
void unloadScene (const std::string &path)
 Immediately queue an unload for the scene with the given path.
void unloadScene (int sceneId)
 Immediately queue an unload for the scene with the given ID.
void changeScene (std::unique_ptr< Scene > nextScene)
 Direct scene swap (used by editor and legacy code). Unloads all current scenes and activates nextScene immediately.
void update (float dt)
 Called each frame. Flushes all pending loads, unloads, and async merges.
void setContext (Registry *reg, VulkanRenderer *rend)
 Provide registry and renderer pointers needed for scene construction.
Scene * getCurrentScene () const
 Returns the first loaded scene (oldest), or nullptr. Backward-compatible.
std::vector< SceneInfo > getLoadedSceneInfos () const
 Returns all currently loaded scene entries.
SceneInfo getSceneInfo (int id) const
 Returns info for a scene by id, or invalid SceneInfo (id=-1) if not found.
SceneInfo getSceneInfo (const std::string &nameOrPath) const
 Returns info for a scene by path/name, or invalid SceneInfo if not found.
bool hasActiveAsyncLoads () const
 True if any async loads are currently in flight.

Public Attributes

std::function< void(const SceneInfo &)> onSceneLoadedCallback
std::function< void(const SceneInfo &)> onSceneUnloadedCallback

Private Member Functions

std::string resolvePath (const std::string &nameOrPath) const
 Resolves a name-or-path string to a canonical scene file path. "level1" -> "assets/scenes/level1.json" "assets/scenes/level1.json" -> "assets/scenes/level1.json".
std::string pathToName (const std::string &path) const
int findSceneIdByPath (const std::string &path) const
void flushPendingUnloads ()
void flushPendingLoads ()
void flushPendingAsyncMerges ()
void unloadSceneByPath (const std::string &path)
void unloadAllScenes ()
SceneInfo spawnScene (const std::string &path, LoadSceneMode mode)
SceneInfo spawnSceneFromContent (const std::string &path, const std::string &jsonContent, LoadSceneMode mode)
void fireSceneLoaded (const SceneInfo &info)
void fireSceneUnloaded (const SceneInfo &info)
void cancelAllAsyncOps ()

Private Attributes

Registry * registry = nullptr
VulkanRenderer * renderer = nullptr
std::atomic< int > nextSceneId {0}
std::vector< LoadedSceneEntry > loadedScenes
std::vector< PendingLoad > pendingLoads
std::vector< std::string > pendingUnloadPaths
std::vector< PendingAsyncMerge > pendingAsyncMerges
std::mutex asyncMergeMutex
std::vector< std::shared_ptr< AsyncOperation > > activeAsyncOps
std::mutex activeAsyncMutex

Detailed Description

Manages a set of simultaneously loaded Scenes. Supports Single and Additive loading, async loads via JobSystem, explicit per-scene unloading, and deferred scene transitions.

Member Function Documentation

◆ cancelAllAsyncOps()

void SceneManager::cancelAllAsyncOps ( )
private

◆ changeScene()

void SceneManager::changeScene ( std::unique_ptr< Scene > nextScene)

Direct scene swap (used by editor and legacy code). Unloads all current scenes and activates nextScene immediately.

◆ findSceneIdByPath()

int SceneManager::findSceneIdByPath ( const std::string & path) const
private

◆ fireSceneLoaded()

void SceneManager::fireSceneLoaded ( const SceneInfo & info)
private

◆ fireSceneUnloaded()

void SceneManager::fireSceneUnloaded ( const SceneInfo & info)
private

◆ flushPendingAsyncMerges()

void SceneManager::flushPendingAsyncMerges ( )
private

◆ flushPendingLoads()

void SceneManager::flushPendingLoads ( )
private

◆ flushPendingUnloads()

void SceneManager::flushPendingUnloads ( )
private

◆ getCurrentScene()

Scene * SceneManager::getCurrentScene ( ) const

Returns the first loaded scene (oldest), or nullptr. Backward-compatible.

◆ getLoadedSceneInfos()

std::vector< SceneInfo > SceneManager::getLoadedSceneInfos ( ) const

Returns all currently loaded scene entries.

◆ getSceneInfo() [1/2]

SceneInfo SceneManager::getSceneInfo ( const std::string & nameOrPath) const

Returns info for a scene by path/name, or invalid SceneInfo if not found.

◆ getSceneInfo() [2/2]

SceneInfo SceneManager::getSceneInfo ( int id) const

Returns info for a scene by id, or invalid SceneInfo (id=-1) if not found.

◆ hasActiveAsyncLoads()

bool SceneManager::hasActiveAsyncLoads ( ) const

True if any async loads are currently in flight.

◆ loadScene() [1/2]

void SceneManager::loadScene ( const std::string & path,
LoadSceneMode mode,
bool discardPendingAsync = true )

Queue a scene to load next frame by path.

Parameters
pathScene file path.
modeSingle or Additive.
discardPendingAsyncCancel in-flight async ops when true.

◆ loadScene() [2/2]

void SceneManager::loadScene ( int sceneId,
LoadSceneMode mode,
bool discardPendingAsync = true )

Queue a scene to load next frame by its existing id.

Parameters
sceneIdNumeric scene ID (must exist in loadedScenes).
modeSingle or Additive.
discardPendingAsyncCancel in-flight async ops when true.

◆ loadSceneAsync()

std::shared_ptr< AsyncOperation > SceneManager::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 thread next frame.

Parameters
pathScene file path.
modeSingle or Additive.
discardPendingAsyncCancel in-flight async ops when true.
Returns
Shared AsyncOperation handle for tracking progress.

◆ pathToName()

std::string SceneManager::pathToName ( const std::string & path) const
private

◆ resolvePath()

std::string SceneManager::resolvePath ( const std::string & nameOrPath) const
private

Resolves a name-or-path string to a canonical scene file path. "level1" -> "assets/scenes/level1.json" "assets/scenes/level1.json" -> "assets/scenes/level1.json".

◆ setContext()

void SceneManager::setContext ( Registry * reg,
VulkanRenderer * rend )

Provide registry and renderer pointers needed for scene construction.

◆ spawnScene()

SceneInfo SceneManager::spawnScene ( const std::string & path,
LoadSceneMode mode )
private

Spawns a DefaultScene from path and registers it with a new id.

◆ spawnSceneFromContent()

SceneInfo SceneManager::spawnSceneFromContent ( const std::string & path,
const std::string & jsonContent,
LoadSceneMode mode )
private

Spawns a scene from pre-loaded JSON string content.

◆ unloadAllScenes()

void SceneManager::unloadAllScenes ( )
private

◆ unloadScene() [1/2]

void SceneManager::unloadScene ( const std::string & path)

Immediately queue an unload for the scene with the given path.

◆ unloadScene() [2/2]

void SceneManager::unloadScene ( int sceneId)

Immediately queue an unload for the scene with the given ID.

◆ unloadSceneByPath()

void SceneManager::unloadSceneByPath ( const std::string & path)
private

◆ update()

void SceneManager::update ( float dt)

Called each frame. Flushes all pending loads, unloads, and async merges.

Parameters
dtDelta time in seconds.

Member Data Documentation

◆ activeAsyncMutex

std::mutex SceneManager::activeAsyncMutex
mutableprivate

◆ activeAsyncOps

std::vector<std::shared_ptr<AsyncOperation> > SceneManager::activeAsyncOps
private

◆ asyncMergeMutex

std::mutex SceneManager::asyncMergeMutex
mutableprivate

◆ loadedScenes

std::vector<LoadedSceneEntry> SceneManager::loadedScenes
private

Currently active scenes. Accessed only on main thread.

◆ nextSceneId

std::atomic<int> SceneManager::nextSceneId {0}
private

◆ onSceneLoadedCallback

std::function<void(const SceneInfo&)> SceneManager::onSceneLoadedCallback

◆ onSceneUnloadedCallback

std::function<void(const SceneInfo&)> SceneManager::onSceneUnloadedCallback

◆ pendingAsyncMerges

std::vector<PendingAsyncMerge> SceneManager::pendingAsyncMerges
private

◆ pendingLoads

std::vector<PendingLoad> SceneManager::pendingLoads
private

◆ pendingUnloadPaths

std::vector<std::string> SceneManager::pendingUnloadPaths
private

◆ registry

Registry* SceneManager::registry = nullptr
private

◆ renderer

VulkanRenderer* SceneManager::renderer = nullptr
private

The documentation for this class was generated from the following files: