diff options
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/DI/object_wrapper.hpp | 2 | ||||
-rw-r--r-- | src/bootstrap.cpp | 21 | ||||
-rw-r--r-- | src/engine/graphics/bounds.cpp | 65 | ||||
-rw-r--r-- | src/engine/graphics/bounds.hpp | 28 | ||||
-rw-r--r-- | src/engine/graphics/scene.cpp | 57 | ||||
-rw-r--r-- | src/engine/graphics/scene.hpp | 31 | ||||
-rw-r--r-- | src/engine/graphics/string_matrix.cpp | 41 | ||||
-rw-r--r-- | src/engine/graphics/string_matrix.hpp | 60 | ||||
-rw-r--r-- | src/game/game.cpp | 14 | ||||
-rw-r--r-- | src/game/game.hpp | 10 | ||||
-rw-r--r-- | src/interfaces/bounds.hpp | 41 | ||||
-rw-r--r-- | src/interfaces/matrix.hpp | 48 | ||||
-rw-r--r-- | src/interfaces/scene.hpp | 15 |
14 files changed, 427 insertions, 9 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a32942b..385283d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,9 @@ file(GLOB SOURCES argument_parser.cpp game/game.cpp engine/graphics/vector2.cpp + engine/graphics/bounds.cpp + engine/graphics/scene.cpp + engine/graphics/string_matrix.cpp randomization/generator.cpp randomization/seed_generator.cpp DI/object_type.cpp) diff --git a/src/DI/object_wrapper.hpp b/src/DI/object_wrapper.hpp index 0ae66df..19d8cdd 100644 --- a/src/DI/object_wrapper.hpp +++ b/src/DI/object_wrapper.hpp @@ -9,7 +9,7 @@ template <class Interface, class ObjectImpl> class ObjectWrapper : public IWrapper<std::shared_ptr<Interface>> { public: - explicit ObjectWrapper(const Container &container) : _container(container) {} + explicit ObjectWrapper(const Container &container) noexcept : _container(container) {} [[nodiscard]] std::shared_ptr<Interface> get() const; diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index ce07621..8a8aa25 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -2,12 +2,18 @@ // Interfaces #include "interfaces/argument_parser.hpp" +#include "interfaces/bounds.hpp" #include "interfaces/game.hpp" +#include "interfaces/matrix.hpp" #include "interfaces/randomization.hpp" +#include "interfaces/scene.hpp" #include "interfaces/vector2.hpp" // Implementations #include "argument_parser.hpp" +#include "engine/graphics/bounds.hpp" +#include "engine/graphics/scene.hpp" +#include "engine/graphics/string_matrix.hpp" #include "engine/graphics/vector2.hpp" #include "game/game.hpp" #include "randomization/generator.hpp" @@ -15,6 +21,7 @@ #include <memory> #include <random> +#include <string_view> Container bootstrap() { @@ -22,6 +29,7 @@ Container bootstrap() container.bind<IArgumentParser>().to<ArgumentParser>(); container.bind<IGame>().to<Game>(); + container.bind<IScene>().to<Scene>(); container.bind<IRandomNumberGeneratorFactory>().to_factory( [](const unsigned int &seed) @@ -44,5 +52,18 @@ Container bootstrap() std::make_shared<Vector2>(options)); }); + container.bind<IMatrixFactory<std::string_view>>().to_factory( + [](const IBounds &bounds) + { + return std::dynamic_pointer_cast<IMatrix<std::string_view>>( + std::make_shared<StringMatrix>(bounds)); + }); + + container.bind<IBoundsFactory>().to_factory( + [](const IBoundsOptions &options) + { + return std::dynamic_pointer_cast<IBounds>(std::make_shared<Bounds>(options)); + }); + return container; } diff --git a/src/engine/graphics/bounds.cpp b/src/engine/graphics/bounds.cpp new file mode 100644 index 0000000..8cb83eb --- /dev/null +++ b/src/engine/graphics/bounds.cpp @@ -0,0 +1,65 @@ +#include "bounds.hpp" + +Bounds::Bounds(const IBoundsOptions &options) + : _width(options.width), _height(options.height) +{ +} + +unsigned int Bounds::width() const +{ + return _width; +} + +void Bounds::width(unsigned int width) +{ + _width = width; +} + +unsigned int Bounds::height() const +{ + return _height; +} + +void Bounds::height(unsigned int height) +{ + _height = height; +} + +CoordsValidation Bounds::validate_coords(const IVector2 &coords) const +{ + if (coords.x() >= _width) + { + return CoordsValidation::X_HIGH; + } + + if (coords.y() >= _height) + { + return CoordsValidation::Y_HIGH; + } + + return CoordsValidation::VALID; +} + +const IBounds &Bounds::operator*=(const IBounds &bounds) +{ + _width *= bounds.width(); + _height *= bounds.height(); + + return *this; +} + +const IBounds &Bounds::operator+=(const IBounds &bounds) +{ + _width += bounds.width(); + _height += bounds.height(); + + return *this; +} + +const IBounds &Bounds::operator-=(const IBounds &bounds) +{ + _width -= bounds.width(); + _height -= bounds.height(); + + return *this; +} diff --git a/src/engine/graphics/bounds.hpp b/src/engine/graphics/bounds.hpp new file mode 100644 index 0000000..3f4dd5b --- /dev/null +++ b/src/engine/graphics/bounds.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "interfaces/bounds.hpp" +#include "interfaces/vector2.hpp" + +class Bounds : public IBounds +{ +public: + explicit Bounds(const IBoundsOptions &options); + + [[nodiscard]] unsigned int width() const override; + + void width(unsigned int width) override; + + [[nodiscard]] unsigned int height() const override; + + void height(unsigned int height) override; + + [[nodiscard]] CoordsValidation validate_coords(const IVector2 &coords) const override; + + const IBounds &operator*=(const IBounds &bounds) override; + const IBounds &operator+=(const IBounds &bounds) override; + const IBounds &operator-=(const IBounds &bounds) override; + +private: + unsigned int _width = 0U; + unsigned int _height = 0U; +}; diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp new file mode 100644 index 0000000..3f63807 --- /dev/null +++ b/src/engine/graphics/scene.cpp @@ -0,0 +1,57 @@ +#include "scene.hpp" + +#include <fmt/core.h> +#include <iostream> + +Scene::Scene(IMatrixFactory<std::string_view> matrix_factory) + : _is_shown(false), _matrix_factory(std::move(matrix_factory)) +{ +} + +void Scene::enter() +{ + if (_is_shown) + { + return; + } + + fmt::print(ENABLE_ALT_BUFFER, fmt::arg("esc", ESC)); + std::cout.flush(); + + _is_shown = true; +} + +void Scene::leave() +{ + if (!_is_shown) + { + return; + } + + fmt::print(DISABLE_ALT_BUFFER, fmt::arg("esc", ESC)); + std::cout.flush(); + + _is_shown = false; +} + +/* +void do_in_statusbar(const std::function<void()> &routine) +{ + const auto prev_pos = Cursor::where(); + + const auto window_size = Window::size(); + + Cursor::hide(); + + Cursor::move_to(Vector2({1, window_size.height()})); + + std::cout << fmt::format(EscapeSequences::ERASE_LINE, fmt::arg("esc", ESC)); + std::cout.flush(); + + routine(); + + Cursor::move_to(prev_pos); + + Cursor::show(); +} +*/ diff --git a/src/engine/graphics/scene.hpp b/src/engine/graphics/scene.hpp new file mode 100644 index 0000000..b26ac05 --- /dev/null +++ b/src/engine/graphics/scene.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include "DI/auto_wirable.hpp" +#include "interfaces/matrix.hpp" +#include "interfaces/scene.hpp" + +#include <fmt/core.h> +#include <string_view> + +constexpr fmt::string_view ESC = "\x1B"; + +constexpr fmt::string_view ENABLE_ALT_BUFFER = "{esc}[?1049h"; +constexpr fmt::string_view DISABLE_ALT_BUFFER = "{esc}[?1049l"; + +class Scene : public IScene, + public AutoWirable<IScene, Scene, IMatrixFactory<std::string_view>> +{ +public: + explicit Scene(IMatrixFactory<std::string_view> matrix_factory); + + void enter() override; + + void leave() override; + + // void do_in_statusbar(const std::function<void()> &routine); + +private: + bool _is_shown; + + IMatrixFactory<std::string_view> _matrix_factory; +}; diff --git a/src/engine/graphics/string_matrix.cpp b/src/engine/graphics/string_matrix.cpp new file mode 100644 index 0000000..e259e96 --- /dev/null +++ b/src/engine/graphics/string_matrix.cpp @@ -0,0 +1,41 @@ +#include "string_matrix.hpp" + +StringMatrix::StringMatrix(const IBounds &bounds) + : _rows(bounds.height()), _columns(bounds.width()) +{ + _matrix.reserve(bounds.height()); + _matrix.assign(_matrix.capacity(), std::vector<std::string_view>(bounds.width())); +}; + +void StringMatrix::fill(std::string_view element) +{ + for (unsigned int row = 0U; row < _matrix.capacity(); row++) + { + std::vector<std::string_view> row_vector = _matrix[row]; + + for (unsigned int column = 0U; column < row_vector.capacity(); column++) + { + _matrix[row][column] = element; + } + } +} + +std::string_view StringMatrix::get(const IVector2 &pos) const +{ + return _matrix[pos.y()][pos.x()]; +} + +void StringMatrix::set(const IVector2 &pos, std::string_view element) +{ + _matrix[pos.y()][pos.x()] = element; +} + +unsigned int StringMatrix::rows() const +{ + return _rows; +} + +unsigned int StringMatrix::columns() const +{ + return _columns; +} diff --git a/src/engine/graphics/string_matrix.hpp b/src/engine/graphics/string_matrix.hpp new file mode 100644 index 0000000..662ea7b --- /dev/null +++ b/src/engine/graphics/string_matrix.hpp @@ -0,0 +1,60 @@ +#pragma once + +#include "interfaces/bounds.hpp" +#include "interfaces/matrix.hpp" +#include "interfaces/vector2.hpp" + +#include <string_view> +#include <vector> + +/** + * A Matrix. + */ +class StringMatrix : public IMatrix<std::string_view> +{ +public: + /** + * Creates a matrix. + * + * @param bounds The bounds of the matrix + */ + explicit StringMatrix(const IBounds &bounds); + + /** + * Fills the matrix with a element. + * + * @param element A element + */ + void fill(std::string_view element) override; + + /** + * Returns a element of the matrix. + * + * @param pos The position of a element + */ + [[nodiscard]] std::string_view get(const IVector2 &pos) const override; + + /** + * Sets a element of the matrix. + * + * @param pos The position of a element + * @param element A new element + */ + void set(const IVector2 &pos, std::string_view element) override; + + /** + * Returns the number of rows the matrix has. + */ + [[nodiscard]] unsigned int rows() const override; + + /** + * Returns the number of columns the matrix has. + */ + [[nodiscard]] unsigned int columns() const override; + +private: + std::vector<std::vector<std::string_view>> _matrix; + + unsigned int _rows; + unsigned int _columns; +}; diff --git a/src/game/game.cpp b/src/game/game.cpp index e785e18..9b08adc 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -1,9 +1,15 @@ #include "game.hpp" -#include <iostream> +#include <chrono> +#include <thread> -Game::Game(IVector2Factory vector2_factory) : _vector2_factory(std::move(vector2_factory)) +Game::Game(const std::shared_ptr<IScene> &scene) : _scene(scene) {} + +void Game::run() { -} + _scene->enter(); -void Game::run() {} + std::this_thread::sleep_for(std::chrono::seconds(3)); + + _scene->leave(); +} diff --git a/src/game/game.hpp b/src/game/game.hpp index 4c7e5e1..6abb349 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -2,15 +2,17 @@ #include "DI/auto_wirable.hpp" #include "interfaces/game.hpp" -#include "interfaces/vector2.hpp" +#include "interfaces/scene.hpp" -class Game : public IGame, public AutoWirable<IGame, Game, IVector2Factory> +#include <memory> + +class Game : public IGame, public AutoWirable<IGame, Game, IScene> { public: - explicit Game(IVector2Factory vector2_factory); + explicit Game(const std::shared_ptr<IScene> &scene); void run() override; private: - IVector2Factory _vector2_factory; + const std::shared_ptr<IScene> &_scene; }; diff --git a/src/interfaces/bounds.hpp b/src/interfaces/bounds.hpp new file mode 100644 index 0000000..9cec97e --- /dev/null +++ b/src/interfaces/bounds.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "interfaces/vector2.hpp" + +#include <functional> +#include <memory> + +enum CoordsValidation +{ + VALID, + X_HIGH, + Y_HIGH +}; + +class IBounds +{ +public: + [[nodiscard]] virtual unsigned int width() const = 0; + + virtual void width(unsigned int width) = 0; + + [[nodiscard]] virtual unsigned int height() const = 0; + + virtual void height(unsigned int height) = 0; + + [[nodiscard]] virtual CoordsValidation + validate_coords(const IVector2 &coords) const = 0; + + virtual const IBounds &operator*=(const IBounds &bounds) = 0; + virtual const IBounds &operator+=(const IBounds &bounds) = 0; + virtual const IBounds &operator-=(const IBounds &bounds) = 0; +}; + +struct IBoundsOptions +{ + unsigned int width; + unsigned int height; +}; + +using IBoundsFactory = + std::function<std::shared_ptr<IBounds>(const IBoundsOptions &options)>; diff --git a/src/interfaces/matrix.hpp b/src/interfaces/matrix.hpp new file mode 100644 index 0000000..5dc5f2e --- /dev/null +++ b/src/interfaces/matrix.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include "interfaces/bounds.hpp" +#include "interfaces/vector2.hpp" + +#include <functional> +#include <memory> + +template <typename Element> +class IMatrix +{ +public: + /** + * Fills the matrix with a element. + * + * @param element A element + */ + virtual void fill(Element element) = 0; + + /** + * Returns a element of the matrix. + * + * @param pos The position of a element + */ + [[nodiscard]] virtual Element get(const IVector2 &pos) const = 0; + + /** + * Sets a element of the matrix. + * + * @param pos The position of a element + * @param element A new element + */ + virtual void set(const IVector2 &pos, Element element) = 0; + + /** + * Returns the number of rows the matrix has. + */ + [[nodiscard]] virtual unsigned int rows() const = 0; + + /** + * Returns the number of columns the matrix has. + */ + [[nodiscard]] virtual unsigned int columns() const = 0; +}; + +template <typename Element> +using IMatrixFactory = + std::function<std::shared_ptr<IMatrix<Element>>(const IBounds &bounds)>; diff --git a/src/interfaces/scene.hpp b/src/interfaces/scene.hpp new file mode 100644 index 0000000..8b34dae --- /dev/null +++ b/src/interfaces/scene.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "interfaces/matrix.hpp" + +#include <functional> +#include <memory> +#include <string_view> + +class IScene +{ +public: + virtual void enter() = 0; + + virtual void leave() = 0; +}; |