Skip to content

Commit

Permalink
Add string-based renderer backend configuration
Browse files Browse the repository at this point in the history
Rather than using integer-indices, just use plaintext case-insensitive
names and leave the actual enum indexes as an implementation detail.
  • Loading branch information
Wunkolo committed Jul 18, 2023
1 parent 2c57936 commit 528ed51
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions include/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ enum class RendererType : s8 {
OpenGL = 1,
};

std::optional<RendererType> fromString(std::string inString);
const char* toString(RendererType rendererType);

class GPU;

class Renderer {
Expand Down
16 changes: 14 additions & 2 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@ void EmulatorConfig::load(const std::filesystem::path& path) {
if (gpuResult.is_ok()) {
auto gpu = gpuResult.unwrap();

rendererType = RendererType(toml::find_or<toml::integer>(gpu, "Renderer", int64_t(rendererType)));
// Get renderer
auto rendererResult = toml::expect<std::string>(gpu, "Renderer");
if (rendererResult.is_ok()) {
auto rendererName = rendererResult.unwrap();
if (auto configRendererType = fromString(rendererName); configRendererType.has_value()) {
rendererType = configRendererType.value();
} else {
Helpers::warn("Invalid renderer specified: %s\n", rendererName.c_str());
}
} else {
Helpers::warn("Renderer not specified: %s\n", rendererResult.unwrap_err());
}

shaderJitEnabled = toml::find_or<toml::boolean>(gpu, "EnableShaderJIT", false);
}
}
Expand All @@ -57,7 +69,7 @@ void EmulatorConfig::save(const std::filesystem::path& path) {
printf("Saving new configuration file %s\n", path.string().c_str());
}

data["GPU"]["Renderer"] = static_cast<s8>(rendererType);
data["GPU"]["Renderer"] = toString(rendererType);
data["GPU"]["EnableShaderJIT"] = shaderJitEnabled;

std::ofstream file(path, std::ios::out);
Expand Down
2 changes: 1 addition & 1 deletion src/core/PICA/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ GPU::GPU(Memory& mem, EmulatorConfig& config) : mem(mem), config(config) {
}
#endif
default: {
Helpers::panic("Invalid rendering backend index: %d", static_cast<s8>(config.rendererType));
Helpers::panic("Rendering backend not supported: %s", toString(config.rendererType));
break;
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
#include "renderer.hpp"

std::optional<RendererType> fromString(std::string inString) {
// case-insensitive
std::transform(inString.begin(), inString.end(), inString.begin(), [](unsigned char c) { return std::tolower(c); });

if (inString == "null")
return RendererType::Null;
else if (inString == "opengl")
return RendererType::OpenGL;

return std::nullopt;
}

const char* toString(RendererType rendererType) {
switch (rendererType) {
case RendererType::Null: return "null";
case RendererType::OpenGL: return "opengl";
default: return "Invalid";
}
}

Renderer::Renderer(GPU& gpu, const std::array<u32, regNum>& internalRegs) : gpu(gpu), regs(internalRegs) {}
Renderer::~Renderer() {}

0 comments on commit 528ed51

Please sign in to comment.