mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-03 10:55:57 -04:00
Constexprify more Rect methods
This commit is contained in:
parent
4f3256fda5
commit
beaa9ed3b8
@ -27,15 +27,6 @@
|
||||
|
||||
namespace SDL2pp {
|
||||
|
||||
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);
|
||||
}
|
||||
@ -52,60 +43,6 @@ 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();
|
||||
}
|
||||
@ -177,10 +114,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;
|
||||
@ -188,10 +121,6 @@ Rect& Rect::operator+=(const Point& offset) {
|
||||
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;
|
||||
|
@ -162,7 +162,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
|
||||
@ -172,7 +175,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
|
||||
@ -180,7 +186,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
|
||||
@ -190,7 +198,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
|
||||
@ -198,7 +209,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
|
||||
@ -208,7 +221,10 @@ public:
|
||||
/// \returns Reference to self
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
Rect& SetY(int ny);
|
||||
Rect& SetY(int ny) {
|
||||
y = ny;
|
||||
return *this;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Get width of the rect
|
||||
@ -216,7 +232,9 @@ public:
|
||||
/// \returns Width of the rect
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
int GetW() const;
|
||||
constexpr int GetW() const {
|
||||
return w;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Set width of the rect
|
||||
@ -226,7 +244,10 @@ public:
|
||||
/// \returns Reference to self
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
Rect& SetW(int nw);
|
||||
Rect& SetW(int nw) {
|
||||
w = nw;
|
||||
return *this;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Get height of the rect
|
||||
@ -234,7 +255,9 @@ public:
|
||||
/// \returns Height of the rect
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
int GetH() const;
|
||||
constexpr int GetH() const {
|
||||
return h;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Set height of the rect
|
||||
@ -244,7 +267,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
|
||||
@ -252,7 +278,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
|
||||
@ -262,7 +290,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
|
||||
@ -272,7 +303,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
|
||||
@ -284,7 +317,10 @@ 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
|
||||
@ -424,7 +460,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
|
||||
@ -434,7 +472,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
|
||||
|
@ -49,5 +49,27 @@ BEGIN_TEST()
|
||||
|
||||
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);
|
||||
}
|
||||
END_TEST()
|
||||
|
Loading…
x
Reference in New Issue
Block a user