SDL_rect.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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_EnclosePoints
  49. * \sa SDL_PointInRect
  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_UnionRect
  64. * \sa SDL_EnclosePoints
  65. */
  66. typedef struct SDL_Rect
  67. {
  68. int x, y;
  69. int w, h;
  70. } SDL_Rect;
  71. /**
  72. * A rectangle, with the origin at the upper left (floating point).
  73. */
  74. typedef struct SDL_FRect
  75. {
  76. float x;
  77. float y;
  78. float w;
  79. float h;
  80. } SDL_FRect;
  81. /**
  82. * Returns true if point resides inside a rectangle.
  83. */
  84. SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
  85. {
  86. return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  87. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  88. }
  89. /**
  90. * Returns true if the rectangle has no area.
  91. */
  92. SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
  93. {
  94. return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
  95. }
  96. /**
  97. * Returns true if the two rectangles are equal.
  98. */
  99. SDL_FORCE_INLINE SDL_bool SDL_RectEquals(const SDL_Rect *a, const SDL_Rect *b)
  100. {
  101. return (a && b && (a->x == b->x) && (a->y == b->y) &&
  102. (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
  103. }
  104. /**
  105. * Determine whether two rectangles intersect.
  106. *
  107. * If either pointer is NULL the function will return SDL_FALSE.
  108. *
  109. * \param A an SDL_Rect structure representing the first rectangle
  110. * \param B an SDL_Rect structure representing the second rectangle
  111. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  112. *
  113. * \since This function is available since SDL 2.0.0.
  114. *
  115. * \sa SDL_IntersectRect
  116. */
  117. extern DECLSPEC SDL_bool SDLCALL SDL_HasIntersection(const SDL_Rect * A,
  118. const SDL_Rect * B);
  119. /**
  120. * Calculate the intersection of two rectangles.
  121. *
  122. * If `result` is NULL then this function will return SDL_FALSE.
  123. *
  124. * \param A an SDL_Rect structure representing the first rectangle
  125. * \param B an SDL_Rect structure representing the second rectangle
  126. * \param result an SDL_Rect structure filled in with the intersection of
  127. * rectangles `A` and `B`
  128. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  129. *
  130. * \since This function is available since SDL 2.0.0.
  131. *
  132. * \sa SDL_HasIntersection
  133. */
  134. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRect(const SDL_Rect * A,
  135. const SDL_Rect * B,
  136. SDL_Rect * result);
  137. /**
  138. * Calculate the union of two rectangles.
  139. *
  140. * \param A an SDL_Rect structure representing the first rectangle
  141. * \param B an SDL_Rect structure representing the second rectangle
  142. * \param result an SDL_Rect structure filled in with the union of rectangles
  143. * `A` and `B`
  144. *
  145. * \since This function is available since SDL 2.0.0.
  146. */
  147. extern DECLSPEC void SDLCALL SDL_UnionRect(const SDL_Rect * A,
  148. const SDL_Rect * B,
  149. SDL_Rect * result);
  150. /**
  151. * Calculate a minimal rectangle enclosing a set of points.
  152. *
  153. * If `clip` is not NULL then only points inside of the clipping rectangle are
  154. * considered.
  155. *
  156. * \param points an array of SDL_Point structures representing points to be
  157. * enclosed
  158. * \param count the number of structures in the `points` array
  159. * \param clip an SDL_Rect used for clipping or NULL to enclose all points
  160. * \param result an SDL_Rect structure filled in with the minimal enclosing
  161. * rectangle
  162. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  163. * points were outside of the clipping rectangle.
  164. *
  165. * \since This function is available since SDL 2.0.0.
  166. */
  167. extern DECLSPEC SDL_bool SDLCALL SDL_EnclosePoints(const SDL_Point * points,
  168. int count,
  169. const SDL_Rect * clip,
  170. SDL_Rect * result);
  171. /**
  172. * Calculate the intersection of a rectangle and line segment.
  173. *
  174. * This function is used to clip a line segment to a rectangle. A line segment
  175. * contained entirely within the rectangle or that does not intersect will
  176. * remain unchanged. A line segment that crosses the rectangle at either or
  177. * both ends will be clipped to the boundary of the rectangle and the new
  178. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  179. *
  180. * \param rect an SDL_Rect structure representing the rectangle to intersect
  181. * \param X1 a pointer to the starting X-coordinate of the line
  182. * \param Y1 a pointer to the starting Y-coordinate of the line
  183. * \param X2 a pointer to the ending X-coordinate of the line
  184. * \param Y2 a pointer to the ending Y-coordinate of the line
  185. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  186. *
  187. * \since This function is available since SDL 2.0.0.
  188. */
  189. extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRectAndLine(const SDL_Rect *
  190. rect, int *X1,
  191. int *Y1, int *X2,
  192. int *Y2);
  193. /* Ends C function definitions when using C++ */
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #include "close_code.h"
  198. #endif /* SDL_rect_h_ */
  199. /* vi: set ts=4 sw=4 expandtab: */