src/video/SDL_rect.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5156 307ccc9c135e
child 5262 b530ef003506
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@5154
    24
#include "SDL_rect.h"
slouken@1895
    25
slouken@1895
    26
SDL_bool
slouken@1895
    27
SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
slouken@1895
    28
{
slouken@1895
    29
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    30
slouken@1895
    31
    /* Horizontal intersection */
slouken@1895
    32
    Amin = A->x;
slouken@1895
    33
    Amax = Amin + A->w;
slouken@1895
    34
    Bmin = B->x;
slouken@1895
    35
    Bmax = Bmin + B->w;
slouken@1895
    36
    if (Bmin > Amin)
slouken@1895
    37
        Amin = Bmin;
slouken@1895
    38
    if (Bmax < Amax)
slouken@1895
    39
        Amax = Bmax;
slouken@1895
    40
    if (Amax <= Amin)
slouken@1895
    41
        return SDL_FALSE;
slouken@1895
    42
slouken@1895
    43
    /* Vertical intersection */
slouken@1895
    44
    Amin = A->y;
slouken@1895
    45
    Amax = Amin + A->h;
slouken@1895
    46
    Bmin = B->y;
slouken@1895
    47
    Bmax = Bmin + B->h;
slouken@1895
    48
    if (Bmin > Amin)
slouken@1895
    49
        Amin = Bmin;
slouken@1895
    50
    if (Bmax < Amax)
slouken@1895
    51
        Amax = Bmax;
slouken@1895
    52
    if (Amax <= Amin)
slouken@1895
    53
        return SDL_FALSE;
slouken@1895
    54
slouken@1895
    55
    return SDL_TRUE;
slouken@1895
    56
}
slouken@1895
    57
slouken@1895
    58
SDL_bool
slouken@1895
    59
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    60
{
slouken@1895
    61
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    62
slouken@1895
    63
    /* Horizontal intersection */
slouken@1895
    64
    Amin = A->x;
slouken@1895
    65
    Amax = Amin + A->w;
slouken@1895
    66
    Bmin = B->x;
slouken@1895
    67
    Bmax = Bmin + B->w;
slouken@1895
    68
    if (Bmin > Amin)
slouken@1895
    69
        Amin = Bmin;
slouken@1895
    70
    result->x = Amin;
slouken@1895
    71
    if (Bmax < Amax)
slouken@1895
    72
        Amax = Bmax;
slouken@1895
    73
    result->w = Amax - Amin;
slouken@1895
    74
slouken@1895
    75
    /* Vertical intersection */
slouken@1895
    76
    Amin = A->y;
slouken@1895
    77
    Amax = Amin + A->h;
slouken@1895
    78
    Bmin = B->y;
slouken@1895
    79
    Bmax = Bmin + B->h;
slouken@1895
    80
    if (Bmin > Amin)
slouken@1895
    81
        Amin = Bmin;
slouken@1895
    82
    result->y = Amin;
slouken@1895
    83
    if (Bmax < Amax)
slouken@1895
    84
        Amax = Bmax;
slouken@1895
    85
    result->h = Amax - Amin;
slouken@1895
    86
slouken@1895
    87
    return !SDL_RectEmpty(result);
slouken@1895
    88
}
slouken@1895
    89
slouken@1895
    90
void
slouken@1895
    91
SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    92
{
slouken@1895
    93
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    94
slouken@1895
    95
    /* Horizontal union */
slouken@1895
    96
    Amin = A->x;
slouken@1895
    97
    Amax = Amin + A->w;
slouken@1895
    98
    Bmin = B->x;
slouken@1895
    99
    Bmax = Bmin + B->w;
slouken@1895
   100
    if (Bmin < Amin)
slouken@1895
   101
        Amin = Bmin;
slouken@1895
   102
    result->x = Amin;
slouken@1895
   103
    if (Bmax > Amax)
slouken@1895
   104
        Amax = Bmax;
slouken@1895
   105
    result->w = Amax - Amin;
slouken@1895
   106
slouken@1895
   107
    /* Vertical intersection */
slouken@1895
   108
    Amin = A->y;
slouken@1895
   109
    Amax = Amin + A->h;
slouken@1895
   110
    Bmin = B->y;
slouken@1895
   111
    Bmax = Bmin + B->h;
slouken@1895
   112
    if (Bmin < Amin)
slouken@1895
   113
        Amin = Bmin;
slouken@1895
   114
    result->y = Amin;
slouken@1895
   115
    if (Bmax > Amax)
slouken@1895
   116
        Amax = Bmax;
slouken@1895
   117
    result->h = Amax - Amin;
slouken@1895
   118
}
slouken@1895
   119
slouken@2909
   120
SDL_bool
slouken@3536
   121
SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
slouken@3536
   122
                  SDL_Rect * result)
slouken@3536
   123
{
slouken@4456
   124
    int minx = 0;
slouken@4456
   125
    int miny = 0;
slouken@4456
   126
    int maxx = 0;
slouken@4456
   127
    int maxy = 0;
slouken@3536
   128
    int x, y, i;
slouken@3536
   129
slouken@3536
   130
    if (count < 1) {
slouken@3536
   131
        return SDL_FALSE;
slouken@3536
   132
    }
slouken@3536
   133
slouken@3536
   134
    if (clip) {
slouken@3536
   135
        SDL_bool added = SDL_FALSE;
slouken@3536
   136
        int clip_minx = clip->x;
slouken@3536
   137
        int clip_miny = clip->y;
slouken@3536
   138
        int clip_maxx = clip->x+clip->w-1;
slouken@3536
   139
        int clip_maxy = clip->y+clip->h-1;
slouken@3536
   140
slouken@3536
   141
        for (i = 0; i < count; ++i) {
slouken@3536
   142
            x = points[i].x;
slouken@3536
   143
            y = points[i].y;
slouken@3536
   144
slouken@3536
   145
            if (x < clip_minx || x > clip_maxx ||
slouken@3536
   146
                y < clip_miny || y > clip_maxy) {
slouken@3536
   147
                continue;
slouken@3536
   148
            }
slouken@3536
   149
            if (!added) {
slouken@3536
   150
                minx = maxx = x;
slouken@3536
   151
                miny = maxy = y;
slouken@3536
   152
                added = SDL_TRUE;
slouken@3536
   153
                continue;
slouken@3536
   154
            }
slouken@3536
   155
            if (x < minx) {
slouken@3536
   156
                minx = x;
slouken@3536
   157
            } else if (x > maxx) {
slouken@3536
   158
                maxx = x;
slouken@3536
   159
            }
slouken@3536
   160
            if (y < miny) {
slouken@3536
   161
                miny = y;
slouken@3536
   162
            } else if (y > maxy) {
slouken@3536
   163
                maxy = y;
slouken@3536
   164
            }
slouken@3536
   165
        }
slouken@3536
   166
        if (!added) {
slouken@3536
   167
            return SDL_FALSE;
slouken@3536
   168
        }
slouken@3536
   169
    } else {
slouken@3536
   170
        /* No clipping, always add the first point */
slouken@3536
   171
        minx = maxx = points[0].x;
slouken@3536
   172
        miny = maxy = points[0].y;
slouken@3536
   173
slouken@3536
   174
        for (i = 1; i < count; ++i) {
slouken@3536
   175
            x = points[i].x;
slouken@3536
   176
            y = points[i].y;
slouken@3536
   177
slouken@3536
   178
            if (x < minx) {
slouken@3536
   179
                minx = x;
slouken@3536
   180
            } else if (x > maxx) {
slouken@3536
   181
                maxx = x;
slouken@3536
   182
            }
slouken@3536
   183
            if (y < miny) {
slouken@3536
   184
                miny = y;
slouken@3536
   185
            } else if (y > maxy) {
slouken@3536
   186
                maxy = y;
slouken@3536
   187
            }
slouken@3536
   188
        }
slouken@3536
   189
    }
slouken@3536
   190
slouken@3536
   191
    if (result) {
slouken@3536
   192
        result->x = minx;
slouken@3536
   193
        result->y = miny;
slouken@3536
   194
        result->w = (maxx-minx)+1;
slouken@3536
   195
        result->h = (maxy-miny)+1;
slouken@3536
   196
    }
slouken@3536
   197
    return SDL_TRUE;
slouken@3536
   198
}
slouken@3536
   199
