From 7fe47a9d5b1727a61dc724523b530762f6d6ba19 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Thu, 30 Jun 2022 21:38:53 -0700 Subject: Restructure project --- app/source/Cplt/Entrypoint/Backend_OpenGL3.cpp | 121 +++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 app/source/Cplt/Entrypoint/Backend_OpenGL3.cpp (limited to 'app/source/Cplt/Entrypoint/Backend_OpenGL3.cpp') diff --git a/app/source/Cplt/Entrypoint/Backend_OpenGL3.cpp b/app/source/Cplt/Entrypoint/Backend_OpenGL3.cpp new file mode 100644 index 0000000..28a34ca --- /dev/null +++ b/app/source/Cplt/Entrypoint/Backend_OpenGL3.cpp @@ -0,0 +1,121 @@ +#include + +#if BUILD_CORE_WITH_OPENGL3_BACKEND +# include + +# include +# include +# include +# include +# include +# include + +# define IMGUI_IMPL_OPENGL_LOADER_CUSTOM +# include + +class OpenGL3Backend : public RenderingBackend +{ +private: + GLFWwindow* mWindow; + +public: + OpenGL3Backend() + { + glfwSetErrorCallback(&GlfwErrorCallback); + if (!glfwInit()) { + throw std::runtime_error("Failed to initialize GLFW."); + } + +# if defined(__APPLE__) + // GL 3.2 + GLSL 150 + const char* glslVersion = "#version 150"; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac +# else + // GL 3.0 + GLSL 130 + const char* glslVersion = "#version 130"; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only + // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only +# endif + + 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_ImplOpenGL3_Init(glslVersion); + } + + virtual ~OpenGL3Backend() + { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + + glfwDestroyWindow(mWindow); + glfwTerminate(); + } + + virtual void RunUntilWindowClose(void (*windowContent)()) + { + while (!glfwWindowShouldClose(mWindow)) { + glfwPollEvents(); + + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + windowContent(); + + 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_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + glfwSwapBuffers(mWindow); + } + } + + static void GlfwErrorCallback(int errorCode, const char* message) + { + std::cerr << "GLFW Error " << errorCode << ": " << message << "\n"; + } +}; + +std::unique_ptr RenderingBackend::CreateOpenGL3Backend() +{ + try { + return std::make_unique(); + } catch (std::exception& e) { + return nullptr; + } +} + +#else // ^^ BUILD_CORE_WITH_OPENGL3_BACKEND | !BUILD_CORE_WITH_OPENGL3_BACKEND vv + +std::unique_ptr RenderingBackend::CreateOpenGL3Backend() +{ + return nullptr; +} + +#endif -- cgit v1.2.3-70-g09d2