#ifndef GUI_HPP_ #define GUI_HPP_ #include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include #include #include #define GL_SILENCE_DEPRECATION #include #include class ImGuiContext { public: enum class InitStatus { Ok, ErrorVideoAPI, ErrorCreateWindow }; ImGuiContext() = default; ~ImGuiContext() { cleanup(); } ImGuiContext(const ImGuiContext &) = delete; ImGuiContext &operator=(const ImGuiContext &) = delete; InitStatus init(const char *title, int width = 1280, int height = 720) { glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) { return InitStatus::ErrorVideoAPI; } glfw_initialized = true; const char *glsl_version = "#version 130"; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(width, height, title, nullptr, nullptr); if (!window) { return InitStatus::ErrorCreateWindow; } glfwMakeContextCurrent(window); glfwSwapInterval(1); // V-Sync IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImPlot::CreateContext(); ImPlot3D::CreateContext(); imgui_context_created = true; ImGui::StyleColorsDark(); ImGui_ImplGlfw_InitForOpenGL(window, true); imgui_glfw_initialized = true; ImGui_ImplOpenGL3_Init(glsl_version); imgui_opengl_initialized = true; ImGuiIO &io = ImGui::GetIO(); io.Fonts->AddFontFromFileTTF("resources/DroidSans.ttf", 16.0f, nullptr, io.Fonts->GetGlyphRangesCyrillic()); ImFontConfig config; config.MergeMode = true; // Объединение с основным шрифтом config.GlyphMinAdvanceX = 13.0f; // Размер иконок static const ImWchar icon_ranges[] = { 0xf000, 0xf3ff, 0}; // Диапазон символов Font Awesome io.Fonts->AddFontFromFileTTF("resources/fontawesome.ttf", 13.0f, &config, icon_ranges); // Включение докинга (обязательно!) io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; return InitStatus::Ok; } void main_loop(std::function loop) { while (!this->should_close()) { this->new_frame(); ImGui::DockSpaceOverViewport(ImGui::GetMainViewport()->ID); loop(); this->render(); this->poll_events(); } } void cleanup() { // Обратный порядок очистки if (imgui_opengl_initialized) { ImGui_ImplOpenGL3_Shutdown(); imgui_opengl_initialized = false; } if (imgui_glfw_initialized) { ImGui_ImplGlfw_Shutdown(); imgui_glfw_initialized = false; } if (imgui_context_created) { ImGui::DestroyContext(); ImPlot::DestroyContext(); ImPlot3D::DestroyContext(); imgui_context_created = false; } if (window) { // glfwDestroyWindow(window); // window = nullptr; } if (glfw_initialized) { glfwTerminate(); glfw_initialized = false; } } GLFWwindow *get_window() const { return window; } bool should_close() const { return glfwWindowShouldClose(window); } void new_frame() { ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); } void render() { ImGui::Render(); int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(window); } void poll_events() { glfwPollEvents(); } private: static void glfw_error_callback(int error, const char *description) { fprintf(stderr, "GLFW Error %d: %s\n", error, description); } GLFWwindow *window = nullptr; bool glfw_initialized = false; bool imgui_context_created = false; bool imgui_glfw_initialized = false; bool imgui_opengl_initialized = false; }; #endif