From 4c15108233679c073a9607a0a94483e1d8452974 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 2 Jul 2023 20:16:21 -0600 Subject: [PATCH] Implement `sf::Rect::getPosition()` and `sf::Rect::getSize()` --- include/SFML/Graphics/Rect.h | 24 +++++++++++++++++++++ src/SFML/Graphics/Rect.cpp | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/include/SFML/Graphics/Rect.h b/include/SFML/Graphics/Rect.h index 6322929..b3e3be1 100644 --- a/include/SFML/Graphics/Rect.h +++ b/include/SFML/Graphics/Rect.h @@ -30,6 +30,8 @@ //////////////////////////////////////////////////////////// #include +#include + //////////////////////////////////////////////////////////// /// sfFloatRect and sfIntRect are utility classes for @@ -77,5 +79,27 @@ CSFML_GRAPHICS_API sfBool sfIntRect_contains(const sfIntRect* rect, int x, int y CSFML_GRAPHICS_API sfBool sfFloatRect_intersects(const sfFloatRect* rect1, const sfFloatRect* rect2, sfFloatRect* intersection); CSFML_GRAPHICS_API sfBool sfIntRect_intersects(const sfIntRect* rect1, const sfIntRect* rect2, sfIntRect* intersection); +//////////////////////////////////////////////////////////// +/// \brief Get the position of the rectangle's top-left corner +/// +/// \return Position of rectangle +/// +/// \see getSize +/// +//////////////////////////////////////////////////////////// +CSFML_GRAPHICS_API sfVector2f sfFloatRect_getPosition(const sfFloatRect* rect); +CSFML_GRAPHICS_API sfVector2i sfIntRect_getPosition(const sfIntRect* rect); + +//////////////////////////////////////////////////////////// +/// \brief Get the size of the rectangle +/// +/// \return Size of rectangle +/// +/// \see getPosition +/// +//////////////////////////////////////////////////////////// +CSFML_GRAPHICS_API sfVector2f sfFloatRect_getSize(const sfFloatRect* rect); +CSFML_GRAPHICS_API sfVector2i sfIntRect_getSize(const sfIntRect* rect); + #endif // SFML_RECT_H diff --git a/src/SFML/Graphics/Rect.cpp b/src/SFML/Graphics/Rect.cpp index 8a4a9c8..c959522 100644 --- a/src/SFML/Graphics/Rect.cpp +++ b/src/SFML/Graphics/Rect.cpp @@ -30,6 +30,21 @@ #include +namespace +{ +sfVector2f toCType(const sf::Vector2f& vector) +{ + sfVector2f vec = {vector.x, vector.y}; + return vec; +} + +sfVector2i toCType(const sf::Vector2i& vector) +{ + sfVector2i vec = {vector.x, vector.y}; + return vec; +} +} + //////////////////////////////////////////////////////////// /// Check if a point is inside a rectangle's area //////////////////////////////////////////////////////////// @@ -98,3 +113,29 @@ sfBool sfIntRect_intersects(const sfIntRect* rect1, const sfIntRect* rect2, sfIn return SFMLRect1.intersects(SFMLRect2); } } + + +//////////////////////////////////////////////////////////// +/// Get the position of the rectangle's top-left corner +//////////////////////////////////////////////////////////// +sfVector2f sfFloatRect_getPosition(const sfFloatRect* rect) +{ + return toCType(sf::FloatRect(rect->left, rect->top, rect->width, rect->height).getPosition()); +} +sfVector2i sfIntRect_getPosition(const sfIntRect* rect) +{ + return toCType(sf::IntRect(rect->left, rect->top, rect->width, rect->height).getPosition()); +} + + +//////////////////////////////////////////////////////////// +/// Get the size of the rectangle +//////////////////////////////////////////////////////////// +sfVector2f sfFloatRect_getSize(const sfFloatRect* rect) +{ + return toCType(sf::FloatRect(rect->left, rect->top, rect->width, rect->height).getSize()); +} +sfVector2i sfIntRect_getSize(const sfIntRect* rect) +{ + return toCType(sf::IntRect(rect->left, rect->top, rect->width, rect->height).getSize()); +}