Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
scene_manager.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
10
11#include <functional>
12#include <memory>
13#include <string>
14#include <unordered_map>
15
16namespace rendering_engine
17{
18class IRenderer;
19class IApplication;
20class Scene;
21class TextureCache;
22class ModelCache;
23class MaterialCache;
24class TextRenderer;
25
26struct RenderResourceContext;
27
28/**
29 * @class SceneManager
30 * @brief Manages scenes, resource caches, and scene transitions within the rendering engine.
31 *
32 * SceneManager handles the creation, updating, and rendering of scenes.
33 * It maintains ownership of core caches (TextureCache, ModelCache, MaterialCache),
34 * and provides the RenderResourceContext that allows drawable objects
35 * to access shared rendering resources.
36 *
37 * The class also supports dynamic scene registration through factories,
38 * enabling runtime creation of scenes by name.
39 *
40 * @see Scene, RenderResourceContext, TextureCache, ModelCache, MaterialCache
41 */
43{
44public:
45 using Factory = std::function<std::unique_ptr<Scene>(SceneManager&)>;
46 /**
47 * @brief Constructs a SceneManager instance.
48 * @param renderer Pointer to the active renderer instance.
49 * @param app Pointer to the owning application instance.
50 */
51 SceneManager(IRenderer* renderer, IApplication* app);
52 /**
53 * @brief Destructor. Cleans up resources and active scenes.
54 */
56 /**
57 * @brief Initializes the current scene and related caches.
58 */
59 virtual void Initialize();
60 /**
61 * @brief Updates the currently active scene.
62 * @param deltaTime Time elapsed since the previous frame, in milliseconds.
63 */
64 virtual void Update(float deltaTime);
65 /**
66 * @brief Draws the active scene using the associated renderer.
67 */
68 virtual void Draw();
69 /**
70 * @brief Releases all scene-related resources and shuts down the manager.
71 */
72 virtual void Shutdown();
73 /**
74 * @brief Returns the application associated with this SceneManager.
75 * @return Pointer to the IApplication instance.
76 */
77 IApplication* GetApplication();
78
79 /**
80 * @brief Type alias for the factory used to create scenes.
81 */
82
83 static std::vector<std::pair<std::string, Factory>>& GetPendingRegistrations();
84
85 /**
86 * @brief Loads a new scene by name.
87 * @param sceneName The name of the registered scene to load.
88 *
89 * If the scene has been registered via SceneAutoRegistrar, it will be instantiated
90 * and prepared for initialization on the next update.
91 */
92 void LoadScene(std::string sceneName);
93 /**
94 * @brief Retrieves the current RenderResourceContext.
95 *
96 * The RenderResourceContext provides references to shared caches (material, model, texture)
97 * and allows drawable objects to access resources during initialization and rendering.
98 *
99 * @return A RenderResourceContext structure populated with shared cache pointers.
100 */
101 RenderResourceContext GetRenderResourceContext() const;
102 /**
103 * @brief Registers a scene type with a name string.
104 * @param name Unique string identifier for the scene.
105 * @param factory Function that constructs the scene when requested.
106 * @return True if registration succeeded, false if a scene with the same name already exists.
107 *
108 * @note Typically used via the REG_SCENE() macro.
109 */
110 static bool RegisterScene(const std::string& name, Factory factory);
111 /**
112 * @brief Creates a scene instance by its registered name.
113 * @param name The name of the registered scene type.
114 * @param sm Reference to the SceneManager creating the scene.
115 * @return Unique pointer to the newly created Scene instance.
116 */
117 static std::unique_ptr<Scene> CreateScene(const std::string& name, SceneManager& sm);
118
119 std::shared_ptr<TextRenderer> GetTextRenderer();
120
121private:
122
123 static std::unordered_map<std::string, Factory>& GetMap()
124 {
125 static std::unordered_map<std::string, Factory> map;
126 return map;
127 }
128public:
129 inline static std::string sStartSceneName;
130
131private:
132 IRenderer* mRenderer;
133 IApplication* mApp;
134
135 std::unique_ptr<Scene> mCurrentScene;
136 std::unique_ptr<Scene> mNextScene;
137
138 std::shared_ptr<TextureCache> mTextureCache;
139 std::shared_ptr<ModelCache> mModelCache;
140 std::shared_ptr<MaterialCache> mMaterialCache;
141 std::shared_ptr<TextRenderer> mTextRenderer;
142};
143/**
144 * @brief Template-based auto-registrar for scenes.
145 *
146 * Registers a scene type automatically at static initialization time.
147 * Used internally by the REG_SCENE() macro.
148 */
149template <class TScene>
151{
152 explicit SceneAutoRegistrar(const char* name)
153 {
155 list.emplace_back(
156 std::string{ name },
157 [](SceneManager& sm) {
158 return std::make_unique<TScene>(sm);
159 });
160 }
161};
162/**
163 * @brief Convenience macro for registering a scene type with a name.
164 * @param Type Class type of the scene.
165 * @param NameStr Name string used for registration.
166 */
167#define REG_SCENE(Type, NameStr) \
168 static rendering_engine::SceneAutoRegistrar<Type> \
169 _auto_registrar_##Type##__{NameStr}
170
171 /**
172 * @brief Helper struct for automatically setting the start scene name.
173 */
175{
177};
178/**
179 * @brief Helper macros for concatenating tokens during static registration.
180 */
181#define RE_CAT_INNER(a,b) a##b
182#define RE_CAT(a,b) RE_CAT_INNER(a,b)
183 /**
184 * @brief Macro for defining the application's start scene.
185 * @param NameStr Name of the scene to start with.
186 *
187 * Example:
188 * @code
189 * START_SCENE("MainMenu");
190 * @endcode
191 */
192#define START_SCENE(NameStr) \
193 static StartSceneAutoSetter RE_CAT(_start_scene_, __COUNTER__){NameStr}
194
195} // namespace rendering_engine
Defines a generic application interface for rendering-based programs.
Defines an abstract interface for rendering backends.
Definition: i_renderer.hpp:29
Manages scenes, resource caches, and scene transitions within the rendering engine.
static std::string sStartSceneName
static std::vector< std::pair< std::string, Factory > > & GetPendingRegistrations()
Type alias for the factory used to create scenes.
std::function< std::unique_ptr< Scene >(SceneManager &)> Factory
#define RE_API
Aggregates pointers to global rendering resource managers.
Template-based auto-registrar for scenes.
Helper struct for automatically setting the start scene name.