| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #ifndef GUI_HPP_
- #define GUI_HPP_
- #include "imgui.h"
- #include "imgui_impl_glfw.h"
- #include "imgui_impl_opengl3.h"
- #include <implot.h>
- #include <implot3d.h>
- #include <stdio.h>
- #define GL_SILENCE_DEPRECATION
- #include <GLFW/glfw3.h>
- #include <functional>
- 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<void()> 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
|