src/video/SDL_rect.c
changeset 2909 3da0bb421d83
parent 2859 99210400e8b9
child 2920 cdb01906cb7e
     1.1 --- a/src/video/SDL_rect.c	Tue Dec 23 01:28:06 2008 +0000
     1.2 +++ b/src/video/SDL_rect.c	Tue Dec 23 02:23:18 2008 +0000
     1.3 @@ -118,6 +118,75 @@
     1.4      result->h = Amax - Amin;
     1.5  }
     1.6  
     1.7 +SDL_bool
     1.8 +SDL_IntersectRectAndLine(const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2)
     1.9 +{
    1.10 +    int x1, y1;
    1.11 +    int x2, y2;
    1.12 +    int rectx1;
    1.13 +    int recty1;
    1.14 +    int rectx2;
    1.15 +    int recty2;
    1.16 +
    1.17 +    if (!rect || !X1 || !Y1 || !X2 || !Y2) {
    1.18 +        SDL_FALSE;
    1.19 +    }
    1.20 +
    1.21 +    x1 = *X1;
    1.22 +    y1 = *Y1;
    1.23 +    x2 = *X2;
    1.24 +    y2 = *Y2;
    1.25 +    rectx1 = rect->x;
    1.26 +    recty1 = rect->y;
    1.27 +    rectx2 = rect->x + rect->w - 1;
    1.28 +    recty2 = rect->y + rect->h - 1;
    1.29 +
    1.30 +    /* Check to see if entire line is inside rect */
    1.31 +    if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
    1.32 +        y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
    1.33 +        return SDL_TRUE;
    1.34 +    }
    1.35 +
    1.36 +    /* Check to see if entire line is outside rect */
    1.37 +    if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
    1.38 +        (y1 < recty1 && y2 < recty2) || (y1 > recty2 && y2 > recty2)) {
    1.39 +        return SDL_FALSE;
    1.40 +    }
    1.41 +
    1.42 +    if (y1 = y2) {
    1.43 +        /* Horizontal line, easy to clip */
    1.44 +        if (x1 < rectx1) {
    1.45 +            *X1 = rectx1;
    1.46 +        } else if (x1 > rectx2) {
    1.47 +            *X1 = rectx2;
    1.48 +        }
    1.49 +        if (x2 < rectx1) {
    1.50 +            *X2 = rectx1;
    1.51 +        } else if (x2 > rectx2) {
    1.52 +            *X2 = rectx2;
    1.53 +        }
    1.54 +        return SDL_TRUE;
    1.55 +    }
    1.56 +
    1.57 +    if (x1 == x2) {
    1.58 +        /* Vertical line, easy to clip */
    1.59 +        if (y1 < recty1) {
    1.60 +            *Y1 = recty1;
    1.61 +        } else if (y1 > recty2) {
    1.62 +            *Y1 = recty2;
    1.63 +        }
    1.64 +        if (y2 < recty1) {
    1.65 +            *Y2 = recty1;
    1.66 +        } else if (y2 > recty2) {
    1.67 +            *Y2 = recty2;
    1.68 +        }
    1.69 +        return SDL_TRUE;
    1.70 +    }
    1.71 +
    1.72 +    /* FIXME: need code to clip diagonal line to rect */
    1.73 +    return SDL_FALSE;
    1.74 +}
    1.75 +
    1.76  void
    1.77  SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
    1.78  {