From 38f14606c78c119d452f302f17329455e29a9a6f Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 10 Mar 2022 19:12:31 +0100 Subject: refactor: rename game initializer & move input config --- src/engine/engine.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/engine/engine.cpp (limited to 'src/engine/engine.cpp') diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp new file mode 100644 index 0000000..0227d5c --- /dev/null +++ b/src/engine/engine.cpp @@ -0,0 +1,67 @@ +#include "engine.hpp" + +#include "input_actions.hpp" + +#include "util/function.hpp" + +#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(); + + 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); + + std::atexit(normalize_lambda( + [scene, this]() + { + scene->leave(); + _input_handler->leave_raw_mode(); + })); + + const std::unordered_map input_config = { + {'q', InputActions::exit_success}, + {'k', InputActions::move_cursor(Vector2::up(), _cursor_controller, _window)}, + {'j', InputActions::move_cursor(Vector2::down(), _cursor_controller, _window)}, + {'h', InputActions::move_cursor(Vector2::left(), _cursor_controller, _window)}, + {'l', InputActions::move_cursor(Vector2::right(), _cursor_controller, _window)}}; + + _configure_input(input_config); + + _input_handler->listen(); + + auto game = _game_factory(); + + game->run(); +} + +void CLIGameEngine::_configure_input( + const std::unordered_map &input_config) +{ + for (const auto &config_pair : input_config) + { + _input_handler->attach(config_pair.first, config_pair.second); + } +} -- cgit v1.2.3-18-g5258