#include "OpenGL2.hpp" #include #include #include #include #include #include OpenGL2Backend::OpenGL2Backend() { #if IMGUI_INCLUDE_OPENGL2_BACKEND glfwSetErrorCallback(GlfwErrorCallback); if (!glfwInit()) { throw std::runtime_error("Failed to initialize GLFW."); } mWindow = glfwCreateWindow(1280, 720, "Cplt", nullptr, nullptr); if (mWindow == nullptr) { throw std::runtime_error("Failed to create GLFW window."); } glfwMakeContextCurrent(mWindow); glfwSwapInterval(1); // Enable vsync if (gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) == 0) { throw std::runtime_error("Failed to initialize OpenGL."); } IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGui_ImplGlfw_InitForOpenGL(mWindow, true); ImGui_ImplOpenGL2_Init(); #else throw std::runtime_error("Backend opengl2 is not available in this build.\n"); #endif } OpenGL2Backend::~OpenGL2Backend() { ImGui_ImplOpenGL2_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); glfwDestroyWindow(mWindow); glfwTerminate(); } void OpenGL2Backend::BeginFrame() { glfwPollEvents(); ImGui_ImplOpenGL2_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); } void OpenGL2Backend::EndFrame() { int displayWidth, displayHeight; glfwGetFramebufferSize(mWindow, &displayWidth, &displayHeight); glViewport(0, 0, displayWidth, displayHeight); const ImVec4 kClearColor = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); glClearColor(kClearColor.x * kClearColor.w, kClearColor.y * kClearColor.w, kClearColor.z * kClearColor.w, kClearColor.w); glClear(GL_COLOR_BUFFER_BIT); ImGui::Render(); ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); glfwMakeContextCurrent(mWindow); glfwSwapBuffers(mWindow); }