diff --git a/CMakeLists.txt b/CMakeLists.txt index bca630c..af47194 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ add_subdirectory(src/Engine) add_executable(VaneEngine src/main.cpp src/glad.c + include/types.h ) diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..9849df4 --- /dev/null +++ b/include/types.h @@ -0,0 +1,12 @@ +#pragma once + +namespace Vane { +enum BACKEND { + AUTO = 0, + DX11 = 1, + DX12 = 2, + VULKAN = 3, + OPENGL = 4 +}; + +} \ No newline at end of file diff --git a/src/Engine/Engine.cpp b/src/Engine/Engine.cpp index bfce9a9..f08cb0c 100644 --- a/src/Engine/Engine.cpp +++ b/src/Engine/Engine.cpp @@ -14,6 +14,8 @@ Engine::~Engine() { void Engine::initialize() { config.initialize(); + + } diff --git a/src/Engine/EngineConfig.cpp b/src/Engine/EngineConfig.cpp index 3132867..36e46dd 100644 --- a/src/Engine/EngineConfig.cpp +++ b/src/Engine/EngineConfig.cpp @@ -4,11 +4,12 @@ #include "EngineConfig.h" -#include namespace Vane { -bool EngineConfig::readConfig() { + + +bool EngineConfig::parseConfigFile() { auto configPath = EngineConfig::configPath(); if (!std::filesystem::exists(configPath)) { @@ -16,7 +17,7 @@ bool EngineConfig::readConfig() { } 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"; @@ -26,6 +27,38 @@ bool EngineConfig::readConfig() { return true; } +BACKEND EngineConfig::getBackend() const { + std::optional opt_backend = + _table["engine"]["backend"].value(); + + 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 { @@ -68,13 +101,13 @@ 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(); } @@ -82,5 +115,13 @@ void EngineConfig::initialize() { // CHECKING SYNTAX AND VALUES } +void EngineConfig::saveChanges() const { + auto path = configPath(); + std::ofstream config(path); + + config << _table; + + config.close(); +} } // Vane \ No newline at end of file diff --git a/src/Engine/include/EngineConfig.h b/src/Engine/include/EngineConfig.h index a384187..4422a6c 100644 --- a/src/Engine/include/EngineConfig.h +++ b/src/Engine/include/EngineConfig.h @@ -1,9 +1,12 @@ #pragma once #include +#include #include #include +#include "types.h" + #ifdef _WIN32 #elif unix @@ -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(); }; diff --git a/include/Physics/PhysicsEngine.h b/src/Physics/include/PhysicsEngine.h similarity index 100% rename from include/Physics/PhysicsEngine.h rename to src/Physics/include/PhysicsEngine.h diff --git a/src/Renderer/Window.cpp b/src/Renderer/Window.cpp index c1806e6..6f2eca1 100644 --- a/src/Renderer/Window.cpp +++ b/src/Renderer/Window.cpp @@ -20,21 +20,51 @@ 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()) @@ -42,7 +72,7 @@ bool Window::createWindow(const char *title, int width, int height) { return false; } - initializeRenderer(window); + initializeRenderer(window, backend); return true; } @@ -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(); } diff --git a/src/Renderer/include/Window.h b/src/Renderer/include/Window.h index b28763c..e51faad 100644 --- a/src/Renderer/include/Window.h +++ b/src/Renderer/include/Window.h @@ -2,6 +2,7 @@ #include #include "Renderer.h" +#include "types.h" namespace Vane { class Window { @@ -9,7 +10,7 @@ class Window { 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; @@ -17,7 +18,7 @@ class Window { SDL_Window* window = nullptr; Renderer* renderer = nullptr; - void initializeRenderer(SDL_Window*); + void initializeRenderer(SDL_Window*, Vane::BACKEND); void cleanup() const; }; diff --git a/src/main.cpp b/src/main.cpp index 8a98aa8..b75309f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); }