src/video/SDL_rect.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3542 97eae5a705f9
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
     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_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
   123                   SDL_Rect * result)
   124 {
   125     int minx, miny;
   126     int maxx, maxy;
   127     int x, y, i;
   128 
   129     if (count < 1) {
   130         return SDL_FALSE;
   131     }
   132 
   133     if (clip) {
   134         SDL_bool added = SDL_FALSE;
   135         int clip_minx = clip->x;
   136         int clip_miny = clip->y;
   137         int clip_maxx = clip->x+clip->w-1;
   138         int clip_maxy = clip->y+clip->h-1;
   139 
   140         for (i = 0; i < count; ++i) {
   141             x = points[i].x;
   142             y = points[i].y;
   143 
   144             if (x < clip_minx || x > clip_maxx ||
   145                 y < clip_miny || y > clip_maxy) {
   146                 continue;
   147             }
   148             if (!added) {
   149                 minx = maxx = x;
   150                 miny = maxy = y;
   151                 added = SDL_TRUE;
   152                 continue;
   153             }
   154             if (x < minx) {
   155                 minx = x;
   156             } else if (x > maxx) {
   157                 maxx = x;
   158             }
   159             if (y < miny) {
   160                 miny = y;
   161             } else if (y > maxy) {
   162                 maxy = y;
   163             }
   164         }
   165         if (!added) {
   166             return SDL_FALSE;
   167         }
   168     } else {
   169         /* No clipping, always add the first point */
   170         minx = maxx = points[0].x;
   171         miny = maxy = points[0].y;
   172 
   173         for (i = 1; i < count; ++i) {
   174             x = points[i].x;
   175             y = points[i].y;
   176 
   177             if (x < minx) {
   178                 minx = x;
   179             } else if (x > maxx) {
   180                 maxx = x;
   181             }
   182             if (y < miny) {
   183                 miny = y;
   184             } else if (y > maxy) {
   185                 maxy = y;
   186             }
   187         }
   188     }
   189 
   190     if (result) {
   191         result->x = minx;
   192         result->y = miny;
   193         result->w = (maxx-minx)+1;
   194         result->h = (maxy-miny)+1;
   195     }
   196     return SDL_TRUE;
   197 }
   198 
   199 /* Use the Cohen-Sutherland algorithm for line clipping */
   200 #define CODE_BOTTOM 1
   201 #define CODE_TOP    2
   202 #define CODE_LEFT   4
   203 #define CODE_RIGHT  8
   204 
   205 static int ComputeOutCode(const SDL_Rect * rect, int x, int y)
   206 {
   207     int code = 0;
   208     if (y < 0) {
   209         code |= CODE_TOP;
   210     } else if (y >= rect->y + rect->h) {
   211         code |= CODE_BOTTOM;
   212     }
   213     if (x < 0) {
   214         code |= CODE_LEFT;
   215     } else if (x >= rect->x + rect->w) {
   216         code |= CODE_RIGHT;
   217     }
   218     return code;
   219 }
   220 
   221 SDL_bool
   222 SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
   223                          int *Y2)
   224 {
   225     int x, y;
   226     int x1, y1;
   227     int x2, y2;
   228     int rectx1;
   229     int recty1;
   230     int rectx2;
   231     int recty2;
   232     int outcode1, outcode2;
   233 
   234     if (!rect || !X1 || !Y1 || !X2 || !Y2) {
   235         return SDL_FALSE;
   236     }
   237 
   238     x1 = *X1;
   239     y1 = *Y1;
   240     x2 = *X2;
   241     y2 = *Y2;
   242     rectx1 = rect->x;
   243     recty1 = rect->y;
   244     rectx2 = rect->x + rect->w - 1;
   245     recty2 = rect->y + rect->h - 1;
   246 
   247     /* Check to see if entire line is inside rect */
   248     if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
   249         y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
   250         return SDL_TRUE;
   251     }
   252 
   253     /* Check to see if entire line is to one side of rect */
   254     if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
   255         (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
   256         return SDL_FALSE;
   257     }
   258 
   259     if (y1 == y2) {
   260         /* Horizontal line, easy to clip */
   261         if (x1 < rectx1) {
   262             *X1 = rectx1;
   263         } else if (x1 > rectx2) {
   264             *X1 = rectx2;
   265         }
   266         if (x2 < rectx1) {
   267             *X2 = rectx1;
   268         } else if (x2 > rectx2) {
   269             *X2 = rectx2;
   270         }
   271         return SDL_TRUE;
   272     }
   273 
   274     if (x1 == x2) {
   275         /* Vertical line, easy to clip */
   276         if (y1 < recty1) {
   277             *Y1 = recty1;
   278         } else if (y1 > recty2) {
   279             *Y1 = recty2;
   280         }
   281         if (y2 < recty1) {
   282             *Y2 = recty1;
   283         } else if (y2 > recty2) {
   284             *Y2 = recty2;
   285         }
   286         return SDL_TRUE;
   287     }
   288 
   289     /* More complicated Cohen-Sutherland algorithm */
   290     outcode1 = ComputeOutCode(rect, x1, y1);
   291     outcode2 = ComputeOutCode(rect, x2, y2);
   292     while (outcode1 || outcode2) {
   293         if (outcode1 & outcode2) {
   294             return SDL_FALSE;
   295         }
   296 
   297         if (outcode1) {
   298             if (outcode1 & CODE_TOP) {
   299                 y = recty1;
   300                 x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
   301             } else if (outcode1 & CODE_BOTTOM) {
   302                 y = recty2;
   303                 x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
   304             } else if (outcode1 & CODE_LEFT) {
   305                 x = rectx1;
   306                 y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
   307             } else if (outcode1 & CODE_RIGHT) {
   308                 x = rectx2;
   309                 y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
   310             }
   311             x1 = x;
   312             y1 = y;
   313             outcode1 = ComputeOutCode(rect, x, y);
   314         } else {
   315             if (outcode2 & CODE_TOP) {
   316                 y = recty1;
   317                 x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
   318             } else if (outcode2 & CODE_BOTTOM) {
   319                 y = recty2;
   320                 x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
   321             } else if (outcode2 & CODE_LEFT) {
   322                 x = rectx1;
   323                 y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
   324             } else if (outcode2 & CODE_RIGHT) {
   325                 x = rectx2;
   326                 y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
   327             }
   328             x2 = x;
   329             y2 = y;
   330             outcode2 = ComputeOutCode(rect, x, y);
   331         }
   332     }
   333     *X1 = x1;
   334     *Y1 = y1;
   335     *X2 = x2;
   336     *Y2 = y2;
   337     return SDL_TRUE;
   338 }
   339 
   340 void
   341 SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
   342 {
   343     SDL_DirtyRect *dirty;
   344 
   345     /* FIXME: At what point is this optimization too expensive? */
   346     for (dirty = list->list; dirty; dirty = dirty->next) {
   347         if (SDL_HasIntersection(&dirty->rect, rect)) {
   348             SDL_UnionRect(&dirty->rect, rect, &dirty->rect);
   349             return;
   350         }
   351     }
   352 
   353     if (list->free) {
   354         dirty = list->free;
   355         list->free = dirty->next;
   356     } else {
   357         dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
   358         if (!dirty) {
   359             return;
   360         }
   361     }
   362     dirty->rect = *rect;
   363     dirty->next = list->list;
   364     list->list = dirty;
   365 }
   366 
   367 void
   368 SDL_ClearDirtyRects(SDL_DirtyRectList * list)
   369 {
   370     SDL_DirtyRect *prev, *curr;
   371 
   372     /* Skip to the end of the free list */
   373     prev = NULL;
   374     for (curr = list->free; curr; curr = curr->next) {
   375         prev = curr;
   376     }
   377 
   378     /* Add the list entries to the end */
   379     if (prev) {
   380         prev->next = list->list;
   381     } else {
   382         list->free = list->list;
   383     }
   384     list->list = NULL;
   385 }
   386 
   387 void
   388 SDL_FreeDirtyRects(SDL_DirtyRectList * list)
   389 {
   390     while (list->list) {
   391         SDL_DirtyRect *elem = list->list;
   392         list->list = elem->next;
   393         SDL_free(elem);
   394     }
   395     while (list->free) {
   396         SDL_DirtyRect *elem = list->free;
   397         list->free = elem->next;
   398         SDL_free(elem);
   399     }
   400 }
   401 
   402 /* vi: set ts=4 sw=4 expandtab: */