diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-27 17:12:49 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:53 +0200 |
commit | fe79577396231f2edb7927f1f61ce814f03851a7 (patch) | |
tree | d353a1caa449ec772dcf9a8681084fc2d6e64116 /src/engine | |
parent | 6964d48c970e5f7b11897096c816271785af23ac (diff) |
add basic engine graphics
Diffstat (limited to 'src/engine')
-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 |
6 files changed, 282 insertions, 0 deletions
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; +}; |