#include "engine.hpp" #include "strings.hpp" #include "util/function.hpp" #include #include #include CLIGameEngine::CLIGameEngine(IGameFactory game_factory, ISceneFactory scene_factory, std::shared_ptr input_handler, std::shared_ptr cursor_controller, std::shared_ptr window) noexcept : _game_factory(game_factory), _scene_factory(scene_factory), _input_handler(std::move(input_handler)), _cursor_controller(std::move(cursor_controller)), _window(std::move(window)) { } void CLIGameEngine::start() noexcept { auto scene = _scene_factory(_cursor_controller, _window); scene->enter(); _input_handler->enter_raw_mode(); const auto window_size = _window->size(); const auto center_position = Vector2({.x = static_cast(window_size.get_width()) / 2, .y = static_cast(window_size.get_height()) / 2}); _cursor_controller->move_to(center_position); scene->write_status(fmt::format(STATUS_BAR_COORDINATES, fmt::arg("x", center_position.get_x()), fmt::arg("y", center_position.get_y()))); std::atexit(normalize_lambda( [this, scene]() { scene->leave(); _input_handler->leave_raw_mode(); for (auto row : *scene->get_matrix()) { for (auto &col : row) { fmt::print("{}", col); } fmt::print("\n"); } std::cout.flush(); })); auto game = _game_factory(); _configure_input(game->get_input_config(_window, scene, _cursor_controller)); _input_handler->listen(); game->run(); } void CLIGameEngine::_configure_input( const std::unordered_map> &input_config) noexcept { for (const auto &config_pair : input_config) { auto key = config_pair.first; auto command = config_pair.second; _input_handler->attach(key, command); } }