src/video/SDL_rect.c
author Sam Lantinga
Thu, 25 Dec 2008 05:26:29 +0000
changeset 2920 cdb01906cb7e
parent 2909 3da0bb421d83
child 2994 7563b99e9a49
permissions -rw-r--r--
indent
     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         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 outside rect */
   152     if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
   153         (y1 < recty1 && y2 < recty2) || (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     /* FIXME: need code to clip diagonal line to rect */
   188     return SDL_FALSE;
   189 }
   190 
   191 void
   192 SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
   193 {
   194     SDL_DirtyRect *dirty;
   195 
   196     /* FIXME: At what point is this optimization too expensive? */
   197     for (dirty = list->list; dirty; dirty = dirty->next) {
   198         if (SDL_HasIntersection(&dirty->rect, rect)) {
   199             SDL_UnionRect(&dirty->rect, rect, &dirty->rect);
   200             return;
   201         }
   202     }
   203 
   204     if (list->free) {
   205         dirty = list->free;
   206         list->free = dirty->next;
   207     } else {
   208         dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
   209         if (!dirty) {
   210             return;
   211         }
   212     }
   213     dirty->rect = *rect;
   214     dirty->next = list->list;
   215     list->list = dirty;
   216 }
   217 
   218 void
   219 SDL_ClearDirtyRects(SDL_DirtyRectList * list)
   220 {
   221     SDL_DirtyRect *prev, *curr;
   222 
   223     /* Skip to the end of the free list */
   224     prev = NULL;
   225     for (curr = list->free; curr; curr = curr->next) {
   226         prev = curr;
   227     }
   228 
   229     /* Add the list entries to the end */
   230     if (prev) {
   231         prev->next = list->list;
   232     } else {
   233         list->free = list->list;
   234     }
   235     list->list = NULL;
   236 }
   237 
   238 void
   239 SDL_FreeDirtyRects(SDL_DirtyRectList * list)
   240 {
   241     while (list->list) {
   242         SDL_DirtyRect *elem = list->list;
   243         list->list = elem->next;
   244         SDL_free(elem);
   245     }
   246     while (list->free) {
   247         SDL_DirtyRect *elem = list->free;
   248         list->free = elem->next;
   249         SDL_free(elem);
   250     }
   251 }
   252 
   253 /* vi: set ts=4 sw=4 expandtab: */