-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFEngine.cpp
455 lines (363 loc) · 10.1 KB
/
FEngine.cpp
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "FEngine.h"
using namespace FocalEngine;
#ifdef FOCAL_ENGINE_SHARED
extern "C" __declspec(dllexport) void* GetEngine()
{
return FEngine::GetInstancePointer();
}
#endif
FEngine::FEngine()
{
}
FEngine::~FEngine()
{
}
#include "ResourceManager/Timestamp.h"
std::string FEngine::GetEngineBuildVersion()
{
return ENGINE_BUILD_TIMESTAMP;
}
bool FEngine::IsNotTerminated()
{
return APPLICATION.IsNotTerminated();
}
void FEngine::InternalUpdate()
{
CurrentDeltaTime = CPUTime + GPUTime;
ViewportCheckForModification();
INSTANCED_RENDERING_SYSTEM.Update();
SCENE_MANAGER.Update();
CAMERA_SYSTEM.Update(CurrentDeltaTime);
VIRTUAL_UI_SYSTEM.Update();
NATIVE_SCRIPT_SYSTEM.Update(CurrentDeltaTime);
for (size_t i = 0; i < OnAfterUpdateCallbacks.size(); i++)
{
if (OnAfterUpdateCallbacks[i] == nullptr)
continue;
OnAfterUpdateCallbacks[i]();
}
// Instead of updating TRANSFORM_SYSTEM in the beginning of the frame, we update it here.
// To ensure that all the other systems are updated before the TRANSFORM_SYSTEM will kick in.
TRANSFORM_SYSTEM.Update();
INPUT.Update();
}
void FEngine::BeginFrame(const bool InternalCall)
{
if (!APPLICATION.IsNotTerminated())
return;
if (!InternalCall)
TIME.BeginTimeStamp();
FE_GL_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
APPLICATION.BeginFrame();
if (APPLICATION.GetMainWindow() == nullptr)
return;
APPLICATION.GetMainWindow()->BeginFrame();
#ifdef FE_DEBUG_ENABLED
std::vector<std::string> ShaderList = RESOURCE_MANAGER.GetShaderIDList();
const std::vector<std::string> TempList = RESOURCE_MANAGER.GetEnginePrivateShaderIDList();
for (size_t i = 0; i < TempList.size(); i++)
{
ShaderList.push_back(TempList[i]);
}
for (size_t i = 0; i < ShaderList.size(); i++)
{
if (RESOURCE_MANAGER.GetShader(ShaderList[i])->IsDebugRequest())
{
RESOURCE_MANAGER.GetShader(ShaderList[i])->ThisFrameDebugBind = 0;
}
}
#endif
InternalUpdate();
}
void FEngine::Render(const bool InternalCall)
{
std::vector<FEScene*> ActiveScenes = SCENE_MANAGER.GetScenesByFlagMask(FESceneFlag::Active | FESceneFlag::Renderable);
for (size_t i = 0; i < ActiveScenes.size(); i++)
{
RENDERER.Render(ActiveScenes[i]);
}
if (bVRActive)
{
OpenXR_MANAGER.Update();
RENDERER.SetGLViewport(0, 0, ENGINE.GetDefaultViewport()->GetWidth(), ENGINE.GetDefaultViewport()->GetHeight());
}
APPLICATION.GetMainWindow()->Render();
if (!InternalCall) CPUTime = TIME.EndTimeStamp();
}
void FEngine::EndFrame(const bool InternalCall)
{
INPUT.EndFrame();
if (!InternalCall) TIME.BeginTimeStamp();
APPLICATION.GetMainWindow()->EndFrame();
APPLICATION.EndFrame();
if (!InternalCall) GPUTime = TIME.EndTimeStamp();
// FIXME: Since AssetPackage doesn't extract assets directly to memory, we need to delete the directory after the frame completes.
if (RESOURCE_MANAGER.PrivateEngineAssetPackage != nullptr)
{
FILE_SYSTEM.DeleteDirectory(FILE_SYSTEM.GetCurrentWorkingPath() + "/SubSystems");
delete RESOURCE_MANAGER.PrivateEngineAssetPackage;
RESOURCE_MANAGER.PrivateEngineAssetPackage = nullptr;
}
CurentFrameIndex++;
}
void FEngine::InitWindow(const int Width, const int Height, std::string WindowTitle)
{
FEWindow* NewWindow = APPLICATION.AddWindow(Width, Height, WindowTitle);
// Early initialization of INPUT system.
INPUT;
APPLICATION.GetMainWindow()->AddOnResizeCallback(&FEngine::WindowResizeCallback);
APPLICATION.GetMainWindow()->AddOnDropCallback(&FEngine::DropCallback);
CreateViewport(NewWindow);
FE_GL_ERROR(glEnable(GL_DEPTH_TEST));
// tessellation parameter
FE_GL_ERROR(glPatchParameteri(GL_PATCH_VERTICES, 4));
RENDERER.Init();
// Early initialization of the systems.
TRANSFORM_SYSTEM;
LIGHT_SYSTEM;
CAMERA_SYSTEM;
INSTANCED_RENDERING_SYSTEM;
TERRAIN_SYSTEM;
SKY_DOME_SYSTEM;
PREFAB_INSTANCE_SYSTEM;
VIRTUAL_UI_SYSTEM;
NATIVE_SCRIPT_SYSTEM;
}
void FEngine::SetWindowCaption(const std::string NewCaption)
{
if (APPLICATION.GetMainWindow() != nullptr)
APPLICATION.GetMainWindow()->SetTitle(NewCaption);
}
void FEngine::AddWindowResizeCallback(void(*Func)(int, int))
{
if (Func != nullptr)
ClientWindowResizeCallbacks.push_back(Func);
}
void FEngine::AddWindowCloseCallback(void(*Func)())
{
APPLICATION.GetMainWindow()->AddOnCloseCallback(Func);
}
void FEngine::WindowResizeCallback(const int Width, const int Height)
{
for (size_t i = 0; i < ENGINE.ClientWindowResizeCallbacks.size(); i++)
{
if (ENGINE.ClientWindowResizeCallbacks[i] == nullptr)
continue;
ENGINE.ClientWindowResizeCallbacks[i](Width, Height);
}
}
void FEngine::RenderTo(FEFramebuffer* RenderTo)
{
RenderTo->Bind();
BeginFrame(true);
Render(true);
RenderTo->UnBind();
}
double FEngine::GetCpuTime()
{
return CPUTime;
}
double FEngine::GetGpuTime()
{
return GPUTime;
}
FEPostProcess* FEngine::CreatePostProcess(const std::string Name, int ScreenWidth, int ScreenHeight)
{
if (ScreenWidth < 2 || ScreenHeight < 2)
{
ScreenWidth = ENGINE.GetDefaultViewport()->GetWidth();
ScreenHeight = ENGINE.GetDefaultViewport()->GetHeight();
}
return RESOURCE_MANAGER.CreatePostProcess(ScreenWidth, ScreenHeight, Name);
}
void FEngine::Terminate()
{
APPLICATION.Close();
}
void FEngine::SaveScreenshot(std::string FileName, FEScene* SceneToWorkWith)
{
RENDERER.SaveScreenshot(FileName, SceneToWorkWith);
}
void FEngine::DropCallback(const int Count, const char** Paths)
{
for (size_t i = 0; i < ENGINE.ClientDropCallbacks.size(); i++)
{
if (ENGINE.ClientDropCallbacks[i] == nullptr)
continue;
ENGINE.ClientDropCallbacks[i](Count, Paths);
}
}
void FEngine::AddDropCallback(void(*Func)(int, const char**))
{
if (Func != nullptr)
ClientDropCallbacks.push_back(Func);
}
bool FEngine::IsVsyncEnabled()
{
return bVsyncEnabled;
}
void FEngine::SetVsyncEnabled(bool NewValue)
{
bVsyncEnabled = NewValue;
if (bVsyncEnabled)
{
glfwSwapInterval(1);
}
else
{
glfwSwapInterval(0);
}
}
void FEngine::DisableVR()
{
bVRActive = false;
RENDERER.bVRActive = false;
}
bool FEngine::EnableVR()
{
if (!bVRInitializedCorrectly)
{
bVRInitializedCorrectly = OpenXR_MANAGER.Init(APPLICATION.GetMainWindow()->GetTitle());
}
if (bVRInitializedCorrectly)
{
bVRActive = true;
RENDERER.bVRActive = true;
RENDERER.UpdateVRRenderTargetSize(static_cast<int>(OpenXR_MANAGER.EyeResolution().x), static_cast<int>(OpenXR_MANAGER.EyeResolution().y));
}
else
{
bVRActive = false;
RENDERER.bVRActive = false;
}
return bVRActive;
}
bool FEngine::IsVRInitializedCorrectly()
{
return bVRInitializedCorrectly;
}
bool FEngine::IsVREnabled()
{
return bVRActive;
}
void FEngine::AddOnAfterUpdateCallback(std::function<void()> Callback)
{
OnAfterUpdateCallbacks.push_back(Callback);
}
std::string FEngine::CreateViewport(ImGuiWindow* ImGuiWindowPointer)
{
for (size_t i = 0; i < Viewports.size(); i++)
{
if (Viewports[i]->WindowHandle == ImGuiWindowPointer)
return Viewports[i]->ID;
}
FEViewport* NewViewport = new FEViewport();
NewViewport->Type = FE_VIEWPORT_IMGUI_WINDOW;
NewViewport->WindowHandle = ImGuiWindowPointer;
Viewports.push_back(NewViewport);
return NewViewport->ID;
}
std::string FEngine::CreateViewport(FEWindow* FEWindowPointer)
{
for (size_t i = 0; i < Viewports.size(); i++)
{
if (Viewports[i]->WindowHandle == FEWindowPointer)
return Viewports[i]->ID;
}
FEViewport* NewViewport = new FEViewport();
NewViewport->Type = FE_VIEWPORT_FEWINDOW;
NewViewport->WindowHandle = FEWindowPointer;
Viewports.push_back(NewViewport);
return NewViewport->ID;
}
FEViewport* FEngine::GetViewport(std::string ViewportID)
{
for (size_t i = 0; i < Viewports.size(); i++)
{
if (Viewports[i]->ID == ViewportID)
return Viewports[i];
}
return nullptr;
}
void FEngine::ViewportCheckForModificationIndividual(FEViewport* ViewPort, bool& bMoved, bool& bResize)
{
bMoved = false;
bResize = false;
switch (ViewPort->Type)
{
case FE_VIEWPORT_VIRTUAL:
return;
case FE_VIEWPORT_OS_WINDOW:
return;
case FE_VIEWPORT_GLFW_WINDOW:
return;
case FE_VIEWPORT_FEWINDOW:
{
FEWindow* Window = static_cast<FEWindow*>(ViewPort->WindowHandle);
if (ViewPort->X != 0 || ViewPort->Y != 0)
bMoved = true;
ViewPort->X = 0;
ViewPort->Y = 0;
if (ViewPort->Width != Window->GetWidth() || ViewPort->Height != Window->GetHeight())
bResize = true;
ViewPort->Width = Window->GetWidth();
ViewPort->Height = Window->GetHeight();
return;
}
case FE_VIEWPORT_IMGUI_WINDOW:
{
ImGuiWindow* Window = static_cast<ImGuiWindow*>(ViewPort->WindowHandle);
if (ViewPort->X != static_cast<int>(Window->ContentRegionRect.GetTL().x) || ViewPort->Y != static_cast<int>(Window->ContentRegionRect.GetTL().y))
bMoved = true;
ViewPort->X = static_cast<int>(Window->ContentRegionRect.GetTL().x);
ViewPort->Y = static_cast<int>(Window->ContentRegionRect.GetTL().y);
if (ViewPort->Width != static_cast<int>(Window->ContentRegionRect.GetWidth()) || ViewPort->Height != static_cast<int>(Window->ContentRegionRect.GetHeight()))
bResize = true;
ViewPort->Width = static_cast<int>(Window->ContentRegionRect.GetWidth());
ViewPort->Height = static_cast<int>(Window->ContentRegionRect.GetHeight());
return;
}
}
}
void FEngine::AddOnViewportMovedCallback(std::function<void(std::string)> Callback)
{
OnViewportMovedCallbacks.push_back(Callback);
}
void FEngine::ViewportCheckForModification()
{
for (size_t i = 0; i < Viewports.size(); i++)
{
bool bMoved, bResize;
ViewportCheckForModificationIndividual(Viewports[i], bMoved, bResize);
if (bMoved)
{
for (size_t j = 0; j < OnViewportMovedCallbacks.size(); j++)
{
if (OnViewportMovedCallbacks[j] != nullptr)
OnViewportMovedCallbacks[j](Viewports[i]->ID);
}
}
if (bResize)
{
for (size_t j = 0; j < OnViewportResizeCallbacks.size(); j++)
{
if (OnViewportResizeCallbacks[j] != nullptr)
OnViewportResizeCallbacks[j](Viewports[i]->ID);
}
}
}
}
void FEngine::AddOnViewportResizeCallback(std::function<void(std::string)> Callback)
{
OnViewportResizeCallbacks.push_back(Callback);
}
FEViewport* FEngine::GetDefaultViewport()
{
if (Viewports.size() == 0)
return nullptr;
return Viewports[0];
}
unsigned long long FEngine::GetCurrentFrameIndex()
{
return CurentFrameIndex;
}