gui.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef GUI_HPP_
  2. #define GUI_HPP_
  3. #include "imgui.h"
  4. #include "imgui_impl_glfw.h"
  5. #include "imgui_impl_opengl3.h"
  6. #include <implot.h>
  7. #include <implot3d.h>
  8. #include <stdio.h>
  9. #define GL_SILENCE_DEPRECATION
  10. #include <GLFW/glfw3.h>
  11. #include <functional>
  12. class ImGuiContext {
  13. public:
  14. enum class InitStatus {
  15. Ok,
  16. ErrorVideoAPI,
  17. ErrorCreateWindow
  18. };
  19. ImGuiContext() = default;
  20. ~ImGuiContext() { cleanup(); }
  21. ImGuiContext(const ImGuiContext &) = delete;
  22. ImGuiContext &operator=(const ImGuiContext &) = delete;
  23. InitStatus init(const char *title, int width = 1280, int height = 720) {
  24. glfwSetErrorCallback(glfw_error_callback);
  25. if (!glfwInit()) {
  26. return InitStatus::ErrorVideoAPI;
  27. }
  28. glfw_initialized = true;
  29. const char *glsl_version = "#version 130";
  30. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  31. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  32. window = glfwCreateWindow(width, height, title, nullptr, nullptr);
  33. if (!window) {
  34. return InitStatus::ErrorCreateWindow;
  35. }
  36. glfwMakeContextCurrent(window);
  37. glfwSwapInterval(1); // V-Sync
  38. IMGUI_CHECKVERSION();
  39. ImGui::CreateContext();
  40. ImPlot::CreateContext();
  41. ImPlot3D::CreateContext();
  42. imgui_context_created = true;
  43. ImGui::StyleColorsDark();
  44. ImGui_ImplGlfw_InitForOpenGL(window, true);
  45. imgui_glfw_initialized = true;
  46. ImGui_ImplOpenGL3_Init(glsl_version);
  47. imgui_opengl_initialized = true;
  48. ImGuiIO &io = ImGui::GetIO();
  49. io.Fonts->AddFontFromFileTTF("resources/DroidSans.ttf", 16.0f, nullptr,
  50. io.Fonts->GetGlyphRangesCyrillic());
  51. ImFontConfig config;
  52. config.MergeMode = true; // Объединение с основным шрифтом
  53. config.GlyphMinAdvanceX = 13.0f; // Размер иконок
  54. static const ImWchar icon_ranges[] = {
  55. 0xf000, 0xf3ff, 0}; // Диапазон символов Font Awesome
  56. io.Fonts->AddFontFromFileTTF("resources/fontawesome.ttf", 13.0f,
  57. &config, icon_ranges);
  58. // Включение докинга (обязательно!)
  59. io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
  60. return InitStatus::Ok;
  61. }
  62. void main_loop(std::function<void()> loop) {
  63. while (!this->should_close()) {
  64. this->new_frame();
  65. ImGui::DockSpaceOverViewport(ImGui::GetMainViewport()->ID);
  66. loop();
  67. this->render();
  68. this->poll_events();
  69. }
  70. }
  71. void cleanup() {
  72. // Обратный порядок очистки
  73. if (imgui_opengl_initialized) {
  74. ImGui_ImplOpenGL3_Shutdown();
  75. imgui_opengl_initialized = false;
  76. }
  77. if (imgui_glfw_initialized) {
  78. ImGui_ImplGlfw_Shutdown();
  79. imgui_glfw_initialized = false;
  80. }
  81. if (imgui_context_created) {
  82. ImGui::DestroyContext();
  83. ImPlot::DestroyContext();
  84. ImPlot3D::DestroyContext();
  85. imgui_context_created = false;
  86. }
  87. if (window) {
  88. // glfwDestroyWindow(window);
  89. // window = nullptr;
  90. }
  91. if (glfw_initialized) {
  92. glfwTerminate();
  93. glfw_initialized = false;
  94. }
  95. }
  96. GLFWwindow *get_window() const { return window; }
  97. bool should_close() const { return glfwWindowShouldClose(window); }
  98. void new_frame() {
  99. ImGui_ImplOpenGL3_NewFrame();
  100. ImGui_ImplGlfw_NewFrame();
  101. ImGui::NewFrame();
  102. }
  103. void render() {
  104. ImGui::Render();
  105. int display_w, display_h;
  106. glfwGetFramebufferSize(window, &display_w, &display_h);
  107. glViewport(0, 0, display_w, display_h);
  108. glClear(GL_COLOR_BUFFER_BIT);
  109. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  110. glfwSwapBuffers(window);
  111. }
  112. void poll_events() { glfwPollEvents(); }
  113. private:
  114. static void glfw_error_callback(int error, const char *description) {
  115. fprintf(stderr, "GLFW Error %d: %s\n", error, description);
  116. }
  117. GLFWwindow *window = nullptr;
  118. bool glfw_initialized = false;
  119. bool imgui_context_created = false;
  120. bool imgui_glfw_initialized = false;
  121. bool imgui_opengl_initialized = false;
  122. };
  123. #endif