src/video/SDL_rect.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Dec 2009 09:57:54 +0000
changeset 3542 97eae5a705f9
parent 3541 0c429a5fda8a
child 3697 f7b03b6838cb
permissions -rw-r--r--
Make sure we fully clip the first point before starting to adjust the second point.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@1895
    24
#include "SDL_video.h"
slouken@1895
    25
#include "SDL_rect_c.h"
slouken@1895
    26
slouken@1895
    27
SDL_bool
slouken@1895
    28
SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
slouken@1895
    29
{
slouken@1895
    30
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    31
slouken@1895
    32
    /* Horizontal intersection */
slouken@1895
    33
    Amin = A->x;
slouken@1895
    34
    Amax = Amin + A->w;
slouken@1895
    35
    Bmin = B->x;
slouken@1895
    36
    Bmax = Bmin + B->w;
slouken@1895
    37
    if (Bmin > Amin)
slouken@1895
    38
        Amin = Bmin;
slouken@1895
    39
    if (Bmax < Amax)
slouken@1895
    40
        Amax = Bmax;
slouken@1895
    41
    if (Amax <= Amin)
slouken@1895
    42
        return SDL_FALSE;
slouken@1895
    43
slouken@1895
    44
    /* Vertical intersection */
slouken@1895
    45
    Amin = A->y;
slouken@1895
    46
    Amax = Amin + A->h;
slouken@1895
    47
    Bmin = B->y;
slouken@1895
    48
    Bmax = Bmin + B->h;
slouken@1895
    49
    if (Bmin > Amin)
slouken@1895
    50
        Amin = Bmin;
slouken@1895
    51
    if (Bmax < Amax)
slouken@1895
    52
        Amax = Bmax;
slouken@1895
    53
    if (Amax <= Amin)
slouken@1895
    54
        return SDL_FALSE;
slouken@1895
    55
slouken@1895
    56
    return SDL_TRUE;
slouken@1895
    57
}
slouken@1895
    58
slouken@1895
    59
SDL_bool
slouken@1895
    60
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    61
{
slouken@1895
    62
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    63
slouken@1895
    64
    /* Horizontal intersection */
slouken@1895
    65
    Amin = A->x;
slouken@1895
    66
    Amax = Amin + A->w;
slouken@1895
    67
    Bmin = B->x;
slouken@1895
    68
    Bmax = Bmin + B->w;
slouken@1895
    69
    if (Bmin > Amin)
slouken@1895
    70
        Amin = Bmin;
slouken@1895
    71
    result->x = Amin;
slouken@1895
    72
    if (Bmax < Amax)
slouken@1895
    73
        Amax = Bmax;
slouken@1895
    74
    result->w = Amax - Amin;
slouken@1895
    75
slouken@1895
    76
    /* Vertical intersection */
slouken@1895
    77
    Amin = A->y;
slouken@1895
    78
    Amax = Amin + A->h;
slouken@1895
    79
    Bmin = B->y;
slouken@1895
    80
    Bmax = Bmin + B->h;
slouken@1895
    81
    if (Bmin > Amin)
slouken@1895
    82
        Amin = Bmin;
slouken@1895
    83
    result->y = Amin;
slouken@1895
    84
    if (Bmax < Amax)
slouken@1895
    85
        Amax = Bmax;
slouken@1895
    86
    result->h = Amax - Amin;
slouken@1895
    87
slouken@1895
    88
    return !SDL_RectEmpty(result);
slouken@1895
    89
}
slouken@1895
    90
slouken@1895
    91
void
slouken@1895
    92
SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    93
{
slouken@1895
    94
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    95
slouken@1895
    96
    /* Horizontal union */
slouken@1895
    97
    Amin = A->x;
slouken@1895
    98
    Amax = Amin + A->w;
slouken@1895
    99
    Bmin = B->x;
slouken@1895
   100
    Bmax = Bmin + B->w;
slouken@1895
   101
    if (Bmin < Amin)
slouken@1895
   102
        Amin = Bmin;
slouken@1895
   103
    result->x = Amin;
slouken@1895
   104
    if (Bmax > Amax)
slouken@1895
   105
        Amax = Bmax;
slouken@1895
   106
    result->w = Amax - Amin;
slouken@1895
   107
slouken@1895
   108
    /* Vertical intersection */
slouken@1895
   109
    Amin = A->y;
slouken@1895
   110
    Amax = Amin + A->h;
slouken@1895
   111
    Bmin = B->y;
slouken@1895
   112
    Bmax = Bmin + B->h;
slouken@1895
   113
    if (Bmin < Amin)
slouken@1895
   114
        Amin = Bmin;
slouken@1895
   115
    result->y = Amin;
slouken@1895
   116
    if (Bmax > Amax)
slouken@1895
   117
        Amax = Bmax;
slouken@1895
   118
    result->h = Amax - Amin;
slouken@1895
   119
}
slouken@1895
   120