slouken@3541
   200
/* Use the Cohen-Sutherland algorithm for line clipping */
slouken@3541
   201
#define CODE_BOTTOM 1
slouken@3541
   202
#define CODE_TOP    2
slouken@3541
   203
#define CODE_LEFT   4
slouken@3541
   204
#define CODE_RIGHT  8
slouken@3541
   205
slouken@3541
   206
static int ComputeOutCode(const SDL_Rect * rect, int x, int y)
slouken@3541
   207
{
slouken@3541
   208
    int code = 0;
slouken@3541
   209
    if (y < 0) {
slouken@3541
   210
        code |= CODE_TOP;
slouken@3541
   211
    } else if (y >= rect->y + rect->h) {
slouken@3541
   212
        code |= CODE_BOTTOM;
slouken@3541
   213
    }
slouken@3541
   214
    if (x < 0) {
slouken@3541
   215
        code |= CODE_LEFT;
slouken@3541
   216
    } else if (x >= rect->x + rect->w) {
slouken@3541
   217
        code |= CODE_RIGHT;
slouken@3541
   218
    }
slouken@3541
   219
    return code;
slouken@3541
   220
}
slouken@3541
   221
slouken@3536
   222
SDL_bool
slouken@2920
   223
SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
slouken@2920
   224
                         int *Y2)
slouken@2909
   225
{
slouken@4456
   226
    int x = 0;
slouken@4456
   227
    int y = 0;
slouken@2909
   228
    int x1, y1;
slouken@2909
   229
    int x2, y2;
slouken@2909
   230
    int rectx1;
slouken@2909
   231
    int recty1;
slouken@2909
   232
    int rectx2;
slouken@2909
   233
    int recty2;
slouken@3541
   234
    int outcode1, outcode2;
slouken@2909
   235
slouken@2909
   236
    if (!rect || !X1 || !Y1 || !X2 || !Y2) {
slouken@3046
   237
        return SDL_FALSE;
slouken@2909
   238
    }
slouken@2909
   239
slouken@2909
   240
    x1 = *X1;
slouken@2909
   241
    y1 = *Y1;
slouken@2909
   242
    x2 = *X2;
slouken@2909
   243
    y2 = *Y2;
slouken@2909
   244
    rectx1 = rect->x;
slouken@2909
   245
    recty1 = rect->y;
slouken@2909
   246
    rectx2 = rect->x + rect->w - 1;
slouken@2909
   247
    recty2 = rect->y + rect->h - 1;
slouken@2909
   248
slouken@2909
   249
    /* Check to see if entire line is inside rect */
slouken@2909
   250
    if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
slouken@2909
   251
        y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
slouken@2909
   252
        return SDL_TRUE;
slouken@2909
   253
    }
slouken@2909
   254
slouken@2994
   255
    /* Check to see if entire line is to one side of rect */
slouken@2909
   256
    if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
slouken@3004
   257
        (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
slouken@2909
   258
        return SDL_FALSE;
slouken@2909
   259
    }
slouken@2909
   260
