aboutsummaryrefslogtreecommitdiff
path: root/src/engine/engine.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-10 19:12:31 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:55 +0200
commit38f14606c78c119d452f302f17329455e29a9a6f (patch)
tree03f6dfd9d3576e87260f7cb3bc436ad076b629c5 /src/engine/engine.cpp
parent09848ad31af6a1c70d64fccee711e231afb5a77f (diff)
refactor: rename game initializer & move input config
Diffstat (limited to 'src/engine/engine.cpp')
-rw-r--r--src/engine/engine.cpp67
1 files changed, 67 insertions, 0 deletions
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 <cstdlib>
+#include <utility>
+
+CLIGameEngine::CLIGameEngine(IGameFactory game_factory, ISceneFactory scene_factory,
+ std::shared_ptr<IInputHandler> input_handler,
+ std::shared_ptr<ICursorController> cursor_controller,
+ std::shared_ptr<IWindow> 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<Vector2::Value>(window_size.get_width()) / 2,
+ .y = static_cast<Vector2::Value>(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<char, Callback> 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<char, Callback> &input_config)
+{
+ for (const auto &config_pair : input_config)
+ {
+ _input_handler->attach(config_pair.first, config_pair.second);
+ }
+}