src/video/SDL_rect.c
author Sam Lantinga
Mon, 19 Jan 2009 03:28:23 +0000
changeset 3046 47965eacde88
parent 3004 f3d7226a8dfd
child 3536 0267b8b1595c
permissions -rw-r--r--
Fix recommended by Eddy L O Jansson
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_video.h"
    25 #include "SDL_rect_c.h"
    26 
    27 SDL_bool
    28 SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
    29 {
    30     int Amin, Amax, Bmin, Bmax;
    31 
    32     /* Horizontal intersection */
    33     Amin = A->x;
    34     Amax = Amin + A->w;
    35     Bmin = B->x;
    36     Bmax = Bmin + B->w;
    37     if (Bmin > Amin)
    38         Amin = Bmin;
    39     if (Bmax < Amax)
    40         Amax = Bmax;
    41     if (Amax <= Amin)
    42         return SDL_FALSE;
    43 
    44     /* Vertical intersection */
    45     Amin = A->y;
    46     Amax = Amin + A->h;
    47     Bmin = B->y;
    48     Bmax = Bmin + B->h;
    49     if (Bmin > Amin)
    50         Amin = Bmin;
    51     if (Bmax < Amax)
    52         Amax = Bmax;
    53     if (Amax <= Amin)
    54         return SDL_FALSE;
    55 
    56     return SDL_TRUE;
    57 }
    58 
    59 SDL_bool
    60 SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
    61 {
    62     int Amin, Amax, Bmin, Bmax;
    63 
    64     /* Horizontal intersection */
    65     Amin = A->x;
    66     Amax = Amin + A->w;
    67     Bmin = B->x;
    68     Bmax = Bmin + B->w;
    69     if (Bmin > Amin)
    70         Amin = Bmin;
    71     result->x = Amin;
    72     if (Bmax < Amax)
    73         Amax = Bmax;
    74     result->w = Amax - Amin;
    75 
    76     /* Vertical intersection */
    77     Amin = A->y;
    78     Amax = Amin + A->h;
    79     Bmin = B->y;
    80     Bmax = Bmin + B->h;
    81     if (Bmin > Amin)
    82         Amin = Bmin;
    83     result->y = Amin;
    84     if (Bmax < Amax)
    85         Amax = Bmax;
    86     result->h = Amax - Amin;
    87 
    88     return !SDL_RectEmpty(result);
    89 }
    90 
    91 void
    92 SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
    93 {
    94     int Amin, Amax, Bmin, Bmax;
    95 
    96     /* Horizontal union */
    97     Amin = A->x;
    98     Amax = Amin + A->w;
    99     Bmin = B->x;
   100     Bmax = Bmin + B->w;
   101     if (Bmin < Amin)
   102         Amin = Bmin;
   103     result->x = Amin;
   104     if (Bmax > Amax)
   105         Amax = Bmax;
   106     result->w = Amax - Amin;
   107 
   108     /* Vertical intersection */
   109     Amin = A->y;
   110     Amax = Amin + A->h;
   111     Bmin = B->y;
   112     Bmax = Bmin + B->h;
   113     if (Bmin < Amin)
   114         Amin = Bmin;
   115     result->y = Amin;
   116     if (Bmax > Amax)
   117         Amax = Bmax;
   118     result->h = Amax - Amin;
   119 }
   120 
   121 SDL_bool
   122 SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
   123                          int *Y2)
   124 {
   125     int x1, y1;
   126     int x2, y2;
   127     int rectx1;
   128     int recty1;
   129     int rectx2;
   130     int recty2;
   131 
   132     if (!rect || !X1 || !Y1 || !X2 || !Y2) {
   133         return SDL_FALSE;
   134     }
   135 
   136     x1 = *X1;
   137     y1 = *Y1;
   138     x2 = *X2;
   139     y2 = *Y2;
   140     rectx1 = rect->x;
   141     recty1 = rect->y;
   142     rectx2 = rect->x + rect->w - 1;
   143     recty2 = rect->y + rect->h - 1;
   144 
   145     /* Check to see if entire line is inside rect */
   146     if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
   147         y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
   148         return SDL_TRUE;
   149     }
   150 
   151     /* Check to see if entire line is to one side of rect */
   152     if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
   153         (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
   154         return SDL_FALSE;
   155     }
   156 
   157     if (y1 == y2) {
   158         /* Horizontal line, easy to clip */
   159         if (x1 < rectx1) {
   160             *X1 = rectx1;
   161         } else if (x1 > rectx2) {
   162             *X1 = rectx2;
   163         }
   164         if (x2 < rectx1) {
   165             *X2 = rectx1;
   166         } else if (x2 > rectx2) {
   167             *X2 = rectx2;
   168         }
   169         return SDL_TRUE;
   170     }
   171 
   172     if (x1 == x2) {
   173         /* Vertical line, easy to clip */
   174         if (y1 < recty1) {
   175             *Y1 = recty1;
   176         } else if (y1 > recty2) {
   177             *Y1 = recty2;
   178         }
   179         if (y2 < recty1) {
   180             *Y2 = recty1;
   181         } else if (y2 > recty2) {
   182             *Y2 = recty2;
   183         }
   184         return SDL_TRUE;
   185     }
   186 
   187     else {
   188         /* The task of clipping a line with finite slope ratios in a fixed-
   189          * precision coordinate space is not as immediately simple as it is
   190          * with coordinates of arbitrary precision. If the ratio of slopes
   191          * between the input line segment and the result line segment is not
   192          * a whole number, you have in fact *moved* the line segment a bit,
   193          * and there can be no avoiding it without more precision
   194          */
   195         int *x_result_[] = { X1, X2, NULL }, **x_result = x_result_;
   196         int *y_result_[] = { Y1, Y2, NULL }, **y_result = y_result_;
   197         SDL_bool intersection = SDL_FALSE;
   198         double b, m, left, right, bottom, top;
   199         int xl, xh, yl, yh;
   200 
   201         /* solve mx+b line formula */
   202         m = (double) (y1 - y2) / (double) (x1 - x2);
   203         b = y2 - m * (double) x2;
   204 
   205         /* find some linear intersections */
   206         left = (m * (double) rectx1) + b;
   207         right = (m * (double) rectx2) + b;
   208         top = (recty1 - b) / m;
   209         bottom = (recty2 - b) / m;
   210 
   211         /* sort end-points' x and y components individually */
   212         if (x1 < x2) {
   213             xl = x1;
   214             xh = x2;
   215         } else {
   216             xl = x2;
   217             xh = x1;
   218         }
   219         if (y1 < y2) {
   220             yl = y1;
   221             yh = y2;
   222         } else {
   223             yl = y2;
   224             yh = y1;
   225         }
   226 
   227 #define RISING(a, b, c) (((a)<=(b))&&((b)<=(c)))
   228 
   229         /* check for a point that's entirely inside the rect */
   230         if (RISING(rectx1, x1, rectx2) && RISING(recty1, y1, recty2)) {
   231             x_result++;
   232             y_result++;
   233             intersection = SDL_TRUE;
   234         } else
   235             /* it was determined earlier that *both* end-points are not contained */
   236         if (RISING(rectx1, x2, rectx2) && RISING(recty1, y2, recty2)) {
   237             **(x_result++) = x2;
   238             **(y_result++) = y2;
   239             intersection = SDL_TRUE;
   240         }
   241 
   242         if (RISING(recty1, left, recty2) && RISING(xl, rectx1, xh)) {
   243             **(x_result++) = rectx1;
   244             **(y_result++) = (int) left;
   245             intersection = SDL_TRUE;
   246         }
   247 
   248         if (*x_result == NULL)
   249             return intersection;
   250         if (RISING(recty1, right, recty2) && RISING(xl, rectx2, xh)) {
   251             **(x_result++) = rectx2;
   252             **(y_result++) = (int) right;
   253             intersection = SDL_TRUE;
   254         }
   255 
   256         if (*x_result == NULL)
   257             return intersection;
   258         if (RISING(rectx1, top, rectx2) && RISING(yl, recty1, yh)) {
   259             **(x_result++) = (int) top;
   260             **(y_result++) = recty1;
   261             intersection = SDL_TRUE;
   262         }
   263 
   264         if (*x_result == NULL)
   265             return intersection;
   266         if (RISING(rectx1, bottom, rectx2) && RISING(yl, recty2, yh)) {
   267             **(x_result++) = (int) bottom;
   268             **(y_result++) = recty2;
   269             intersection = SDL_TRUE;
   270         }
   271 
   272         return intersection;
   273     }
   274 
   275     return SDL_FALSE;
   276 }
   277 
   278 void
   279 SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
   280 {
   281     SDL_DirtyRect *dirty;
   282 
   283     /* FIXME: At what point is this optimization too expensive? */
   284     for (dirty = list->list; dirty; dirty = dirty->next) {
   285         if (SDL_HasIntersection(&dirty->rect, rect)) {
   286             SDL_UnionRect(&dirty->rect, rect, &dirty->rect);
   287             return;
   288         }
   289     }
   290 
   291     if (list->free) {
   292         dirty = list->free;
   293         list->free = dirty->next;
   294     } else {
   295         dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
   296         if (!dirty) {
   297             return;
   298         }
   299     }
   300     dirty->rect = *rect;
   301     dirty->next = list->list;
   302     list->list = dirty;
   303 }
   304 
   305 void
   306 SDL_ClearDirtyRects(SDL_DirtyRectList * list)
   307 {
   308     SDL_DirtyRect *prev, *curr;
   309 
   310     /* Skip to the end of the free list */
   311     prev = NULL;
   312     for (curr = list->free; curr; curr = curr->next) {
   313         prev = curr;
   314     }
   315 
   316     /* Add the list entries to the end */
   317     if (prev) {
   318         prev->next = list->list;
   319     } else {
   320         list->free = list->list;
   321     }
   322     list->list = NULL;
   323 }
   324 
   325 void
   326 SDL_FreeDirtyRects(SDL_DirtyRectList * list)
   327 {
   328     while (list->list) {
   329         SDL_DirtyRect *elem = list->list;
   330         list->list = elem->next;
   331         SDL_free(elem);
   332     }
   333     while (list->free) {
   334         SDL_DirtyRect *elem = list->free;
   335         list->free = elem->next;
   336         SDL_free(elem);
   337     }
   338 }
   339 
   340 /* vi: set ts=4 sw=4 expandtab: */