From ae15df6689b1245200f8a9eb074c59a200561e49 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 1 Jul 2022 16:37:19 +0200 Subject: refactor: game move opening rle file to method --- src/game/game.cpp | 132 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 64 deletions(-) (limited to 'src/game/game.cpp') 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> 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> 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); +} -- cgit v1.2.3-18-g5258