-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cpp
More file actions
377 lines (346 loc) · 14 KB
/
Core.cpp
File metadata and controls
377 lines (346 loc) · 14 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <map>
#include <string>
#include "Core.h"
#include <fstream>
#include "FileSystem/loadStrFromFile.h"
#include "Graphics/Graphics.h"
#include "Graphics/Mesh/MeshUtil.h"
#include "Graphics/Renderer/RenderUtil.h"
#include "Graphics/Renderer/DeferredRenderer.h"
#include "Graphics/Mesh/MeshRenderer.h"
#include "System/Clock.h"
#include <thread>
#include "DefaultTags.h"
#include "System/Debug.h"
#include "libs/imgui/imgui.h"
#include "libs/imgui/backends/imgui_impl_opengl3.h"
#include "libs/imgui/backends/imgui_impl_glfw.h"
#include "NlohmannJson/json.hpp"
#include "System/DLLoader.h"
#include "GameNode/ComponentFactory.h"
#include "Editor/UI/UISpace.h"
#include "Graphics/Renderer/IdRenderer.h"
#include "Editor/UI/EditorUI/GameAssetsBrowser.h"
#include "Graphics/Renderer/IdRenderer.h"
#include "Editor/UI/SelectedObjectOutlinePostProcess.h"
#include "Graphics/GUI/Text/FreeTypeUtil.h"
using Json = nlohmann::json;
namespace TealEngine
{
namespace Core
{
DeferredRenderer renderer;
IdRenderer idRenderer;
GameNode* sceneRoot;
Clock sceneclock;
UISpace uiSpace;
GameNode3D* idCameraNode;
Camera* idCamera;
ActionList actionList;
Project currentProject;
TextureManager textureManager;
ModelsManager modelsManager;
ShadersManager shadersManager;
MaterialsManager materialsManager;
FontManager fontManager;
PhysicsScene physicsScene;
ShapesRenderer shapesRenderer;
Profiler profiler;
Json originalSceneJson;
GameNode* originalScene;
bool modulesNeedReload = false;
void requestModulesReload() { modulesNeedReload = true; }
GameNode* nextScene = nullptr;
std::string nextScenePath = "";
EngineState engineState = EngineState::GAME_STOPPED;
EngineState targetEngineState = EngineState::GAME_STOPPED;
void playImpl()
{
originalScene = sceneRoot;
sceneRoot = GameNode::nodeFromJson(sceneRoot->toJson());
}
void stopImpl()
{
delete sceneRoot;
sceneRoot = originalScene;
}
void update()
{
Profiler::ProfilerPoint frameProf(&profiler, "Frame");
static float updateTimer = 0.0f;
sceneclock.update();
auto frameStart = std::chrono::high_resolution_clock::now();
Profiler::ProfilerPoint renderProf(&profiler, "Render");
//render ids
Profiler::ProfilerPoint idsProf(&profiler, "Ids");
/*
if(renderer.getActiveCamera())
{
if(idCamera->renderTexture.getWidth() != renderer.getActiveCamera()->renderTexture.getWidth() || idCamera->renderTexture.getHeight() != renderer.getActiveCamera()->renderTexture.getHeight())
{
idCamera->renderTexture.create(renderer.getActiveCamera()->renderTexture.getWidth(), renderer.getActiveCamera()->renderTexture.getHeight());
idRenderer.resize(idCamera->renderTexture.getWidth(), idCamera->renderTexture.getHeight());
}
idCameraNode->setRelativeTransform(renderer.getActiveCamera()->getParentOfType<GameNode3D>()->getWorldTransform());
switch(renderer.getActiveCamera()->getProjectionType())
{
case CameraProjectionType::PERSPECTIVE_CAMERA_PROJECTION:
idCamera->setPerspectiveProjection(renderer.getActiveCamera()->getFOV(), renderer.getActiveCamera()->getAspect(), renderer.getActiveCamera()->getNear(), renderer.getActiveCamera()->getFar());
break;
case CameraProjectionType::ORTHO_CAMERA_PROJECTION:
//idCamera->setOrthoProjection(renderer.getActiveCamera()->width, );
break;
}
idRenderer.render(sceneRoot);
}*/
idsProf.end();
//render game
Profiler::ProfilerPoint defferedProf(&profiler, "Deferred render");
renderer.render(sceneRoot);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
//Display render texture on screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0, Graphics::window->getWindowWidth(), Graphics::window->getWindowHeight());
if(renderer.getActiveCamera())
{
glDisable(GL_BLEND);
Render::renderTexture(renderer.getActiveCamera()->renderTexture.id());
}
defferedProf.end();
//Render GUI
Profiler::ProfilerPoint guiProf(&profiler, "Engine GUI");
sceneRoot->GUIRender();
guiProf.end();
//ImGUI
Profiler::ProfilerPoint imGuiProf(&profiler, "ImGui GUI");
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
//Editor ui
uiSpace.display();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
imGuiProf.end();
renderProf.end();
Profiler::ProfilerPoint updateProf(&profiler, "Update");
//update modules
if(modulesNeedReload)
{
reloadModules();
modulesNeedReload = false;
}
//Switch scene
if(nextScene)
{
delete sceneRoot;
sceneRoot = nextScene;
nextScene = nullptr;
}
else if(nextScenePath.length())
{
delete sceneRoot;
sceneRoot = GameNode::loadNodeFromJsonFile(nextScenePath);
nextScenePath = "";
}
//Update engine state
if(engineState == EngineState::GAME_STOPPED && targetEngineState != EngineState::GAME_STOPPED)
{
//we need engine state to be "GAME_PLAYING" so that onAttach and other play only callbacks get called
engineState = EngineState::GAME_PLAYING;
playImpl();
}
else if(engineState != EngineState::GAME_STOPPED && targetEngineState == EngineState::GAME_STOPPED)
{
stopImpl();
}
engineState = targetEngineState;
Input::inputUpdate();
//run game logics
if(engineState == EngineState::GAME_PLAYING)
{
sceneRoot->updateAll();
physicsScene.CollisionCheck();
physicsScene.CollisionFlush();
}
else
{
sceneRoot->editorUpdate();
}
//destruct objects marked as destroyed
GameNode::cleanupDestroyed();
updateProf.end();
//Swap buffers and other
Profiler::ProfilerPoint displayProf(&profiler, "Display");
Graphics::display();
displayProf.end();
}
void init()
{
//load settings
std::fstream settingsFile("settings.json");
Json settingsJson = Json::parse(settingsFile);
//initialise systems
Graphics::init(settingsJson["title"]);
BasicMeshes::init();
Input::init();
lightInit();
uiSpace.init();
FreeTypeUtil::FreeTypeInitialize();
//setup deferred renderer
renderer.resize(Graphics::window->getWindowWidth(), Graphics::window->getWindowHeight());
renderer.setDepthTest(true);
renderer.setDepthClear(true);
renderer.setColorClear(true);
renderer.setClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
//setup id renderer
IdRenderer::loadIdShader();
idRenderer.setDepthTest(true);
idRenderer.setDepthClear(true);
idRenderer.setColorClear(true);
idRenderer.setClearColor(vec4(0.0f, 0.0f, 0.0f, 0.0f));
//ImGUI
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("./Assets/Fonts/DroidSansMono.ttf", 18.0f);
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImVec4* colors = ImGui::GetStyle().Colors;
ImGuiStyle& style = ImGui::GetStyle();
style.Alpha = 1.0f;
style.Colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.13f, 0.14f, 0.15f, 0.20f);
style.Colors[ImGuiCol_ChildBg] = ImVec4(0.13f, 0.14f, 0.15f, 1.00f);
style.Colors[ImGuiCol_PopupBg] = ImVec4(0.13f, 0.14f, 0.15f, 1.00f);
style.Colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);
style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style.Colors[ImGuiCol_FrameBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.38f, 0.38f, 0.38f, 1.00f);
style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.67f, 0.67f, 0.67f, 0.39f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.08f, 0.08f, 0.09f, 1.00f);
style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.08f, 0.08f, 0.09f, 1.00f);
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f);
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f);
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f);
style.Colors[ImGuiCol_CheckMark] = ImVec4(0.11f, 0.64f, 0.92f, 1.00f);
style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.11f, 0.64f, 0.92f, 1.00f);
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.08f, 0.50f, 0.72f, 1.00f);
style.Colors[ImGuiCol_Button] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.38f, 0.38f, 0.38f, 1.00f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.67f, 0.67f, 0.67f, 0.39f);
style.Colors[ImGuiCol_Header] = ImVec4(0.22f, 0.22f, 0.22f, 1.00f);
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.67f, 0.67f, 0.67f, 0.39f);
style.Colors[ImGuiCol_Separator] = style.Colors[ImGuiCol_Border];
style.Colors[ImGuiCol_SeparatorHovered] = ImVec4(0.41f, 0.42f, 0.44f, 1.00f);
style.Colors[ImGuiCol_SeparatorActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.29f, 0.30f, 0.31f, 0.67f);
style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
style.Colors[ImGuiCol_Tab] = ImVec4(0.08f, 0.08f, 0.09f, 0.83f);
style.Colors[ImGuiCol_TabHovered] = ImVec4(0.33f, 0.34f, 0.36f, 0.83f);
style.Colors[ImGuiCol_TabActive] = ImVec4(0.23f, 0.23f, 0.24f, 1.00f);
style.Colors[ImGuiCol_TabUnfocused] = ImVec4(0.08f, 0.08f, 0.09f, 1.00f);
style.Colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.13f, 0.14f, 0.15f, 1.00f);
style.Colors[ImGuiCol_DockingPreview] = ImVec4(0.26f, 0.59f, 0.98f, 0.70f);
style.Colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f);
style.Colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);
style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
style.Colors[ImGuiCol_DragDropTarget] = ImVec4(0.11f, 0.64f, 0.92f, 1.00f);
style.Colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
style.Colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);
style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);
style.Colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
style.GrabRounding = style.FrameRounding = 2.3f;
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL((GLFWwindow*)Graphics::window->gl_window_ptr_(), true);
ImGui_ImplOpenGL3_Init("#version 130");
SelectedObjectOutlinePostProcess::loadShader();
gameAssetsBrowser->setPath(std::filesystem::current_path());
gameAssetsBrowser->setRootPath(std::filesystem::current_path());
sceneRoot = new GameNode3D();
idCameraNode = new GameNode3D();
idCamera = new Camera();
idCamera->renderTexture = Texture(GL_TEXTURE_2D, GL_R32I, GL_RED_INTEGER, GL_INT);
idCamera->renderTexture.create(Graphics::window->getWindowWidth(), Graphics::window->getWindowHeight());
idCameraNode->attachComponent(idCamera);
idRenderer.setCamera(idCamera);
}
GameNode* getRoot()
{
return sceneRoot;
}
void setProject(Project project)
{
currentProject = project;
requestModulesReload();
setScene(currentProject.getDefaultScene());
gameAssetsBrowser->setPath(project.getPath()+"/GameAssets");
gameAssetsBrowser->setRootPath(project.getPath()+"/GameAssets");
textureManager.clearAll();
textureManager.loadRecursive(project.getPath()+"/Assets");
textureManager.loadRecursive(project.getPath()+"/GameAssets");
modelsManager.clearAll();
modelsManager.loadRecursive(project.getPath()+"/Assets");
modelsManager.loadRecursive(project.getPath()+"/GameAssets");
shadersManager.clearAll();
shadersManager.loadRecursive(project.getPath()+"/Assets");
shadersManager.loadRecursive(project.getPath()+"/GameAssets");
materialsManager.clearAll();
materialsManager.loadRecursive(project.getPath()+"/Assets");
materialsManager.loadRecursive(project.getPath()+"/GameAssets");
fontManager.clearAll();
fontManager.loadRecursive(project.getPath()+"/Assets");
fontManager.loadRecursive(project.getPath()+"/GameAssets");
std::filesystem::current_path(project.getPath());
}
void setScene(GameNode* node)
{
nextScene = node;
}
void setScene(const std::string& scenePath)
{
nextScenePath = scenePath;
}
void play()
{
targetEngineState = EngineState::GAME_PLAYING;
}
void pause()
{
targetEngineState = EngineState::GAME_PAUSED;
}
void stop()
{
targetEngineState = EngineState::GAME_STOPPED;
}
EngineState getEngineState()
{
return engineState;
}
void reloadModules()
{
currentProject.buildLibs();
Json currentScene;
if(sceneRoot)
{
//save current scene to restore after new libs are loaded
currentScene = sceneRoot->toJson();
//delete scene while old libs are still loaded so proper destructors are called
delete sceneRoot;
}
currentProject.loadLibs();
//restore scene with new libs loaded
if(sceneRoot)
sceneRoot = GameNode::nodeFromJson(currentScene);
}
}
}