SDL_rect.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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_c.h"
  20. /* There's no float version of this at the moment, because it's not a public API
  21. and internally we only need the int version. */
  22. bool SDL_GetSpanEnclosingRect(int width, int height,
  23. int numrects, const SDL_Rect *rects, SDL_Rect *span)
  24. {
  25. int i;
  26. int span_y1, span_y2;
  27. int rect_y1, rect_y2;
  28. if (width < 1) {
  29. SDL_InvalidParamError("width");
  30. return false;
  31. } else if (height < 1) {
  32. SDL_InvalidParamError("height");
  33. return false;
  34. } else if (!rects) {
  35. SDL_InvalidParamError("rects");
  36. return false;
  37. } else if (!span) {
  38. SDL_InvalidParamError("span");
  39. return false;
  40. } else if (numrects < 1) {
  41. SDL_InvalidParamError("numrects");
  42. return false;
  43. }
  44. // Initialize to empty rect
  45. span_y1 = height;
  46. span_y2 = 0;
  47. for (i = 0; i < numrects; ++i) {
  48. rect_y1 = rects[i].y;
  49. rect_y2 = rect_y1 + rects[i].h;
  50. // Clip out of bounds rectangles, and expand span rect
  51. if (rect_y1 < 0) {
  52. span_y1 = 0;
  53. } else if (rect_y1 < span_y1) {
  54. span_y1 = rect_y1;
  55. }
  56. if (rect_y2 > height) {
  57. span_y2 = height;
  58. } else if (rect_y2 > span_y2) {
  59. span_y2 = rect_y2;
  60. }
  61. }
  62. if (span_y2 > span_y1) {
  63. span->x = 0;
  64. span->y = span_y1;
  65. span->w = width;
  66. span->h = (span_y2 - span_y1);
  67. return true;
  68. }
  69. return false;
  70. }
  71. // For use with the Cohen-Sutherland algorithm for line clipping, in SDL_rect_impl.h
  72. #define CODE_BOTTOM 1
  73. #define CODE_TOP 2
  74. #define CODE_LEFT 4
  75. #define CODE_RIGHT 8
  76. // Same code twice, for float and int versions...
  77. #define RECTTYPE SDL_Rect
  78. #define POINTTYPE SDL_Point
  79. #define SCALARTYPE int
  80. #define BIGSCALARTYPE Sint64
  81. #define COMPUTEOUTCODE ComputeOutCode
  82. #define ENCLOSEPOINTS_EPSILON 1
  83. #define SDL_RECT_CAN_OVERFLOW SDL_RectCanOverflow
  84. #define SDL_HASINTERSECTION SDL_HasRectIntersection
  85. #define SDL_INTERSECTRECT SDL_GetRectIntersection
  86. #define SDL_RECTEMPTY SDL_RectEmpty
  87. #define SDL_UNIONRECT SDL_GetRectUnion
  88. #define SDL_ENCLOSEPOINTS SDL_GetRectEnclosingPoints
  89. #define SDL_INTERSECTRECTANDLINE SDL_GetRectAndLineIntersection
  90. #include "SDL_rect_impl.h"
  91. #define RECTTYPE SDL_FRect
  92. #define POINTTYPE SDL_FPoint
  93. #define SCALARTYPE float
  94. #define BIGSCALARTYPE double
  95. #define COMPUTEOUTCODE ComputeOutCodeFloat
  96. #define ENCLOSEPOINTS_EPSILON 0.0f
  97. #define SDL_RECT_CAN_OVERFLOW SDL_RectCanOverflowFloat
  98. #define SDL_HASINTERSECTION SDL_HasRectIntersectionFloat
  99. #define SDL_INTERSECTRECT SDL_GetRectIntersectionFloat
  100. #define SDL_RECTEMPTY SDL_RectEmptyFloat
  101. #define SDL_UNIONRECT SDL_GetRectUnionFloat
  102. #define SDL_ENCLOSEPOINTS SDL_GetRectEnclosingPointsFloat
  103. #define SDL_INTERSECTRECTANDLINE SDL_GetRectAndLineIntersectionFloat
  104. #include "SDL_rect_impl.h"