From fe79577396231f2edb7927f1f61ce814f03851a7 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 27 Feb 2022 17:12:49 +0100 Subject: add basic engine graphics --- src/engine/graphics/bounds.cpp | 65 +++++++++++++++++++++++++++++++++++ src/engine/graphics/bounds.hpp | 28 +++++++++++++++ src/engine/graphics/scene.cpp | 57 ++++++++++++++++++++++++++++++ src/engine/graphics/scene.hpp | 31 +++++++++++++++++ src/engine/graphics/string_matrix.cpp | 41 ++++++++++++++++++++++ src/engine/graphics/string_matrix.hpp | 60 ++++++++++++++++++++++++++++++++ 6 files changed, 282 insertions(+) create mode 100644 src/engine/graphics/bounds.cpp create mode 100644 src/engine/graphics/bounds.hpp create mode 100644 src/engine/graphics/scene.cpp create mode 100644 src/engine/graphics/scene.hpp create mode 100644 src/engine/graphics/string_matrix.cpp create mode 100644 src/engine/graphics/string_matrix.hpp (limited to 'src/engine/graphics') 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 +#include + +Scene::Scene(IMatrixFactory 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 &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 +#include + +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> +{ +public: + explicit Scene(IMatrixFactory matrix_factory); + + void enter() override; + + void leave() override; + + // void do_in_statusbar(const std::function &routine); + +private: + bool _is_shown; + + IMatrixFactory _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(bounds.width())); +}; + +void StringMatrix::fill(std::string_view element) +{ + for (unsigned int row = 0U; row < _matrix.capacity(); row++) + { + std::vector 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 +#include + +/** + * A Matrix. + */ +class StringMatrix : public IMatrix +{ +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> _matrix; + + unsigned int _rows; + unsigned int _columns; +}; -- cgit v1.2.3-18-g5258