Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
rendering_engine::SceneManager Class Reference

Manages scenes, resource caches, and scene transitions within the rendering engine. More...

#include <scene_manager.hpp>

Public Types

using Factory = std::function< std::unique_ptr< Scene >(SceneManager &)>
 

Public Member Functions

 SceneManager (IRenderer *renderer, IApplication *app)
 Constructs a SceneManager instance. More...
 
 ~SceneManager ()
 Destructor. Cleans up resources and active scenes. More...
 
virtual void Initialize ()
 Initializes the current scene and related caches. More...
 
virtual void Update (float deltaTime)
 Updates the currently active scene. More...
 
virtual void Draw ()
 Draws the active scene using the associated renderer. More...
 
virtual void Shutdown ()
 Releases all scene-related resources and shuts down the manager. More...
 
IApplicationGetApplication ()
 Returns the application associated with this SceneManager. More...
 
void LoadScene (std::string sceneName)
 Loads a new scene by name. More...
 
RenderResourceContext GetRenderResourceContext () const
 Retrieves the current RenderResourceContext. More...
 
std::shared_ptr< TextRendererGetTextRenderer ()
 

Static Public Member Functions

static std::vector< std::pair< std::string, Factory > > & GetPendingRegistrations ()
 Type alias for the factory used to create scenes. More...
 
static bool RegisterScene (const std::string &name, Factory factory)
 Registers a scene type with a name string. More...
 
static std::unique_ptr< SceneCreateScene (const std::string &name, SceneManager &sm)
 Creates a scene instance by its registered name. More...
 

Static Public Attributes

static std::string sStartSceneName
 

Detailed Description

Manages scenes, resource caches, and scene transitions within the rendering engine.

SceneManager handles the creation, updating, and rendering of scenes. It maintains ownership of core caches (TextureCache, ModelCache, MaterialCache), and provides the RenderResourceContext that allows drawable objects to access shared rendering resources.

The class also supports dynamic scene registration through factories, enabling runtime creation of scenes by name.

See also
Scene, RenderResourceContext, TextureCache, ModelCache, MaterialCache

Definition at line 42 of file scene_manager.hpp.

Member Typedef Documentation

◆ Factory

using rendering_engine::SceneManager::Factory = std::function<std::unique_ptr<Scene>(SceneManager&)>

Definition at line 45 of file scene_manager.hpp.

Constructor & Destructor Documentation

◆ SceneManager()

rendering_engine::SceneManager::SceneManager ( IRenderer renderer,
IApplication app 
)

Constructs a SceneManager instance.

Parameters
rendererPointer to the active renderer instance.
appPointer to the owning application instance.

Definition at line 17 of file scene_manager.cpp.

18 :
19 mRenderer(renderer),
20 mApp(app),
21 mCurrentScene(nullptr),
22 mNextScene(nullptr)
23{}

◆ ~SceneManager()

rendering_engine::SceneManager::~SceneManager ( )

Destructor. Cleans up resources and active scenes.

Definition at line 25 of file scene_manager.cpp.

26{
27}

Member Function Documentation

◆ CreateScene()

std::unique_ptr< Scene > rendering_engine::SceneManager::CreateScene ( const std::string &  name,
SceneManager sm 
)
static

Creates a scene instance by its registered name.

Parameters
nameThe name of the registered scene type.
smReference to the SceneManager creating the scene.
Returns
Unique pointer to the newly created Scene instance.

Definition at line 181 of file scene_manager.cpp.

182{
183 std::unique_ptr<Scene> scene = nullptr;
184 auto& map = GetMap();
185 auto it = map.find(name);
186 if (it != map.end())
187 {
188 scene = (it->second)(sm);
189 }
190 return scene;
191}

◆ Draw()

void rendering_engine::SceneManager::Draw ( )
virtual

Draws the active scene using the associated renderer.

Definition at line 128 of file scene_manager.cpp.

129{
130 if (mCurrentScene)
131 {
132 mCurrentScene->Draw();
133 }
134}

◆ GetApplication()

