diff options
author | rtk0c <[email protected]> | 2021-03-28 15:13:05 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-03-28 15:31:35 -0700 |
commit | a7e5e42a188f9e6ab13706a15e6b50f36f0e00e8 (patch) | |
tree | 61545e68e1186be3900303ded6d12086c345af3f /core/src/Entrypoint/main.cpp | |
parent | bdcc81822adddf2c6ad7f10d9e090d913475c1e0 (diff) |
Fix backend compiling/loading mechanism
Diffstat (limited to 'core/src/Entrypoint/main.cpp')
-rw-r--r-- | core/src/Entrypoint/main.cpp | 135 |
1 files changed, 90 insertions, 45 deletions
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(); } |