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