SDL_rect.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 (using integers).
  34. *
  35. * \since This struct is available since SDL 3.0.0.
  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 (using floating point values).
  47. *
  48. * \since This struct is available since SDL 3.0.0.
  49. *
  50. * \sa SDL_GetRectEnclosingPointsFloat
  51. * \sa SDL_PointInRectFloat
  52. */
  53. typedef struct SDL_FPoint
  54. {
  55. float x;
  56. float y;
  57. } SDL_FPoint;
  58. /**
  59. * A rectangle, with the origin at the upper left (using integers).
  60. *
  61. * \since This struct is available since SDL 3.0.0.
  62. *
  63. * \sa SDL_RectEmpty
  64. * \sa SDL_RectsEqual
  65. * \sa SDL_HasRectIntersection
  66. * \sa SDL_GetRectIntersection
  67. * \sa SDL_GetRectAndLineIntersection
  68. * \sa SDL_GetRectUnion
  69. * \sa SDL_GetRectEnclosingPoints
  70. */
  71. typedef struct SDL_Rect
  72. {
  73. int x, y;
  74. int w, h;
  75. } SDL_Rect;
  76. /**
  77. * A rectangle, with the origin at the upper left (using floating point
  78. * values).
  79. *
  80. * \since This struct is available since SDL 3.0.0.
  81. *
  82. * \sa SDL_RectEmptyFloat
  83. * \sa SDL_RectsEqualFloat
  84. * \sa SDL_RectsEqualEpsilon
  85. * \sa SDL_HasRectIntersectionFloat
  86. * \sa SDL_GetRectIntersectionFloat
  87. * \sa SDL_GetRectAndLineIntersectionFloat
  88. * \sa SDL_GetRectUnionFloat
  89. * \sa SDL_GetRectEnclosingPointsFloat
  90. * \sa SDL_PointInRectFloat
  91. */
  92. typedef struct SDL_FRect
  93. {
  94. float x;
  95. float y;
  96. float w;
  97. float h;
  98. } SDL_FRect;
  99. /**
  100. * Determine whether a point resides inside a rectangle.
  101. *
  102. * A point is considered part of a rectangle if both `p` and `r` are
  103. * not NULL, and `p`'s x and y coordinates are >= to the rectangle's
  104. * top left corner, and < the rectangle's x+w and y+h. So a 1x1 rectangle
  105. * considers point (0,0) as "inside" and (0,1) as not.
  106. *
  107. * Note that this is a forced-inline function in a header, and not a public
  108. * API function available in the SDL library (which is to say, the code is
  109. * embedded in the calling program and the linker and dynamic loader will
  110. * not be able to find this function inside SDL itself).
  111. *
  112. * \param p the point to test.
  113. * \param r the rectangle to test.
  114. * \returns SDL_TRUE if `p` is contained by `r`, SDL_FALSE otherwise.
  115. *
  116. * \threadsafety It is safe to call this function from any thread.
  117. *
  118. * \since This function is available since SDL 3.0.0.
  119. */
  120. SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
  121. {
  122. return ( p && r && (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  123. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  124. }
  125. /**
  126. * Determine whether a rectangle has no area.
  127. *
  128. * A rectangle is considered "empty" for this function if `r` is NULL,
  129. * or if `r`'s width and/or height are <= 0.
  130. *
  131. * Note that this is a forced-inline function in a header, and not a public
  132. * API function available in the SDL library (which is to say, the code is
  133. * embedded in the calling program and the linker and dynamic loader will
  134. * not be able to find this function inside SDL itself).
  135. *
  136. * \param r the rectangle to test.
  137. * \returns SDL_TRUE if the rectangle is "empty", SDL_FALSE otherwise.
  138. *
  139. * \threadsafety It is safe to call this function from any thread.
  140. *
  141. * \since This function is available since SDL 3.0.0.
  142. */
  143. SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
  144. {
  145. return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
  146. }
  147. /**
  148. * Determine whether two rectangles are equal.
  149. *
  150. * Rectangles are considered equal if both are not NULL and each of their
  151. * x, y, width and height match.
  152. *
  153. * Note that this is a forced-inline function in a header, and not a public
  154. * API function available in the SDL library (which is to say, the code is
  155. * embedded in the calling program and the linker and dynamic loader will
  156. * not be able to find this function inside SDL itself).
  157. *
  158. * \param a the first rectangle to test.
  159. * \param b the second rectangle to test.
  160. * \returns SDL_TRUE if the rectangles are equal, SDL_FALSE otherwise.
  161. *
  162. * \threadsafety It is safe to call this function from any thread.
  163. *
  164. * \since This function is available since SDL 3.0.0.
  165. */
  166. SDL_FORCE_INLINE SDL_bool SDL_RectsEqual(const SDL_Rect *a, const SDL_Rect *b)
  167. {
  168. return (a && b && (a->x == b->x) && (a->y == b->y) &&
  169. (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
  170. }
  171. /**
  172. * Determine whether two rectangles intersect.
  173. *
  174. * If either pointer is NULL the function will return SDL_FALSE.
  175. *
  176. * \param A an SDL_Rect structure representing the first rectangle
  177. * \param B an SDL_Rect structure representing the second rectangle
  178. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  179. *
  180. * \threadsafety It is safe to call this function from any thread.
  181. *
  182. * \since This function is available since SDL 3.0.0.
  183. *
  184. * \sa SDL_GetRectIntersection
  185. */
  186. extern DECLSPEC SDL_bool SDLCALL SDL_HasRectIntersection(const SDL_Rect * A,
  187. const SDL_Rect * B);
  188. /**
  189. * Calculate the intersection of two rectangles.
  190. *
  191. * If `result` is NULL then this function will return SDL_FALSE.
  192. *
  193. * \param A an SDL_Rect structure representing the first rectangle
  194. * \param B an SDL_Rect structure representing the second rectangle
  195. * \param result an SDL_Rect structure filled in with the intersection of
  196. * rectangles `A` and `B`
  197. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  198. *
  199. * \since This function is available since SDL 3.0.0.
  200. *
  201. * \sa SDL_HasRectIntersection
  202. */
  203. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectIntersection(const SDL_Rect * A,
  204. const SDL_Rect * B,
  205. SDL_Rect * result);
  206. /**
  207. * Calculate the union of two rectangles.
  208. *
  209. * \param A an SDL_Rect structure representing the first rectangle
  210. * \param B an SDL_Rect structure representing the second rectangle
  211. * \param result an SDL_Rect structure filled in with the union of rectangles
  212. * `A` and `B`
  213. * \returns 0 on success or a negative error code on failure; call
  214. * SDL_GetError() for more information.
  215. *
  216. * \since This function is available since SDL 3.0.0.
  217. */
  218. extern DECLSPEC int SDLCALL SDL_GetRectUnion(const SDL_Rect * A,
  219. const SDL_Rect * B,
  220. SDL_Rect * result);
  221. /**
  222. * Calculate a minimal rectangle enclosing a set of points.
  223. *
  224. * If `clip` is not NULL then only points inside of the clipping rectangle are
  225. * considered.
  226. *
  227. * \param points an array of SDL_Point structures representing points to be
  228. * enclosed
  229. * \param count the number of structures in the `points` array
  230. * \param clip an SDL_Rect used for clipping or NULL to enclose all points
  231. * \param result an SDL_Rect structure filled in with the minimal enclosing
  232. * rectangle
  233. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  234. * points were outside of the clipping rectangle.
  235. *
  236. * \since This function is available since SDL 3.0.0.
  237. */
  238. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectEnclosingPoints(const SDL_Point * points,
  239. int count,
  240. const SDL_Rect * clip,
  241. SDL_Rect * result);
  242. /**
  243. * Calculate the intersection of a rectangle and line segment.
  244. *
  245. * This function is used to clip a line segment to a rectangle. A line segment
  246. * contained entirely within the rectangle or that does not intersect will
  247. * remain unchanged. A line segment that crosses the rectangle at either or
  248. * both ends will be clipped to the boundary of the rectangle and the new
  249. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  250. *
  251. * \param rect an SDL_Rect structure representing the rectangle to intersect
  252. * \param X1 a pointer to the starting X-coordinate of the line
  253. * \param Y1 a pointer to the starting Y-coordinate of the line
  254. * \param X2 a pointer to the ending X-coordinate of the line
  255. * \param Y2 a pointer to the ending Y-coordinate of the line
  256. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  257. *
  258. * \since This function is available since SDL 3.0.0.
  259. */
  260. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectAndLineIntersection(const SDL_Rect *
  261. rect, int *X1,
  262. int *Y1, int *X2,
  263. int *Y2);
  264. /* SDL_FRect versions... */
  265. /**
  266. * Determine whether a point resides inside a floating point rectangle.
  267. *
  268. * A point is considered part of a rectangle if both `p` and `r` are
  269. * not NULL, and `p`'s x and y coordinates are >= to the rectangle's
  270. * top left corner, and < the rectangle's x+w and y+h. So a 1x1 rectangle
  271. * considers point (0,0) as "inside" and (0,1) as not.
  272. *
  273. * Note that this is a forced-inline function in a header, and not a public
  274. * API function available in the SDL library (which is to say, the code is
  275. * embedded in the calling program and the linker and dynamic loader will
  276. * not be able to find this function inside SDL itself).
  277. *
  278. * \param p the point to test.
  279. * \param r the rectangle to test.
  280. * \returns SDL_TRUE if `p` is contained by `r`, SDL_FALSE otherwise.
  281. *
  282. * \threadsafety It is safe to call this function from any thread.
  283. *
  284. * \since This function is available since SDL 3.0.0.
  285. */
  286. SDL_FORCE_INLINE SDL_bool SDL_PointInRectFloat(const SDL_FPoint *p, const SDL_FRect *r)
  287. {
  288. return ( p && r && (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  289. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  290. }
  291. /**
  292. * Determine whether a floating point rectangle has no area.
  293. *
  294. * A rectangle is considered "empty" for this function if `r` is NULL,
  295. * or if `r`'s width and/or height are <= 0.0f.
  296. *
  297. * Note that this is a forced-inline function in a header, and not a public
  298. * API function available in the SDL library (which is to say, the code is
  299. * embedded in the calling program and the linker and dynamic loader will
  300. * not be able to find this function inside SDL itself).
  301. *
  302. * \param r the rectangle to test.
  303. * \returns SDL_TRUE if the rectangle is "empty", SDL_FALSE otherwise.
  304. *
  305. * \threadsafety It is safe to call this function from any thread.
  306. *
  307. * \since This function is available since SDL 3.0.0.
  308. */
  309. SDL_FORCE_INLINE SDL_bool SDL_RectEmptyFloat(const SDL_FRect *r)
  310. {
  311. return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
  312. }
  313. /**
  314. * Determine whether two floating point rectangles are equal, within some given epsilon.
  315. *
  316. * Rectangles are considered equal if both are not NULL and each of their
  317. * x, y, width and height are within `epsilon` of each other. If you don't
  318. * know what value to use for `epsilon`, you should call the
  319. * SDL_RectsEqualFloat function instead.
  320. *
  321. * Note that this is a forced-inline function in a header, and not a public
  322. * API function available in the SDL library (which is to say, the code is
  323. * embedded in the calling program and the linker and dynamic loader will
  324. * not be able to find this function inside SDL itself).
  325. *
  326. * \param a the first rectangle to test.
  327. * \param b the second rectangle to test.
  328. * \returns SDL_TRUE if the rectangles are equal, SDL_FALSE otherwise.
  329. *
  330. * \threadsafety It is safe to call this function from any thread.
  331. *
  332. * \since This function is available since SDL 3.0.0.
  333. *
  334. * \sa SDL_RectsEqualFloat
  335. */
  336. SDL_FORCE_INLINE SDL_bool SDL_RectsEqualEpsilon(const SDL_FRect *a, const SDL_FRect *b, const float epsilon)
  337. {
  338. return (a && b && ((a == b) ||
  339. ((SDL_fabsf(a->x - b->x) <= epsilon) &&
  340. (SDL_fabsf(a->y - b->y) <= epsilon) &&
  341. (SDL_fabsf(a->w - b->w) <= epsilon) &&
  342. (SDL_fabsf(a->h - b->h) <= epsilon))))
  343. ? SDL_TRUE : SDL_FALSE;
  344. }
  345. /**
  346. * Determine whether two floating point rectangles are equal, within a default epsilon.
  347. *
  348. * Rectangles are considered equal if both are not NULL and each of their
  349. * x, y, width and height are within SDL_FLT_EPSILON of each other. This is
  350. * often a reasonable way to compare two floating point rectangles and
  351. * deal with the slight precision variations in floating point calculations
  352. * that tend to pop up.
  353. *
  354. * Note that this is a forced-inline function in a header, and not a public
  355. * API function available in the SDL library (which is to say, the code is
  356. * embedded in the calling program and the linker and dynamic loader will
  357. * not be able to find this function inside SDL itself).
  358. *
  359. * \param a the first rectangle to test.
  360. * \param b the second rectangle to test.
  361. * \returns SDL_TRUE if the rectangles are equal, SDL_FALSE otherwise.
  362. *
  363. * \threadsafety It is safe to call this function from any thread.
  364. *
  365. * \since This function is available since SDL 3.0.0.
  366. *
  367. * \sa SDL_RectsEqualEpsilon
  368. */
  369. SDL_FORCE_INLINE SDL_bool SDL_RectsEqualFloat(const SDL_FRect *a, const SDL_FRect *b)
  370. {
  371. return SDL_RectsEqualEpsilon(a, b, SDL_FLT_EPSILON);
  372. }
  373. /**
  374. * Determine whether two rectangles intersect with float precision.
  375. *
  376. * If either pointer is NULL the function will return SDL_FALSE.
  377. *
  378. * \param A an SDL_FRect structure representing the first rectangle
  379. * \param B an SDL_FRect structure representing the second rectangle
  380. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  381. *
  382. * \since This function is available since SDL 3.0.0.
  383. *
  384. * \sa SDL_GetRectIntersection
  385. */
  386. extern DECLSPEC SDL_bool SDLCALL SDL_HasRectIntersectionFloat(const SDL_FRect * A,
  387. const SDL_FRect * B);
  388. /**
  389. * Calculate the intersection of two rectangles with float precision.
  390. *
  391. * If `result` is NULL then this function will return SDL_FALSE.
  392. *
  393. * \param A an SDL_FRect structure representing the first rectangle
  394. * \param B an SDL_FRect structure representing the second rectangle
  395. * \param result an SDL_FRect structure filled in with the intersection of
  396. * rectangles `A` and `B`
  397. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  398. *
  399. * \since This function is available since SDL 3.0.0.
  400. *
  401. * \sa SDL_HasRectIntersectionFloat
  402. */
  403. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectIntersectionFloat(const SDL_FRect * A,
  404. const SDL_FRect * B,
  405. SDL_FRect * result);
  406. /**
  407. * Calculate the union of two rectangles with float precision.
  408. *
  409. * \param A an SDL_FRect structure representing the first rectangle
  410. * \param B an SDL_FRect structure representing the second rectangle
  411. * \param result an SDL_FRect structure filled in with the union of rectangles
  412. * `A` and `B`
  413. * \returns 0 on success or a negative error code on failure; call
  414. * SDL_GetError() for more information.
  415. *
  416. * \since This function is available since SDL 3.0.0.
  417. */
  418. extern DECLSPEC int SDLCALL SDL_GetRectUnionFloat(const SDL_FRect * A,
  419. const SDL_FRect * B,
  420. SDL_FRect * result);
  421. /**
  422. * Calculate a minimal rectangle enclosing a set of points with float
  423. * precision.
  424. *
  425. * If `clip` is not NULL then only points inside of the clipping rectangle are
  426. * considered.
  427. *
  428. * \param points an array of SDL_FPoint structures representing points to be
  429. * enclosed
  430. * \param count the number of structures in the `points` array
  431. * \param clip an SDL_FRect used for clipping or NULL to enclose all points
  432. * \param result an SDL_FRect structure filled in with the minimal enclosing
  433. * rectangle
  434. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  435. * points were outside of the clipping rectangle.
  436. *
  437. * \since This function is available since SDL 3.0.0.
  438. */
  439. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectEnclosingPointsFloat(const SDL_FPoint * points,
  440. int count,
  441. const SDL_FRect * clip,
  442. SDL_FRect * result);
  443. /**
  444. * Calculate the intersection of a rectangle and line segment with float
  445. * precision.
  446. *
  447. * This function is used to clip a line segment to a rectangle. A line segment
  448. * contained entirely within the rectangle or that does not intersect will
  449. * remain unchanged. A line segment that crosses the rectangle at either or
  450. * both ends will be clipped to the boundary of the rectangle and the new
  451. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  452. *
  453. * \param rect an SDL_FRect structure representing the rectangle to intersect
  454. * \param X1 a pointer to the starting X-coordinate of the line
  455. * \param Y1 a pointer to the starting Y-coordinate of the line
  456. * \param X2 a pointer to the ending X-coordinate of the line
  457. * \param Y2 a pointer to the ending Y-coordinate of the line
  458. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  459. *
  460. * \since This function is available since SDL 3.0.0.
  461. */
  462. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectAndLineIntersectionFloat(const SDL_FRect *
  463. rect, float *X1,
  464. float *Y1, float *X2,
  465. float *Y2);
  466. /* Ends C function definitions when using C++ */
  467. #ifdef __cplusplus
  468. }
  469. #endif
  470. #include <SDL3/SDL_close_code.h>
  471. #endif /* SDL_rect_h_ */