-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngineInit.cpp
More file actions
302 lines (261 loc) · 9.84 KB
/
Copy pathEngineInit.cpp
File metadata and controls
302 lines (261 loc) · 9.84 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
#include "EngineInit.h"
#include "Core/Logger.h"
#include "Core/GarbageCollection.h"
#include "Core/ThreadSyncManager.h"
#include "Core/ThreadPool.h"
#include "Core/Factory.hpp"
#include "Core/AssetResolver.h"
#include "Window/Window.h"
#include "Render/VulkanImpl/VulkanRenderer.h"
#include "Sound/SoundSystem.h"
#include "Game/ImGUImpl.h"
#include "Game/RenderThread.h"
#include "Game/Input.h"
#include "Game/Component/Component.h"
#include "Game/ComponentModule.h"
#include "Game/GameManager.h"
#include "Game/ScriptableObject.h"
#if SH_EDITOR
#include "Editor/EditorResource.h"
#include "Editor/EditorWorld.h"
#include "Editor/AssetDatabase.h"
#include "Editor/UI/CustomInspector.h"
#else
#include "Core/AssetBundle.h"
#include "Render/VulkanImpl/VulkanShaderPassBuilder.h"
#include "Render/VulkanImpl/VulkanContext.h"
#include "Game/World.h"
#include "Game/AssetLoaderFactory.h"
#include "Game/Asset/TextureLoader.h"
#include "Game/Asset/ModelLoader.h"
#include "Game/Asset/MeshLoader.h"
#include "Game/Asset/MaterialLoader.h"
#include "Game/Asset/ShaderLoader.h"
#include "Game/Asset/WorldLoader.h"
#include "Game/Asset/PrefabLoader.h"
#include "Game/Asset/TextLoader.h"
#include "Game/Asset/BinaryLoader.h"
#include "Game/Asset/FontLoader.h"
#include "Game/Asset/SoundLoader.h"
#include "Game/Asset/ScriptableObjectLoader.h"
#include "Game/Asset/ComputeShaderLoader.h"
#include "Game/Asset/TextureAsset.h"
#include "Game/Asset/MaterialAsset.h"
#include "Game/Asset/ShaderAsset.h"
#include "Game/Asset/ModelAsset.h"
#include "Game/Asset/MeshAsset.h"
#include "Game/Asset/WorldAsset.h"
#include "Game/Asset/PrefabAsset.h"
#include "Game/Asset/TextAsset.h"
#include "Game/Asset/BinaryAsset.h"
#include "Game/Asset/FontAsset.h"
#include "Game/Asset/SoundAsset.h"
#include "Game/Asset/ScriptableObjectAsset.h"
#include "Game/Asset/ComputeShaderAsset.h"
#endif
namespace sh
{
EngineInit::EngineInit() :
moduleLoader()
{
}
EngineInit::~EngineInit()
{
Clean();
}
void EngineInit::Clean()
{
SH_INFO("Engine shutdown");
gameManager->Destroy();
#if SH_EDITOR
project.reset();
editor::AssetDatabase::Destroy();
editor::EditorResource::Destroy();
editor::CustomInspectorManager::Destroy();
#endif
renderer->WaitForCurrentFrame();
while(gc->GetRootSetCount() != gc->GetObjectCount())
{
gc->Collect();
gc->DestroyPendingKillObjs();
}
gui.reset();
renderer.reset();
sound::SoundSystem::Destroy();
window.reset();
}
inline void EngineInit::InitResource()
{
SH_INFO("Resource initialization");
#if SH_EDITOR
editor::EditorResource::GetInstance()->LoadAllAssets(*project);
#endif
}
void EngineInit::ProcessInput()
{
game::Input::Update();
sh::window::Event e;
while (window->PollEvent(e))
{
game::Input::UpdateEvent(e);
gui->ProcessEvent(e);
switch (e.type)
{
case sh::window::Event::EventType::Close:
bStop = true;
break;
case sh::window::Event::EventType::Resize:
if (window->GetWidth() == 0)
{
renderer->Pause(true);
}
else
{
renderer->Pause(false);
renderThread->AddBeginTaskFromOtherThread(
[renderer = renderer.get(), width = window->GetWidth(), height = window->GetHeight()]
{
renderer->GetContext()->SetViewport({ 0, 0.f }, { width, height });
}
);
gui->Resize();
}
break;
case sh::window::Event::EventType::WindowFocus:
SH_INFO("FocusIn");
break;
case sh::window::Event::EventType::WindowFocusOut:
SH_INFO("FocusOut");
sh::game::Input::ResetKeyState();
break;
}
}
}
void EngineInit::Start()
{
SH_INFO("Engine start");
game::ComponentModule* componentModule = game::ComponentModule::GetInstance();
componentModule->RegisterWaitingComponents();
SH_INFO_FORMAT("Init {} type", game::ScriptableObject::GetStaticType().name.ToString()); // 타입이 존재하는 곳을 메인 static 영역에 지정하기 위해..
SH_INFO_FORMAT("System thread count: {}", std::thread::hardware_concurrency());
SH_INFO("GarbageCollection initialization");
gc = core::GarbageCollection::GetInstance(); // GC 초기화
SH_INFO("Window initialization");
window = std::make_unique<window::Window>();
window->Create(u8"ShellEngine", 1024, 768, sh::window::Window::Style::Resize);
window->SetFps(limitFps);
SH_INFO("Renderer initialization");
renderer = std::make_unique<sh::render::vk::VulkanRenderer>();
renderer->CreateContext(*window);
renderer->Init(*window);
renderer->GetContext()->SetViewport({ 150.f, 0.f }, { window->GetWidth() - 150.f, window->GetHeight() - 180});
SH_INFO("SoundSystem initialization");
sound::SoundSystem::GetInstance()->Init();
SH_INFO("UIContext initialization");
gui = std::make_unique<game::ImGUImpl>(*window, static_cast<render::vk::VulkanRenderer&>(*renderer));
gui->Init();
SH_INFO("GameManager initialization");
gameManager = game::GameManager::GetInstance();
gameManager->Init(*renderer, *gui);
auto& worldFactory = *core::Factory<game::World, game::World*>::GetInstance();
worldFactory.Register(game::World::GetStaticType().name.ToString(),
[renderer = renderer.get(), componentModule, gui = gui.get()]() -> game::World*
{
return core::SObject::Create<game::World>(*renderer, *gui);
}
);
SH_INFO("Thread creation");
core::ThreadPool::GetInstance()->Init(std::max(2u, std::thread::hardware_concurrency() / 2));
core::ThreadSyncManager::Init();
#if SH_EDITOR
game::Component::SetIsEditor(true);
// 에셋이 다른 에셋을 필요로 하고 있지만 로드가 안 된 상태에서 호출될 것이다.
// 두 에셋이 순환 참조를 하고 있을 때 무한 루프에 빠질 걱정은 할 필요 없다.
// 한 에셋을 로드 할 때 UUID를 먼저 지정한 후에 resolver를 호출 하기 때문
core::AssetResolverRegistry::SetResolver(
[this](const core::UUID& uuid) -> core::SObject*
{
SH_INFO_FORMAT("Using resolver: {}", uuid.ToString());
static auto& assetDatabase = *editor::AssetDatabase::GetInstance();
auto assetInfoPtr = assetDatabase.GetAssetPath(uuid);
if (assetInfoPtr == nullptr)
return nullptr;
if (!assetInfoPtr->originalPath.empty())
return assetDatabase.ImportAsset(assetInfoPtr->originalPath);
return assetDatabase.ImportAsset(assetInfoPtr->cachePath);
}
);
project = std::make_unique<editor::Project>(*renderer, *gui);
worldFactory.Register(editor::EditorWorld::GetStaticType().name.ToString(),
[project = project.get()]() -> game::World*
{
return core::SObject::Create<editor::EditorWorld>(*project);
}
);
auto defaultWorld = core::SObject::Create<editor::EditorWorld>(*project); // 기본 월드
gameManager->LoadWorld(defaultWorld->GetUUID());
#else
static render::vk::VulkanShaderPassBuilder vkShaderPassBuilder{ static_cast<render::vk::VulkanContext&>(*renderer->GetContext()) };
auto assetLoaderFactory = game::AssetLoaderFactory::GetInstance();
assetLoaderFactory->RegisterLoader(game::TextureAsset::ASSET_NAME, std::make_unique<game::TextureLoader>(*renderer->GetContext()));
assetLoaderFactory->RegisterLoader(game::ModelAsset::ASSET_NAME, std::make_unique<game::ModelLoader>(*renderer->GetContext()));
assetLoaderFactory->RegisterLoader(game::MeshAsset::ASSET_NAME, std::make_unique<game::MeshLoader>(*renderer->GetContext()));
assetLoaderFactory->RegisterLoader(game::MaterialAsset::ASSET_NAME, std::make_unique<game::MaterialLoader>(*renderer->GetContext()));
assetLoaderFactory->RegisterLoader(game::ShaderAsset::ASSET_NAME, std::make_unique<game::ShaderLoader>(&vkShaderPassBuilder));
assetLoaderFactory->RegisterLoader(game::WorldAsset::ASSET_NAME, std::make_unique<game::WorldLoader>());
assetLoaderFactory->RegisterLoader(game::PrefabAsset::ASSET_NAME, std::make_unique<game::PrefabLoader>());
assetLoaderFactory->RegisterLoader(game::TextAsset::ASSET_NAME, std::make_unique<game::TextLoader>());
assetLoaderFactory->RegisterLoader(game::BinaryAsset::ASSET_NAME, std::make_unique<game::BinaryLoader>());
assetLoaderFactory->RegisterLoader(game::FontAsset::ASSET_NAME, std::make_unique<game::FontLoader>());
assetLoaderFactory->RegisterLoader(game::SoundAsset::ASSET_NAME, std::make_unique<game::SoundLoader>());
assetLoaderFactory->RegisterLoader(game::ScriptableObjectAsset::ASSET_NAME, std::make_unique<game::ScriptableObjectLoader>());
assetLoaderFactory->RegisterLoader(game::ComputeShaderAsset::ASSET_NAME, std::make_unique<game::ComputeShaderLoader>(*renderer->GetContext()));
core::AssetResolverRegistry::SetResolver(
[this](const core::UUID& uuid) -> core::SObject*
{
auto asset = assetBundle->LoadAsset(uuid);
if (asset == nullptr)
return nullptr;
static game::AssetLoaderFactory& factory = *game::AssetLoaderFactory::GetInstance();
auto assetLoader = factory.GetLoader(asset->GetType());
if (assetLoader == nullptr)
return nullptr;
core::SObject* assetPtr = assetLoader->Load(*asset);
return assetPtr;
}
);
assetBundle = std::make_unique<core::AssetBundle>();
if (!assetBundle->LoadBundle("assets.bundle"))
return;
if (!gameManager->LoadGame("gameManager.bin", *assetBundle))
return;
#endif
SH_INFO("Render thread creation");
renderThread = game::RenderThread::GetInstance();
renderThread->Init(*renderer);
core::ThreadSyncManager::AddThread(*renderThread);
InitResource();
renderThread->Run();
SH_INFO("Start loop");
Loop();
SH_INFO("End loop");
renderThread->Stop();
renderThread->GetThread().join();
}
inline void EngineInit::Loop()
{
while (!bStop)
{
window->ProcessFrame();
std::string deltaTime = std::to_string(window->GetDeltaTime());
deltaTime.erase(deltaTime.begin() + 5, deltaTime.end());
window->SetTitle("ShellEngine [DeltaTime:" + deltaTime + "ms]");
ProcessInput();
if (bStop)
return;
gameManager->UpdateWorlds(window->GetDeltaTime()); // 스레드간 Sync도 여기서 이뤄짐
gc->Update();
core::ThreadSyncManager::AwakeThread();
}
}
}