#include "application.h" #include "rendering/render_api.h" // RendererAPI #include "settings_manager.h" #include "platform/opengl/opengl_device.h" #include "platform/vulkan/vulkan_device.h" #include #include #include #include namespace Donut { Application* Application::s_instance = nullptr; // Scroll is delivered through a GLFW callback; accumulate it here and drain it // once per frame in update_input (chaining ImGui's own scroll handler). static double g_scroll_accum = 0.0; static GLFWscrollfun g_prev_scroll = nullptr; static void donut_scroll_callback(GLFWwindow* w, double x, double y) { if (g_prev_scroll) g_prev_scroll(w, x, y); g_scroll_accum += y; } Application::Application(const std::string& name, int width, int height) { s_instance = this; // Select the render API before the window is created: the window is built // differently for Vulkan (GLFW_NO_API) than for OpenGL. Logger::init(); SettingsManager::initialize(); { const auto& settings = SettingsManager::get_settings_const(); RendererAPI::set_api(settings.graphics.render_api == "Vulkan" ? RendererAPI::API::Vulkan : RendererAPI::API::OpenGL); } if (RendererAPI::get_api() == RendererAPI::API::Vulkan) RHI::vulkan_prepare_glfw(); // must precede glfwInit() inside the Window ctor m_window = create_scope(name, width, height); m_window->set_event_callback([this](Event& event) { on_event(event); }); on_init(); } Application::~Application() { on_shutdown(); s_instance = nullptr; } auto Application::on_init() -> void { int w = 0, h = 0; glfwGetFramebufferSize((GLFWwindow*)m_window->get_native_window(), &w, &h); if (RendererAPI::get_api() == RendererAPI::API::Vulkan) m_device = RHI::create_vulkan_device(); else m_device = RHI::create_opengl_device(); RHI::NativeWindow native{ m_window->get_native_window(), w, h }; if (!m_device->init(native)) { DONUT_ERROR("RHI device initialization failed"); return; } m_scene = create_scope(); m_render_path = create_scope(); m_render_path->init(*m_device, m_scene->hdri_path); m_ui = create_scope(); m_device->init_imgui(); // Load the UI font into the device-created ImGui context (backend-agnostic; // the backend rebuilds the atlas on the next frame). ImGuiIO& io = ImGui::GetIO(); if (!io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Regular.ttf", 16.0f)) io.Fonts->AddFontDefault(); g_prev_scroll = glfwSetScrollCallback((GLFWwindow*)m_window->get_native_window(), donut_scroll_callback); m_start_time = glfwGetTime(); DONUT_INFO("Application ready on {} backend", m_device->device_name()); } auto Application::run() -> void { while (m_running) { glfwPollEvents(); if (!m_minimized && m_device) render_frame(); } } auto Application::render_frame() -> void { RHI::CommandList* cmd = m_device->begin_frame(glm::vec4(0.05f, 0.06f, 0.10f, 1.0f)); if (!cmd) return; // frame skipped (e.g. swapchain recreation) m_device->imgui_new_frame(); View view = m_ui->draw(UIContext{ *m_scene, m_device->device_name() }); update_input(view); m_render_path->sync_hdri(m_scene->hdri_path); int w = 0, h = 0; glfwGetFramebufferSize((GLFWwindow*)m_window->get_native_window(), &w, &h); float time = (float)(glfwGetTime() - m_start_time); m_render_path->render(*cmd, *m_scene, view, w, h, m_user_moving, time); m_device->end_frame(); } auto Application::update_input(View view) -> void { GLFWwindow* window = (GLFWwindow*)m_window->get_native_window(); bool over_ui = ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureMouse; double now = glfwGetTime(); float dt = m_last_frame_time > 0.0 ? (float)(now - m_last_frame_time) : 0.0f; m_last_frame_time = now; double mx = 0, my = 0; glfwGetCursorPos(window, &mx, &my); bool left_down = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS; // Tabs without a live viewport (Setup / Export) have no camera to drive. if (view == View::None) { m_left_was_down = left_down; // keep the drag-edge state coherent g_scroll_accum = 0.0; // discard scroll over inert tabs m_user_moving = false; return; } const bool scene_view = view == View::Scene; if (!scene_view && m_scene->sim_camera.get_camera_mode() == CameraMode::FPS) { Camera& cam = m_scene->sim_camera; if (left_down && !m_left_was_down) { m_fps_last_x = mx; m_fps_last_y = my; } if (left_down && !over_ui) cam.on_mouse_move(float(mx - m_fps_last_x), float(m_fps_last_y - my)); m_fps_last_x = mx; m_fps_last_y = my; m_left_was_down = left_down; bool over_kb = ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureKeyboard; bool moved = false; if (!over_kb && dt > 0.0f) { float mdt = dt * (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS ? 4.0f : 1.0f); if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { cam.move_forward(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { cam.move_backward(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { cam.move_left(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { cam.move_right(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) { cam.move_up(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) { cam.move_down(mdt); moved = true; } } g_scroll_accum = 0.0; m_user_moving = moved || (left_down && !over_ui); } else { Camera& cam = scene_view ? m_scene->scene_camera : m_scene->sim_camera; if (left_down && !m_left_was_down && !over_ui) cam.process_orbital_mouse_button(GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, 0); else if (!left_down && m_left_was_down) cam.process_orbital_mouse_button(GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE, 0); m_left_was_down = left_down; cam.process_orbital_mouse_move(mx, my); double scroll = g_scroll_accum; g_scroll_accum = 0.0; if (scroll != 0.0 && !over_ui) cam.process_orbital_scroll(0.0, scroll); m_user_moving = cam.is_dragging() || cam.is_panning(); } } auto Application::close() -> void { m_running = false; } auto Application::on_event(Event& event) -> void { EventDispatcher dispatcher(event); dispatcher.dispatch([this, &event](WindowCloseEvent& e) { m_running = false; event.handled = true; return true; }); dispatcher.dispatch([this, &event](WindowResizeEvent& e) { m_minimized = (e.get_width() == 0 || e.get_height() == 0); if (m_device) m_device->resize(e.get_width(), e.get_height()); event.handled = true; return true; }); } auto Application::on_shutdown() -> void { if (m_device) { m_device->wait_idle(); if (m_render_path) m_render_path->shutdown(); m_render_path.reset(); m_ui.reset(); m_scene.reset(); m_device->shutdown(); m_device.reset(); } SettingsManager::shutdown(); Logger::shutdown(); } }