aboutsummaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/bounds.cpp48
-rw-r--r--src/engine/bounds.hpp32
-rw-r--r--src/engine/matrix.hpp9
-rw-r--r--src/engine/matrix.tpp17
-rw-r--r--src/engine/vector2.cpp23
-rw-r--r--src/engine/vector2.hpp20
6 files changed, 119 insertions, 30 deletions
diff --git a/src/engine/bounds.cpp b/src/engine/bounds.cpp
new file mode 100644
index 0000000..782fd02
--- /dev/null
+++ b/src/engine/bounds.cpp
@@ -0,0 +1,48 @@
+#include "bounds.hpp"
+
+Bounds::Bounds(BoundsOpts opts) : _width(opts.width), _height(opts.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;
+}
+
+void Bounds::verify_coords(const Vector2 &coords) const
+{
+ if (coords.x() >= _width)
+ {
+ throw "X coordinate cannot be higher than or equal to bounds width";
+ }
+
+ if (coords.y() >= _height)
+ {
+ throw "Y coordinate cannot be higher than or equal to bounds height";
+ }
+}
+
+Bounds Bounds::operator*(const Bounds &bounds) const
+{
+ return Bounds(
+ {.width = _width * bounds.width(), .height = _height * bounds.height()});
+}
+
+Bounds Bounds::operator+(const Bounds &bounds) const
+{
+ return Bounds(
+ {.width = _width + bounds.width(), .height = _height + bounds.height()});
+}
diff --git a/src/engine/bounds.hpp b/src/engine/bounds.hpp
new file mode 100644
index 0000000..964e73a
--- /dev/null
+++ b/src/engine/bounds.hpp
@@ -0,0 +1,32 @@
+#pragma once
+
+#include "engine/vector2.hpp"
+
+struct BoundsOpts
+{
+ unsigned int width;
+ unsigned int height;
+};
+
+class Bounds
+{
+public:
+ explicit Bounds(BoundsOpts opts);
+
+ [[nodiscard]] unsigned int width() const;
+
+ void width(unsigned int width);
+
+ [[nodiscard]] unsigned int height() const;
+
+ void height(unsigned int height);
+
+ void verify_coords(const Vector2 &coords) const;
+
+ Bounds operator*(const Bounds &bounds) const;
+ Bounds operator+(const Bounds &bounds) const;
+
+private:
+ unsigned int _width = 0U;
+ unsigned int _height = 0U;
+};
diff --git a/src/engine/matrix.hpp b/src/engine/matrix.hpp
index ddc1a1c..c243eaf 100644
--- a/src/engine/matrix.hpp
+++ b/src/engine/matrix.hpp
@@ -1,6 +1,8 @@
#pragma once
-#include "vector2.hpp"
+#include "engine/bounds.hpp"
+#include "engine/vector2.hpp"
+
#include <vector>
/**
@@ -13,10 +15,9 @@ public:
/**
* Creates a matrix.
*
- * @param rows The number of rows of the matrix
- * @param columns The number of columns of the matrix
+ * @param bounds The bounds of the matrix
*/
- Matrix(unsigned int rows, unsigned int columns);
+ explicit Matrix(const Bounds &bounds);
/**
* Fills the matrix with a element.
diff --git a/src/engine/matrix.tpp b/src/engine/matrix.tpp
index bddf76a..d25de28 100644
--- a/src/engine/matrix.tpp
+++ b/src/engine/matrix.tpp
@@ -1,14 +1,15 @@
+#pragma once
+
#include "matrix.hpp"
+
#include <iostream>
template <typename Element>
-Matrix<Element>::Matrix(unsigned int rows, unsigned int columns)
+Matrix<Element>::Matrix(const Bounds &bounds)
+ : _rows(bounds.height()), _columns(bounds.width())
{
- _rows = rows;
- _columns = columns;
-
- _matrix.reserve(rows);
- _matrix.assign(_matrix.capacity(), std::vector<Element>(columns));
+ _matrix.reserve(bounds.height());
+ _matrix.assign(_matrix.capacity(), std::vector<Element>(bounds.width()));
};
template <typename Element>
@@ -28,9 +29,9 @@ void Matrix<Element>::fill(Element element)
template <typename Element>
void Matrix<Element>::print()
{
- for (std::vector<Element> row : _matrix)
+ for (const std::vector<Element> &row : _matrix)
{
- for (Element element : row)
+ for (const Element &element : row)
{
std::cout << element;
}
diff --git a/src/engine/vector2.cpp b/src/engine/vector2.cpp
index effc8b5..d091ea5 100644
--- a/src/engine/vector2.cpp
+++ b/src/engine/vector2.cpp
@@ -1,10 +1,8 @@
#include "vector2.hpp"
-Vector2::Vector2(unsigned int x, unsigned int y)
-{
- _x = x;
- _y = y;
-}
+#include <memory>
+
+Vector2::Vector2(Vector2Opts opts) : _x(opts.x), _y(opts.y) {}
unsigned int Vector2::x() const
{
@@ -28,17 +26,22 @@ void Vector2::y(unsigned int y)
std::shared_ptr<Vector2> Vector2::copy()
{
- return std::shared_ptr<Vector2>(new Vector2(*this));
+ return std::make_shared<Vector2>(*this);
+}
+
+Vector2 Vector2::operator*(const Vector2 &vector2) const
+{
+ return Vector2({.x = _x * vector2.x(), .y = _y * vector2.y()});
}
-Vector2 Vector2::operator+(const Vector2 vector2)
+Vector2 Vector2::operator+(const Vector2 &vector2) const
{
- return Vector2(_x + vector2.x(), _y + vector2.y());
+ return Vector2({.x = _x + vector2.x(), .y = _y + vector2.y()});
}
-Vector2 Vector2::operator-(const Vector2 vector2)
+Vector2 Vector2::operator-(const Vector2 &vector2) const
{
- return Vector2(_x - vector2.x(), _y - vector2.y());
+ return Vector2({.x = _x - vector2.x(), .y = _y - vector2.y()});
}
Vector2 &Vector2::operator+=(const Vector2 &vector2)
diff --git a/src/engine/vector2.hpp b/src/engine/vector2.hpp
index 3dc1db1..8423431 100644
--- a/src/engine/vector2.hpp
+++ b/src/engine/vector2.hpp
@@ -2,6 +2,12 @@
#include <memory>
+struct Vector2Opts
+{
+ unsigned int x;
+ unsigned int y;
+};
+
/**
* A 2D Vector.
*/
@@ -10,16 +16,13 @@ class Vector2
public:
/**
* Creates a 2D vector.
- *
- * @param x A X coordinate
- * @param y A Y coordinate
*/
- Vector2(unsigned int x, unsigned int y);
+ explicit Vector2(Vector2Opts opts);
/**
* Returns the X coordinate.
*/
- unsigned int x() const;
+ [[nodiscard]] unsigned int x() const;
/**
* Sets the X coordinate.
@@ -31,7 +34,7 @@ public:
/**
* Returns the Y coordinate.
*/
- unsigned int y() const;
+ [[nodiscard]] unsigned int y() const;
/**
* Sets the Y coordinate.
@@ -47,8 +50,9 @@ public:
*/
std::shared_ptr<Vector2> copy();
- Vector2 operator+(const Vector2 vector2);
- Vector2 operator-(const Vector2 vector2);
+ Vector2 operator*(const Vector2 &vector2) const;
+ Vector2 operator+(const Vector2 &vector2) const;
+ Vector2 operator-(const Vector2 &vector2) const;
Vector2 &operator+=(const Vector2 &vector2);
Vector2 &operator-=(const Vector2 &vector2);