diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index d64136c..09d924f 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -25,139 +25,6 @@ namespace SDL2pp { -Point::Point() { - x = 0; - y = 0; -} - -Point::Point(const SDL_Point& point) { - x = point.x; - y = point.y; -} - -Point::Point(int nx, int ny) { - x = nx; - y = ny; -} - -bool Point::operator==(const Point& other) const { - return x == other.x && y == other.y; -} - -bool Point::operator!=(const Point& other) const { - return !(*this == other); -} - -int Point::GetX() const { - return x; -} - -Point& Point::SetX(int nx) { - x = nx; - return *this; -} - -int Point::GetY() const { - return y; -} - -Point& Point::SetY(int ny) { - y = ny; - return *this; -} - -Point Point::operator-() const { - return Point(-x, -y); -} - -Point Point::operator+(const Point& other) const { - return Point(x + other.x, y + other.y); -} - -Point Point::operator-(const Point& other) const { - return Point(x - other.x, y - other.y); -} - -Point Point::operator/(int value) const { - return Point(x / value, y / value); -} - -Point Point::operator/(const Point& other) const { - return Point(x / other.x, y / other.y); -} - -Point Point::operator%(int value) const { - return Point(x % value, y % value); -} - -Point Point::operator%(const Point& other) const { - return Point(x % other.x, y % other.y); -} - -Point Point::operator*(int value) const { - return Point(x * value, y * value); -} - -Point Point::operator*(const Point& other) const { - return Point(x * other.x, y * other.y); -} - -Point& Point::operator+=(const Point& other) { - x += other.x; - y += other.y; - - return *this; -} - -Point& Point::operator-=(const Point& other) { - x -= other.x; - y -= other.y; - - return *this; -} - -Point& Point::operator/=(int value) { - x /= value; - y /= value; - - return *this; -} - -Point& Point::operator/=(const Point& other) { - x /= other.x; - y /= other.y; - - return *this; -} - -Point& Point::operator%=(int value) { - x %= value; - y %= value; - - return *this; -} - -Point& Point::operator%=(const Point& other) { - x %= other.x; - y %= other.y; - - return *this; -} - -Point& Point::operator*=(int value) { - x *= value; - y *= value; - - return *this; -} - -Point& Point::operator*=(const Point& other) { - x *= other.x; - y *= other.y; - - return *this; -} - Point Point::GetClamped(const Rect& rect) const { Point p = *this; p.Clamp(rect); diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 23b4165..1fd8eda 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -53,7 +53,8 @@ public: /// Creates a Point(0, 0) /// //////////////////////////////////////////////////////////// - Point(); + constexpr Point() : SDL_Point{0, 0} { + } //////////////////////////////////////////////////////////// /// \brief Construct a point from existing SDL_Point @@ -61,16 +62,18 @@ public: /// \param[in] point Existing SDL_Point /// //////////////////////////////////////////////////////////// - Point(const SDL_Point& point); + constexpr Point(const SDL_Point& point) : SDL_Point{point.x, point.y} { + } //////////////////////////////////////////////////////////// /// \brief Construct the point from given coordinates /// - /// \param[in] nx X coordinate - /// \param[in] ny Y coordinate + /// \param[in] x X coordinate + /// \param[in] y Y coordinate /// //////////////////////////////////////////////////////////// - Point(int nx, int ny); + constexpr Point(int x, int y) : SDL_Point{x, y} { + } //////////////////////////////////////////////////////////// /// \brief Copy constructor @@ -108,7 +111,9 @@ public: /// \returns True if two points are identical /// //////////////////////////////////////////////////////////// - bool operator==(const Point& other) const; + constexpr bool operator==(const Point& other) const { + return x == other.x && y == other.y; + } //////////////////////////////////////////////////////////// /// \brief Inequality operator @@ -118,7 +123,9 @@ public: /// \returns True if two points are not identical /// //////////////////////////////////////////////////////////// - bool operator!=(const Point& other) const; + constexpr bool operator!=(const Point& other) const { + return x != other.x || y != other.y; + } //////////////////////////////////////////////////////////// /// \brief Get X coordinate of the point @@ -126,7 +133,9 @@ public: /// \returns X coordinate of the point /// //////////////////////////////////////////////////////////// - int GetX() const; + constexpr int GetX() const { + return x; + } //////////////////////////////////////////////////////////// /// \brief Set X coordinate of the point @@ -136,7 +145,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& SetX(int nx); + Point& SetX(int nx) { + x = nx; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get Y coordinate of the point @@ -144,7 +156,9 @@ public: /// \returns Y coordinate of the point /// //////////////////////////////////////////////////////////// - int GetY() const; + constexpr int GetY() const { + return y; + } //////////////////////////////////////////////////////////// /// \brief Set Y coordinate of the point @@ -154,7 +168,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& SetY(int ny); + Point& SetY(int ny) { + y = ny; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise negation @@ -162,7 +179,9 @@ public: /// \returns New Point representing memberwise negation /// //////////////////////////////////////////////////////////// - Point operator-() const; + constexpr Point operator-() const { + return Point(-x, -y); + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise addition with another point @@ -172,7 +191,9 @@ public: /// \returns New Point representing memberwise addition with another point /// //////////////////////////////////////////////////////////// - Point operator+(const Point& other) const; + constexpr Point operator+(const Point& other) const { + return Point(x + other.x, y + other.y); + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise subtraction with another point @@ -182,7 +203,9 @@ public: /// \returns New Point representing memberwise subtraction of another point /// //////////////////////////////////////////////////////////// - Point operator-(const Point& other) const; + constexpr Point operator-(const Point& other) const { + return Point(x - other.x, y - other.y); + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise division by an integer @@ -193,7 +216,9 @@ public: /// point by an integer /// //////////////////////////////////////////////////////////// - Point operator/(int value) const; + constexpr Point operator/(int value) const { + return Point(x / value, y / value); + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise division by another point @@ -204,7 +229,9 @@ public: /// point by another point /// //////////////////////////////////////////////////////////// - Point operator/(const Point& other) const; + constexpr Point operator/(const Point& other) const { + return Point(x / other.x, y / other.y); + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise remainder from division @@ -216,7 +243,9 @@ public: /// from division by an integer /// //////////////////////////////////////////////////////////// - Point operator%(int value) const; + constexpr Point operator%(int value) const { + return Point(x % value, y % value); + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise remainder from division @@ -228,7 +257,9 @@ public: /// from division by another point /// //////////////////////////////////////////////////////////// - Point operator%(const Point& other) const; + constexpr Point operator%(const Point& other) const { + return Point(x % other.x, y % other.y); + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise multiplication by an @@ -240,7 +271,9 @@ public: /// of point by an integer /// //////////////////////////////////////////////////////////// - Point operator*(int value) const; + constexpr Point operator*(int value) const { + return Point(x * value, y * value); + } //////////////////////////////////////////////////////////// /// \brief Get point's memberwise multiplication by anoter @@ -252,7 +285,9 @@ public: /// of point by another point /// //////////////////////////////////////////////////////////// - Point operator*(const Point& other) const; + constexpr Point operator*(const Point& other) const { + return Point(x * other.x, y * other.y); + } //////////////////////////////////////////////////////////// /// \brief Memberwise add another point @@ -262,7 +297,11 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& operator+=(const Point& other); + Point& operator+=(const Point& other) { + x += other.x; + y += other.y; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Memberwise subtract another point @@ -272,7 +311,11 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& operator-=(const Point& other); + Point& operator-=(const Point& other) { + x -= other.x; + y -= other.y; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Memberwise divide by an integer @@ -282,17 +325,25 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& operator/=(int value); + Point& operator/=(int value) { + x /= value; + y /= value; + return *this; + } - //////////////////////////////////////////////////////////// - /// \brief Memberwise divide by another point + /////////////////////////////////////////////////////////// + /// nbrief Memberwise divide by another point /// /// \param[in] other Divisor /// /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& operator/=(const Point& other); + Point& operator/=(const Point& other) { + x /= other.x; + y /= other.y; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Memberwise remainder from division by an integer @@ -302,7 +353,11 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& operator%=(int value); + Point& operator%=(int value) { + x %= value; + y %= value; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Memberwise remainder from division by another @@ -313,7 +368,11 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& operator%=(const Point& other); + Point& operator%=(const Point& other) { + x %= other.x; + y %= other.y; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Memberwise multiply by an integer @@ -323,7 +382,11 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& operator*=(int value); + Point& operator*=(int value) { + x *= value; + y *= value; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Memberwise multiply by another point @@ -333,7 +396,11 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Point& operator*=(const Point& other); + Point& operator*=(const Point& other) { + x *= other.x; + y *= other.y; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get a point with coordinates modified so it fits diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index 21e570a..9035753 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -27,129 +27,6 @@ namespace SDL2pp { -Rect::Rect() { - x = 0; - y = 0; - w = 0; - h = 0; -} - -Rect::Rect(const SDL_Rect& rect) { - x = rect.x; - y = rect.y; - w = rect.w; - h = rect.h; -} - -Rect::Rect(const Point& corner, const Point& size) { - x = corner.x; - y = corner.y; - w = size.x; - h = size.y; -} - -Rect::Rect(int nx, int ny, int nw, int nh) { - x = nx; - y = ny; - w = nw; - h = nh; -} - -bool Rect::operator==(const Rect& other) const { - return x == other.x && y == other.y && - w == other.w && h == other.h; -} - -bool Rect::operator!=(const Rect& other) const { - return !(*this == other); -} - -Rect Rect::FromCenter(int cx, int cy, int w, int h) { - return Rect(cx - w/2, cy - h/2, w, h); -} - -Rect Rect::FromCenter(const Point& center, const Point& size) { - return Rect(center - size / 2, size); -} - -Rect Rect::FromCorners(int x1, int y1, int x2, int y2) { - return Rect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); -} - -Rect Rect::FromCorners(const Point& p1, const Point& p2) { - return Rect(p1, p2 - p1 + Point(1, 1)); -} - -int Rect::GetX() const { - return x; -} - -Rect& Rect::SetX(int nx) { - x = nx; - return *this; -} - -int Rect::GetY() const { - return y; -} - -Rect& Rect::SetY(int ny) { - y = ny; - return *this; -} - -int Rect::GetW() const { - return w; -} - -Rect& Rect::SetW(int nw) { - w = nw; - return *this; -} - -int Rect::GetH() const { - return h; -} - -Rect& Rect::SetH(int nh) { - h = nh; - return *this; -} - -int Rect::GetX2() const { - return x + w - 1; -} - -Rect& Rect::SetX2(int x2) { - w = x2 - x + 1; - return *this; -} - -int Rect::GetY2() const { - return y + h - 1; -} - -Rect& Rect::SetY2(int y2) { - h = y2 - y + 1; - return *this; -} - -bool Rect::Contains(int px, int py) const { - return px >= x && py >= y && px <= GetX2() && py <= GetY2(); -} - -bool Rect::Contains(const Point& point) const { - return point.x >= x && point.y >= y && point.x <= GetX2() && point.y <= GetY2(); -} - -bool Rect::Contains(const Rect& rect) const { - return rect.x >= x && rect.y >= y && rect.GetX2() <= GetX2() && rect.GetY2() <= GetY2(); -} - -bool Rect::Intersects(const Rect& rect) const { - return !(rect.GetX2() < x || rect.GetY2() < y || rect.x > GetX2() || rect.y > GetY2()); -} - Rect Rect::GetUnion(const Rect& rect) const { return Rect::FromCorners( std::min(x, rect.x), @@ -205,28 +82,6 @@ bool Rect::IntersectLine(Point& p1, Point& p2) const { return SDL_IntersectRectAndLine(this, &p1.x, &p1.y, &p2.x, &p2.y) == SDL_TRUE; } -Rect Rect::operator+(const Point& offset) const { - return Rect(x + offset.x, y + offset.y, w, h); -} - -Rect& Rect::operator+=(const Point& offset) { - x += offset.x; - y += offset.y; - - return *this; -} - -Rect Rect::operator-(const Point& offset) const { - return Rect(x - offset.x, y - offset.y, w, h); -} - -Rect& Rect::operator-=(const Point& offset) { - x -= offset.x; - y -= offset.y; - - return *this; -} - } std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect) { diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index 074ac08..c5397c3 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -25,13 +25,10 @@ #include #include - -struct SDL_Rect; +#include namespace SDL2pp { -class Point; - //////////////////////////////////////////////////////////// /// \brief 2D rectangle /// @@ -55,7 +52,8 @@ public: /// Creates a Rect(0, 0, 0, 0) /// //////////////////////////////////////////////////////////// - Rect(); + constexpr Rect() : SDL_Rect{0, 0, 0, 0} { + } //////////////////////////////////////////////////////////// /// \brief Construct a rect from existing SDL_Rect @@ -63,7 +61,8 @@ public: /// \param[in] rect Existing SDL_Rect /// //////////////////////////////////////////////////////////// - Rect(const SDL_Rect& rect); + constexpr Rect(const SDL_Rect& rect) : SDL_Rect{rect.x, rect.y, rect.w, rect.h} { + } //////////////////////////////////////////////////////////// /// \brief Construct the rect from given corner coordinates, and size @@ -72,7 +71,8 @@ public: /// \param[in] size Dimensions of the rectangle /// //////////////////////////////////////////////////////////// - Rect(const Point& corner, const Point& size); + constexpr Rect(const Point& corner, const Point& size) : SDL_Rect{corner.x, corner.y, size.x, size.y} { + } //////////////////////////////////////////////////////////// /// \brief Construct the rect from given corner coordinates, width and height @@ -83,7 +83,8 @@ public: /// \param[in] h Height of the rectangle /// //////////////////////////////////////////////////////////// - Rect(int x, int y, int w, int h); + constexpr Rect(int x, int y, int w, int h) : SDL_Rect{x, y, w, h} { + } //////////////////////////////////////////////////////////// /// \brief Construct the rect from given center coordinates, width and height @@ -94,7 +95,9 @@ public: /// \param[in] h Height of the rectangle /// //////////////////////////////////////////////////////////// - static Rect FromCenter(int cx, int cy, int w, int h); + static Rect FromCenter(int cx, int cy, int w, int h) { + return Rect(cx - w/2, cy - h/2, w, h); + } //////////////////////////////////////////////////////////// /// \brief Construct the rect from given center coordinates and size @@ -103,7 +106,9 @@ public: /// \param[in] size Dimensions of the rectangle /// //////////////////////////////////////////////////////////// - static Rect FromCenter(const Point& center, const Point& size); + static Rect FromCenter(const Point& center, const Point& size) { + return Rect(center - size / 2, size); + } //////////////////////////////////////////////////////////// /// \brief Construct the rect from given corners coordinates @@ -114,7 +119,9 @@ public: /// \param[in] y2 Y coordinate of the bottom right rectangle corner /// //////////////////////////////////////////////////////////// - static Rect FromCorners(int x1, int y1, int x2, int y2); + static Rect FromCorners(int x1, int y1, int x2, int y2) { + return Rect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); + } //////////////////////////////////////////////////////////// /// \brief Construct the rect from given centers coordinates @@ -123,7 +130,9 @@ public: /// \param[in] p2 Coordinates of the bottom right rectangle corner /// //////////////////////////////////////////////////////////// - static Rect FromCorners(const Point& p1, const Point& p2); + static Rect FromCorners(const Point& p1, const Point& p2) { + return Rect(p1, p2 - p1 + Point(1, 1)); + } //////////////////////////////////////////////////////////// /// \brief Copy constructor @@ -161,7 +170,10 @@ public: /// \returns True if two rectangles are identical /// //////////////////////////////////////////////////////////// - bool operator==(const Rect& other) const; + constexpr bool operator==(const Rect& other) const { + return x == other.x && y == other.y && + w == other.w && h == other.h; + } //////////////////////////////////////////////////////////// /// \brief Inequality operator @@ -171,7 +183,10 @@ public: /// \returns True if two rectangles are not identical /// //////////////////////////////////////////////////////////// - bool operator!=(const Rect& other) const; + constexpr bool operator!=(const Rect& other) const { + return x != other.x || y != other.y || + w != other.w || h != other.h; + } //////////////////////////////////////////////////////////// /// \brief Get X coordinate of the rect corner @@ -179,7 +194,9 @@ public: /// \returns X coordinate of the rect corner /// //////////////////////////////////////////////////////////// - int GetX() const; + constexpr int GetX() const { + return x; + } //////////////////////////////////////////////////////////// /// \brief Set X coordinate of the rect corner @@ -189,7 +206,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetX(int nx); + Rect& SetX(int nx) { + x = nx; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get Y coordinate of the rect corner @@ -197,7 +217,9 @@ public: /// \returns Y coordinate of the rect corner /// //////////////////////////////////////////////////////////// - int GetY() const; + constexpr int GetY() const { + return y; + } //////////////////////////////////////////////////////////// /// \brief Set Y coordinate of the rect corner @@ -207,7 +229,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetY(int ny); + Rect& SetY(int ny) { + y = ny; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get width of the rect @@ -215,7 +240,9 @@ public: /// \returns Width of the rect /// //////////////////////////////////////////////////////////// - int GetW() const; + constexpr int GetW() const { + return w; + } //////////////////////////////////////////////////////////// /// \brief Set width of the rect @@ -225,7 +252,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetW(int nw); + Rect& SetW(int nw) { + w = nw; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get height of the rect @@ -233,7 +263,9 @@ public: /// \returns Height of the rect /// //////////////////////////////////////////////////////////// - int GetH() const; + constexpr int GetH() const { + return h; + } //////////////////////////////////////////////////////////// /// \brief Set height of the rect @@ -243,7 +275,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetH(int nh); + Rect& SetH(int nh) { + h = nh; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get X coordinate of the rect second corner @@ -251,7 +286,9 @@ public: /// \returns X coordinate of the rect second corner /// //////////////////////////////////////////////////////////// - int GetX2() const; + constexpr int GetX2() const { + return x + w - 1; + } //////////////////////////////////////////////////////////// /// \brief Set X coordinate of the rect second corner @@ -261,7 +298,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetX2(int x2); + Rect& SetX2(int x2) { + w = x2 - x + 1; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get Y coordinate of the rect second corner @@ -271,7 +311,9 @@ public: /// This modifies rectangle width internally /// //////////////////////////////////////////////////////////// - int GetY2() const; + constexpr int GetY2() const { + return y + h - 1; + } //////////////////////////////////////////////////////////// /// \brief Set Y coordinate of the rect second corner @@ -283,18 +325,23 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetY2(int y2); + Rect& SetY2(int y2) { + h = y2 - y + 1; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Check whether the rect contains given point /// - /// \param[in] x X coordinate of a point - /// \param[in] y Y coordinate of a point + /// \param[in] px X coordinate of a point + /// \param[in] py Y coordinate of a point /// /// \returns True if the point is contained in the rect /// //////////////////////////////////////////////////////////// - bool Contains(int x, int y) const; + constexpr bool Contains(int px, int py) const { + return px >= x && py >= y && px <= GetX2() && py <= GetY2(); + } //////////////////////////////////////////////////////////// /// \brief Check whether the rect contains given point @@ -304,7 +351,9 @@ public: /// \returns True if the point is contained in the rect /// //////////////////////////////////////////////////////////// - bool Contains(const Point& point) const; + constexpr bool Contains(const Point& point) const { + return point.x >= x && point.y >= y && point.x <= GetX2() && point.y <= GetY2(); + } //////////////////////////////////////////////////////////// /// \brief Check whether the rect contains another rect @@ -314,7 +363,9 @@ public: /// \returns True if the checked rect is contained in this rect /// //////////////////////////////////////////////////////////// - bool Contains(const Rect& rect) const; + constexpr bool Contains(const Rect& rect) const { + return rect.x >= x && rect.y >= y && rect.GetX2() <= GetX2() && rect.GetY2() <= GetY2(); + } //////////////////////////////////////////////////////////// /// \brief Check whether the rect intersects another rect @@ -324,7 +375,9 @@ public: /// \returns True if rectangles intersect /// //////////////////////////////////////////////////////////// - bool Intersects(const Rect& rect) const; + constexpr bool Intersects(const Rect& rect) const { + return !(rect.GetX2() < x || rect.GetY2() < y || rect.x > GetX2() || rect.y > GetY2()); + } //////////////////////////////////////////////////////////// /// \brief Calculate union with another rect @@ -423,7 +476,9 @@ public: /// \returns Moved rectangle /// //////////////////////////////////////////////////////////// - Rect operator+(const Point& offset) const; + constexpr Rect operator+(const Point& offset) const { + return Rect(x + offset.x, y + offset.y, w, h); + } //////////////////////////////////////////////////////////// /// \brief Get rectangle moved by an opposite of given offset @@ -433,7 +488,9 @@ public: /// \returns Moved rectangle /// //////////////////////////////////////////////////////////// - Rect operator-(const Point& offset) const; + constexpr Rect operator-(const Point& offset) const { + return Rect(x - offset.x, y - offset.y, w, h); + } //////////////////////////////////////////////////////////// /// \brief Move by then given offset @@ -443,7 +500,11 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& operator+=(const Point& offset); + Rect& operator+=(const Point& offset) { + x += offset.x; + y += offset.y; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Move by an opposite of the given offset @@ -453,7 +514,11 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& operator-=(const Point& offset); + Rect& operator-=(const Point& offset) { + x -= offset.x; + y -= offset.y; + return *this; + } }; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f853d69..0d4ef8c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,7 @@ # simple command-line tests SET(CLI_TESTS test_pointrect + test_pointrect_constexpr test_rwops test_optional test_error diff --git a/tests/test_pointrect_constexpr.cc b/tests/test_pointrect_constexpr.cc new file mode 100644 index 0000000..d360555 --- /dev/null +++ b/tests/test_pointrect_constexpr.cc @@ -0,0 +1,82 @@ +#include +#include + +#include "testing.h" + +using namespace SDL2pp; + +BEGIN_TEST() + // note that this is merely a compilation test; EXPECT_s are mainly + // used to silence `unused variable' warnings + { + constexpr SDL_Point sp{1, 2}; + constexpr Point p1; + constexpr Point p2(sp); + constexpr Point p3(1, 2); + constexpr Point p4(p2); + + constexpr bool b1 = p2 == p4; + constexpr bool b2 = p1 != p2; + + EXPECT_TRUE(b1); + EXPECT_TRUE(b2); + + constexpr int x = p1.GetX(); + constexpr int y = p1.GetY(); + + EXPECT_TRUE(x == 0 && y == 0); + + constexpr Point neg = -p1; + constexpr Point sum = p1 + p2; + constexpr Point diff = p1 - p2; + constexpr Point mul1 = p1 * p2; + constexpr Point mul2 = p1 * 2; + constexpr Point div1 = p1 / p2; + constexpr Point div2 = p1 * 2; + constexpr Point rem1 = p1 % p2; + constexpr Point rem2 = p1 % 2; + + EXPECT_EQUAL(neg + sum + diff + mul1 + mul2 + div1 + div2 + rem1 + rem2, Point(0, 0)); + } + + { + constexpr SDL_Rect sr{0, 0, 1, 1}; + constexpr Rect r1; + constexpr Rect r2(sr); + constexpr Rect r3(Point(0, 0), Point(1, 1)); + constexpr Rect r4(0, 0, 1, 1); + constexpr Rect r5(r4); + + EXPECT_EQUAL(r4, r2); + EXPECT_EQUAL(r5, r3); + + constexpr bool b1 = r2 == r3; + constexpr bool b2 = r1 != r3; + + EXPECT_TRUE(b1); + EXPECT_TRUE(b2); + + constexpr int x = r5.GetX(); + constexpr int y = r5.GetY(); + constexpr int w = r5.GetW(); + constexpr int h = r5.GetH(); + constexpr int x2 = r5.GetX2(); + constexpr int y2 = r5.GetY2(); + + EXPECT_TRUE(x == y); + EXPECT_TRUE(w == h); + EXPECT_TRUE(x2 == y2); + + constexpr Rect add = r1 + Point(1, 1); + constexpr Rect sub = r1 - Point(1, 1); + + EXPECT_EQUAL(add - Point(2, 2), sub); + + constexpr bool b3 = r2.Contains(0, 0); + constexpr bool b4 = r2.Contains(Point(0, 0)); + constexpr bool b5 = r2.Contains(Rect(0, 0, 1, 1)); + constexpr bool b6 = r2.Intersects(Rect(0, 0, 1, 1)); + + EXPECT_TRUE(b3 && b4 && b5 && b6); + } +END_TEST()