SDL_rect.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * \file SDL_rect.h
  20. *
  21. * Header file for SDL_rect definition and management functions.
  22. */
  23. #ifndef SDL_rect_h_
  24. #define SDL_rect_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_begin_code.h>
  28. /* Set up for C function definitions, even when using C++ */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /**
  33. * The structure that defines a point (integer)
  34. *
  35. * \sa SDL_GetRectEnclosingPoints
  36. * \sa SDL_PointInRect
  37. */
  38. typedef struct SDL_Point
  39. {
  40. int x;
  41. int y;
  42. } SDL_Point;
  43. /**
  44. * The structure that defines a point (floating point)
  45. *
  46. * \sa SDL_GetRectEnclosingPointsFloat
  47. * \sa SDL_PointInRectFloat
  48. */
  49. typedef struct SDL_FPoint
  50. {
  51. float x;
  52. float y;
  53. } SDL_FPoint;
  54. /**
  55. * A rectangle, with the origin at the upper left (integer).
  56. *
  57. * \sa SDL_RectEmpty
  58. * \sa SDL_RectsEqual
  59. * \sa SDL_HasRectIntersection
  60. * \sa SDL_GetRectIntersection
  61. * \sa SDL_GetRectAndLineIntersection
  62. * \sa SDL_GetRectUnion
  63. * \sa SDL_GetRectEnclosingPoints
  64. */
  65. typedef struct SDL_Rect
  66. {
  67. int x, y;
  68. int w, h;
  69. } SDL_Rect;
  70. /**
  71. * A rectangle, with the origin at the upper left (floating point).
  72. *
  73. * \sa SDL_RectEmptyFloat
  74. * \sa SDL_RectsEqualFloat
  75. * \sa SDL_RectsEqualEpsilon
  76. * \sa SDL_HasRectIntersectionFloat
  77. * \sa SDL_GetRectIntersectionFloat
  78. * \sa SDL_GetRectAndLineIntersectionFloat
  79. * \sa SDL_GetRectUnionFloat
  80. * \sa SDL_GetRectEnclosingPointsFloat
  81. * \sa SDL_PointInRectFloat
  82. */
  83. typedef struct SDL_FRect
  84. {
  85. float x;
  86. float y;
  87. float w;
  88. float h;
  89. } SDL_FRect;
  90. /**
  91. * Returns true if point resides inside a rectangle.
  92. */
  93. SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
  94. {
  95. return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  96. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  97. }
  98. /**
  99. * Returns true if the rectangle has no area.
  100. */
  101. SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
  102. {
  103. return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
  104. }
  105. /**
  106. * Returns true if the two rectangles are equal.
  107. */
  108. SDL_FORCE_INLINE SDL_bool SDL_RectsEqual(const SDL_Rect *a, const SDL_Rect *b)
  109. {
  110. return (a && b && (a->x == b->x) && (a->y == b->y) &&
  111. (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
  112. }
  113. /**
  114. * Determine whether two rectangles intersect.
  115. *
  116. * If either pointer is NULL the function will return SDL_FALSE.
  117. *
  118. * \param A an SDL_Rect structure representing the first rectangle
  119. * \param B an SDL_Rect structure representing the second rectangle
  120. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  121. *
  122. * \since This function is available since SDL 3.0.0.
  123. *
  124. * \sa SDL_GetRectIntersection
  125. */
  126. extern DECLSPEC SDL_bool SDLCALL SDL_HasRectIntersection(const SDL_Rect * A,
  127. const SDL_Rect * B);
  128. /**
  129. * Calculate the intersection of two rectangles.
  130. *
  131. * If `result` is NULL then this function will return SDL_FALSE.
  132. *
  133. * \param A an SDL_Rect structure representing the first rectangle
  134. * \param B an SDL_Rect structure representing the second rectangle
  135. * \param result an SDL_Rect structure filled in with the intersection of
  136. * rectangles `A` and `B`
  137. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  138. *
  139. * \since This function is available since SDL 3.0.0.
  140. *
  141. * \sa SDL_HasRectIntersection
  142. */
  143. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectIntersection(const SDL_Rect * A,
  144. const SDL_Rect * B,
  145. SDL_Rect * result);
  146. /**
  147. * Calculate the union of two rectangles.
  148. *
  149. * \param A an SDL_Rect structure representing the first rectangle
  150. * \param B an SDL_Rect structure representing the second rectangle
  151. * \param result an SDL_Rect structure filled in with the union of rectangles
  152. * `A` and `B`
  153. * \returns 0 on success or a negative error code on failure; call
  154. * SDL_GetError() for more information.
  155. *
  156. * \since This function is available since SDL 3.0.0.
  157. */
  158. extern DECLSPEC int SDLCALL SDL_GetRectUnion(const SDL_Rect * A,
  159. const SDL_Rect * B,
  160. SDL_Rect * result);
  161. /**
  162. * Calculate a minimal rectangle enclosing a set of points.
  163. *
  164. * If `clip` is not NULL then only points inside of the clipping rectangle are
  165. * considered.
  166. *
  167. * \param points an array of SDL_Point structures representing points to be
  168. * enclosed
  169. * \param count the number of structures in the `points` array
  170. * \param clip an SDL_Rect used for clipping or NULL to enclose all points
  171. * \param result an SDL_Rect structure filled in with the minimal enclosing
  172. * rectangle
  173. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  174. * points were outside of the clipping rectangle.
  175. *
  176. * \since This function is available since SDL 3.0.0.
  177. */
  178. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectEnclosingPoints(const SDL_Point * points,
  179. int count,
  180. const SDL_Rect * clip,
  181. SDL_Rect * result);
  182. /**
  183. * Calculate the intersection of a rectangle and line segment.
  184. *
  185. * This function is used to clip a line segment to a rectangle. A line segment
  186. * contained entirely within the rectangle or that does not intersect will
  187. * remain unchanged. A line segment that crosses the rectangle at either or
  188. * both ends will be clipped to the boundary of the rectangle and the new
  189. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  190. *
  191. * \param rect an SDL_Rect structure representing the rectangle to intersect
  192. * \param X1 a pointer to the starting X-coordinate of the line
  193. * \param Y1 a pointer to the starting Y-coordinate of the line
  194. * \param X2 a pointer to the ending X-coordinate of the line
  195. * \param Y2 a pointer to the ending Y-coordinate of the line
  196. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  197. *
  198. * \since This function is available since SDL 3.0.0.
  199. */
  200. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectAndLineIntersection(const SDL_Rect *
  201. rect, int *X1,
  202. int *Y1, int *X2,
  203. int *Y2);
  204. /* SDL_FRect versions... */
  205. /**
  206. * Returns true if point resides inside a rectangle.
  207. */
  208. SDL_FORCE_INLINE SDL_bool SDL_PointInRectFloat(const SDL_FPoint *p, const SDL_FRect *r)
  209. {
  210. return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  211. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  212. }
  213. /**
  214. * Returns true if the rectangle has no area.
  215. *
  216. * \since This function is available since SDL 3.0.0.
  217. */
  218. SDL_FORCE_INLINE SDL_bool SDL_RectEmptyFloat(const SDL_FRect *r)
  219. {
  220. return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
  221. }
  222. /**
  223. * Returns true if the two rectangles are equal, within some given epsilon.
  224. *
  225. * \since This function is available since SDL 3.0.0.
  226. */
  227. SDL_FORCE_INLINE SDL_bool SDL_RectsEqualEpsilon(const SDL_FRect *a, const SDL_FRect *b, const float epsilon)
  228. {
  229. return (a && b && ((a == b) ||
  230. ((SDL_fabsf(a->x - b->x) <= epsilon) &&
  231. (SDL_fabsf(a->y - b->y) <= epsilon) &&
  232. (SDL_fabsf(a->w - b->w) <= epsilon) &&
  233. (SDL_fabsf(a->h - b->h) <= epsilon))))
  234. ? SDL_TRUE : SDL_FALSE;
  235. }
  236. /**
  237. * Returns true if the two rectangles are equal, using a default epsilon.
  238. *
  239. * \since This function is available since SDL 3.0.0.
  240. */
  241. SDL_FORCE_INLINE SDL_bool SDL_RectsEqualFloat(const SDL_FRect *a, const SDL_FRect *b)
  242. {
  243. return SDL_RectsEqualEpsilon(a, b, SDL_FLT_EPSILON);
  244. }
  245. /**
  246. * Determine whether two rectangles intersect with float precision.
  247. *
  248. * If either pointer is NULL the function will return SDL_FALSE.
  249. *
  250. * \param A an SDL_FRect structure representing the first rectangle
  251. * \param B an SDL_FRect structure representing the second rectangle
  252. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  253. *
  254. * \since This function is available since SDL 3.0.0.
  255. *
  256. * \sa SDL_GetRectIntersection
  257. */
  258. extern DECLSPEC SDL_bool SDLCALL SDL_HasRectIntersectionFloat(const SDL_FRect * A,
  259. const SDL_FRect * B);
  260. /**
  261. * Calculate the intersection of two rectangles with float precision.
  262. *
  263. * If `result` is NULL then this function will return SDL_FALSE.
  264. *
  265. * \param A an SDL_FRect structure representing the first rectangle
  266. * \param B an SDL_FRect structure representing the second rectangle
  267. * \param result an SDL_FRect structure filled in with the intersection of
  268. * rectangles `A` and `B`
  269. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  270. *
  271. * \since This function is available since SDL 3.0.0.
  272. *
  273. * \sa SDL_HasRectIntersectionFloat
  274. */
  275. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectIntersectionFloat(const SDL_FRect * A,
  276. const SDL_FRect * B,
  277. SDL_FRect * result);
  278. /**
  279. * Calculate the union of two rectangles with float precision.
  280. *
  281. * \param A an SDL_FRect structure representing the first rectangle
  282. * \param B an SDL_FRect structure representing the second rectangle
  283. * \param result an SDL_FRect structure filled in with the union of rectangles
  284. * `A` and `B`
  285. * \returns 0 on success or a negative error code on failure; call
  286. * SDL_GetError() for more information.
  287. *
  288. * \since This function is available since SDL 3.0.0.
  289. */
  290. extern DECLSPEC int SDLCALL SDL_GetRectUnionFloat(const SDL_FRect * A,
  291. const SDL_FRect * B,
  292. SDL_FRect * result);
  293. /**
  294. * Calculate a minimal rectangle enclosing a set of points with float
  295. * precision.
  296. *
  297. * If `clip` is not NULL then only points inside of the clipping rectangle are
  298. * considered.
  299. *
  300. * \param points an array of SDL_FPoint structures representing points to be
  301. * enclosed
  302. * \param count the number of structures in the `points` array
  303. * \param clip an SDL_FRect used for clipping or NULL to enclose all points
  304. * \param result an SDL_FRect structure filled in with the minimal enclosing
  305. * rectangle
  306. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  307. * points were outside of the clipping rectangle.
  308. *
  309. * \since This function is available since SDL 3.0.0.
  310. */
  311. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectEnclosingPointsFloat(const SDL_FPoint * points,
  312. int count,
  313. const SDL_FRect * clip,
  314. SDL_FRect * result);
  315. /**
  316. * Calculate the intersection of a rectangle and line segment with float
  317. * precision.
  318. *
  319. * This function is used to clip a line segment to a rectangle. A line segment
  320. * contained entirely within the rectangle or that does not intersect will
  321. * remain unchanged. A line segment that crosses the rectangle at either or
  322. * both ends will be clipped to the boundary of the rectangle and the new
  323. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  324. *
  325. * \param rect an SDL_FRect structure representing the rectangle to intersect
  326. * \param X1 a pointer to the starting X-coordinate of the line
  327. * \param Y1 a pointer to the starting Y-coordinate of the line
  328. * \param X2 a pointer to the ending X-coordinate of the line
  329. * \param Y2 a pointer to the ending Y-coordinate of the line
  330. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  331. *
  332. * \since This function is available since SDL 3.0.0.
  333. */
  334. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectAndLineIntersectionFloat(const SDL_FRect *
  335. rect, float *X1,
  336. float *Y1, float *X2,
  337. float *Y2);
  338. /* Ends C function definitions when using C++ */
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #include <SDL3/SDL_close_code.h>
  343. #endif /* SDL_rect_h_ */