-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathEngine.cpp
More file actions
182 lines (138 loc) · 5.7 KB
/
Engine.cpp
File metadata and controls
182 lines (138 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "Engine.h"
#include "Timer.h"
#include "Input.h"
#include "ViewFrustum.h"
#include "ResourceManager.h"
#include "SceneBase.h"
#include "FrameStats.h"
#include "Platform/Platform.h"
#include <GLFW/glfw3.h>
#include <pugixml.hpp>
#include <iostream>
#include <algorithm>
#include <execution>
#include <thread>
/***********************************************************************************/
void connectWindowInstanceToInput(GLFWwindow* window) {
const auto resizeCallback = [](GLFWwindow* w, auto width, auto height) {
Input::GetInstance().windowResized(width, height);
};
glfwSetWindowSizeCallback(window, resizeCallback);
const auto keyCallback = [](GLFWwindow* w, auto key, auto scancode, auto action, auto mode) {
Input::GetInstance().keyPressed(key, scancode, action, mode);
};
glfwSetKeyCallback(window, keyCallback);
const auto cursorPosCallback = [](GLFWwindow* w, auto xPos, auto yPos) {
Input::GetInstance().mouseMoved(xPos, yPos);
};
glfwSetCursorPosCallback(window, cursorPosCallback);
}
/***********************************************************************************/
double frameTimeMilliseconds(const unsigned int numFramesRendered) {
return 1000.0 / static_cast<double>(numFramesRendered);
}
/***********************************************************************************/
double framesPerSecond(const double frameTimeMilliseconds) {
return 1.0 / (frameTimeMilliseconds / 1000.0);
}
/***********************************************************************************/
Engine::Engine(const std::filesystem::path& configPath) {
std::cout << "**************************************************\n";
std::cout << "Engine starting up...\n";
#ifdef _DEBUG
std::cout << "DEBUG MODE!!\n";
#endif
std::cout << "**************************************************\n";
std::cout << "Available processor cores: " << std::thread::hardware_concurrency() << '\n';
std::cout << "**************************************************\n";
std::cout << "Loading Engine config file...\n";
pugi::xml_document doc;
const auto& result{ doc.load_string(ResourceManager::GetInstance().LoadTextFile(configPath).data()) };
std::cout << "Engine config load result: " << result.description() << std::endl;
const auto& engineNode{ doc.child("Engine") };
std::cout << "**************************************************\n";
std::cout << "Initializing Window...\n";
auto* window{ m_window.Init(engineNode.child("Window")) };
connectWindowInstanceToInput(window);
std::cout << "**************************************************\n";
std::cout << "Initializing OpenGL Renderer...\n";
m_renderer.Init(engineNode.child("Renderer"));
m_guiSystem.Init(m_window.m_window);
}
/***********************************************************************************/
void Engine::AddScene(const std::shared_ptr<SceneBase>& scene) {
m_scenes.try_emplace(scene->GetName(), scene);
}
/***********************************************************************************/
void Engine::SetActiveScene(const std::string_view sceneName) {
const auto& scene{ m_scenes.find(sceneName.data()) };
if (scene == m_scenes.end()) {
std::cerr << "Engine Error: Scene not found: " << sceneName << std::endl;
return;
}
m_activeScene = scene->second.get();
m_renderer.UpdateView(m_camera);
}
/***********************************************************************************/
void Engine::Execute() {
if (m_activeScene == nullptr) {
std::cerr << "Engine Error: No active scene specified!" << std::endl;
std::abort();
}
std::cout << "**************************************************\n";
std::cout << "Engine initialization complete!\n";
std::cout << "**************************************************\n";
bool hasOneSecondPassed{ false };
Timer timer(1.0, [&]() {
hasOneSecondPassed = true;
});
// Main loop
unsigned int numFramesRendered{ 0 };
FrameStats frameStats;
bool showFrameStats{ true };
while (!m_window.ShouldClose()) {
timer.Update(glfwGetTime());
if (hasOneSecondPassed) {
const auto frameTime{ frameTimeMilliseconds(numFramesRendered) };
frameStats.frameTimeMilliseconds = frameTime;
frameStats.videoMemoryUsageKB = m_renderer.GetVideoMemUsageKB();
frameStats.ramUsageKB = Platform::maxRSSKb();
numFramesRendered = 0;
hasOneSecondPassed = false;
}
const auto dt{ timer.GetDelta() };
Input::GetInstance().Update();
m_window.Update();
const auto& [width, height] = m_window.GetFramebufferDims();
m_camera.Update(dt);
m_activeScene->Update(dt);
m_renderer.Update(m_camera);
const auto& renderList{ cullViewFrustum() };
m_renderer.Render(m_camera, renderList.cbegin(), renderList.cend(), *m_activeScene, false);
m_guiSystem.Render(width, height, frameStats);
m_window.SwapBuffers();
++numFramesRendered;
}
shutdown();
}
/***********************************************************************************/
void Engine::shutdown() {
m_guiSystem.Shutdown();
m_renderer.Shutdown();
ResourceManager::GetInstance().ReleaseAllResources();
m_window.Shutdown();
}
/***********************************************************************************/
std::vector<ModelPtr> Engine::cullViewFrustum() const {
std::vector<ModelPtr> renderList;
const auto& dims{ m_window.GetFramebufferDims() };
const ViewFrustum viewFrustum(m_camera.GetViewMatrix(), m_camera.GetProjMatrix(dims.first, dims.second));
std::for_each(std::execution::par_unseq, m_activeScene->m_sceneModels.cbegin(), m_activeScene->m_sceneModels.cend(),
[&](const auto& model) {
const auto result{ viewFrustum.TestIntersection(model->GetBoundingBox()) };
if (result == BoundingVolume::TestResult::INSIDE || result == BoundingVolume::TestResult::INTERSECT) {
renderList.push_back(model);
}
});
return renderList;
}