#include "simulation_state.h" #include "rendering/renderer.h" #include "core/application.h" #include "core/window.h" #include "core/event.h" #include #include namespace Donut { void SimulationState::on_enter() { DONUT_INFO("Entering Simulation State"); m_initialized = true; } void SimulationState::on_exit() { DONUT_INFO("Exiting Simulation State"); } void SimulationState::on_update(float delta_time) { m_engine.update_window_dimensions(); m_engine.update_physics(delta_time); if (m_engine.get_camera().is_dragging()) { GLFWwindow* window = static_cast(Application::get().get_window().get_native_window()); double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); m_engine.get_camera().process_orbital_mouse_move(xpos, ypos); } } void SimulationState::on_render() { if (!m_initialized) return; RenderCommand::set_clear_color(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)); RenderCommand::clear(); m_engine.generate_grid(m_engine.get_objects()); glm::mat4 view = glm::lookAt(m_engine.get_camera().get_orbital_position(), m_engine.get_camera().get_target(), glm::vec3(0,1,0)); glm::mat4 proj = glm::perspective(glm::radians(60.0f), float(m_engine.get_width())/m_engine.get_height(), 1e9f, 1e14f); glm::mat4 view_proj = proj * view; m_engine.draw_grid(view_proj); RenderCommand::set_viewport(0, 0, m_engine.get_width(), m_engine.get_height()); m_engine.dispatch_compute(m_engine.get_camera()); m_engine.draw_full_screen_quad(); } void SimulationState::on_event(Event& event) { if (!m_initialized) return; if (event.get_event_type() == EventType::MouseButtonPressed) { MouseButtonPressedEvent& e = (MouseButtonPressedEvent&)event; int button = e.get_mouse_button(); m_engine.get_camera().process_orbital_mouse_button(button, GLFW_PRESS, 0); } else if (event.get_event_type() == EventType::MouseButtonReleased) { MouseButtonReleasedEvent& e = (MouseButtonReleasedEvent&)event; int button = e.get_mouse_button(); m_engine.get_camera().process_orbital_mouse_button(button, GLFW_RELEASE, 0); } if (event.get_event_type() == EventType::MouseScrolled) { MouseScrolledEvent& e = (MouseScrolledEvent&)event; m_engine.get_camera().process_orbital_scroll(e.get_x_offset(), e.get_y_offset()); } if (event.get_event_type() == EventType::KeyPressed) { KeyPressedEvent& e = (KeyPressedEvent&)event; if (e.get_key_code() == GLFW_KEY_G) { m_engine.set_gravity(!m_engine.get_gravity()); DONUT_INFO("Gravity turned {}", m_engine.get_gravity() ? "ON" : "OFF"); } } } void SimulationState::on_im_ui_render() { ImGui::Begin("Simulation Controls"); ImGui::Text("Black Hole Simulation"); if (ImGui::Button("Back to Config")) Application::get().get_state_manager().switch_to_state("Config"); ImGui::Separator(); ImGui::Text("Simulation Info:"); ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate); ImGui::Text("Frame Time: %.3f ms", 1000.0f / ImGui::GetIO().Framerate); ImGui::Text("Resolution: %dx%d", m_engine.get_width(), m_engine.get_height()); ImGui::Text("Compute Resolution: %dx%d", m_engine.get_compute_width(), m_engine.get_compute_height()); ImGui::Text("Objects: %zu", m_engine.get_objects().size()); ImGui::Separator(); ImGui::Text("Controls:"); ImGui::Text("Left Mouse: Orbit camera"); ImGui::Text("Scroll: Zoom in/out"); ImGui::Text("G: Toggle gravity"); ImGui::Text("Right Mouse: Enable gravity (hold)"); ImGui::Separator(); ImGui::Text("Physics:"); bool gravity = m_engine.get_gravity(); if (ImGui::Checkbox("Gravity Enabled", &gravity)) m_engine.set_gravity(gravity); ImGui::Text("Camera:"); ImGui::Text("Position: (%.2e, %.2e, %.2e)", m_engine.get_camera().get_orbital_position().x, m_engine.get_camera().get_orbital_position().y, m_engine.get_camera().get_orbital_position().z); ImGui::Text("Radius: %.2e", m_engine.get_camera().get_radius()); ImGui::Text("Azimuth: %.2f", m_engine.get_camera().get_azimuth()); ImGui::Text("Elevation: %.2f", m_engine.get_camera().get_elevation()); ImGui::End(); } };