IApplication * rendering_engine::SceneManager::GetApplication ( )

Returns the application associated with this SceneManager.

Returns
Pointer to the IApplication instance.

Definition at line 148 of file scene_manager.cpp.

149{
150 return mApp;
151}

◆ GetPendingRegistrations()

std::vector< std::pair< std::string, SceneManager::Factory > > & rendering_engine::SceneManager::GetPendingRegistrations ( )
static

Type alias for the factory used to create scenes.

Definition at line 153 of file scene_manager.cpp.

154{
155 static std::vector<std::pair<std::string, Factory>> pending;
156 return pending;
157}

◆ GetRenderResourceContext()

RenderResourceContext rendering_engine::SceneManager::GetRenderResourceContext ( ) const

Retrieves the current RenderResourceContext.

The RenderResourceContext provides references to shared caches (material, model, texture) and allows drawable objects to access resources during initialization and rendering.

Returns
A RenderResourceContext structure populated with shared cache pointers.

Definition at line 164 of file scene_manager.cpp.

165{
166 RenderResourceContext rrc;
167 rrc.renderer = mRenderer;
168 rrc.meshCache = mModelCache.get();
169 rrc.textureCache = mTextureCache.get();
170 rrc.materialCache = mMaterialCache.get();
171
172 return rrc;
173}

◆ GetTextRenderer()

std::shared_ptr< TextRenderer > rendering_engine::SceneManager::GetTextRenderer ( )

Definition at line 193 of file scene_manager.cpp.

194{
195 return mTextRenderer;
196}

◆ Initialize()

void rendering_engine::SceneManager::Initialize ( )
virtual

Initializes the current scene and related caches.

Definition at line 29 of file scene_manager.cpp.

30{
31 const auto start = std::chrono::steady_clock::now();
32
33 // Apply pending registrations once (first initialization only)
34 static bool sRegistrationsApplied = false;
35 if (!sRegistrationsApplied)
36 {
37 auto& pending = GetPendingRegistrations();
38 for (auto& entry : pending)
39 {
40 RegisterScene(entry.first, entry.second);
41 }
42 pending.clear();
43 sRegistrationsApplied = true;
44 }
45 // Innitialize resources in next order.
46 // 1. Textures
47 // 2. Models
48 // 3. Materials
49 mTextureCache = std::make_shared<TextureCache>(mRenderer);
50 mModelCache = std::make_shared<ModelCache>(mRenderer);
51 mModelCache->CreateQuad2D();
52
53 mMaterialCache = std::make_shared<MaterialCache>(mRenderer);
54 mMaterialCache->CreateBuildInMaterials();
55
56 mTextRenderer = std::make_shared<TextRenderer>(GetRenderResourceContext());
57 mTextRenderer->StoreFontAtlasesInFiles(false);
58
60 {
61 LOG_INFO("Loading assets from package.");
62 mTextureCache->LoadTexturesFromPackage();
63 mModelCache->LoadModelsFromPackage();
64 mTextRenderer->LoadFontsFromPackage();
65 }
66 else
67 {
68 LOG_INFO("Loading assets from folders.");
69 const auto textureFolder = Utility::GetTextureFolderPath();
70 mTextureCache->LoadTexturesFromFolder(textureFolder.string());
71 const auto modelsFolder = Utility::GetModelsFolderPath();
72 mModelCache->LoadModelsFromFolder(modelsFolder.string());
73 const auto fontsFolder = Utility::GetFontsFolderPath();
74 mTextRenderer->LoadFontsFromFolder(fontsFolder.string());
75 }
76
77 if(!GetMap().empty())
78 {
79 const std::string startScene = sStartSceneName;
80
81 if(!startScene.empty())
82 {
83 LoadScene(startScene);
84 }
85 }
86 const auto end = std::chrono::steady_clock::now();
87 const float initMs =
88 std::chrono::duration<float, std::milli>(end - start).count();
89
90 LOG_INFO("SceneManager initialized in " +
91 std::to_string(initMs) + " ms.");
92}
static std::string sStartSceneName
static bool RegisterScene(const std::string &name, Factory factory)
Registers a scene type with a name string.
static std::vector< std::pair< std::string, Factory > > & GetPendingRegistrations()
Type alias for the factory used to create scenes.
void LoadScene(std::string sceneName)
Loads a new scene by name.
RenderResourceContext GetRenderResourceContext() const
Retrieves the current RenderResourceContext.
static boost::filesystem::path GetFontsFolderPath()
Returns absolute path to Content/Fonts.
Definition: utility.cpp:254
static boost::filesystem::path GetTextureFolderPath()
Returns absolute path to Content/Textures.
Definition: utility.cpp:244
static boost::filesystem::path GetModelsFolderPath()
Returns absolute path to Content/Models.
Definition: utility.cpp:249
static bool IsPackageProvided()
Checks whether packed assets (Pack.bin / Pack.json) exist.
Definition: utility.cpp:274
#define LOG_INFO(msg)
Definition: logger.hpp:39

