summaryrefslogtreecommitdiff
path: root/core/src/Entrypoint/OpenGL3.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-03-28 13:24:52 -0700
committerrtk0c <[email protected]>2021-03-28 13:24:52 -0700
commitbdcc81822adddf2c6ad7f10d9e090d913475c1e0 (patch)
tree91997b21958992f4e9e48839166ea4feaa0712b1 /core/src/Entrypoint/OpenGL3.cpp
parent442d2d75d71bbc057e667edc301a79fa1cc813be (diff)
Initial code structure
Diffstat (limited to 'core/src/Entrypoint/OpenGL3.cpp')
-rw-r--r--core/src/Entrypoint/OpenGL3.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/core/src/Entrypoint/OpenGL3.cpp b/core/src/Entrypoint/OpenGL3.cpp
new file mode 100644
index 0000000..c1f66d5
--- /dev/null
+++ b/core/src/Entrypoint/OpenGL3.cpp
@@ -0,0 +1,85 @@
+#include "OpenGL3.hpp"
+
+#include <glad/glad.h>
+
+#include <GLFW/glfw3.h>
+#include <backend/imgui_impl_glfw.h>
+#include <backend/imgui_impl_opengl3.h>
+#include <imgui.h>
+#include <stdexcept>
+
+OpenGL3Backend::OpenGL3Backend() {
+#if IMGUI_INCLUDE_OPENGL3_BACKEND
+ glfwSetErrorCallback(GlfwErrorCallback);
+ if (!glfwInit()) {
+ throw std::runtime_error("Failed to initialize GLFW.");
+ }
+
+# if PLATFORM_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);
+#else
+ throw std::runtime_error("Backend opengl3 is not available in this build.\n");
+#endif
+}
+
+OpenGL3Backend::~OpenGL3Backend() {
+ ImGui_ImplOpenGL3_Shutdown();
+ ImGui_ImplGlfw_Shutdown();
+ ImGui::DestroyContext();
+
+ glfwDestroyWindow(mWindow);
+ glfwTerminate();
+}
+
+void OpenGL3Backend::BeginFrame() {
+ glfwPollEvents();
+
+ ImGui_ImplOpenGL3_NewFrame();
+ ImGui_ImplGlfw_NewFrame();
+ ImGui::NewFrame();
+}
+
+void OpenGL3Backend::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_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
+
+ glfwSwapBuffers(mWindow);
+}