From fc944859d19880062965ff68f3b13d616d614649 Mon Sep 17 00:00:00 2001
From: Eddy Jansson <[EMAIL REDACTED]>
Date: Wed, 20 Apr 2022 05:46:45 +0200
Subject: [PATCH] SDL_Rect: Use a default epsilon in SDL_FRectEquals()
Add SDL_FRectEqualsEpsilon() for when more control over
equality test is required.
---
include/SDL_rect.h | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/include/SDL_rect.h b/include/SDL_rect.h
index 55a6473f2dc..70f2293888e 100644
--- a/include/SDL_rect.h
+++ b/include/SDL_rect.h
@@ -239,12 +239,28 @@ SDL_FORCE_INLINE SDL_bool SDL_FRectEmpty(const SDL_FRect *r)
}
/**
- * Returns true if the two rectangles are equal.
+ * Returns true if the two rectangles are equal, within some given epsilon.
+ *
+ * \since This function is available since SDL 2.0.22.
+ */
+SDL_FORCE_INLINE SDL_bool SDL_FRectEqualsEpsilon(const SDL_FRect *a, const SDL_FRect *b, const float epsilon)
+{
+ return (a && b && ((a == b) ||
+ ((SDL_fabs(a->x - b->x) <= epsilon) &&
+ (SDL_fabs(a->y - b->y) <= epsilon) &&
+ (SDL_fabs(a->w - b->w) <= epsilon) &&
+ (SDL_fabs(a->h - b->h) <= epsilon))))
+ ? SDL_TRUE : SDL_FALSE;
+}
+
+/**
+ * Returns true if the two rectangles are equal, using a default epsilon.
+ *
+ * \since This function is available since SDL 2.0.22.
*/
SDL_FORCE_INLINE SDL_bool SDL_FRectEquals(const SDL_FRect *a, const SDL_FRect *b)
{
- return (a && b && (a->x == b->x) && (a->y == b->y) &&
- (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
+ return SDL_FRectEqualsEpsilon(a, b, SDL_FLT_EPSILON);
}
/**