diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index 815de3f..c1bf0b5 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -54,11 +54,21 @@ Rect Rect::GetExtension(unsigned int amount) const { return r; } +Rect Rect::GetExtension(unsigned int hamount, unsigned int vamount) const { + Rect r = *this; + r.Extend(hamount, vamount); + return r; +} + Rect& Rect::Extend(unsigned int amount) { - x -= amount; - y -= amount; - w += amount * 2; - h += amount * 2; + return Extend(amount, amount); +} + +Rect& Rect::Extend(unsigned int hamount, unsigned int vamount) { + x -= hamount; + y -= vamount; + w += hamount * 2; + h += vamount * 2; return *this; } diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index 81691a7..89de653 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -385,6 +385,19 @@ public: //////////////////////////////////////////////////////////// Rect GetExtension(unsigned int amount) const; + //////////////////////////////////////////////////////////// + /// \brief Get a rect extended by specified amount of pixels + /// + /// \param[in] hamount Number of pixels to extend by + /// in horizontal direction + /// \param[in] vamount Number of pixels to extend by + /// in vertical direction + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Rect GetExtension(unsigned int hamount, unsigned int vamount) const; + //////////////////////////////////////////////////////////// /// \brief Extend a rect by specified amount of pixels /// @@ -395,6 +408,19 @@ public: //////////////////////////////////////////////////////////// Rect& Extend(unsigned int amount); + //////////////////////////////////////////////////////////// + /// \brief Extend a rect by specified amount of pixels + /// + /// \param[in] hamount Number of pixels to extend by + /// in horizontal direction + /// \param[in] vamount Number of pixels to extend by + /// in vertical direction + /// + /// \returns Extended rect + /// + //////////////////////////////////////////////////////////// + Rect& Extend(unsigned int hamount, unsigned int vamount); + //////////////////////////////////////////////////////////// /// \brief Calculate intersection with another rect /// diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 1d7dc66..bf30654 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -283,9 +283,11 @@ BEGIN_TEST() // Rect extend EXPECT_EQUAL(Rect(10, 20, 30, 40).GetExtension(0), Rect(10, 20, 30, 40)); EXPECT_EQUAL(Rect(10, 20, 30, 40).GetExtension(10), Rect(0, 10, 50, 60)); + EXPECT_EQUAL(Rect(10, 20, 30, 40).GetExtension(10, 20), Rect(0, 0, 50, 80)); EXPECT_EQUAL(Rect(10, 20, 30, 40).Extend(0), Rect(10, 20, 30, 40)); EXPECT_EQUAL(Rect(10, 20, 30, 40).Extend(10), Rect(0, 10, 50, 60)); + EXPECT_EQUAL(Rect(10, 20, 30, 40).Extend(10, 20), Rect(0, 0, 50, 80)); } {