aboutsummaryrefslogtreecommitdiff
path: root/core/src/Entrypoint
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/Entrypoint')
-rw-r--r--core/src/Entrypoint/Common.cpp11
-rw-r--r--core/src/Entrypoint/Common.hpp20
-rw-r--r--core/src/Entrypoint/DirectX11.cpp13
-rw-r--r--core/src/Entrypoint/DirectX11.hpp11
-rw-r--r--core/src/Entrypoint/DirectX12.cpp13
-rw-r--r--core/src/Entrypoint/DirectX12.hpp11
-rw-r--r--core/src/Entrypoint/Metal.hpp11
-rw-r--r--core/src/Entrypoint/Metal.mm13
-rw-r--r--core/src/Entrypoint/OpenGL2.cpp70
-rw-r--r--core/src/Entrypoint/OpenGL2.hpp15
-rw-r--r--core/src/Entrypoint/OpenGL3.cpp85
-rw-r--r--core/src/Entrypoint/OpenGL3.hpp15
-rw-r--r--core/src/Entrypoint/Vulkan.cpp13
-rw-r--r--core/src/Entrypoint/Vulkan.hpp11
-rw-r--r--core/src/Entrypoint/main.cpp91
15 files changed, 403 insertions, 0 deletions
diff --git a/core/src/Entrypoint/Common.cpp b/core/src/Entrypoint/Common.cpp
new file mode 100644
index 0000000..62baf9d
--- /dev/null
+++ b/core/src/Entrypoint/Common.cpp
@@ -0,0 +1,11 @@
+#include "Common.hpp"
+
+#include <iostream>
+
+GLFWwindow* RenderingBackend::GetWindow() const {
+ return mWindow;
+}
+
+void RenderingBackend::GlfwErrorCallback(int error, const char* message) {
+ std::cerr << "GLFW Error " << error << ": " << message << "\n";
+}
diff --git a/core/src/Entrypoint/Common.hpp b/core/src/Entrypoint/Common.hpp
new file mode 100644
index 0000000..2a4dcbf
--- /dev/null
+++ b/core/src/Entrypoint/Common.hpp
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <glad/glad.h>
+
+#include <GLFW/glfw3.h>
+
+class RenderingBackend {
+protected:
+ GLFWwindow* mWindow;
+
+public:
+ virtual ~RenderingBackend() = default;
+ virtual void BeginFrame() = 0;
+ virtual void EndFrame() = 0;
+
+ GLFWwindow* GetWindow() const;
+
+ /// Common GLFW error handle callback for each rendering backend to use.
+ static void GlfwErrorCallback(int error, const char* message);
+};
diff --git a/core/src/Entrypoint/DirectX11.cpp b/core/src/Entrypoint/DirectX11.cpp
new file mode 100644
index 0000000..617447c
--- /dev/null
+++ b/core/src/Entrypoint/DirectX11.cpp
@@ -0,0 +1,13 @@
+#include "DirectX11.hpp"
+
+DirectX11Backend::DirectX11Backend() {
+ // TODO
+}
+
+void DirectX11Backend::BeginFrame() {
+ // TODO
+}
+
+void DirectX11Backend::EndFrame() {
+ // TODO
+}
diff --git a/core/src/Entrypoint/DirectX11.hpp b/core/src/Entrypoint/DirectX11.hpp
new file mode 100644
index 0000000..134a20f
--- /dev/null
+++ b/core/src/Entrypoint/DirectX11.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "Entrypoint/Common.hpp"
+
+class DirectX11Backend : public RenderingBackend {
+public:
+ DirectX11Backend();
+ virtual ~DirectX11Backend() = default;
+ virtual void BeginFrame() override;
+ virtual void EndFrame() override;
+};
diff --git a/core/src/Entrypoint/DirectX12.cpp b/core/src/Entrypoint/DirectX12.cpp
new file mode 100644
index 0000000..1769d24
--- /dev/null
+++ b/core/src/Entrypoint/DirectX12.cpp
@@ -0,0 +1,13 @@
+#include "DirectX12.hpp"
+
+DirectX12Backend::DirectX12Backend() {
+ // TODO
+}
+
+void DirectX12Backend::BeginFrame() {
+ // TODO
+}
+
+void DirectX12Backend::EndFrame() {
+ // TODO
+}
diff --git a/core/src/Entrypoint/DirectX12.hpp b/core/src/Entrypoint/DirectX12.hpp
new file mode 100644
index 0000000..8b9a4f0
--- /dev/null
+++ b/core/src/Entrypoint/DirectX12.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "Entrypoint/Common.hpp"
+
+class DirectX12Backend : public RenderingBackend {
+public:
+ DirectX12Backend();
+ virtual ~DirectX12Backend() = default;
+ virtual void BeginFrame() override;
+ virtual void EndFrame() override;
+};
diff --git a/core/src/Entrypoint/Metal.hpp b/core/src/Entrypoint/Metal.hpp
new file mode 100644
index 0000000..cd96d0f
--- /dev/null
+++ b/core/src/Entrypoint/Metal.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "Entrypoint/Common.hpp"
+
+class MetalBackend : public RenderingBackend {
+public:
+ MetalBackend();
+ virtual ~MetalBackend() = default;
+ virtual void BeginFrame() override;
+ virtual void EndFrame() override;
+};
diff --git a/core/src/Entrypoint/Metal.mm b/core/src/Entrypoint/Metal.mm
new file mode 100644
index 0000000..3f69634
--- /dev/null
+++ b/core/src/Entrypoint/Metal.mm
@@ -0,0 +1,13 @@
+#include "Metal.hpp"
+
+MetalBackend::MetalBackend() {
+ // TODO
+}
+
+void MetalBackend::BeginFrame() {
+ // TODO
+}
+
+void MetalBackend::EndFrame() {
+ // TODO
+}
diff --git a/core/src/Entrypoint/OpenGL2.cpp b/core/src/Entrypoint/OpenGL2.cpp
new file mode 100644
index 0000000..de13a02
--- /dev/null
+++ b/core/src/Entrypoint/OpenGL2.cpp
@@ -0,0 +1,70 @@
+#include "OpenGL2.hpp"
+
+#include <glad/glad.h>
+
+#include <GLFW/glfw3.h>
+#include <backend/imgui_impl_glfw.h>
+#include <backend/imgui_impl_opengl2.h>
+#include <imgui.h>
+#include <stdexcept>
+
+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);
+}
diff --git a/core/src/Entrypoint/OpenGL2.hpp b/core/src/Entrypoint/OpenGL2.hpp
new file mode 100644
index 0000000..86fbad7
--- /dev/null
+++ b/core/src/Entrypoint/OpenGL2.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "Entrypoint/Common.hpp"
+
+#include <glad/glad.h>
+
+#include <GLFW/glfw3.h>
+
+class OpenGL2Backend : public RenderingBackend {
+public:
+ OpenGL2Backend();
+ virtual ~OpenGL2Backend();
+ virtual void BeginFrame() override;
+ virtual void EndFrame() override;
+};
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);
+}
diff --git a/core/src/Entrypoint/OpenGL3.hpp b/core/src/Entrypoint/OpenGL3.hpp
new file mode 100644
index 0000000..29086a2
--- /dev/null
+++ b/core/src/Entrypoint/OpenGL3.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "Entrypoint/Common.hpp"
+
+#include <glad/glad.h>
+
+#include <GLFW/glfw3.h>
+
+class OpenGL3Backend : public RenderingBackend {
+public:
+ OpenGL3Backend();
+ virtual ~OpenGL3Backend() ;
+ virtual void BeginFrame() override;
+ virtual void EndFrame() override;
+};
diff --git a/core/src/Entrypoint/Vulkan.cpp b/core/src/Entrypoint/Vulkan.cpp
new file mode 100644
index 0000000..5af1772
--- /dev/null
+++ b/core/src/Entrypoint/Vulkan.cpp
@@ -0,0 +1,13 @@
+#include "Vulkan.hpp"
+
+VulkanBackend::VulkanBackend() {
+ // TODO
+}
+
+void VulkanBackend::BeginFrame() {
+ // TODO
+}
+
+void VulkanBackend::EndFrame() {
+ // TODO
+}
diff --git a/core/src/Entrypoint/Vulkan.hpp b/core/src/Entrypoint/Vulkan.hpp
new file mode 100644
index 0000000..6404806
--- /dev/null
+++ b/core/src/Entrypoint/Vulkan.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "Entrypoint/Common.hpp"
+
+class VulkanBackend : public RenderingBackend {
+public:
+ VulkanBackend();
+ virtual ~VulkanBackend() = default;
+ virtual void BeginFrame() override;
+ virtual void EndFrame() override;
+};
diff --git a/core/src/Entrypoint/main.cpp b/core/src/Entrypoint/main.cpp
new file mode 100644
index 0000000..3adf757
--- /dev/null
+++ b/core/src/Entrypoint/main.cpp
@@ -0,0 +1,91 @@
+#include "Entrypoint/Common.hpp"
+#include "Entrypoint/DirectX11.hpp"
+#include "Entrypoint/DirectX12.hpp"
+#include "Entrypoint/Metal.hpp"
+#include "Entrypoint/OpenGL2.hpp"
+#include "Entrypoint/OpenGL3.hpp"
+#include "Entrypoint/Vulkan.hpp"
+
+#include <glad/glad.h>
+
+#include <GLFW/glfw3.h>
+#include <imgui.h>
+#include <argparse/argparse.hpp>
+#include <iostream>
+#include <memory>
+#include <string>
+
+using namespace std::literals::string_literals;
+
+int main(int argc, char* argv[]) {
+ argparse::ArgumentParser parser;
+ parser.add_argument("--rendering-backend")
+ .help("Which rendering backend to use. If equals 'default', the preferred API for each platform will be used")
+ .default_value("default"s);
+
+ try {
+ parser.parse_args(argc, argv);
+ } catch (const std::runtime_error& error) {
+ std::cout << error.what() << '\n';
+ std::cout << parser;
+ return -1;
+ }
+
+ std::unique_ptr<RenderingBackend> backend;
+ auto backendOption = parser.get<std::string>("--rendering-backend");
+ if (backendOption == "default") {
+ // TODO better api selection mechanism, use lower tier if higher tier is unavilable instead of bailing
+#if PLATFORM_WIN32
+# if IMGUI_INCLUDE_DX12_BACKEND
+ backend = std::make_unique<DirectX12Backend>();
+# elif IMGUI_INCLUDE_DX11_BACKEND
+ backend = std::make_unique<DirectX11Backend>();
+# elif IMGUI_INCLUDE_VULKAN_BACKEND
+ backend = std::make_unique<VulkanBackend>();
+# elif IMGUI_INCLUDE_OPENGL3_BACKEND
+ backend = std::make_unique<OpenGL3Backend>();
+# elif IMGUI_INCLUDE_OPENGL2_BACKEND
+ backend = std::make_unique<OpenGL2Backend>();
+# endif
+#elif PLATFORM_MACOS
+ backend = std::make_unique<MetalBackend>();
+#elif PLATFORM_LINUX
+# if IMGUI_INCLUDE_VULKAN_BACKEND
+ backend = std::make_unique<VulkanBackend>();
+// # elif IMGUI_INCLUDE_OPENGL3_BACKEND
+// backend = std::make_unique<OpenGL3Backend>();
+# elif IMGUI_INCLUDE_OPENGL2_BACKEND
+ backend = std::make_unique<OpenGL2Backend>();
+# endif
+#endif
+ } else if (backendOption == "opengl2") {
+ backend = std::make_unique<OpenGL2Backend>();
+ } else if (backendOption == "opengl3") {
+ backend = std::make_unique<OpenGL3Backend>();
+ } else if (backendOption == "vulkan") {
+ backend = std::make_unique<VulkanBackend>();
+ } else if (backendOption == "dx11") {
+ backend = std::make_unique<DirectX11Backend>();
+ } else if (backendOption == "dx12") {
+ backend = std::make_unique<DirectX12Backend>();
+ } else if (backendOption == "metal") {
+ backend = std::make_unique<MetalBackend>();
+ } else {
+ std::cout << "Unknown backend '" << backendOption << "'.\n";
+ return -1;
+ }
+
+ auto& io = ImGui::GetIO();
+ io.Fonts->AddFontFromFileTTF("fonts/NotoSansSC-Regular.otf", 18, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
+
+ auto window = backend->GetWindow();
+ bool showDemo = true;
+ while (!glfwWindowShouldClose(window)) {
+ backend->BeginFrame();
+ if (showDemo)
+ ImGui::ShowDemoWindow(&showDemo);
+ backend->EndFrame();
+ }
+
+ return 0;
+}