#include "scene.hpp" #include #include #include #include #include #include "engine/escape.hpp" #include "engine/io/terminal.hpp" class IComponent; Scene::Scene(const IMatrixFactory &matrix_factory) noexcept : _matrix(matrix_factory(size())), _is_shown(false) { _matrix->fill(' '); } void Scene::enter() noexcept { if (_is_shown) { return; } // Enable alternative buffer fmt::print(ENABLE_ALT_BUFFER, fmt::arg("esc", ESC)); std::cout.flush(); _original_terminal_state = get_terminal_state(STDIN_FILENO); auto new_terminal_state = TerminalState(_original_terminal_state); new_terminal_state.set_local_mode_flag(TerminalLocalModeFlag::echo, false); new_terminal_state.set_local_mode_flag(TerminalLocalModeFlag::icanon, false); new_terminal_state.set_local_mode_flag(TerminalLocalModeFlag::isig, false); set_terminal_state(STDIN_FILENO, new_terminal_state); _is_shown = true; } void Scene::leave() noexcept { if (!_is_shown) { return; } // Disable alternative buffer fmt::print(DISABLE_ALT_BUFFER, fmt::arg("esc", ESC)); std::cout.flush(); set_terminal_state(STDIN_FILENO, _original_terminal_state); _original_terminal_state = {}; _is_shown = false; } auto Scene::size() const noexcept -> Bounds { winsize window_size = {}; // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) ioctl(0, TIOCGWINSZ, &window_size); return Bounds({window_size.ws_col, window_size.ws_row}); } auto Scene::get_matrix() const noexcept -> const std::shared_ptr> & { return _matrix; } void Scene::register_component( const std::shared_ptr &component, const Vector2 &position) noexcept { _components.emplace_back(std::make_pair(component, position)); } auto Scene::get_components() const noexcept -> std::vector, Vector2>> { return _components; }