diff options
| author | hachem <im@hachem.wtf> | 2025-08-14 01:24:24 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-14 01:24:24 +0200 |
| commit | 4de723231b38d7c7e272e71b9580443874c278ab (patch) | |
| tree | c93be963476510b8af30ffb4af7d69d468926fe3 /src/core | |
| parent | 0d8b874c0048ca088bb9e353a5f7c62e1f8cd0e8 (diff) | |
feat: implement an ImGui layer
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/application.cpp | 15 | ||||
| -rw-r--r-- | src/core/application.h | 1 | ||||
| -rw-r--r-- | src/core/state.h | 1 | ||||
| -rw-r--r-- | src/core/state_manager.cpp | 6 | ||||
| -rw-r--r-- | src/core/state_manager.h | 1 | ||||
| -rw-r--r-- | src/core/window.cpp | 62 | ||||
| -rw-r--r-- | src/core/window.h | 7 |
7 files changed, 84 insertions, 9 deletions
diff --git a/src/core/application.cpp b/src/core/application.cpp index f2284cd..0698ce3 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -2,8 +2,8 @@ #include "rendering/renderer.h" -#include "states/config_state.h" #include "states/simulation_state.h" +#include "states/config_state.h" #include <GLFW/glfw3.h> @@ -86,12 +86,6 @@ namespace Donut event.handled = true; return true; } - else if (e.get_key_code() == GLFW_KEY_3) - { - m_state_manager->switch_to_state("Menu"); - event.handled = true; - return true; - } return true; }); @@ -105,12 +99,13 @@ namespace Donut RendererAPI::set_api(RendererAPI::API::OpenGL); Renderer::init(); + m_window->init_im_gui(); Renderer::on_window_resize(1280, 720); RenderCommand::set_face_culling(false); m_state_manager = create_scope<StateManager>(); - m_state_manager->register_state("Config", create_scope<ConfigState>()); + m_state_manager->register_state("Config", create_scope<ConfigState>()); m_state_manager->register_state("Simulation", create_scope<SimulationState>()); m_state_manager->switch_to_state("Config"); } @@ -136,5 +131,9 @@ namespace Donut void Application::on_render() { m_state_manager->render(); + + m_window->begin_im_gui_frame(); + m_state_manager->on_im_ui_render(); + m_window->end_im_gui_frame(); } }; diff --git a/src/core/application.h b/src/core/application.h index 048503b..d0abac3 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -19,6 +19,7 @@ namespace Donut void close(); Window& get_window() { return *m_window; } + StateManager& get_state_manager() { return *m_state_manager; } static Application& get() { return *s_instance; } private: void on_init(); diff --git a/src/core/state.h b/src/core/state.h index f5d808a..f4fe9b3 100644 --- a/src/core/state.h +++ b/src/core/state.h @@ -13,6 +13,7 @@ namespace Donut virtual void on_exit() = 0; virtual void on_update(float delta_time) = 0; virtual void on_render() = 0; + virtual void on_im_ui_render() = 0; virtual void on_event(Event& event) = 0; }; }; diff --git a/src/core/state_manager.cpp b/src/core/state_manager.cpp index 728df15..5086ddd 100644 --- a/src/core/state_manager.cpp +++ b/src/core/state_manager.cpp @@ -74,6 +74,12 @@ namespace Donut m_current_state->on_render(); } + void StateManager::on_im_ui_render() + { + if (m_current_state) + m_current_state->on_im_ui_render(); + } + void StateManager::on_event(Event& event) { if (m_current_state) diff --git a/src/core/state_manager.h b/src/core/state_manager.h index 469c48f..584df11 100644 --- a/src/core/state_manager.h +++ b/src/core/state_manager.h @@ -16,6 +16,7 @@ namespace Donut void update(float delta_time); void render(); + void on_im_ui_render(); void on_event(Event& event); void register_state(const std::string& state_name, Scope<State> state); diff --git a/src/core/window.cpp b/src/core/window.cpp index f598357..df19b86 100644 --- a/src/core/window.cpp +++ b/src/core/window.cpp @@ -2,6 +2,10 @@ #include <cstdint> +#include <imgui.h> +#include <imgui_impl_glfw.h> +#include <imgui_impl_opengl3.h> + namespace Donut { static bool s_glfw_initialized = false; @@ -66,6 +70,9 @@ namespace Donut void Window::shutdown() { DONUT_INFO("Shutting down window: ", m_title); + + shutdown_im_gui(); + glfwDestroyWindow(m_window); s_glfw_window_count--; @@ -102,6 +109,61 @@ namespace Donut GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN); } + void Window::init_im_gui() + { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + + ImGui::StyleColorsDark(); + + ImGuiStyle& style = ImGui::GetStyle(); + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + style.WindowRounding = 0.0f; + style.Colors[ImGuiCol_WindowBg].w = 1.0f; + } + + ImGui_ImplGlfw_InitForOpenGL(m_window, true); + ImGui_ImplOpenGL3_Init("#version 130"); + + DONUT_INFO("ImGUI initialized successfully"); + } + + void Window::shutdown_im_gui() + { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + + DONUT_INFO("ImGUI shutdown"); + } + + void Window::begin_im_gui_frame() + { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + } + + void Window::end_im_gui_frame() + { + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + ImGuiIO& io = ImGui::GetIO(); + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + GLFWwindow* backup_current_context = glfwGetCurrentContext(); + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + glfwMakeContextCurrent(backup_current_context); + } + } + void Window::glfw_error_callback(int error, const char* description) { DONUT_ERROR("GLFW Error (", error, "): ", description); diff --git a/src/core/window.h b/src/core/window.h index 256f708..96678f8 100644 --- a/src/core/window.h +++ b/src/core/window.h @@ -35,10 +35,15 @@ namespace Donut void set_cursor_visible(bool visible); bool is_cursor_locked() const { return m_cursor_locked; } bool is_cursor_visible() const { return m_cursor_visible; } + + void init_im_gui(); + void begin_im_gui_frame(); + void end_im_gui_frame(); private: void init(); void shutdown(); - + void shutdown_im_gui(); + static void glfw_error_callback(int error, const char* description); static void glfw_window_close_callback(GLFWwindow* window); static void glfw_window_size_callback(GLFWwindow* window, int width, int height); |
