Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
scene_manager.cpp
Go to the documentation of this file.
1#include "scene_manager.hpp"
2#include "scene.hpp"
3#include "i_renderer.hpp"
4#include "i_application.hpp"
5#include "texture_cache.hpp"
6#include "model_cache.hpp"
7#include "material_cache.hpp"
8#include "text_renderer.hpp"
9#include "utility.hpp"
10#include "logger.hpp"
11
12#include <chrono>
13
14namespace rendering_engine
15{
16
18 :
19 mRenderer(renderer),
20 mApp(app),
21 mCurrentScene(nullptr),
22 mNextScene(nullptr)
23{}
24
26{
27}
28
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}
93
94void SceneManager::Update(float deltaTime)
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}
127
129{
130 if (mCurrentScene)
131 {
132 mCurrentScene->Draw();
133 }
134}
135
137{
138 if (mCurrentScene)
139 {
140 mCurrentScene->Shutdown();
141 }
142 mMaterialCache->ReleaseAll();
143 mModelCache->ReleaseAll();
144 mTextureCache->ReleaseAll();
145 mTextRenderer->Shutdown();
146}
147
149{
150 return mApp;
151}
152
153std::vector<std::pair<std::string, SceneManager::Factory>>& SceneManager::GetPendingRegistrations()
154{
155 static std::vector<std::pair<std::string, Factory>> pending;
156 return pending;
157}
158
159void SceneManager::LoadScene(std::string sceneName)
160{
161 mNextScene = CreateScene(sceneName, *this);
162}
163
165{
167 rrc.renderer = mRenderer;
168 rrc.meshCache = mModelCache.get();
169 rrc.textureCache = mTextureCache.get();
170 rrc.materialCache = mMaterialCache.get();
171
172 return rrc;
173}
174
175bool SceneManager::RegisterScene(const std::string& name, Factory factory)
176{
177 auto& map = GetMap();
178 return map.emplace(name, std::move(factory)).second;
179}
180
181std::unique_ptr<Scene> SceneManager::CreateScene(const std::string& name, SceneManager& sm)
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}
192
193std::shared_ptr<TextRenderer> SceneManager::GetTextRenderer()
194{
195 return mTextRenderer;
196}
197
198} // 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.
~SceneManager()
Destructor. Cleans up resources and active scenes.
virtual void Draw()
Draws the active scene using the associated renderer.
IApplication * GetApplication()
Returns the application associated with this SceneManager.
static std::string sStartSceneName
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.
std::shared_ptr< TextRenderer > GetTextRenderer()
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
Engine-wide logging system for runtime diagnostics and performance tracking.
#define LOG_INFO(msg)
Definition: logger.hpp:39
Aggregates pointers to global rendering resource managers.