aboutsummaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'core/src')
-rw-r--r--core/src/Entrypoint/Common.cpp7
-rw-r--r--core/src/Entrypoint/Common.hpp1
-rw-r--r--core/src/Entrypoint/OpenGL2.cpp37
-rw-r--r--core/src/Entrypoint/OpenGL3.cpp37
-rw-r--r--core/src/Entrypoint/OpenGL3.hpp1
-rw-r--r--core/src/Entrypoint/main.cpp135
6 files changed, 149 insertions, 69 deletions
diff --git a/core/src/Entrypoint/Common.cpp b/core/src/Entrypoint/Common.cpp
index 62baf9d..c949830 100644
--- a/core/src/Entrypoint/Common.cpp
+++ b/core/src/Entrypoint/Common.cpp
@@ -1,11 +1,14 @@
#include "Common.hpp"
+#include <backend/imgui_impl_glfw.h>
#include <iostream>
+#include <backend/imgui_impl_glfw.cpp>
+
GLFWwindow* RenderingBackend::GetWindow() const {
- return mWindow;
+ return mWindow;
}
void RenderingBackend::GlfwErrorCallback(int error, const char* message) {
- std::cerr << "GLFW Error " << error << ": " << message << "\n";
+ std::cerr << "GLFW Error " << error << ": " << message << "\n";
}
diff --git a/core/src/Entrypoint/Common.hpp b/core/src/Entrypoint/Common.hpp
index 2a4dcbf..216c885 100644
--- a/core/src/Entrypoint/Common.hpp
+++ b/core/src/Entrypoint/Common.hpp
@@ -1,7 +1,6 @@
#pragma once
#include <glad/glad.h>
-
#include <GLFW/glfw3.h>
class RenderingBackend {
diff --git a/core/src/Entrypoint/OpenGL2.cpp b/core/src/Entrypoint/OpenGL2.cpp
index de13a02..216399e 100644
--- a/core/src/Entrypoint/OpenGL2.cpp
+++ b/core/src/Entrypoint/OpenGL2.cpp
@@ -1,15 +1,17 @@
#include "OpenGL2.hpp"
-#include <glad/glad.h>
+#if BUILD_CORE_WITH_OPENGL2_BACKEND
+# 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>
-#include <GLFW/glfw3.h>
-#include <backend/imgui_impl_glfw.h>
-#include <backend/imgui_impl_opengl2.h>
-#include <imgui.h>
-#include <stdexcept>
+# define IMGUI_IMPL_OPENGL_LOADER_GLAD
+# include <backend/imgui_impl_opengl2.cpp>
OpenGL2Backend::OpenGL2Backend() {
-#if IMGUI_INCLUDE_OPENGL2_BACKEND
glfwSetErrorCallback(GlfwErrorCallback);
if (!glfwInit()) {
throw std::runtime_error("Failed to initialize GLFW.");
@@ -31,9 +33,6 @@ OpenGL2Backend::OpenGL2Backend() {
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() {
@@ -68,3 +67,21 @@ void OpenGL2Backend::EndFrame() {
glfwMakeContextCurrent(mWindow);
glfwSwapBuffers(mWindow);
}
+
+#else // ^^ BUILD_CORE_WITH_OPENGL2_BACKEND | !BUILD_CORE_WITH_OPENGL2_BACKEND vv
+# include <stdexcept>
+
+OpenGL2Backend::OpenGL2Backend() {
+ throw std::runtime_error("Backend opengl2 is not available in this build.\n");
+}
+
+OpenGL2Backend::~OpenGL2Backend() {
+}
+
+void OpenGL2Backend::BeginFrame() {
+}
+
+void OpenGL2Backend::EndFrame() {
+}
+
+#endif
diff --git a/core/src/Entrypoint/OpenGL3.cpp b/core/src/Entrypoint/OpenGL3.cpp
index c1f66d5..7d5cae1 100644
--- a/core/src/Entrypoint/OpenGL3.cpp
+++ b/core/src/Entrypoint/OpenGL3.cpp
@@ -1,15 +1,17 @@
#include "OpenGL3.hpp"
-#include <glad/glad.h>
+#if BUILD_CORE_WITH_OPENGL3_BACKEND
+# 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>
-#include <GLFW/glfw3.h>
-#include <backend/imgui_impl_glfw.h>
-#include <backend/imgui_impl_opengl3.h>
-#include <imgui.h>
-#include <stdexcept>
+# define IMGUI_IMPL_OPENGL_LOADER_GLAD
+# include <backend/imgui_impl_opengl3.cpp>
OpenGL3Backend::OpenGL3Backend() {
-#if IMGUI_INCLUDE_OPENGL3_BACKEND
glfwSetErrorCallback(GlfwErrorCallback);
if (!glfwInit()) {
throw std::runtime_error("Failed to initialize GLFW.");
@@ -47,9 +49,6 @@ OpenGL3Backend::OpenGL3Backend() {
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() {
@@ -83,3 +82,21 @@ void OpenGL3Backend::EndFrame() {
glfwSwapBuffers(mWindow);
}
+
+#else // ^^ BUILD_CORE_WITH_OPENGL3_BACKEND | !BUILD_CORE_WITH_OPENGL3_BACKEND vv
+# include <stdexcept>
+
+OpenGL3Backend::OpenGL3Backend() {
+ throw std::runtime_error("Backend opengl3 is not available in this build.\n");
+}
+
+OpenGL3Backend::~OpenGL3Backend() {
+}
+
+void OpenGL3Backend::BeginFrame() {
+}
+
+void OpenGL3Backend::EndFrame() {
+}
+
+#endif
diff --git a/core/src/Entrypoint/OpenGL3.hpp b/core/src/Entrypoint/OpenGL3.hpp
index 29086a2..52978fa 100644
--- a/core/src/Entrypoint/OpenGL3.hpp
+++ b/core/src/Entrypoint/OpenGL3.hpp
@@ -3,7 +3,6 @@
#include "Entrypoint/Common.hpp"
#include <glad/glad.h>
-
#include <GLFW/glfw3.h>
class OpenGL3Backend : public RenderingBackend {
diff --git a/core/src/Entrypoint/main.cpp b/core/src/Entrypoint/main.cpp
index 3adf757..b82214d 100644
--- a/core/src/Entrypoint/main.cpp
+++ b/core/src/Entrypoint/main.cpp
@@ -13,9 +13,98 @@
#include <argparse/argparse.hpp>
#include <iostream>
#include <memory>
+#include <stdexcept>
#include <string>
+#include <string_view>
using namespace std::literals::string_literals;
+using namespace std::literals::string_view_literals;
+
+static std::unique_ptr<RenderingBackend> CreateDefaultBackend() {
+#if PLATFORM_WIN32
+# if BUILD_CORE_WITH_DX12_BACKEND
+ try {
+ auto backend = std::make_unique<DirectX12Backend>();
+ return backend;
+ } catch (const std::exception&) {
+ }
+# elif BUILD_CORE_WITH_DX11_BACKEND
+ try {
+ auto backend = std::make_unique<DirectX11Backend>();
+ return backend;
+ } catch (const std::exception&) {
+ }
+# elif BUILD_CORE_WITH_VULKAN_BACKEND
+ try {
+ auto backend = std::make_unique<VulkanBackend>();
+ return backend;
+ } catch (const std::exception&) {
+ }
+# elif BUILD_CORE_WITH_OPENGL3_BACKEND
+ try {
+ auto backend = std::make_unique<OpenGL3Backend>();
+ return backend;
+ } catch (const std::exception&) {
+ }
+# elif BUILD_CORE_WITH_OPENGL2_BACKEND
+ try {
+ auto backend = std::make_unique<OpenGL2Backend>();
+ return backend;
+ } catch (const std::exception&) {
+ }
+# endif
+#elif PLATFORM_MACOS
+ // We currently only support using metal on macos
+ backend = std::make_unique<MetalBackend>();
+#elif PLATFORM_LINUX
+# if BUILD_CORE_WITH_VULKAN_BACKEND
+ try {
+ auto backend = std::make_unique<VulkanBackend>();
+ return backend;
+ } catch (const std::exception&) {
+ }
+# elif BUILD_CORE_WITH_OPENGL3_BACKEND
+ try {
+ auto backend = std::make_unique<OpenGL3Backend>();
+ return backend;
+ } catch (const std::exception&) {
+ }
+# elif BUILD_CORE_WITH_OPENGL2_BACKEND
+ try {
+ auto backend = std::make_unique<OpenGL2Backend>();
+ return backend;
+ } catch (const std::exception&) {
+ }
+# endif
+#endif
+
+ return nullptr;
+}
+
+static std::unique_ptr<RenderingBackend> CreateBackend(std::string_view option) {
+ if (option == "default") {
+ return CreateDefaultBackend();
+ } else if (option == "opengl2") {
+ return std::make_unique<OpenGL2Backend>();
+ } else if (option == "opengl3") {
+ return std::make_unique<OpenGL3Backend>();
+ } else if (option == "vulkan") {
+ return std::make_unique<VulkanBackend>();
+ } else if (option == "dx11") {
+ return std::make_unique<DirectX11Backend>();
+ } else if (option == "dx12") {
+ return std::make_unique<DirectX12Backend>();
+ } else if (option == "metal") {
+ return std::make_unique<MetalBackend>();
+ } else {
+ std::string message;
+ message += "Unknown backend '";
+ message += option;
+ message += "'.\n";
+ throw std::runtime_error(message);
+ return nullptr;
+ }
+}
int main(int argc, char* argv[]) {
argparse::ArgumentParser parser;
@@ -31,59 +120,15 @@ int main(int argc, char* argv[]) {
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 backend = CreateBackend(backendOption);
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();
}