aboutsummaryrefslogtreecommitdiff
path: root/src/Core
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2025-08-14 01:24:24 +0200
committerhachem <im@hachem.wtf>2025-08-14 01:24:24 +0200
commit613f99e937e55f73dcec1251fdce4a9fda077a0f (patch)
treeb86ed54045783b47a0f856476a6465d3777b7005 /src/Core
parente050dce79b67ae6386240567a994454711ab998d (diff)
[add]: implement an ImGUI layer
Diffstat (limited to 'src/Core')
-rw-r--r--src/Core/Application.cpp15
-rw-r--r--src/Core/Application.h1
-rw-r--r--src/Core/State.h1
-rw-r--r--src/Core/StateManager.cpp6
-rw-r--r--src/Core/StateManager.h1
-rw-r--r--src/Core/Window.cpp62
-rw-r--r--src/Core/Window.h7
7 files changed, 84 insertions, 9 deletions
diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp
index a886327..9aac646 100644
--- a/src/Core/Application.cpp
+++ b/src/Core/Application.cpp
@@ -2,8 +2,8 @@
#include "Rendering/Renderer.h"
-#include "States/ConfigState.h"
#include "States/SimulationState.h"
+#include "States/ConfigState.h"
#include <GLFW/glfw3.h>
@@ -86,12 +86,6 @@ namespace Donut
event.Handled = true;
return true;
}
- else if (e.GetKeyCode() == GLFW_KEY_3)
- {
- m_StateManager->SwitchToState("Menu");
- event.Handled = true;
- return true;
- }
return true;
});
@@ -105,12 +99,13 @@ namespace Donut
RendererAPI::SetAPI(RendererAPI::API::OpenGL);
Renderer::Init();
+ m_Window->InitImGui();
Renderer::OnWindowResize(1280, 720);
RenderCommand::SetFaceCulling(false);
m_StateManager = CreateScope<StateManager>();
- m_StateManager->RegisterState("Config", CreateScope<ConfigState>());
+ m_StateManager->RegisterState("Config", CreateScope<ConfigState>());
m_StateManager->RegisterState("Simulation", CreateScope<SimulationState>());
m_StateManager->SwitchToState("Config");
}
@@ -136,5 +131,9 @@ namespace Donut
void Application::OnRender()
{
m_StateManager->Render();
+
+ m_Window->BeginImGuiFrame();
+ m_StateManager->OnImUIRender();
+ m_Window->EndImGuiFrame();
}
};
diff --git a/src/Core/Application.h b/src/Core/Application.h
index 3d0ba25..39d28bb 100644
--- a/src/Core/Application.h
+++ b/src/Core/Application.h
@@ -19,6 +19,7 @@ namespace Donut
void Close();
Window& GetWindow() { return *m_Window; }
+ StateManager& GetStateManager() { return *m_StateManager; }
static Application& Get() { return *s_Instance; }
private:
void OnInit();
diff --git a/src/Core/State.h b/src/Core/State.h
index 2d368d7..cd1b7ee 100644
--- a/src/Core/State.h
+++ b/src/Core/State.h
@@ -13,6 +13,7 @@ namespace Donut
virtual void OnExit() = 0;
virtual void OnUpdate(float deltaTime) = 0;
virtual void OnRender() = 0;
+ virtual void OnImUIRender() = 0;
virtual void OnEvent(Event& event) = 0;
};
}; \ No newline at end of file
diff --git a/src/Core/StateManager.cpp b/src/Core/StateManager.cpp
index 266bafa..e2aba02 100644
--- a/src/Core/StateManager.cpp
+++ b/src/Core/StateManager.cpp
@@ -74,6 +74,12 @@ namespace Donut
m_CurrentState->OnRender();
}
+ void StateManager::OnImUIRender()
+ {
+ if (m_CurrentState)
+ m_CurrentState->OnImUIRender();
+ }
+
void StateManager::OnEvent(Event& event)
{
if (m_CurrentState)
diff --git a/src/Core/StateManager.h b/src/Core/StateManager.h
index c0b2b13..4ffc86d 100644
--- a/src/Core/StateManager.h
+++ b/src/Core/StateManager.h
@@ -16,6 +16,7 @@ namespace Donut
void Update(float deltaTime);
void Render();
+ void OnImUIRender();
void OnEvent(Event& event);
void RegisterState(const std::string& stateName, Scope<State> state);
diff --git a/src/Core/Window.cpp b/src/Core/Window.cpp
index 73bd8db..d60ebc9 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_GLFWInitialized = false;
@@ -66,6 +70,9 @@ namespace Donut
void Window::Shutdown()
{
DONUT_INFO("Shutting down window: ", m_Title);
+
+ ShutdownImGui();
+
glfwDestroyWindow(m_Window);
s_GLFWWindowCount--;
@@ -102,6 +109,61 @@ namespace Donut
GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN);
}
+ void Window::InitImGui()
+ {
+ 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::ShutdownImGui()
+ {
+ ImGui_ImplOpenGL3_Shutdown();
+ ImGui_ImplGlfw_Shutdown();
+ ImGui::DestroyContext();
+
+ DONUT_INFO("ImGUI shutdown");
+ }
+
+ void Window::BeginImGuiFrame()
+ {
+ ImGui_ImplOpenGL3_NewFrame();
+ ImGui_ImplGlfw_NewFrame();
+ ImGui::NewFrame();
+ }
+
+ void Window::EndImGuiFrame()
+ {
+ 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::GLFWErrorCallback(int error, const char* description)
{
DONUT_ERROR("GLFW Error (", error, "): ", description);
diff --git a/src/Core/Window.h b/src/Core/Window.h
index c598bf2..26df099 100644
--- a/src/Core/Window.h
+++ b/src/Core/Window.h
@@ -35,10 +35,15 @@ namespace Donut
void SetCursorVisible(bool visible);
bool IsCursorLocked() const { return m_CursorLocked; }
bool IsCursorVisible() const { return m_CursorVisible; }
+
+ void InitImGui();
+ void BeginImGuiFrame();
+ void EndImGuiFrame();
private:
void Init();
void Shutdown();
-
+ void ShutdownImGui();
+
static void GLFWErrorCallback(int error, const char* description);
static void GLFWWindowCloseCallback(GLFWwindow* window);
static void GLFWWindowSizeCallback(GLFWwindow* window, int width, int height);