SDL_rect.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_FRectEmpty
  49. * \sa SDL_FRectEquals
  50. * \sa SDL_HasIntersectionF
  51. * \sa SDL_IntersectFRect
  52. * \sa SDL_UnionFRect
  53. * \sa SDL_EncloseFPoints
  54. * \sa SDL_PointInFRect
  55. */
  56. typedef struct SDL_FPoint
  57. {
  58. float x;
  59. float y;
  60. } SDL_FPoint;
  61. /**
  62. * A rectangle, with the origin at the upper left (integer).
  63. *
  64. * \sa SDL_RectEmpty
  65. * \sa SDL_RectEquals
  66. * \sa SDL_HasIntersection
  67. * \sa SDL_IntersectRect
  68. * \sa SDL_UnionRect
  69. * \sa SDL_EnclosePoints
  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 (floating point).
  78. */
  79. typedef struct SDL_FRect
  80. {
  81. float x;
  82. float y;
  83. float w;
  84. float h;
  85. } SDL_FRect;
  86. /**
  87. * Returns true if point resides inside a rectangle.
  88. */
  89. SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
  90. {
  91. return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  92. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  93. }
  94. /**
  95. * Returns true if the rectangle has no area.
  96. */
  97. SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
  98. {
  99. return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
  100. }
  101. /**
  102. * Returns true if the two rectangles are equal.
  103. */
  104. SDL_FORCE_INLINE SDL_bool SDL_RectEquals(const SDL_Rect *a, const SDL_Rect *b)
  105. {
  106. return (a && b && (a->x == b->x) && (a->y == b->y) &&
  107. (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
  108. }
  109. /**
  110. * Determine whether two rectangles intersect.
  111. *
  112. * If either pointer is NULL the function will return SDL_FALSE.
  113. *
  114. * \param A an SDL_Rect structure representing the first rectangle
  115. * \param B an SDL_Rect structure representing the second rectangle
  116. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  117. *
  118. * \since This function is available since SDL 2.0.0.
  119. *
  120. * \sa SDL_IntersectRect
  121. */
  122. extern DECLSPEC SDL_bool SDLCALL SDL_HasIntersection(const SDL_Rect * A,
  123. const SDL_Rect * B);
  124. /**
  125. * Calculate the intersection of two rectangles.
  126. *
  127. * If `result` is NULL then this function will return SDL_FALSE.
  128. *
  129. * \param A an SDL_Rect structure representing the first rectangle
  130. * \param B an SDL_Rect structure representing the second rectangle
  131. * \param result an SDL_Rect structure filled in with the intersection of
  132. * rectangles `A` and `B`
  133. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  134. *
  135. * \since This function is available since SDL 2.0.0.
  136. *
  137. * \sa SDL_HasIntersection
  138. */
  139. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRect(const SDL_Rect * A,
  140. const SDL_Rect * B,
  141. SDL_Rect * result);
  142. /**
  143. * Calculate the union of two rectangles.
  144. *
  145. * \param A an SDL_Rect structure representing the first rectangle
  146. * \param B an SDL_Rect structure representing the second rectangle
  147. * \param result an SDL_Rect structure filled in with the union of rectangles
  148. * `A` and `B`
  149. *
  150. * \since This function is available since SDL 2.0.0.
  151. */
  152. extern DECLSPEC void SDLCALL SDL_UnionRect(const SDL_Rect * A,
  153. const SDL_Rect * B,
  154. SDL_Rect * result);
  155. /**
  156. * Calculate a minimal rectangle enclosing a set of points.
  157. *
  158. * If `clip` is not NULL then only points inside of the clipping rectangle are
  159. * considered.
  160. *
  161. * \param points an array of SDL_Point structures representing points to be
  162. * enclosed
  163. * \param count the number of structures in the `points` array
  164. * \param clip an SDL_Rect used for clipping or NULL to enclose all points
  165. * \param result an SDL_Rect structure filled in with the minimal enclosing
  166. * rectangle
  167. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  168. * points were outside of the clipping rectangle.
  169. *
  170. * \since This function is available since SDL 2.0.0.
  171. */
  172. extern DECLSPEC SDL_bool SDLCALL SDL_EnclosePoints(const SDL_Point * points,
  173. int count,
  174. const SDL_Rect * clip,
  175. SDL_Rect * result);
  176. /**
  177. * Calculate the intersection of a rectangle and line segment.
  178. *
  179. * This function is used to clip a line segment to a rectangle. A line segment
  180. * contained entirely within the rectangle or that does not intersect will
  181. * remain unchanged. A line segment that crosses the rectangle at either or
  182. * both ends will be clipped to the boundary of the rectangle and the new
  183. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  184. *
  185. * \param rect an SDL_Rect structure representing the rectangle to intersect
  186. * \param X1 a pointer to the starting X-coordinate of the line
  187. * \param Y1 a pointer to the starting Y-coordinate of the line
  188. * \param X2 a pointer to the ending X-coordinate of the line
  189. * \param Y2 a pointer to the ending Y-coordinate of the line
  190. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  191. *
  192. * \since This function is available since SDL 2.0.0.
  193. */
  194. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRectAndLine(const SDL_Rect *
  195. rect, int *X1,
  196. int *Y1, int *X2,
  197. int *Y2);
  198. /* SDL_FRect versions... */
  199. /**
  200. * Returns true if point resides inside a rectangle.
  201. */
  202. SDL_FORCE_INLINE SDL_bool SDL_PointInFRect(const SDL_FPoint *p, const SDL_FRect *r)
  203. {
  204. return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  205. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  206. }
  207. /**
  208. * Returns true if the rectangle has no area.
  209. */
  210. SDL_FORCE_INLINE SDL_bool SDL_FRectEmpty(const SDL_FRect *r)
  211. {
  212. return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
  213. }
  214. /**
  215. * Returns true if the two rectangles are equal.
  216. */
  217. SDL_FORCE_INLINE SDL_bool SDL_FRectEquals(const SDL_FRect *a, const SDL_FRect *b)
  218. {
  219. return (a && b && (a->x == b->x) && (a->y == b->y) &&
  220. (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
  221. }
  222. /**
  223. * Determine whether two rectangles intersect with float precision.
  224. *
  225. * If either pointer is NULL the function will return SDL_FALSE.
  226. *
  227. * \param A an SDL_FRect structure representing the first rectangle
  228. * \param B an SDL_FRect structure representing the second rectangle
  229. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  230. *
  231. * \since This function is available since SDL 2.0.22.
  232. *
  233. * \sa SDL_IntersectRect
  234. */
  235. extern DECLSPEC SDL_bool SDLCALL SDL_HasIntersectionF(const SDL_FRect * A,
  236. const SDL_FRect * B);
  237. /**
  238. * Calculate the intersection of two rectangles with float precision.
  239. *
  240. * If `result` is NULL then this function will return SDL_FALSE.
  241. *
  242. * \param A an SDL_FRect structure representing the first rectangle
  243. * \param B an SDL_FRect structure representing the second rectangle
  244. * \param result an SDL_FRect structure filled in with the intersection of
  245. * rectangles `A` and `B`
  246. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  247. *
  248. * \since This function is available since SDL 2.0.22.
  249. *
  250. * \sa SDL_HasIntersectionF
  251. */
  252. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectFRect(const SDL_FRect * A,
  253. const SDL_FRect * B,
  254. SDL_FRect * result);
  255. /**
  256. * Calculate the union of two rectangles with float precision.
  257. *
  258. * \param A an SDL_FRect structure representing the first rectangle
  259. * \param B an SDL_FRect structure representing the second rectangle
  260. * \param result an SDL_FRect structure filled in with the union of rectangles
  261. * `A` and `B`
  262. *
  263. * \since This function is available since SDL 2.0.22.
  264. */
  265. extern DECLSPEC void SDLCALL SDL_UnionFRect(const SDL_FRect * A,
  266. const SDL_FRect * B,
  267. SDL_FRect * result);
  268. /**
  269. * Calculate a minimal rectangle enclosing a set of points with float
  270. * precision.
  271. *
  272. * If `clip` is not NULL then only points inside of the clipping rectangle are
  273. * considered.
  274. *
  275. * \param points an array of SDL_FPoint structures representing points to be
  276. * enclosed
  277. * \param count the number of structures in the `points` array
  278. * \param clip an SDL_FRect used for clipping or NULL to enclose all points
  279. * \param result an SDL_FRect structure filled in with the minimal enclosing
  280. * rectangle
  281. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  282. * points were outside of the clipping rectangle.
  283. *
  284. * \since This function is available since SDL 2.0.22.
  285. */
  286. extern DECLSPEC SDL_bool SDLCALL SDL_EncloseFPoints(const SDL_FPoint * points,
  287. int count,
  288. const SDL_FRect * clip,
  289. SDL_FRect * result);
  290. /**
  291. * Calculate the intersection of a rectangle and line segment with float
  292. * precision.
  293. *
  294. * This function is used to clip a line segment to a rectangle. A line segment
  295. * contained entirely within the rectangle or that does not intersect will
  296. * remain unchanged. A line segment that crosses the rectangle at either or
  297. * both ends will be clipped to the boundary of the rectangle and the new
  298. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  299. *
  300. * \param rect an SDL_FRect structure representing the rectangle to intersect
  301. * \param X1 a pointer to the starting X-coordinate of the line
  302. * \param Y1 a pointer to the starting Y-coordinate of the line
  303. * \param X2 a pointer to the ending X-coordinate of the line
  304. * \param Y2 a pointer to the ending Y-coordinate of the line
  305. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  306. *
  307. * \since This function is available since SDL 2.0.22.
  308. */
  309. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectFRectAndLine(const SDL_FRect *
  310. rect, float *X1,
  311. float *Y1, float *X2,
  312. float *Y2);
  313. /* Ends C function definitions when using C++ */
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317. #include "close_code.h"
  318. #endif /* SDL_rect_h_ */
  319. /* vi: set ts=4 sw=4 expandtab: */