From b3a0ed815b207bb6cf55a0df8fcb79e89eecd903 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Mon, 26 Aug 2024 13:45:49 -0600 Subject: [PATCH] Clean up implementation of `sfRect_intersects` --- src/CSFML/Graphics/Rect.cpp | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/src/CSFML/Graphics/Rect.cpp b/src/CSFML/Graphics/Rect.cpp index 50c1658..8160cef 100644 --- a/src/CSFML/Graphics/Rect.cpp +++ b/src/CSFML/Graphics/Rect.cpp @@ -56,42 +56,22 @@ bool sfFloatRect_intersects(const sfFloatRect* rect1, const sfFloatRect* rect2, CSFML_CHECK_RETURN(rect1, false); CSFML_CHECK_RETURN(rect2, false); - sf::FloatRect SFMLRect1 = convertRect(*rect1); - sf::FloatRect SFMLRect2 = convertRect(*rect2); + const std::optional overlap = convertRect(*rect1).findIntersection(convertRect(*rect2)); - if (intersection) - { - std::optional overlap = SFMLRect1.findIntersection(SFMLRect2); + if (intersection && overlap) + *intersection = convertRect(*overlap); - if (overlap) - *intersection = convertRect(*overlap); - - return overlap.has_value(); - } - else - { - return SFMLRect1.findIntersection(SFMLRect2).has_value(); - } + return overlap.has_value(); } bool sfIntRect_intersects(const sfIntRect* rect1, const sfIntRect* rect2, sfIntRect* intersection) { CSFML_CHECK_RETURN(rect1, false); CSFML_CHECK_RETURN(rect2, false); - sf::IntRect SFMLRect1 = convertRect(*rect1); - sf::IntRect SFMLRect2 = convertRect(*rect2); + const std::optional overlap = convertRect(*rect1).findIntersection(convertRect(*rect2)); - if (intersection) - { - std::optional overlap = SFMLRect1.findIntersection(SFMLRect2); + if (intersection && overlap) + *intersection = convertRect(*overlap); - if (overlap) - *intersection = convertRect(*overlap); - - return overlap.has_value(); - } - else - { - return SFMLRect1.findIntersection(SFMLRect2).has_value(); - } + return overlap.has_value(); }