diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index f27212a..2f7a5ab 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -137,4 +137,34 @@ bool Rect::Contains(const Point& point) const { return !(point.GetX() < GetX() || point.GetY() < GetY() || point.GetX() > GetX2() || point.GetY() > GetY2()); } +Rect Rect::operator+(const Point& offset) const { + assert(!IsNull() && !offset.IsNull()); + + return Rect(rect_.x + offset.GetX(), rect_.y + offset.GetY(), rect_.w, rect_.h); +} + +Rect& Rect::operator+=(const Point& offset) { + assert(!IsNull() && !offset.IsNull()); + + rect_.x += offset.GetX(); + rect_.y += offset.GetY(); + + return *this; +} + +Rect Rect::operator-(const Point& offset) const { + assert(!IsNull() && !offset.IsNull()); + + return Rect(rect_.x - offset.GetX(), rect_.y - offset.GetY(), rect_.w, rect_.h); +} + +Rect& Rect::operator-=(const Point& offset) { + assert(!IsNull() && !offset.IsNull()); + + rect_.x -= offset.GetX(); + rect_.y -= offset.GetY(); + + return *this; +} + } diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index e7f3af6..a8305c8 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -78,6 +78,12 @@ public: void SetY2(int y2); bool Contains(const Point& point) const; + + Rect operator+(const Point& offset) const; + Rect& operator+=(const Point& offset); + + Rect operator-(const Point& offset) const; + Rect& operator-=(const Point& offset); }; } diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 46653a0..3e3a3fb 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -173,4 +173,20 @@ BEGIN_TEST() EXPECT_TRUE(!r.Contains(Point(15, 20))); EXPECT_TRUE(!r.Contains(Point(10, 25))); } + + { + // Rect offset + Rect r(1, 2, 3, 4); + + EXPECT_TRUE(r + Point(10, 20) == Rect(11, 22, 3, 4)); + EXPECT_TRUE(r - Point(10, 20) == Rect(-9, -18, 3, 4)); + + r += Point(10, 20); + + EXPECT_TRUE(r == Rect(11, 22, 3, 4)); + + r -= Point(20, 40); + + EXPECT_TRUE(r == Rect(-9, -18, 3, 4)); + } END_TEST()