fix: alot of stuff changed. Mostly bugfixxes / architechture changes
This commit is contained in:
+98
-34
@@ -1,9 +1,12 @@
|
||||
#include <chrono>
|
||||
#include <exception>
|
||||
#include <SDL_vulkan.h>
|
||||
#include <thread>
|
||||
#include <stdexcept>
|
||||
#include <destrum/App.h>
|
||||
|
||||
#include <destrum/FS/AssetFS.h>
|
||||
#include <destrum/Scene/SceneManager.h>
|
||||
#include <destrum/Util/DeltaTime.h>
|
||||
|
||||
#include "imgui.h"
|
||||
@@ -25,41 +28,60 @@ App::App()
|
||||
{
|
||||
}
|
||||
|
||||
App::~App()
|
||||
{
|
||||
if (!cleanedUp) {
|
||||
try {
|
||||
cleanup();
|
||||
} catch (...) {
|
||||
// Destructors must not throw. Initialization failures are already
|
||||
// reported by the caller, while cleanup is best effort here.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void App::init(const AppParams& params)
|
||||
{
|
||||
m_params = params;
|
||||
ZoneScopedN("App::init");
|
||||
tracy::SetThreadName("Main Thread");
|
||||
TracySetProgramName(params.appName.c_str());
|
||||
AssetFS::GetInstance().Init(params.exeDir);
|
||||
// AssetFS::GetInstance().Mount("engine", params.exeDir / "assets" / "engine");
|
||||
// AssetFS::GetInstance().Mount("game", params.exeDir / "assets" / "game");
|
||||
cleanedUp = false;
|
||||
customInitStarted = false;
|
||||
try {
|
||||
m_params = params;
|
||||
ZoneScopedN("App::init");
|
||||
tracy::SetThreadName("Main Thread");
|
||||
TracySetProgramName(params.appName.c_str());
|
||||
AssetFS::GetInstance().Init(params.exeDir);
|
||||
|
||||
window = SDL_CreateWindow(
|
||||
params.windowTitle.c_str(),
|
||||
// pos
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
// size
|
||||
params.windowSize.x,
|
||||
params.windowSize.y,
|
||||
SDL_WINDOW_VULKAN);
|
||||
window = SDL_CreateWindow(
|
||||
params.windowTitle.c_str(),
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
params.windowSize.x,
|
||||
params.windowSize.y,
|
||||
SDL_WINDOW_VULKAN);
|
||||
|
||||
SDL_SetWindowResizable(window, SDL_TRUE);
|
||||
if (!window)
|
||||
{
|
||||
spdlog::error("Failed to create window. SDL Error: {}", SDL_GetError());
|
||||
std::exit(1);
|
||||
if (!window) {
|
||||
spdlog::error("Failed to create window. SDL Error: {}", SDL_GetError());
|
||||
throw std::runtime_error(
|
||||
"Failed to create window: " + std::string{SDL_GetError()});
|
||||
}
|
||||
SDL_SetWindowResizable(window, SDL_TRUE);
|
||||
|
||||
gfxDevice.init(window, params.appName, false);
|
||||
imguiPass.init(window, gfxDevice);
|
||||
|
||||
InputManager::GetInstance().Init();
|
||||
Time::GetInstance().Update();
|
||||
|
||||
customInitStarted = true;
|
||||
customInit();
|
||||
cleanedUp = false;
|
||||
} catch (...) {
|
||||
try {
|
||||
cleanup();
|
||||
} catch (...) {
|
||||
}
|
||||
throw;
|
||||
}
|
||||
|
||||
gfxDevice.init(window, params.appName, false);
|
||||
imguiPass.init(window, gfxDevice);
|
||||
|
||||
|
||||
InputManager::GetInstance().Init();
|
||||
Time::GetInstance().Update();
|
||||
|
||||
customInit();
|
||||
}
|
||||
|
||||
void App::run()
|
||||
@@ -95,9 +117,8 @@ void App::run()
|
||||
accumulator += dt;
|
||||
|
||||
{
|
||||
ZoneScopedN("Input BeginFrame + Camera");
|
||||
ZoneScopedN("Input BeginFrame");
|
||||
InputManager::GetInstance().BeginFrame();
|
||||
camera.Update(dt);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -157,6 +178,10 @@ void App::run()
|
||||
|
||||
if (!isRunning) break;
|
||||
|
||||
// Consume SDL events before updating the base camera so held and
|
||||
// newly pressed inputs are applied in the same frame.
|
||||
camera.Update(dt);
|
||||
|
||||
{
|
||||
ZoneScopedN("ImGui BeginFrame");
|
||||
imguiPass.beginFrame();
|
||||
@@ -258,6 +283,7 @@ void App::run()
|
||||
{
|
||||
ZoneScopedN("Recreate Swapchain");
|
||||
gfxDevice.recreateSwapchain(w, h);
|
||||
imguiPass.onSwapchainRecreated();
|
||||
onWindowResize(w, h);
|
||||
}
|
||||
|
||||
@@ -288,8 +314,46 @@ void App::run()
|
||||
|
||||
void App::cleanup()
|
||||
{
|
||||
if (cleanedUp || cleaningUp) {
|
||||
return;
|
||||
}
|
||||
|
||||
cleaningUp = true;
|
||||
spdlog::info("Cleaning up");
|
||||
customCleanup();
|
||||
|
||||
std::exception_ptr firstException;
|
||||
const auto attempt = [&firstException](auto&& operation) {
|
||||
try {
|
||||
operation();
|
||||
} catch (...) {
|
||||
if (!firstException) {
|
||||
firstException = std::current_exception();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (customInitStarted) {
|
||||
attempt([this] { customCleanup(); });
|
||||
}
|
||||
attempt([] { SceneManager::GetInstance().Destroy(); });
|
||||
attempt([this] { renderer.cleanup(gfxDevice); });
|
||||
attempt([this] { imguiPass.cleanup(); });
|
||||
attempt([this] { resources.cleanup(gfxDevice); });
|
||||
attempt([this] { gfxDevice.cleanup(); });
|
||||
|
||||
if (window) {
|
||||
SDL_DestroyWindow(window);
|
||||
window = nullptr;
|
||||
}
|
||||
|
||||
AssetFS::GetInstance().Reset();
|
||||
customInitStarted = false;
|
||||
cleanedUp = true;
|
||||
cleaningUp = false;
|
||||
|
||||
if (firstException) {
|
||||
std::rethrow_exception(firstException);
|
||||
}
|
||||
}
|
||||
|
||||
void App::drawPhysicsPanel(float dt, float fixedDt, float accumulator)
|
||||
@@ -336,4 +400,4 @@ void App::drawPhysicsPanel(float dt, float fixedDt, float accumulator)
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user