SDL_rect.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "../SDL_internal.h"
  19. #include "SDL_rect.h"
  20. #include "SDL_rect_c.h"
  21. /* There's no float version of this at the moment, because it's not a public API
  22. and internally we only need the int version. */
  23. SDL_bool SDL_GetSpanEnclosingRect(int width, int height,
  24. int numrects, const SDL_Rect *rects, SDL_Rect *span)
  25. {
  26. int i;
  27. int span_y1, span_y2;
  28. int rect_y1, rect_y2;
  29. if (width < 1) {
  30. SDL_InvalidParamError("width");
  31. return SDL_FALSE;
  32. } else if (height < 1) {
  33. SDL_InvalidParamError("height");
  34. return SDL_FALSE;
  35. } else if (rects == NULL) {
  36. SDL_InvalidParamError("rects");
  37. return SDL_FALSE;
  38. } else if (span == NULL) {
  39. SDL_InvalidParamError("span");
  40. return SDL_FALSE;
  41. } else if (numrects < 1) {
  42. SDL_InvalidParamError("numrects");
  43. return SDL_FALSE;
  44. }
  45. /* Initialize to empty rect */
  46. span_y1 = height;
  47. span_y2 = 0;
  48. for (i = 0; i < numrects; ++i) {
  49. rect_y1 = rects[i].y;
  50. rect_y2 = rect_y1 + rects[i].h;
  51. /* Clip out of bounds rectangles, and expand span rect */
  52. if (rect_y1 < 0) {
  53. span_y1 = 0;
  54. } else if (rect_y1 < span_y1) {
  55. span_y1 = rect_y1;
  56. }
  57. if (rect_y2 > height) {
  58. span_y2 = height;
  59. } else if (rect_y2 > span_y2) {
  60. span_y2 = rect_y2;
  61. }
  62. }
  63. if (span_y2 > span_y1) {
  64. span->x = 0;
  65. span->y = span_y1;
  66. span->w = width;
  67. span->h = (span_y2 - span_y1);
  68. return SDL_TRUE;
  69. }
  70. return SDL_FALSE;
  71. }
  72. /* For use with the Cohen-Sutherland algorithm for line clipping, in SDL_rect_impl.h */
  73. #define CODE_BOTTOM 1
  74. #define CODE_TOP 2
  75. #define CODE_LEFT 4
  76. #define CODE_RIGHT 8
  77. /* Same code twice, for float and int versions... */
  78. #define RECTTYPE SDL_Rect
  79. #define POINTTYPE SDL_Point
  80. #define SCALARTYPE int
  81. #define COMPUTEOUTCODE ComputeOutCode
  82. #define SDL_HASINTERSECTION SDL_HasIntersection
  83. #define SDL_INTERSECTRECT SDL_IntersectRect
  84. #define SDL_RECTEMPTY SDL_RectEmpty
  85. #define SDL_UNIONRECT SDL_UnionRect
  86. #define SDL_ENCLOSEPOINTS SDL_EnclosePoints
  87. #define SDL_INTERSECTRECTANDLINE SDL_IntersectRectAndLine
  88. #include "SDL_rect_impl.h"
  89. #define RECTTYPE SDL_FRect
  90. #define POINTTYPE SDL_FPoint
  91. #define SCALARTYPE float
  92. #define COMPUTEOUTCODE ComputeOutCodeF
  93. #define SDL_HASINTERSECTION SDL_HasIntersectionF
  94. #define SDL_INTERSECTRECT SDL_IntersectFRect
  95. #define SDL_RECTEMPTY SDL_FRectEmpty
  96. #define SDL_UNIONRECT SDL_UnionFRect
  97. #define SDL_ENCLOSEPOINTS SDL_EncloseFPoints
  98. #define SDL_INTERSECTRECTANDLINE SDL_IntersectFRectAndLine
  99. #include "SDL_rect_impl.h"
  100. /* vi: set ts=4 sw=4 expandtab: */