slouken@2909
   121
SDL_bool
slouken@3536
   122
SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
slouken@3536
   123
                  SDL_Rect * result)
slouken@3536
   124
{
slouken@3536
   125
    int minx, miny;
slouken@3536
   126
    int maxx, maxy;
slouken@3536
   127
    int x, y, i;
slouken@3536
   128
slouken@3536
   129
    if (count < 1) {
slouken@3536
   130
        return SDL_FALSE;
slouken@3536
   131
    }
slouken@3536
   132
slouken@3536
   133
    if (clip) {
slouken@3536
   134
        SDL_bool added = SDL_FALSE;
slouken@3536
   135
        int clip_minx = clip->x;
slouken@3536
   136
        int clip_miny = clip->y;
slouken@3536
   137
        int clip_maxx = clip->x+clip->w-1;
slouken@3536
   138
        int clip_maxy = clip->y+clip->h-1;
slouken@3536
   139
slouken@3536
   140
        for (i = 0; i < count; ++i) {
slouken@3536
   141
            x = points[i].x;
slouken@3536
   142
            y = points[i].y;
slouken@3536
   143
slouken@3536
   144
            if (x < clip_minx || x > clip_maxx ||
slouken@3536
   145
                y < clip_miny || y > clip_maxy) {
slouken@3536
   146
                continue;
slouken@3536
   147
            }
slouken@3536
   148
            if (!added) {
slouken@3536
   149
                minx = maxx = x;
slouken@3536
   150
                miny = maxy = y;
slouken@3536
   151
                added = SDL_TRUE;
slouken@3536
   152
                continue;
slouken@3536
   153
            }
slouken@3536
   154
            if (x < minx) {
slouken@3536
   155
                minx = x;
slouken@3536
   156
            } else if (x > maxx) {
slouken@3536
   157
                maxx = x;
slouken@3536
   158
            }
slouken@3536
   159
            if (y < miny) {
slouken@3536
   160
                miny = y;
slouken@3536
   161
            } else if (y > maxy) {
slouken@3536
   162
                maxy = y;
slouken@3536
   163
            }
slouken@3536
   164
        }
slouken@3536
   165
        if (!added) {
slouken@3536
   166
            return SDL_FALSE;
slouken@3536
   167
        }
slouken@3536
   168
    } else {
slouken@3536
   169
        /* No clipping, always add the first point */
slouken@3536
   170
        minx = maxx = points[0].x;
slouken@3536
   171
        miny = maxy = points[0].y;
slouken@3536
   172
slouken@3536
   173
        for (i = 1; i < count; ++i) {
slouken@3536
   174
            x = points[i].x;
slouken@3536
   175
            y = points[i].y;
slouken@3536
   176
slouken@3536
   177
            if (x < minx) {
slouken@3536
   178
                minx = x;
slouken@3536
   179
            } else if (x > maxx) {
slouken@3536
   180
                maxx = x;
slouken@3536
   181
            }
slouken@3536
   182
            if (y < miny) {
slouken@3536
   183
                miny = y;
slouken@3536
   184
            } else if (y > maxy) {
slouken@3536
   185
                maxy = y;
slouken@3536
   186
            }
slouken@3536
   187
        }
slouken@3536
   188
    }
slouken@3536
   189
slouken@3536
   190
    if (result) {
slouken@3536
   191
        result->x = minx;
slouken@3536
   192
        result->y = miny;
slouken@3536
   193
        result->w = (maxx-minx)+1;
slouken@3536
   194
        result->h = (maxy-miny)+1;
slouken@3536
   195
    }
slouken@3536
   196
    return SDL_TRUE;
slouken@3536
   197
}
slouken@3536
   198
slouken@3541
   199
/* Use the Cohen-Sutherland algorithm for line clipping */
slouken@3541
   200
#define CODE_BOTTOM 1
slouken@3541
   201
#define CODE_TOP    2
slouken@3541
   202
#define CODE_LEFT   4
slouken@3541
   203
#define CODE_RIGHT  8
slouken@3541
   204
slouken@3541
   205
