Skip to content

Commit

Permalink
Modified renderer selection system. From pre-processor definition bas…
Browse files Browse the repository at this point in the history
…ed it is now runtime based, using a TOML config file.
  • Loading branch information
wowvain-dev committed Mar 11, 2024
1 parent 1b220f0 commit 0c3df73
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 42 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_subdirectory(src/Engine)
add_executable(VaneEngine
src/main.cpp
src/glad.c
include/types.h
)


Expand Down
12 changes: 12 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

namespace Vane {
enum BACKEND {
AUTO = 0,
DX11 = 1,
DX12 = 2,
VULKAN = 3,
OPENGL = 4
};

}
2 changes: 2 additions & 0 deletions src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Engine::~Engine() {

void Engine::initialize() {
config.initialize();


}


Expand Down
53 changes: 47 additions & 6 deletions src/Engine/EngineConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

#include "EngineConfig.h"

#include <filesystem>

namespace Vane {

bool EngineConfig::readConfig() {


bool EngineConfig::parseConfigFile() {
auto configPath = EngineConfig::configPath();

if (!std::filesystem::exists(configPath)) {
return false;
}

try {
tbl = toml::parse_file(configPath);
_table = toml::parse_file(configPath);
// std::cout << tbl << "\n";
} catch (const toml::parse_error& err) {
std::cerr << "Toml parsing error: \n" << err << "\n";
Expand All @@ -26,6 +27,38 @@ bool EngineConfig::readConfig() {
return true;
}

BACKEND EngineConfig::getBackend() const {
std::optional<std::string_view> opt_backend =
_table["engine"]["backend"].value<std::string_view>();

if (!opt_backend.has_value()) {
std::cerr << "Invalid `engine.backend` config value." << std::endl;
std::cerr << "Returning default value..." << std::endl;

return AUTO;
}

auto backend = opt_backend.value();

if (backend == "DX11") {
return DX11;
}
if (backend == "OPENGL") {
return OPENGL;
}
if (backend == "VULKAN") {
return VULKAN;
}
if (backend == "DX12") {
return DX12;
}

std::cerr << "Invalid `engine.backend` config value." << std::endl;
std::cerr << "Returning default value..." << std::endl;
return AUTO;
}


toml::table EngineConfig::defaultConfiguration() {
auto defaults = toml::table {
{"engine", toml::table {
Expand Down Expand Up @@ -68,19 +101,27 @@ std::string EngineConfig::configPath() {
}

void EngineConfig::initialize() {
if (!readConfig()) {
tbl = defaultConfiguration();
if (!parseConfigFile()) {
_table = defaultConfiguration();
auto configPath = EngineConfig::configPath();

std::ofstream config(configPath.c_str());

config << tbl;
config << _table;

config.close();
}

// CHECKING SYNTAX AND VALUES
}

void EngineConfig::saveChanges() const {
auto path = configPath();
std::ofstream config(path);

config << _table;

config.close();
}

} // Vane
11 changes: 8 additions & 3 deletions src/Engine/include/EngineConfig.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#include <toml++/toml.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>

#include "types.h"

#ifdef _WIN32

#elif unix
Expand All @@ -17,14 +20,16 @@
namespace Vane {

class EngineConfig {
public:
toml::table tbl;
toml::table _table;

public:
void initialize();
bool readConfig();
bool parseConfigFile();

BACKEND getBackend() const;

private:
void saveChanges() const;
static toml::table defaultConfiguration();
static std::string configPath();
};
Expand Down
File renamed without changes.
82 changes: 59 additions & 23 deletions src/Renderer/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,59 @@ Window::~Window() {
cleanup();
}

bool Window::createWindow(const char *title, int width, int height) {
window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
#ifdef USE_OPENGL
SDL_WINDOW_OPENGL
#elif USE_VULKAN
SDL_WINDOW_VULKAN
#else
SDL_WINDOW_SHOWN
bool Window::createWindow(const char *title, int width, int height, Vane::BACKEND backend) {
if (backend == AUTO) {
#ifdef _WIN32

window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
SDL_WINDOW_SHOWN);
#elif
window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
SDL_WINDOW_VULKAN);
#endif
);
} else if (backend == OPENGL) {
window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
SDL_WINDOW_OPENGL);
} else if (backend == VULKAN) {
window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
SDL_WINDOW_VULKAN);
} else {
window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
SDL_WINDOW_SHOWN);
}

if (!window) {
std::cerr << fmt::format("Window could not be created! SDL_Error: ", SDL_GetError())
<< std::endl;
return false;
}

initializeRenderer(window);
initializeRenderer(window, backend);

return true;
}
Expand All @@ -54,16 +84,22 @@ void Window::getWindowSize(int &w, int &h) const {
SDL_GetWindowSize(this->window, &w, &h);
}

void Window::initializeRenderer(SDL_Window* window) {
#ifdef USE_OPENGL
renderer = new GLRenderer(window);
#elif USE_DIRECTX
renderer = new DirectXRenderer(window);
void Window::initializeRenderer(SDL_Window* window, BACKEND backend) {
if (backend == AUTO) {
#ifdef _WIN32
renderer = new DX11Renderer(window);
#else
// TODO(wowvain-dev): add Vulkan renderer;
// VULKAN IMPLEMENTATION
#endif


} else if (backend == DX12) {
// DX12 IMPLEMENTATION
} else if (backend == DX11) {
renderer = new DX11Renderer(window);
} else if (backend == VULKAN) {
// VULKAN IMPLEMENTATION
} else if (backend == OPENGL) {
renderer = new GLRenderer(window);
}
renderer->initialize();
}

Expand Down
5 changes: 3 additions & 2 deletions src/Renderer/include/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

#include <SDL2/SDL.h>
#include "Renderer.h"
#include "types.h"

namespace Vane {
class Window {
public:
Window();
~Window();

bool createWindow(const char* title, int width, int height);
bool createWindow(const char* title, int width, int height, Vane::BACKEND backend);
void getWindowSize(int &w, int &h) const;
void mainLoop() const;

private:
SDL_Window* window = nullptr;
Renderer* renderer = nullptr;

void initializeRenderer(SDL_Window*);
void initializeRenderer(SDL_Window*, Vane::BACKEND);
void cleanup() const;
};

Expand Down
11 changes: 3 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@ int main(int argv, char** args) {

Vane::Engine engine;

std::cout << engine.config.tbl << std::endl;
auto activeBackend = engine.config.getBackend();

Vane::Window window;

Vane::Window window;

#ifdef USE_OPENGL
fmt::print("Using OpenGL\n");
#elif USE_DIRECTX
fmt::print("Using DirectX\n");
#endif

if (window.createWindow("My game engine", 1350, 900)) {
if (window.createWindow("My game engine", 1350, 900, activeBackend)) {
window.mainLoop();
}

Expand Down

0 comments on commit 0c3df73

Please sign in to comment.