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

This commit is contained in:
2022-01-20 08:20:17 -05:00
parent 1a917af13c
commit bc6fa836b0
4 changed files with 43 additions and 19 deletions

View File

@@ -3,6 +3,7 @@ target_fps = 61
window_width = 800 window_width = 800
window_height = 600 window_height = 600
window_title = "Roguelike Algorithm Visualizer" window_title = "Roguelike Algorithm Visualizer"
rendering_engine = 0
[overlay] [overlay]
show_fps = true show_fps = true

View File

@@ -3,7 +3,8 @@
// //
#include "engine.hpp" #include "engine.hpp"
#include <cstdio> #include "rendering/sdl/sdlrenderer.hpp"
#include "rlengineexception.hpp"
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <exception> #include <exception>
@@ -41,23 +42,12 @@ void Engine::start_loop() {
} }
} }
Engine::Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer, Engine::Engine(std::unique_ptr<Game> game,
std::unique_ptr<InputProcessor> input_processor) { std::unique_ptr<InputProcessor> input_processor) {
this->renderer = std::move(renderer);
this->game = std::move(game); this->game = std::move(game);
this->input_processor = std::move(input_processor); this->input_processor = std::move(input_processor);
EngineConfig new_config = EngineConfig(); this->parse_config(ENGINE_CONFIG_FILE);
load_config("engine.toml", [&new_config](const toml::table& table) { this->setup_renderer(this->config.selected_renderer);
new_config.show_fps = table["overlay"]["show_fps"].value_or<bool>(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;
} }
std::chrono::time_point<std::chrono::system_clock> std::chrono::time_point<std::chrono::system_clock>
@@ -78,6 +68,31 @@ void Engine::load_config(const std::string &path, const std::function<void (toml
f(table); f(table);
} }
void Engine::parse_config(const std::string& config_file_path) {
EngineConfig new_config = EngineConfig();
load_config("engine.toml", [&new_config](const toml::table& table) {
new_config.show_fps = table["overlay"]["show_fps"].value_or<bool>(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<RendererTypes>(table["display"]["rendering_engine"].value_or(0));
});
this->config = new_config;
}
void Engine::setup_renderer(RendererTypes rendering_engine) {
std::unique_ptr<Renderer> rendering_engine_ptr;
switch(rendering_engine) {
case SDL_HARDWARE: rendering_engine_ptr = std::make_unique<SdlRenderer>(); break;
default: throw RLEngineException("Invalid renderer specified");
}
this->renderer = std::move(rendering_engine_ptr);
}
Engine::~Engine() = default; Engine::~Engine() = default;

View File

@@ -24,6 +24,11 @@
#define DEFAULT_SHOW_FPS false #define DEFAULT_SHOW_FPS false
#define DEFAULT_FONT_NAME "Consolas-Regular" #define DEFAULT_FONT_NAME "Consolas-Regular"
#define DEFAULT_FONT_SIZE 12 #define DEFAULT_FONT_SIZE 12
#define ENGINE_CONFIG_FILE "engine.toml"
enum RendererTypes : int {
SDL_HARDWARE = 0
};
struct EngineConfig { struct EngineConfig {
unsigned int target_fps; unsigned int target_fps;
@@ -32,11 +37,12 @@ struct EngineConfig {
bool show_fps; bool show_fps;
std::string window_title; std::string window_title;
TextRenderDetails default_font; TextRenderDetails default_font;
RendererTypes selected_renderer;
}; };
class Engine { class Engine {
public: public:
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer, Engine(std::unique_ptr<Game> game,
std::unique_ptr<InputProcessor> input_processor); std::unique_ptr<InputProcessor> input_processor);
void start_loop(); void start_loop();
static void load_config(const std::string& path, const std::function<void (toml::table)>& f); static void load_config(const std::string& path, const std::function<void (toml::table)>& f);
@@ -49,16 +55,18 @@ private:
std::unique_ptr<Renderer> renderer = nullptr; std::unique_ptr<Renderer> renderer = nullptr;
std::unique_ptr<InputProcessor> input_processor = nullptr; std::unique_ptr<InputProcessor> input_processor = nullptr;
std::chrono::time_point<std::chrono::system_clock> render_fps(std::chrono::time_point<std::chrono::system_clock> end, std::chrono::time_point<std::chrono::system_clock> lastframe); std::chrono::time_point<std::chrono::system_clock> render_fps(std::chrono::time_point<std::chrono::system_clock> end, std::chrono::time_point<std::chrono::system_clock> lastframe);
void parse_config(const std::string& config_file_path);
void setup_renderer(RendererTypes rendering_engine);
}; };
class MissingGameException : public std::exception { class MissingGameException : public std::exception {
public: 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 { class MissingRendererException : public std::exception {
public: public:
const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override; [[nodiscard]] const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override;
}; };

View File

@@ -11,7 +11,7 @@ int main() {
std::unique_ptr<Renderer> renderer = std::make_unique<SdlRenderer>(); std::unique_ptr<Renderer> renderer = std::make_unique<SdlRenderer>();
std::unique_ptr<Game> game = std::make_unique<Visualizer>(); std::unique_ptr<Game> game = std::make_unique<Visualizer>();
std::unique_ptr<InputProcessor> input_processor = std::make_unique<SdlInputProcessor>(); std::unique_ptr<InputProcessor> input_processor = std::make_unique<SdlInputProcessor>();
auto engine = std::make_unique<Engine>(std::move(game), std::move(renderer), std::move(input_processor)); auto engine = std::make_unique<Engine>(std::move(game), std::move(input_processor));
engine->start_loop(); engine->start_loop();
return 0; return 0;