From b0c265ee3d94921f55266a679d3801a4d2b4505b Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 13 Feb 2022 19:55:53 +0100 Subject: refactor: move some components to a engine dir --- src/engine/matrix.hpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/engine/matrix.tpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ src/engine/vector2.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++ src/engine/vector2.hpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++ src/matrix.hpp | 64 -------------------------------------------------- src/matrix.tpp | 62 ------------------------------------------------ src/maze.hpp | 4 ++-- src/maze.tpp | 1 - src/mazerator.cpp | 4 ++-- src/vector2.cpp | 58 --------------------------------------------- src/vector2.hpp | 59 ---------------------------------------------- 11 files changed, 247 insertions(+), 248 deletions(-) create mode 100644 src/engine/matrix.hpp create mode 100644 src/engine/matrix.tpp create mode 100644 src/engine/vector2.cpp create mode 100644 src/engine/vector2.hpp delete mode 100644 src/matrix.hpp delete mode 100644 src/matrix.tpp delete mode 100644 src/vector2.cpp delete mode 100644 src/vector2.hpp (limited to 'src') diff --git a/src/engine/matrix.hpp b/src/engine/matrix.hpp new file mode 100644 index 0000000..ddc1a1c --- /dev/null +++ b/src/engine/matrix.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include "vector2.hpp" +#include + +/** + * A Matrix. + */ +template +class Matrix +{ +public: + /** + * Creates a matrix. + * + * @param rows The number of rows of the matrix + * @param columns The number of columns of the matrix + */ + Matrix(unsigned int rows, unsigned int columns); + + /** + * Fills the matrix with a element. + * + * @param element A element + */ + void fill(Element element); + + /** + * Prints the matrix. + */ + void print(); + + /** + * Returns a element of the matrix. + * + * @param pos The position of a element + */ + Element get(Vector2 pos); + + /** + * Sets a element of the matrix. + * + * @param pos The position of a element + * @param element A new element + */ + void set(Vector2 pos, Element element); + + /** + * Returns the number of rows the matrix has. + */ + unsigned int rows(); + + /** + * Returns the number of columns the matrix has. + */ + unsigned int columns(); + +private: + std::vector> _matrix; + unsigned int _rows; + unsigned int _columns; +}; + +#include "matrix.tpp" diff --git a/src/engine/matrix.tpp b/src/engine/matrix.tpp new file mode 100644 index 0000000..b9fa495 --- /dev/null +++ b/src/engine/matrix.tpp @@ -0,0 +1,62 @@ +#include "matrix.hpp" +#include + +template +Matrix::Matrix(unsigned int rows, unsigned int columns) +{ + _rows = rows; + _columns = columns; + + _matrix.reserve(rows); + _matrix.assign(_matrix.capacity(), std::vector(columns)); +}; + +template +void Matrix::fill(Element 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; + } +} + +template +void Matrix::print() +{ + for (std::vector row : _matrix) + { + for (Element element : row) + std::cout << element; + + std::cout << "\n"; + } + + std::cout << std::flush; +} + +template +Element Matrix::get(Vector2 pos) +{ + return _matrix[pos.y()][pos.x()]; +} + +template +void Matrix::set(Vector2 pos, Element element) +{ + _matrix[pos.y()][pos.x()] = element; +} + +template +unsigned int Matrix::rows() +{ + return _rows; +} + +template +unsigned int Matrix::columns() +{ + return _columns; +} diff --git a/src/engine/vector2.cpp b/src/engine/vector2.cpp new file mode 100644 index 0000000..effc8b5 --- /dev/null +++ b/src/engine/vector2.cpp @@ -0,0 +1,58 @@ +#include "vector2.hpp" + +Vector2::Vector2(unsigned int x, unsigned int y) +{ + _x = x; + _y = y; +} + +unsigned int Vector2::x() const +{ + return _x; +} + +void Vector2::x(unsigned int x) +{ + _x = x; +} + +unsigned int Vector2::y() const +{ + return _y; +} + +void Vector2::y(unsigned int y) +{ + _y = y; +} + +std::shared_ptr Vector2::copy() +{ + return std::shared_ptr(new Vector2(*this)); +} + +Vector2 Vector2::operator+(const Vector2 vector2) +{ + return Vector2(_x + vector2.x(), _y + vector2.y()); +} + +Vector2 Vector2::operator-(const Vector2 vector2) +{ + return Vector2(_x - vector2.x(), _y - vector2.y()); +} + +Vector2 &Vector2::operator+=(const Vector2 &vector2) +{ + _x += vector2.x(); + _y += vector2.y(); + + return *this; +} + +Vector2 &Vector2::operator-=(const Vector2 &vector2) +{ + _x -= vector2.x(); + _y -= vector2.y(); + + return *this; +} diff --git a/src/engine/vector2.hpp b/src/engine/vector2.hpp new file mode 100644 index 0000000..3dc1db1 --- /dev/null +++ b/src/engine/vector2.hpp @@ -0,0 +1,59 @@ +#pragma once + +#include + +/** + * A 2D Vector. + */ +class Vector2 +{ +public: + /** + * Creates a 2D vector. + * + * @param x A X coordinate + * @param y A Y coordinate + */ + Vector2(unsigned int x, unsigned int y); + + /** + * Returns the X coordinate. + */ + unsigned int x() const; + + /** + * Sets the X coordinate. + * + * @param x A new X coordinate + */ + void x(unsigned int x); + + /** + * Returns the Y coordinate. + */ + unsigned int y() const; + + /** + * Sets the Y coordinate. + * + * @param Y A new Y coordinate + */ + void y(unsigned int y); + + /** + * Creates a copy of the 2D vector. + * + * @returns A identical 2D vector. + */ + std::shared_ptr copy(); + + Vector2 operator+(const Vector2 vector2); + Vector2 operator-(const Vector2 vector2); + + Vector2 &operator+=(const Vector2 &vector2); + Vector2 &operator-=(const Vector2 &vector2); + +private: + unsigned int _x; + unsigned int _y; +}; diff --git a/src/matrix.hpp b/src/matrix.hpp deleted file mode 100644 index ddc1a1c..0000000 --- a/src/matrix.hpp +++ /dev/null @@ -1,64 +0,0 @@ -#pragma once - -#include "vector2.hpp" -#include - -/** - * A Matrix. - */ -template -class Matrix -{ -public: - /** - * Creates a matrix. - * - * @param rows The number of rows of the matrix - * @param columns The number of columns of the matrix - */ - Matrix(unsigned int rows, unsigned int columns); - - /** - * Fills the matrix with a element. - * - * @param element A element - */ - void fill(Element element); - - /** - * Prints the matrix. - */ - void print(); - - /** - * Returns a element of the matrix. - * - * @param pos The position of a element - */ - Element get(Vector2 pos); - - /** - * Sets a element of the matrix. - * - * @param pos The position of a element - * @param element A new element - */ - void set(Vector2 pos, Element element); - - /** - * Returns the number of rows the matrix has. - */ - unsigned int rows(); - - /** - * Returns the number of columns the matrix has. - */ - unsigned int columns(); - -private: - std::vector> _matrix; - unsigned int _rows; - unsigned int _columns; -}; - -#include "matrix.tpp" diff --git a/src/matrix.tpp b/src/matrix.tpp deleted file mode 100644 index b9fa495..0000000 --- a/src/matrix.tpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "matrix.hpp" -#include - -template -Matrix::Matrix(unsigned int rows, unsigned int columns) -{ - _rows = rows; - _columns = columns; - - _matrix.reserve(rows); - _matrix.assign(_matrix.capacity(), std::vector(columns)); -}; - -template -void Matrix::fill(Element 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; - } -} - -template -void Matrix::print() -{ - for (std::vector row : _matrix) - { - for (Element element : row) - std::cout << element; - - std::cout << "\n"; - } - - std::cout << std::flush; -} - -template -Element Matrix::get(Vector2 pos) -{ - return _matrix[pos.y()][pos.x()]; -} - -template -void Matrix::set(Vector2 pos, Element element) -{ - _matrix[pos.y()][pos.x()] = element; -} - -template -unsigned int Matrix::rows() -{ - return _rows; -} - -template -unsigned int Matrix::columns() -{ - return _columns; -} diff --git a/src/maze.hpp b/src/maze.hpp index f343010..e525d70 100644 --- a/src/maze.hpp +++ b/src/maze.hpp @@ -1,7 +1,7 @@ #pragma once -#include "matrix.hpp" -#include "vector2.hpp" +#include "engine/matrix.hpp" +#include "engine/vector2.hpp" #include #include #include diff --git a/src/maze.tpp b/src/maze.tpp index fd71479..19390cc 100644 --- a/src/maze.tpp +++ b/src/maze.tpp @@ -1,4 +1,3 @@ -#include "matrix.hpp" #include "maze.hpp" #include "stack.hpp" #include diff --git a/src/mazerator.cpp b/src/mazerator.cpp index a82bbaf..33ef1b1 100644 --- a/src/mazerator.cpp +++ b/src/mazerator.cpp @@ -1,8 +1,8 @@ +#include "engine/matrix.hpp" +#include "engine/vector2.hpp" #include "getopt.h" -#include "matrix.hpp" #include "maze.hpp" #include "utils.hpp" -#include "vector2.hpp" #include #include #include diff --git a/src/vector2.cpp b/src/vector2.cpp deleted file mode 100644 index effc8b5..0000000 --- a/src/vector2.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "vector2.hpp" - -Vector2::Vector2(unsigned int x, unsigned int y) -{ - _x = x; - _y = y; -} - -unsigned int Vector2::x() const -{ - return _x; -} - -void Vector2::x(unsigned int x) -{ - _x = x; -} - -unsigned int Vector2::y() const -{ - return _y; -} - -void Vector2::y(unsigned int y) -{ - _y = y; -} - -std::shared_ptr Vector2::copy() -{ - return std::shared_ptr(new Vector2(*this)); -} - -Vector2 Vector2::operator+(const Vector2 vector2) -{ - return Vector2(_x + vector2.x(), _y + vector2.y()); -} - -Vector2 Vector2::operator-(const Vector2 vector2) -{ - return Vector2(_x - vector2.x(), _y - vector2.y()); -} - -Vector2 &Vector2::operator+=(const Vector2 &vector2) -{ - _x += vector2.x(); - _y += vector2.y(); - - return *this; -} - -Vector2 &Vector2::operator-=(const Vector2 &vector2) -{ - _x -= vector2.x(); - _y -= vector2.y(); - - return *this; -} diff --git a/src/vector2.hpp b/src/vector2.hpp deleted file mode 100644 index 3dc1db1..0000000 --- a/src/vector2.hpp +++ /dev/null @@ -1,59 +0,0 @@ -#pragma once - -#include - -/** - * A 2D Vector. - */ -class Vector2 -{ -public: - /** - * Creates a 2D vector. - * - * @param x A X coordinate - * @param y A Y coordinate - */ - Vector2(unsigned int x, unsigned int y); - - /** - * Returns the X coordinate. - */ - unsigned int x() const; - - /** - * Sets the X coordinate. - * - * @param x A new X coordinate - */ - void x(unsigned int x); - - /** - * Returns the Y coordinate. - */ - unsigned int y() const; - - /** - * Sets the Y coordinate. - * - * @param Y A new Y coordinate - */ - void y(unsigned int y); - - /** - * Creates a copy of the 2D vector. - * - * @returns A identical 2D vector. - */ - std::shared_ptr copy(); - - Vector2 operator+(const Vector2 vector2); - Vector2 operator-(const Vector2 vector2); - - Vector2 &operator+=(const Vector2 &vector2); - Vector2 &operator-=(const Vector2 &vector2); - -private: - unsigned int _x; - unsigned int _y; -}; -- cgit v1.2.3-18-g5258