slouken@2994
   261
    if (y1 == y2) {
slouken@2909
   262
        /* Horizontal line, easy to clip */
slouken@2909
   263
        if (x1 < rectx1) {
slouken@2909
   264
            *X1 = rectx1;
slouken@2909
   265
        } else if (x1 > rectx2) {
slouken@2909
   266
            *X1 = rectx2;
slouken@2909
   267
        }
slouken@2909
   268
        if (x2 < rectx1) {
slouken@2909
   269
            *X2 = rectx1;
slouken@2909
   270
        } else if (x2 > rectx2) {
slouken@2909
   271
            *X2 = rectx2;
slouken@2909
   272
        }
slouken@2909
   273
        return SDL_TRUE;
slouken@2909
   274
    }
slouken@2909
   275
slouken@2909
   276
    if (x1 == x2) {
slouken@2909
   277
        /* Vertical line, easy to clip */
slouken@2909
   278
        if (y1 < recty1) {
slouken@2909
   279
            *Y1 = recty1;
slouken@2909
   280
        } else if (y1 > recty2) {
slouken@2909
   281
            *Y1 = recty2;
slouken@2909
   282
        }
slouken@2909
   283
        if (y2 < recty1) {
slouken@2909
   284
            *Y2 = recty1;
slouken@2909
   285
        } else if (y2 > recty2) {
slouken@2909
   286
            *Y2 = recty2;
slouken@2909
   287
        }
slouken@2909
   288
        return SDL_TRUE;
slouken@2909
   289
    }
slouken@2909
   290
slouken@3541
   291
    /* More complicated Cohen-Sutherland algorithm */
slouken@3541
   292
    outcode1 = ComputeOutCode(rect, x1, y1);
slouken@3541
   293
    outcode2 = ComputeOutCode(rect, x2, y2);
slouken@3541
   294
    while (outcode1 || outcode2) {
slouken@3541
   295
        if (outcode1 & outcode2) {
slouken@3541
   296
            return SDL_FALSE;
slouken@2994
   297
        }
slouken@2994
   298
slouken@3541
   299
        if (outcode1) {
slouken@3541
   300
            if (outcode1 & CODE_TOP) {
slouken@3541
   301
                y = recty1;
slouken@3541
   302
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   303
            } else if (outcode1 & CODE_BOTTOM) {
slouken@3541
   304
                y = recty2;
slouken@3541
   305
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   306
            } else if (outcode1 & CODE_LEFT) {
slouken@3541
   307
                x = rectx1;
slouken@3541
   308
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   309
            } else if (outcode1 & CODE_RIGHT) {
slouken@3541
   310
                x = rectx2;
slouken@3541
   311
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   312
            }
slouken@3541
   313
            x1 = x;
slouken@3541
   314
            y1 = y;
slouken@3541
   315
            outcode1 = ComputeOutCode(rect, x, y);
slouken@3542
   316
        } else {
slouken@3541
   317
            if (outcode2 & CODE_TOP) {
slouken@3541
   318
                y = recty1;
slouken@3541
   319
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   320
            } else if (outcode2 & CODE_BOTTOM) {
slouken@3541
   321
                y = recty2;
slouken@3541
   322
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   323
            } else if (outcode2 & CODE_LEFT) {
slouken@3541
   324
                x = rectx1;
slouken@3541
   325
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   326
            } else if (outcode2 & CODE_RIGHT) {
slouken@3541
   327
                x = rectx2;
slouken@3541
   328
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   329
            }
slouken@3541
   330
            x2 = x;
slouken@3541
   331
            y2 = y;
slouken@3541
   332
            outcode2 = ComputeOutCode(rect, x, y);
slouken@2994
   333
        }
slouken@2994
   334
    }
slouken@3541
   335
    *X1 = x1;
slouken@3541
   336
    *Y1 = y1;
slouken@3541
   337
    *X2 = x2;
slouken@3541
   338
    *Y2 = y2;
slouken@3541
   339
    return SDL_TRUE;
slouken@2909
   340
}
slouken@2909
   341
slouken@1895
   342
/* vi: set ts=4 sw=4 expandtab: */