aboutsummaryrefslogtreecommitdiff
path: root/src/engine/graphics/matrix.tpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-27 19:21:22 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:57 +0200
commit5c1c672e018e41e5c87a0263a851684a52d9c1fb (patch)
tree815a067fb558fe3c55b19842402d68e0dc0953f2 /src/engine/graphics/matrix.tpp
parent15c7a8fda742cc393310d75e0e1be89c3042eac0 (diff)
refactor: replace string matrix with template matrix
Diffstat (limited to 'src/engine/graphics/matrix.tpp')
-rw-r--r--src/engine/graphics/matrix.tpp156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/engine/graphics/matrix.tpp b/src/engine/graphics/matrix.tpp
new file mode 100644
index 0000000..e0e65bb
--- /dev/null
+++ b/src/engine/graphics/matrix.tpp
@@ -0,0 +1,156 @@
+#pragma once
+
+#include "matrix.hpp"
+
+template <typename Element>
+Matrix<Element>::Matrix(const Bounds &bounds) noexcept
+ : _matrix(new Element *[bounds.get_height()]),
+ _row_cnt(bounds.get_height()),
+ _column_cnt(bounds.get_width())
+{
+ for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt; row++)
+ {
+ *row = static_cast<gsl::owner<Element *>>(new Element[bounds.get_width()]);
+ }
+};
+
+template <typename Element>
+Matrix<Element>::Matrix(const Matrix &matrix) noexcept
+ : _matrix(new Element *[matrix._row_cnt]),
+ _row_cnt(matrix._row_cnt),
+ _column_cnt(matrix._column_cnt)
+{
+ _copy_matrix_from(matrix);
+}
+
+template <typename Element>
+Matrix<Element>::Matrix(Matrix &&matrix) noexcept
+ : _matrix(matrix._matrix), _row_cnt(matrix._row_cnt), _column_cnt(matrix._column_cnt)
+{
+ matrix._matrix = nullptr;
+}
+
+template <typename Element>
+Matrix<Element>::~Matrix() noexcept
+{
+ _delete_matrix();
+}
+
+template <typename Element>
+void Matrix<Element>::fill(Element element) noexcept
+{
+ for (auto row : *this)
+ {
+ for (auto &col : row)
+ {
+ col = element;
+ }
+ }
+}
+
+template <typename Element>
+Element Matrix<Element>::get(const Vector2 &pos) const noexcept
+{
+
+ auto x = static_cast<std::size_t>(pos.get_x());
+ auto y = static_cast<std::size_t>(pos.get_y());
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ return _matrix[y][x];
+}
+
+template <typename Element>
+void Matrix<Element>::set(const Vector2 &pos, Element element) noexcept
+{
+ auto x = static_cast<std::size_t>(pos.get_x());
+ auto y = static_cast<std::size_t>(pos.get_y());
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ _matrix[y][x] = element;
+}
+
+template <typename Element>
+uint32_t Matrix<Element>::get_row_cnt() const noexcept
+{
+ return _row_cnt;
+}
+
+template <typename Element>
+uint32_t Matrix<Element>::get_column_cnt() const noexcept
+{
+ return _column_cnt;
+}
+
+template <typename Element>
+MatrixIterator<Element> Matrix<Element>::begin() const noexcept
+{
+ return MatrixIterator(_matrix, _column_cnt);
+}
+
+template <typename Element>
+MatrixIterator<Element> Matrix<Element>::end() const noexcept
+{
+ return MatrixIterator(_matrix + _row_cnt, _column_cnt);
+}
+
+template <typename Element>
+Matrix<Element> &Matrix<Element>::operator=(const Matrix &rhs) noexcept
+{
+ if (&rhs != this)
+ {
+ _delete_matrix();
+
+ // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
+ _matrix = nullptr;
+
+ _matrix = new Element *[rhs._row_cnt];
+ _copy_matrix_from(rhs);
+ }
+
+ return *this;
+}
+
+template <typename Element>
+Matrix<Element> &Matrix<Element>::operator=(Matrix &&rhs) noexcept
+{
+ if (&rhs != this)
+ {
+ _delete_matrix();
+
+ // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
+ _matrix = rhs._matrix;
+
+ rhs._matrix = nullptr;
+ }
+
+ return *this;
+}
+
+template <typename Element>
+void Matrix<Element>::_delete_matrix() noexcept
+{
+ for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt; row++)
+ {
+ delete[](*row);
+ }
+
+ delete[] _matrix;
+}
+
+template <typename Element>
+void Matrix<Element>::_copy_matrix_from(const Matrix &source) noexcept
+{
+ // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
+ gsl::owner<Element **> source_row = source._matrix;
+
+ for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt;
+ row++, source_row++)
+ {
+ *row = static_cast<gsl::owner<Element *>>(new Element[_column_cnt]);
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ auto *end = *source_row + _column_cnt;
+
+ std::copy(*source_row, end, *row);
+ }
+}