diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-12 13:44:58 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:57:01 +0200 |
commit | 927e065f9829045247be7c0b3296408b6f577c1f (patch) | |
tree | 7da3d9cd5aa4070414a8708a582f6c3ab3e1e708 /src/game/game.cpp | |
parent | eb66598c326862fd9dfc1899be4eac93f81a8023 (diff) |
feat: add reading RLE files
Diffstat (limited to 'src/game/game.cpp')
-rw-r--r-- | src/game/game.cpp | 77 |
1 files changed, 69 insertions, 8 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp index 73ab7f5..8d15b86 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -3,7 +3,10 @@ #include "engine/data/bounds.hpp" #include "engine/escape.hpp" #include "engine/keycodes.hpp" +#include "errors/RLE_reader.hpp" +#include "errors/io.hpp" #include "util/algorithm.hpp" +#include "util/fs.hpp" #include "util/string.hpp" #include <fmt/color.h> @@ -21,7 +24,8 @@ Game::Game( std::shared_ptr<IGenerationTracker> generation_tracker, std::shared_ptr<IStatusManager> status_manager, std::shared_ptr<IUserInputObserver> user_input_observer, - std::shared_ptr<ICellHelper> cell_helper) noexcept + std::shared_ptr<ICellHelper> cell_helper, + std::shared_ptr<IRLEReader> rle_reader) noexcept : _statusline_factory(std::move(statusline_factory)), _scene(std::move(scene)), _cursor_controller(std::move(cursor_controller)), @@ -29,6 +33,7 @@ Game::Game( _status_manager(std::move(status_manager)), _user_input_observer(std::move(user_input_observer)), _cell_helper(std::move(cell_helper)), + _rle_reader(std::move(rle_reader)), _current_mode(Mode::NORMAL), _minimum_cursor_pos_y(0) { @@ -89,18 +94,74 @@ void Game::on_start() noexcept scene_size.get_width(), scene_size.get_height())); - _commands["ping"] = CommandInfo( - {.option_cnt = 0, - .function = [this](CommandInfo::Options /*options*/) + _commands["open"] = CommandInfo( + {.option_cnt = 1U, + .function = [this](CommandInfo::Options options) { - _erase_entire_line(); + const auto rle_file_path = + expand_path_home(std::filesystem::path(options[0])); - const auto position = _cursor_controller->where(); + std::unique_ptr<IMatrix<char>> rle_matrix; - _cursor_controller->move_to(Vector2({.x = 0, .y = position.get_y()})); + 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 (!container_has(_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 << "pong!"; std::cout.flush(); + + _cursor_controller->move_to(previous_pos); }}); } |