🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
13 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  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 "SDL_stdinc.h"
  26. #include "SDL_error.h"
  27. #include "SDL_pixels.h"
  28. #include "SDL_rwops.h"
  29. #include "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_EnclosePoints
  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_EncloseFPoints
  49. * \sa SDL_PointInFRect
  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_RectEquals
  61. * \sa SDL_HasIntersection
  62. * \sa SDL_IntersectRect
  63. * \sa SDL_IntersectRectAndLine
  64. * \sa SDL_UnionRect
  65. * \sa SDL_EnclosePoints
  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_FRectEmpty
  76. * \sa SDL_FRectEquals
  77. * \sa SDL_FRectEqualsEpsilon
  78. * \sa SDL_HasIntersectionF
  79. * \sa SDL_IntersectFRect
  80. * \sa SDL_IntersectFRectAndLine
  81. * \sa SDL_UnionFRect
  82. * \sa SDL_EncloseFPoints
  83. * \sa SDL_PointInFRect
  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_RectEquals(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 2.0.0.
  125. *
  126. * \sa SDL_IntersectRect
  127. */
  128. extern DECLSPEC SDL_bool SDLCALL SDL_HasIntersection(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 2.0.0.
  142. *
  143. * \sa SDL_HasIntersection
  144. */
  145. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRect(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. *
  156. * \since This function is available since SDL 2.0.0.
  157. */
  158. extern DECLSPEC void SDLCALL SDL_UnionRect(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 2.0.0.
  177. */
  178. extern DECLSPEC SDL_bool SDLCALL SDL_EnclosePoints(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 2.0.0.
  199. */
  200. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRectAndLine(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_PointInFRect(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. SDL_FORCE_INLINE SDL_bool SDL_FRectEmpty(const SDL_FRect *r)
  217. {
  218. return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
  219. }
  220. /**
  221. * Returns true if the two rectangles are equal, within some given epsilon.
  222. *
  223. * \since This function is available since SDL 2.0.22.
  224. */
  225. SDL_FORCE_INLINE SDL_bool SDL_FRectEqualsEpsilon(const SDL_FRect *a, const SDL_FRect *b, const float epsilon)
  226. {
  227. return (a && b && ((a == b) ||
  228. ((SDL_fabsf(a->x - b->x) <= epsilon) &&
  229. (SDL_fabsf(a->y - b->y) <= epsilon) &&
  230. (SDL_fabsf(a->w - b->w) <= epsilon) &&
  231. (SDL_fabsf(a->h - b->h) <= epsilon))))
  232. ? SDL_TRUE : SDL_FALSE;
  233. }
  234. /**
  235. * Returns true if the two rectangles are equal, using a default epsilon.
  236. *
  237. * \since This function is available since SDL 2.0.22.
  238. */
  239. SDL_FORCE_INLINE SDL_bool SDL_FRectEquals(const SDL_FRect *a, const SDL_FRect *b)
  240. {
  241. return SDL_FRectEqualsEpsilon(a, b, SDL_FLT_EPSILON);
  242. }
  243. /**
  244. * Determine whether two rectangles intersect with float precision.
  245. *
  246. * If either pointer is NULL the function will return SDL_FALSE.
  247. *
  248. * \param A an SDL_FRect structure representing the first rectangle
  249. * \param B an SDL_FRect structure representing the second rectangle
  250. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  251. *
  252. * \since This function is available since SDL 2.0.22.
  253. *
  254. * \sa SDL_IntersectRect
  255. */
  256. extern DECLSPEC SDL_bool SDLCALL SDL_HasIntersectionF(const SDL_FRect * A,
  257. const SDL_FRect * B);
  258. /**
  259. * Calculate the intersection of two rectangles with float precision.
  260. *
  261. * If `result` is NULL then this function will return SDL_FALSE.
  262. *
  263. * \param A an SDL_FRect structure representing the first rectangle
  264. * \param B an SDL_FRect structure representing the second rectangle
  265. * \param result an SDL_FRect structure filled in with the intersection of
  266. * rectangles `A` and `B`
  267. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  268. *
  269. * \since This function is available since SDL 2.0.22.
  270. *
  271. * \sa SDL_HasIntersectionF
  272. */
  273. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectFRect(const SDL_FRect * A,
  274. const SDL_FRect * B,
  275. SDL_FRect * result);
  276. /**
  277. * Calculate the union of two rectangles with float precision.
  278. *
  279. * \param A an SDL_FRect structure representing the first rectangle
  280. * \param B an SDL_FRect structure representing the second rectangle
  281. * \param result an SDL_FRect structure filled in with the union of rectangles
  282. * `A` and `B`
  283. *
  284. * \since This function is available since SDL 2.0.22.
  285. */
  286. extern DECLSPEC void SDLCALL SDL_UnionFRect(const SDL_FRect * A,
  287. const SDL_FRect * B,
  288. SDL_FRect * result);
  289. /**
  290. * Calculate a minimal rectangle enclosing a set of points with float
  291. * precision.
  292. *
  293. * If `clip` is not NULL then only points inside of the clipping rectangle are
  294. * considered.
  295. *
  296. * \param points an array of SDL_FPoint structures representing points to be
  297. * enclosed
  298. * \param count the number of structures in the `points` array
  299. * \param clip an SDL_FRect used for clipping or NULL to enclose all points
  300. * \param result an SDL_FRect structure filled in with the minimal enclosing
  301. * rectangle
  302. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  303. * points were outside of the clipping rectangle.
  304. *
  305. * \since This function is available since SDL 2.0.22.
  306. */
  307. extern DECLSPEC SDL_bool SDLCALL SDL_EncloseFPoints(const SDL_FPoint * points,
  308. int count,
  309. const SDL_FRect * clip,
  310. SDL_FRect * result);
  311. /**
  312. * Calculate the intersection of a rectangle and line segment with float
  313. * precision.
  314. *
  315. * This function is used to clip a line segment to a rectangle. A line segment
  316. * contained entirely within the rectangle or that does not intersect will
  317. * remain unchanged. A line segment that crosses the rectangle at either or
  318. * both ends will be clipped to the boundary of the rectangle and the new
  319. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  320. *
  321. * \param rect an SDL_FRect structure representing the rectangle to intersect
  322. * \param X1 a pointer to the starting X-coordinate of the line
  323. * \param Y1 a pointer to the starting Y-coordinate of the line
  324. * \param X2 a pointer to the ending X-coordinate of the line
  325. * \param Y2 a pointer to the ending Y-coordinate of the line
  326. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  327. *
  328. * \since This function is available since SDL 2.0.22.
  329. */
  330. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectFRectAndLine(const SDL_FRect *
  331. rect, float *X1,
  332. float *Y1, float *X2,
  333. float *Y2);
  334. /* Ends C function definitions when using C++ */
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #include "close_code.h"
  339. #endif /* SDL_rect_h_ */
  340. /* vi: set ts=4 sw=4 expandtab: */