diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game/game.cpp | 132 | ||||
-rw-r--r-- | src/game/game.hpp | 2 |
2 files changed, 70 insertions, 64 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp index c1bb162..05b02fd 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -146,70 +146,7 @@ void Game::on_start() {.option_cnt = 1U, .function = [this](CommandInfo::Options options) { - const auto rle_file_path = - expand_path_home(std::filesystem::path(options[0])); - - std::unique_ptr<IMatrix<char>> rle_matrix; - - try - { - rle_matrix = _rle_reader->read_RLE_file(rle_file_path); - } - catch (const InvalidRLEFileError &error) - { - _show_command_error(fmt::format("Error: {}", error.what())); - return; - } - catch (const IOError &error) - { - _show_command_error(fmt::format("Error: {}", error.what())); - return; - } - - if (rle_matrix == nullptr) - { - _show_command_error(fmt::format( - "A unknown error occurred while reading RLE file with path '{}'", - rle_file_path.string())); - return; - } - - _return_to_normal_mode(); - - const auto previous_pos = _cursor_controller->where(); - - auto scene_matrix = _scene->get_matrix(); - - for (auto row : *rle_matrix) - { - for (auto &col : row) - { - const auto col_pos = _cursor_controller->where(); - - std::cout.put(col); - - _cursor_controller->move_to(col_pos); - - scene_matrix->set(col_pos, col); - - if (!ranges::contains(_living_cell_positions, col_pos)) - { - _living_cell_positions.push_back(col_pos); - } - - _cursor_controller->move(Vector2::right(), 1U); - } - - fmt::print("\n"); - const auto current_pos = _cursor_controller->where(); - - _cursor_controller->move_to( - Vector2({.x = previous_pos.get_x(), .y = current_pos.get_y() - 1})); - } - - std::cout.flush(); - - _cursor_controller->move_to(previous_pos); + this->_open_rle_file(options); }}); } @@ -646,3 +583,70 @@ void Game::_process_next_generation() noexcept } } } + +void Game::_open_rle_file(CommandInfo::Options options) noexcept +{ + const auto rle_file_path = expand_path_home(std::filesystem::path(options[0])); + + std::unique_ptr<IMatrix<char>> rle_matrix; + + try + { + rle_matrix = _rle_reader->read_RLE_file(rle_file_path); + } + catch (const InvalidRLEFileError &error) + { + _show_command_error(fmt::format("Error: {}", error.what())); + return; + } + catch (const IOError &error) + { + _show_command_error(fmt::format("Error: {}", error.what())); + return; + } + + if (rle_matrix == nullptr) + { + _show_command_error(fmt::format( + "A unknown error occurred while reading RLE file with path '{}'", + rle_file_path.string())); + return; + } + + _return_to_normal_mode(); + + const auto previous_pos = _cursor_controller->where(); + + auto scene_matrix = _scene->get_matrix(); + + for (auto row : *rle_matrix) + { + for (auto &col : row) + { + const auto col_pos = _cursor_controller->where(); + + std::cout.put(col); + + _cursor_controller->move_to(col_pos); + + scene_matrix->set(col_pos, col); + + if (!ranges::contains(_living_cell_positions, col_pos)) + { + _living_cell_positions.push_back(col_pos); + } + + _cursor_controller->move(Vector2::right(), 1U); + } + + fmt::print("\n"); + const auto current_pos = _cursor_controller->where(); + + _cursor_controller->move_to( + Vector2({.x = previous_pos.get_x(), .y = current_pos.get_y() - 1})); + } + + std::cout.flush(); + + _cursor_controller->move_to(previous_pos); +} diff --git a/src/game/game.hpp b/src/game/game.hpp index 9fc5237..d6e2979 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -132,4 +132,6 @@ private: static void _erase_line_from_cursor() noexcept; void _process_next_generation() noexcept; + + void _open_rle_file(CommandInfo::Options options) noexcept; }; |