Files
XCEngine/editor/src/Application.cpp

136 lines
4.1 KiB
C++
Raw Normal View History

#include "Application.h"
2026-03-26 23:52:05 +08:00
#include "Core/EditorWindowTitle.h"
#include "Layers/EditorLayer.h"
#include "Core/EditorContext.h"
#include "Core/EditorConsoleSink.h"
2026-03-26 21:18:33 +08:00
#include "Core/EditorEvents.h"
#include "Core/EventBus.h"
2026-03-26 23:52:05 +08:00
#include "Platform/Win32Utf8.h"
#include <XCEngine/Debug/Logger.h>
#include <XCEngine/Debug/FileLogSink.h>
#include <XCEngine/Debug/ConsoleLogSink.h>
#include <stdio.h>
#include <windows.h>
namespace {
std::string GetExecutableLogPath(const char* fileName) {
2026-03-26 23:52:05 +08:00
return XCEngine::Editor::Platform::GetExecutableDirectoryUtf8() + "\\" + fileName;
}
} // namespace
static LONG WINAPI GlobalExceptionFilter(EXCEPTION_POINTERS* exceptionPointers) {
const std::string logPath = GetExecutableLogPath("crash.log");
FILE* f = nullptr;
fopen_s(&f, logPath.c_str(), "a");
if (f) {
fprintf(f, "[CRASH] ExceptionCode=0x%08X, Address=0x%p\n",
exceptionPointers->ExceptionRecord->ExceptionCode,
exceptionPointers->ExceptionRecord->ExceptionAddress);
fclose(f);
}
fprintf(stderr, "[CRASH] ExceptionCode=0x%08X, Address=0x%p\n",
exceptionPointers->ExceptionRecord->ExceptionCode,
exceptionPointers->ExceptionRecord->ExceptionAddress);
return EXCEPTION_EXECUTE_HANDLER;
}
namespace XCEngine {
namespace Editor {
Application& Application::Get() {
static Application instance;
return instance;
}
bool Application::Initialize(HWND hwnd) {
SetUnhandledExceptionFilter(GlobalExceptionFilter);
2026-03-26 23:52:05 +08:00
{
const std::string stderrPath = GetExecutableLogPath("stderr.log");
freopen(stderrPath.c_str(), "w", stderr);
}
2026-03-26 23:52:05 +08:00
Debug::Logger::Get().AddSink(std::make_unique<Debug::ConsoleLogSink>());
Debug::Logger::Get().AddSink(std::make_unique<Debug::EditorConsoleSink>());
2026-03-26 23:52:05 +08:00
const std::string exeDir = Platform::GetExecutableDirectoryUtf8();
std::string logPath = exeDir + "\\editor.log";
Debug::Logger::Get().AddSink(std::make_unique<Debug::FileLogSink>(logPath.c_str()));
Debug::Logger::Get().Info(Debug::LogCategory::General, "Editor Application starting...");
Debug::Logger::Get().Info(Debug::LogCategory::General, ("Log file: " + logPath).c_str());
m_hwnd = hwnd;
2026-03-26 23:52:05 +08:00
if (!m_windowRenderer.Initialize(hwnd, 1280, 720)) {
MessageBoxW(hwnd, L"Failed to create D3D12 device", L"Error", MB_OK | MB_ICONERROR);
return false;
}
2026-03-26 21:18:33 +08:00
m_editorContext = std::make_shared<EditorContext>();
m_editorContext->SetProjectPath(exeDir);
m_exitRequestedHandlerId = m_editorContext->GetEventBus().Subscribe<EditorExitRequestedEvent>(
[this](const EditorExitRequestedEvent&) {
if (m_hwnd) {
PostMessageW(m_hwnd, WM_CLOSE, 0, 0);
}
});
2026-03-26 22:10:43 +08:00
m_imguiSession.Initialize(m_editorContext->GetProjectPath());
2026-03-26 21:18:33 +08:00
2026-03-26 23:52:05 +08:00
m_imguiBackend.Initialize(hwnd, m_windowRenderer.GetDevice(), m_windowRenderer.GetSrvHeap());
m_editorLayer = new EditorLayer();
m_editorLayer->SetContext(m_editorContext);
m_layerStack.pushLayer(std::unique_ptr<Core::Layer>(m_editorLayer));
m_layerStack.onAttach();
return true;
}
void Application::Shutdown() {
m_layerStack.onDetach();
2026-03-26 21:18:33 +08:00
if (m_editorContext && m_exitRequestedHandlerId) {
m_editorContext->GetEventBus().Unsubscribe<EditorExitRequestedEvent>(m_exitRequestedHandlerId);
m_exitRequestedHandlerId = 0;
}
2026-03-26 22:10:43 +08:00
m_imguiBackend.Shutdown();
m_imguiSession.Shutdown();
2026-03-26 23:52:05 +08:00
m_windowRenderer.Shutdown();
}
void Application::Render() {
2026-03-26 22:10:43 +08:00
m_imguiBackend.BeginFrame();
m_layerStack.onImGuiRender();
UpdateWindowTitle();
ImGui::Render();
2026-03-26 23:52:05 +08:00
2026-03-26 21:18:33 +08:00
float clearColor[4] = { 0.22f, 0.22f, 0.22f, 1.0f };
2026-03-26 23:52:05 +08:00
m_windowRenderer.Render(m_imguiBackend, clearColor);
}
void Application::UpdateWindowTitle() {
if (!m_hwnd || !m_editorContext) {
return;
}
2026-03-26 23:52:05 +08:00
const std::wstring title = Platform::Utf8ToWide(BuildEditorWindowTitle(*m_editorContext));
if (title != m_lastWindowTitle) {
SetWindowTextW(m_hwnd, title.c_str());
m_lastWindowTitle = title;
}
}
void Application::OnResize(int width, int height) {
2026-03-26 23:52:05 +08:00
m_windowRenderer.Resize(width, height);
}
}
}