static int ComputeOutCode(const SDL_Rect * rect, int x, int y)
slouken@3541
   206
{
slouken@3541
   207
    int code = 0;
slouken@3541
   208
    if (y < 0) {
slouken@3541
   209
        code |= CODE_TOP;
slouken@3541
   210
    } else if (y >= rect->y + rect->h) {
slouken@3541
   211
        code |= CODE_BOTTOM;
slouken@3541
   212
    }
slouken@3541
   213
    if (x < 0) {
slouken@3541
   214
        code |= CODE_LEFT;
slouken@3541
   215
    } else if (x >= rect->x + rect->w) {
slouken@3541
   216
        code |= CODE_RIGHT;
slouken@3541
   217
    }
slouken@3541
   218
    return code;
slouken@3541
   219
}
slouken@3541
   220
slouken@3536
   221
SDL_bool
slouken@2920
   222
SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
slouken@2920
   223
                         int *Y2)
slouken@2909
   224
{
slouken@3541
   225
    int x, y;
slouken@2909
   226
    int x1, y1;
slouken@2909
   227
    int x2, y2;
slouken@2909
   228
    int rectx1;
slouken@2909
   229
    int recty1;
slouken@2909
   230
    int rectx2;
slouken@2909
   231
    int recty2;
slouken@3541
   232
    int outcode1, outcode2;
slouken@2909
   233
slouken@2909
   234
    if (!rect || !X1 || !Y1 || !X2 || !Y2) {
slouken@3046
   235
        return SDL_FALSE;
slouken@2909
   236
    }
slouken@2909
   237
slouken@2909
   238
    x1 = *X1;
slouken@2909
   239
    y1 = *Y1;
slouken@2909
   240
    x2 = *X2;
slouken@2909
   241
    y2 = *Y2;
slouken@2909
   242
    rectx1 = rect->x;
slouken@2909
   243
    recty1 = rect->y;
slouken@2909
   244
    rectx2 = rect->x + rect->w - 1;
slouken@2909
   245
    recty2 = rect->y + rect->h - 1;
slouken@2909
   246
slouken@2909
   247
    /* Check to see if entire line is inside rect */
slouken@2909
   248
    if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
slouken@2909
   249
        y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
slouken@2909
   250
        return SDL_TRUE;
slouken@2909
   251
    }
slouken@2909
   252
slouken@2994
   253
    /* Check to see if entire line is to one side of rect */
slouken@2909
   254
    if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
slouken@3004
   255
        (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
slouken@2909
   256
        return SDL_FALSE;
slouken@2909
   257
    }
slouken@2909
   258
slouken@2994
   259
    if (y1 == y2) {
slouken@2909
   260
        /* Horizontal line, easy to clip */
slouken@2909
   261
        if (x1 < rectx1) {
slouken@2909
   262
            *X1 = rectx1;
slouken@2909
   263
        } else if (x1 > rectx2) {
slouken@2909
   264
            *X1 = rectx2;
slouken@2909
   265
        }
slouken@2909
   266
        if (x2 < rectx1) {
slouken@2909
   267
            *X2 = rectx1;
slouken@2909
   268
        } else if (x2 > rectx2) {
slouken@2909
   269
            *X2 = rectx2;
slouken@2909
   270
        }
slouken@2909
   271
        return SDL_TRUE;
slouken@2909
   272
    }
slouken@2909
   273
slouken@2909
   274
    if (x1 == x2) {
slouken@2909
   275
        /* Vertical line, easy to clip */
slouken@2909
   276
        if (y1 < recty1) {
slouken@2909
   277
            *Y1 = recty1;
slouken@2909
   278
        } else if (y1 > recty2) {
slouken@2909
   279
            *Y1 = recty2;
slouken@2909
   280
        }
slouken@2909
   281
        if (y2 < recty1) {
slouken@2909
   282
            *Y2 = recty1;
slouken@2909
   283
        } else if (y2 > recty2) {
slouken@2909
   284
            *Y2 = recty2;
slouken@2909
   285
        }
slouken@2909
   286
        return SDL_TRUE;
slouken@2909
   287
    }
slouken@2909
   288
slouken@3541
   289
    /* More complicated Cohen-Sutherland algorithm */
slouken@3541
   290
    outcode1 = ComputeOutCode(rect, x1, y1);
slouken@3541
   291
    outcode2 = ComputeOutCode(rect, x2, y2);