◆ LoadScene()

void rendering_engine::SceneManager::LoadScene ( std::string  sceneName)

Loads a new scene by name.

Parameters
sceneNameThe name of the registered scene to load.

If the scene has been registered via SceneAutoRegistrar, it will be instantiated and prepared for initialization on the next update.

Definition at line 159 of file scene_manager.cpp.

160{
161 mNextScene = CreateScene(sceneName, *this);
162}
static std::unique_ptr< Scene > CreateScene(const std::string &name, SceneManager &sm)
Creates a scene instance by its registered name.

◆ RegisterScene()

bool rendering_engine::SceneManager::RegisterScene ( const std::string &  name,
Factory  factory 
)
static

Registers a scene type with a name string.

Parameters
nameUnique string identifier for the scene.
factoryFunction that constructs the scene when requested.
Returns
True if registration succeeded, false if a scene with the same name already exists.
Note
Typically used via the REG_SCENE() macro.

Definition at line 175 of file scene_manager.cpp.

176{
177 auto& map = GetMap();
178 return map.emplace(name, std::move(factory)).second;
179}

◆ Shutdown()

void rendering_engine::SceneManager::Shutdown ( )
virtual

Releases all scene-related resources and shuts down the manager.

Definition at line 136 of file scene_manager.cpp.

137{
138 if (mCurrentScene)
139 {
140 mCurrentScene->Shutdown();
141 }
142 mMaterialCache->ReleaseAll();
143 mModelCache->ReleaseAll();
144 mTextureCache->ReleaseAll();
145 mTextRenderer->Shutdown();
146}

◆ Update()

void rendering_engine::SceneManager::Update ( float  deltaTime)
virtual

Updates the currently active scene.

Parameters
deltaTimeTime elapsed since the previous frame, in milliseconds.

Definition at line 94 of file scene_manager.cpp.

95{
96 if (mNextScene)
97 {
98 using clock = std::chrono::steady_clock;
99 auto start = clock::now();
100
101 std::string oldSceneName = mCurrentScene ? typeid(*mCurrentScene).name() : "None";
102 std::string newSceneName = typeid(*mNextScene).name();
103
104 if (mCurrentScene)
105 {
106 mCurrentScene->Shutdown();
107 }
108
109 mCurrentScene = std::move(mNextScene);
110
111 LOG_INFO("Switching scene: " + oldSceneName + " -> " + newSceneName);
112
113 mCurrentScene->Initialize();
114 mNextScene = nullptr;
115
116 auto end = clock::now();
117 auto durationMs = std::chrono::duration<float, std::milli>(end - start).count();
118
119 LOG_INFO("Scene '" + newSceneName + "' loaded in " + std::to_string(durationMs) + " ms");
120 }
121
122 if (mCurrentScene)
123 {
124 mCurrentScene->Update(deltaTime);
125 }
126}

Member Data Documentation

◆ sStartSceneName

std::string rendering_engine::SceneManager::sStartSceneName
inlinestatic

Definition at line 129 of file scene_manager.hpp.


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