Refactor platform enums

This commit is contained in:
2020-11-19 21:25:19 +01:00
parent 8dd9bbd75f
commit ef80e8918f
6 changed files with 95 additions and 38 deletions

View File

@@ -6,50 +6,108 @@
#pragma once
#include <magic_enum.hpp>
#include <string>
namespace openVulkanoCpp
{
namespace RenderAPI
class RenderAPI
{
public:
enum RenderApi
{
VULKAN = 0,
Vulkan = 0,
//OpenGL,
//DirectX11,
//DirectX12,
//Metal,
MAX_VALUE
};
inline std::string ToString(RenderApi api)
constexpr RenderAPI(RenderApi api) : api(api) {}
[[nodiscard]] constexpr std::string_view ToString() const
{
switch (api)
{
case VULKAN: return "Vulkan";
}
return "Invalid";
}
return magic_enum::enum_name(api);
}
namespace Platform
[[nodiscard]] constexpr static RenderAPI GetFromName(std::string_view name)
{
enum Platform
return magic_enum::enum_cast<RenderApi>(name).value_or(Vulkan);
}
constexpr operator std::string_view() const { return ToString(); }
constexpr operator RenderApi() const { return api; }
constexpr bool operator==(const RenderAPI other)
{
Windows = 0, MacOS, Linux, Android, iOS, MAX_VALUE
return other.api == api;
}
constexpr bool operator==(const RenderApi other)
{
return other == api;
}
constexpr bool operator!=(const RenderAPI other)
{
return !(*this == other);
}
constexpr bool operator!=(const RenderApi other)
{
return !(*this == other);
}
private:
RenderApi api;
};
inline std::string ToString(Platform os)
class Platform
{
switch (os)
public:
enum OS
{
case Windows: return "Windows";
case MacOS: return "MacOS";
case Linux: return "Linux";
case Android: return "Android";
case iOS: return "iOS";
Windows = 0, MacOS, Linux, Android, iOS, Unknown
};
constexpr Platform(OS os) : os(os) {}
[[nodiscard]] constexpr std::string_view ToString() const
{
return magic_enum::enum_name(os);
}
return "Invalid";
[[nodiscard]] constexpr static Platform GetFromName(std::string_view name)
{
return magic_enum::enum_cast<OS>(name).value_or(Unknown);
}
constexpr operator std::string_view() const { return ToString(); }
constexpr operator OS() const { return os; }
constexpr bool operator==(const Platform other)
{
return other.os == os;
}
constexpr bool operator==(const OS other)
{
return other == os;
}
constexpr bool operator!=(const Platform other)
{
return !(*this == other);
}
constexpr bool operator!=(const OS other)
{
return !(*this == other);
}
private:
OS os;
};
}

View File

@@ -99,13 +99,13 @@ namespace openVulkanoCpp::GLFW
void WindowGLFW::Init(RenderAPI::RenderApi renderApi)
{
if (renderApi == RenderAPI::VULKAN) glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
if (renderApi == RenderAPI::Vulkan) glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
Create();
if (!window)
{
throw WindowInitFailedException("Failed to initialize window");
}
if (renderApi != RenderAPI::VULKAN) MakeCurrentThread();
if (renderApi != RenderAPI::Vulkan) MakeCurrentThread();
Logger::WINDOW->info("GLFW Window created (id: {0})", GetWindowId());
}

View File

@@ -17,7 +17,6 @@ namespace openVulkanoCpp
GraphicsAppManager::GraphicsAppManager(openVulkanoCpp::IGraphicsApp* app, RenderAPI::RenderApi renderApi)
: app(app), renderApi(renderApi)
{
if (renderApi >= RenderAPI::MAX_VALUE) throw std::runtime_error("Invalid RenderAPI");
Logger::SetupLogger();
if (!app)
{

View File

@@ -37,7 +37,7 @@ namespace openVulkanoCpp
std::string windowTitleFormat;
public:
explicit GraphicsAppManager(IGraphicsApp* app, RenderAPI::RenderApi renderApi = RenderAPI::VULKAN);
explicit GraphicsAppManager(IGraphicsApp* app, RenderAPI::RenderApi renderApi = RenderAPI::Vulkan);
~GraphicsAppManager() noexcept override;

View File

@@ -12,24 +12,24 @@
namespace openVulkanoCpp
{
IRenderer* PlatformProducer::CreateRenderManager(RenderAPI::RenderApi renderApi)
IRenderer* PlatformProducer::CreateRenderManager(RenderAPI renderApi)
{
switch (renderApi)
{
case RenderAPI::VULKAN: return new Vulkan::Renderer();
case RenderAPI::Vulkan: return new Vulkan::Renderer();
default:
Logger::RENDER->error("Unsupported render api requested! Requested %d", static_cast<int>(renderApi));
Logger::RENDER->error("Unsupported render api requested! Requested {}", renderApi.ToString());
throw std::runtime_error("Unsupported render api requested!");
}
}
IPlatform* PlatformProducer::CreatePlatform(RenderAPI::RenderApi renderApi)
IPlatform* PlatformProducer::CreatePlatform(RenderAPI renderApi)
{
switch (renderApi)
{
case RenderAPI::VULKAN: return new GLFW::PlatformGLFW();
case RenderAPI::Vulkan: return new GLFW::PlatformGLFW();
default:
Logger::MANAGER->error("Unsupported render api requested! Requested %d", static_cast<int>(renderApi));
Logger::MANAGER->error("Unsupported render api requested! Requested {}", renderApi.ToString());
throw std::runtime_error("Unsupported render api requested!");
}
}

View File

@@ -26,7 +26,7 @@ namespace openVulkanoCpp
* \return The created Renderer.
* \throws std::runtime_error if the render api is not supported
*/
static IRenderer* CreateRenderManager(RenderAPI::RenderApi renderApi);
static IRenderer* CreateRenderManager(RenderAPI renderApi);
/**
* \brief Creates a platform that fits best for the current environment
@@ -34,6 +34,6 @@ namespace openVulkanoCpp
* \return The created platform
* \throws std::runtime_error if the render api is not supported
*/
static IPlatform* CreatePlatform(RenderAPI::RenderApi renderApi);
static IPlatform* CreatePlatform(RenderAPI renderApi);
};
}