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