#include "Application.h" #include "Core/EditorLoggingSetup.h" #include "Core/EditorWindowTitle.h" #include "Layers/EditorLayer.h" #include "Core/EditorContext.h" #include "Core/EditorEvents.h" #include "Core/EventBus.h" #include "Platform/Win32Utf8.h" #include "Platform/WindowsProcessDiagnostics.h" #include namespace XCEngine { namespace Editor { Application& Application::Get() { static Application instance; return instance; } bool Application::InitializeWindowRenderer(HWND hwnd) { if (m_windowRenderer.Initialize(hwnd, 1280, 720)) { return true; } MessageBoxW(hwnd, L"Failed to create D3D12 device", L"Error", MB_OK | MB_ICONERROR); return false; } void Application::InitializeEditorContext(const std::string& projectPath) { m_editorContext = std::make_shared(); m_editorContext->SetProjectPath(projectPath); m_exitRequestedHandlerId = m_editorContext->GetEventBus().Subscribe( [this](const EditorExitRequestedEvent&) { if (m_hwnd) { PostMessageW(m_hwnd, WM_CLOSE, 0, 0); } }); } void Application::InitializeImGui(HWND hwnd) { m_imguiSession.Initialize(m_editorContext->GetProjectPath()); m_imguiBackend.Initialize(hwnd, m_windowRenderer.GetDevice(), m_windowRenderer.GetSrvHeap()); } void Application::AttachEditorLayer() { m_editorLayer = new EditorLayer(); m_editorLayer->SetContext(m_editorContext); m_layerStack.pushLayer(std::unique_ptr(m_editorLayer)); m_layerStack.onAttach(); } void Application::DetachEditorLayer() { m_layerStack.onDetach(); m_editorLayer = nullptr; } void Application::ShutdownEditorContext() { if (m_editorContext && m_exitRequestedHandlerId) { m_editorContext->GetEventBus().Unsubscribe(m_exitRequestedHandlerId); m_exitRequestedHandlerId = 0; } m_editorContext.reset(); } void Application::RenderEditorFrame() { static constexpr float kClearColor[4] = { 0.22f, 0.22f, 0.22f, 1.0f }; m_imguiBackend.BeginFrame(); m_layerStack.onImGuiRender(); UpdateWindowTitle(); ImGui::Render(); m_windowRenderer.Render(m_imguiBackend, kClearColor); } bool Application::Initialize(HWND hwnd) { Platform::InstallCrashExceptionFilter(); Platform::RedirectStderrToExecutableLog(); const std::string exeDir = Platform::GetExecutableDirectoryUtf8(); ConfigureEditorLogging(exeDir); m_hwnd = hwnd; if (!InitializeWindowRenderer(hwnd)) { return false; } InitializeEditorContext(exeDir); InitializeImGui(hwnd); AttachEditorLayer(); return true; } void Application::Shutdown() { DetachEditorLayer(); m_imguiBackend.Shutdown(); m_imguiSession.Shutdown(); ShutdownEditorContext(); m_windowRenderer.Shutdown(); } void Application::Render() { RenderEditorFrame(); } void Application::UpdateWindowTitle() { if (!m_hwnd || !m_editorContext) { return; } 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) { m_windowRenderer.Resize(width, height); } } }