slouken@3541
   292
    while (outcode1 || outcode2) {
slouken@3541
   293
        if (outcode1 & outcode2) {
slouken@3541
   294
            return SDL_FALSE;
slouken@2994
   295
        }
slouken@2994
   296
slouken@3541
   297
        if (outcode1) {
slouken@3541
   298
            if (outcode1 & CODE_TOP) {
slouken@3541
   299
                y = recty1;
slouken@3541
   300
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   301
            } else if (outcode1 & CODE_BOTTOM) {
slouken@3541
   302
                y = recty2;
slouken@3541
   303
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   304
            } else if (outcode1 & CODE_LEFT) {
slouken@3541
   305
                x = rectx1;
slouken@3541
   306
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   307
            } else if (outcode1 & CODE_RIGHT) {
slouken@3541
   308
                x = rectx2;
slouken@3541
   309
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   310
            }
slouken@3541
   311
            x1 = x;
slouken@3541
   312
            y1 = y;
slouken@3541
   313
            outcode1 = ComputeOutCode(rect, x, y);
slouken@3542
   314
        } else {
slouken@3541
   315
            if (outcode2 & CODE_TOP) {
slouken@3541
   316
                y = recty1;
slouken@3541
   317
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   318
            } else if (outcode2 & CODE_BOTTOM) {
slouken@3541
   319
                y = recty2;
slouken@3541
   320
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   321
            } else if (outcode2 & CODE_LEFT) {
slouken@3541
   322
                x = rectx1;
slouken@3541
   323
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   324
            } else if (outcode2 & CODE_RIGHT) {
slouken@3541
   325
                x = rectx2;
slouken@3541
   326
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   327
            }
slouken@3541
   328
            x2 = x;
slouken@3541
   329
            y2 = y;
slouken@3541
   330
            outcode2 = ComputeOutCode(rect, x, y);
slouken@2994
   331
        }
slouken@2994
   332
    }
slouken@3541
   333
    *X1 = x1;
slouken@3541
   334
    *Y1 = y1;
slouken@3541
   335
    *X2 = x2;
slouken@3541
   336
    *Y2 = y2;
slouken@3541
   337
    return SDL_TRUE;
slouken@2909
   338
}
slouken@2909
   339
slouken@1895
   340
void
slouken@1895
   341
SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
slouken@1895
   342
{
slouken@1895
   343
    SDL_DirtyRect *dirty;
slouken@2223
   344
slouken@2223
   345
    /* FIXME: At what point is this optimization too expensive? */
slouken@2223
   346
    for (dirty = list->list; dirty; dirty = dirty->next) {
slouken@2223
   347
        if (SDL_HasIntersection(&dirty->rect, rect)) {
slouken@2223
   348
            SDL_UnionRect(&dirty->rect, rect, &dirty->rect);
slouken@2223
   349
            return;
slouken@2223
   350
        }
slouken@2223
   351
    }
slouken@1895
   352
slouken@1895
   353
    if (list->free) {
slouken@1895
   354
        dirty = list->free;
slouken@1895
   355
        list->free = dirty->next;
slouken@1895
   356
    } else {
slouken@1895
   357
        dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
slouken@1895
   358
        if (!dirty) {
slouken@1895
   359
            return;
slouken@1895
   360
        }
slouken@1895
   361
    }
slouken@1895
   362
    dirty->rect = *rect;
slouken@1895
   363
    dirty->next = list->list;
slouken@1895
   364
    list->list = dirty;
slouken@1895
   365
}
slouken@1895
   366
slouken@1895
   367
void
slouken@1895
   368
SDL_ClearDirtyRects(SDL_DirtyRectList * list)
slouken@1895
   369
{
slouken@2224
   370
    SDL_DirtyRect *prev, *curr;
slouken@2224
   371
slouken@2224
   372
    /* Skip to the end of the free list */
slouken@2224
   373
    prev = NULL;
slouken@2224
   374
    for (curr = list->free; curr; curr = curr->next) {
slouken@2224
   375
        prev = curr;
slouken@2224
   376
    }
slouken@2224
   377
slouken@2224
   378
    /* Add the list entries to the end */
slouken@2224
   379
    if (prev) {
slouken@2224
   380
        prev->next = list->list;
slouken@2224
   381
    } else {
slouken@2224
   382
        list->free = list->list;
slouken@2224
   383
    }
slouken@2223
   384
    list->list = NULL;
slouken@1895
   385
}
slouken@1895
   386
slouken@1895
   387
void
slouken@1895
   388
SDL_FreeDirtyRects(SDL_DirtyRectList * list)
slouken@1895
   389
{
slouken@1895
   390
    while (list->list) {
slouken@1895
   391
        SDL_DirtyRect *elem = list->list;
slouken@1895
   392
        list->list = elem->next;
slouken@1895
   393
        SDL_free(elem);
slouken@1895
   394
    }
slouken@1895
   395
    while (list->free) {
slouken@1895
   396
        SDL_DirtyRect *elem = list->free;
slouken@1895
   397
        list->free = elem->next;
slouken@1895
   398
        SDL_free(elem);
slouken@1895
   399
    }
slouken@1895
   400
}
slouken@1895
   401
slouken@1895
   402
/* vi: set ts=4 sw=4 expandtab: */