SDL_rect.h 13 KB

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