From 0e40bc7ce8c3b3be083002f88c3317d65f6570ad Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 6 Mar 2022 13:16:05 +0100 Subject: refactor: make vector2 & bounds data classes --- src/engine/data/bounds.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++ src/engine/data/bounds.hpp | 42 +++++++++++++++++++++++++++++ src/engine/data/vector2.cpp | 39 +++++++++++++++++++++++++++ src/engine/data/vector2.hpp | 52 ++++++++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 src/engine/data/bounds.cpp create mode 100644 src/engine/data/bounds.hpp create mode 100644 src/engine/data/vector2.cpp create mode 100644 src/engine/data/vector2.hpp (limited to 'src/engine/data') diff --git a/src/engine/data/bounds.cpp b/src/engine/data/bounds.cpp new file mode 100644 index 0000000..cd08a02 --- /dev/null +++ b/src/engine/data/bounds.cpp @@ -0,0 +1,65 @@ +#include "bounds.hpp" + +Bounds::Bounds(const BoundsOptions &options) + : _width(options.width), _height(options.height) +{ +} + +uint32_t Bounds::width() const noexcept +{ + return _width; +} + +void Bounds::width(uint32_t width) noexcept +{ + _width = width; +} + +uint32_t Bounds::height() const noexcept +{ + return _height; +} + +void Bounds::height(uint32_t height) noexcept +{ + _height = height; +} + +CoordsValidation Bounds::validate_coords(const Vector2 &coords) const noexcept +{ + if (coords.x() >= _width) + { + return CoordsValidation::X_HIGH; + } + + if (coords.y() >= _height) + { + return CoordsValidation::Y_HIGH; + } + + return CoordsValidation::VALID; +} + +const Bounds &Bounds::operator*=(const Bounds &bounds) noexcept +{ + _width *= bounds.width(); + _height *= bounds.height(); + + return *this; +} + +const Bounds &Bounds::operator+=(const Bounds &bounds) noexcept +{ + _width += bounds.width(); + _height += bounds.height(); + + return *this; +} + +const Bounds &Bounds::operator-=(const Bounds &bounds) noexcept +{ + _width -= bounds.width(); + _height -= bounds.height(); + + return *this; +} diff --git a/src/engine/data/bounds.hpp b/src/engine/data/bounds.hpp new file mode 100644 index 0000000..1875bf4 --- /dev/null +++ b/src/engine/data/bounds.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "engine/data/vector2.hpp" + +#include + +enum CoordsValidation +{ + VALID, + X_HIGH, + Y_HIGH +}; + +struct BoundsOptions +{ + uint32_t width; + uint32_t height; +}; + +class Bounds +{ +public: + explicit Bounds(const BoundsOptions &options); + + [[nodiscard]] uint32_t width() const noexcept; + + void width(uint32_t width) noexcept; + + [[nodiscard]] uint32_t height() const noexcept; + + void height(uint32_t height) noexcept; + + [[nodiscard]] CoordsValidation validate_coords(const Vector2 &coords) const noexcept; + + const Bounds &operator*=(const Bounds &bounds) noexcept; + const Bounds &operator+=(const Bounds &bounds) noexcept; + const Bounds &operator-=(const Bounds &bounds) noexcept; + +private: + uint32_t _width = 0U; + uint32_t _height = 0U; +}; diff --git a/src/engine/data/vector2.cpp b/src/engine/data/vector2.cpp new file mode 100644 index 0000000..5d42913 --- /dev/null +++ b/src/engine/data/vector2.cpp @@ -0,0 +1,39 @@ +#include "vector2.hpp" + +Vector2::Vector2(const Vector2Options &options) : _x(options.x), _y(options.y) {} + +uint32_t Vector2::x() const noexcept +{ + return _x; +} + +void Vector2::x(uint32_t x) noexcept +{ + _x = x; +} + +uint32_t Vector2::y() const noexcept +{ + return _y; +} + +void Vector2::y(uint32_t y) noexcept +{ + _y = y; +} + +const Vector2 &Vector2::operator+=(const Vector2 &vector2) noexcept +{ + _x += vector2.x(); + _y += vector2.y(); + + return *this; +} + +const Vector2 &Vector2::operator-=(const Vector2 &vector2) noexcept +{ + _x -= vector2.x(); + _y -= vector2.y(); + + return *this; +} diff --git a/src/engine/data/vector2.hpp b/src/engine/data/vector2.hpp new file mode 100644 index 0000000..580123c --- /dev/null +++ b/src/engine/data/vector2.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include + +struct Vector2Options +{ + uint32_t x; + uint32_t y; +}; + +/** + * A 2D Vector. + */ +class Vector2 +{ +public: + /** + * Creates a 2D vector. + */ + explicit Vector2(const Vector2Options &options); + + /** + * Returns the X coordinate. + */ + [[nodiscard]] uint32_t x() const noexcept; + + /** + * Sets the X coordinate. + * + * @param x A new X coordinate + */ + void x(uint32_t x) noexcept; + + /** + * Returns the Y coordinate. + */ + [[nodiscard]] uint32_t y() const noexcept; + + /** + * Sets the Y coordinate. + * + * @param Y A new Y coordinate + */ + void y(uint32_t y) noexcept; + + const Vector2 &operator+=(const Vector2 &vector2) noexcept; + const Vector2 &operator-=(const Vector2 &vector2) noexcept; + +private: + uint32_t _x; + uint32_t _y; +}; -- cgit v1.2.3-18-g5258