From bc6fa836b096eddd2604ee2287480916967f1b20 Mon Sep 17 00:00:00 2001 From: littlefoot Date: Thu, 20 Jan 2022 08:20:17 -0500 Subject: [PATCH] Moved out renderer to being selected in engine constructor based on a parsed enum. Next is to have the renderer initialize in constructor, and probably make game into a templated parameter that does the same along with input processor --- config/engine.toml | 1 + src/engine/engine.cpp | 45 ++++++++++++++++++++++++++++--------------- src/engine/engine.hpp | 14 +++++++++++--- src/main.cpp | 2 +- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/config/engine.toml b/config/engine.toml index 5ff270d..bd46231 100644 --- a/config/engine.toml +++ b/config/engine.toml @@ -3,6 +3,7 @@ target_fps = 61 window_width = 800 window_height = 600 window_title = "Roguelike Algorithm Visualizer" +rendering_engine = 0 [overlay] show_fps = true diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index a81cff5..15ea3a8 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -3,7 +3,8 @@ // #include "engine.hpp" -#include +#include "rendering/sdl/sdlrenderer.hpp" +#include "rlengineexception.hpp" #include #include #include @@ -41,23 +42,12 @@ void Engine::start_loop() { } } -Engine::Engine(std::unique_ptr game, std::unique_ptr renderer, +Engine::Engine(std::unique_ptr game, std::unique_ptr input_processor) { - this->renderer = std::move(renderer); this->game = std::move(game); this->input_processor = std::move(input_processor); - EngineConfig new_config = EngineConfig(); - load_config("engine.toml", [&new_config](const toml::table& table) { - new_config.show_fps = table["overlay"]["show_fps"].value_or(DEFAULT_SHOW_FPS); - new_config.window_width = table["display"]["window_width"].value_or(DEFAULT_WINDOW_WIDTH); - new_config.window_height = table["display"]["window_height"].value_or(DEFAULT_WINDOW_HEIGHT); - new_config.target_fps = table["display"]["target_fps"].value_or(DEFAULT_TARGET_FPS); - new_config.window_title = table["display"]["window_title"].value_or(DEFAULT_WINDOW_TITLE); - auto default_font_path = fmt::format("fonts/{}.ttf", table["font"]["default_font_name"].value_or(DEFAULT_FONT_NAME)); - auto default_font_size = table["font"]["default_font_size"].value_or(DEFAULT_FONT_SIZE); - new_config.default_font = TextRenderDetails{default_font_path, default_font_size, COLOR_WHITE}; - }); - this->config = new_config; + this->parse_config(ENGINE_CONFIG_FILE); + this->setup_renderer(this->config.selected_renderer); } std::chrono::time_point @@ -78,6 +68,31 @@ void Engine::load_config(const std::string &path, const std::function(DEFAULT_SHOW_FPS); + new_config.window_width = table["display"]["window_width"].value_or(DEFAULT_WINDOW_WIDTH); + new_config.window_height = table["display"]["window_height"].value_or(DEFAULT_WINDOW_HEIGHT); + new_config.target_fps = table["display"]["target_fps"].value_or(DEFAULT_TARGET_FPS); + new_config.window_title = table["display"]["window_title"].value_or(DEFAULT_WINDOW_TITLE); + auto default_font_path = fmt::format("fonts/{}.ttf", table["font"]["default_font_name"].value_or(DEFAULT_FONT_NAME)); + auto default_font_size = table["font"]["default_font_size"].value_or(DEFAULT_FONT_SIZE); + new_config.default_font = TextRenderDetails{default_font_path, default_font_size, COLOR_WHITE}; + new_config.selected_renderer = static_cast(table["display"]["rendering_engine"].value_or(0)); + }); + this->config = new_config; +} + +void Engine::setup_renderer(RendererTypes rendering_engine) { + std::unique_ptr rendering_engine_ptr; + switch(rendering_engine) { + case SDL_HARDWARE: rendering_engine_ptr = std::make_unique(); break; + default: throw RLEngineException("Invalid renderer specified"); + } + this->renderer = std::move(rendering_engine_ptr); +} + Engine::~Engine() = default; diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 4b8490a..34a6a48 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -24,6 +24,11 @@ #define DEFAULT_SHOW_FPS false #define DEFAULT_FONT_NAME "Consolas-Regular" #define DEFAULT_FONT_SIZE 12 +#define ENGINE_CONFIG_FILE "engine.toml" + +enum RendererTypes : int { + SDL_HARDWARE = 0 +}; struct EngineConfig { unsigned int target_fps; @@ -32,11 +37,12 @@ struct EngineConfig { bool show_fps; std::string window_title; TextRenderDetails default_font; + RendererTypes selected_renderer; }; class Engine { public: - Engine(std::unique_ptr game, std::unique_ptr renderer, + Engine(std::unique_ptr game, std::unique_ptr input_processor); void start_loop(); static void load_config(const std::string& path, const std::function& f); @@ -49,16 +55,18 @@ private: std::unique_ptr renderer = nullptr; std::unique_ptr input_processor = nullptr; std::chrono::time_point render_fps(std::chrono::time_point end, std::chrono::time_point lastframe); + void parse_config(const std::string& config_file_path); + void setup_renderer(RendererTypes rendering_engine); }; class MissingGameException : public std::exception { public: - const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override; + [[nodiscard]] const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override; }; class MissingRendererException : public std::exception { public: - const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override; + [[nodiscard]] const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override; }; diff --git a/src/main.cpp b/src/main.cpp index 717977d..38b3091 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,7 @@ int main() { std::unique_ptr renderer = std::make_unique(); std::unique_ptr game = std::make_unique(); std::unique_ptr input_processor = std::make_unique(); - auto engine = std::make_unique(std::move(game), std::move(renderer), std::move(input_processor)); + auto engine = std::make_unique(std::move(game), std::move(input_processor)); engine->start_loop(); return 0;