src/video/SDL_rect.c
author Sam Lantinga
Mon, 10 Jul 2006 07:34:50 +0000
branchSDL-1.3
changeset 1735 8dd28c4ef746
child 2223 175754591a13
permissions -rw-r--r--
SDL_Rect now uses int for position and size.
Added a few more rectangle functions.
Added a dirty rectangle list implementation.
slouken@1735
     1
/*
slouken@1735
     2
    SDL - Simple DirectMedia Layer
slouken@1735
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1735
     4
slouken@1735
     5
    This library is free software; you can redistribute it and/or
slouken@1735
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1735
     7
    License as published by the Free Software Foundation; either
slouken@1735
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1735
     9
slouken@1735
    10
    This library is distributed in the hope that it will be useful,
slouken@1735
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1735
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1735
    13
    Lesser General Public License for more details.
slouken@1735
    14
slouken@1735
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1735
    16
    License along with this library; if not, write to the Free Software
slouken@1735
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1735
    18
slouken@1735
    19
    Sam Lantinga
slouken@1735
    20
    slouken@libsdl.org
slouken@1735
    21
*/
slouken@1735
    22
#include "SDL_config.h"
slouken@1735
    23
slouken@1735
    24
#include "SDL_video.h"
slouken@1735
    25
#include "SDL_rect_c.h"
slouken@1735
    26
slouken@1735
    27
SDL_bool
slouken@1735
    28
SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
slouken@1735
    29
{
slouken@1735
    30
    int Amin, Amax, Bmin, Bmax;
slouken@1735
    31
slouken@1735
    32
    /* Horizontal intersection */
slouken@1735
    33
    Amin = A->x;
slouken@1735
    34
    Amax = Amin + A->w;
slouken@1735
    35
    Bmin = B->x;
slouken@1735
    36
    Bmax = Bmin + B->w;
slouken@1735
    37
    if (Bmin > Amin)
slouken@1735
    38
        Amin = Bmin;
slouken@1735
    39
    if (Bmax < Amax)
slouken@1735
    40
        Amax = Bmax;
slouken@1735
    41
    if (Amax <= Amin)
slouken@1735
    42
        return SDL_FALSE;
slouken@1735
    43
slouken@1735
    44
    /* Vertical intersection */
slouken@1735
    45
    Amin = A->y;
slouken@1735
    46
    Amax = Amin + A->h;
slouken@1735
    47
    Bmin = B->y;
slouken@1735
    48
    Bmax = Bmin + B->h;
slouken@1735
    49
    if (Bmin > Amin)
slouken@1735
    50
        Amin = Bmin;
slouken@1735
    51
    if (Bmax < Amax)
slouken@1735
    52
        Amax = Bmax;
slouken@1735
    53
    if (Amax <= Amin)
slouken@1735
    54
        return SDL_FALSE;
slouken@1735
    55
slouken@1735
    56
    return SDL_TRUE;
slouken@1735
    57
}
slouken@1735
    58
slouken@1735
    59
SDL_bool
slouken@1735
    60
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1735
    61
{
slouken@1735
    62
    int Amin, Amax, Bmin, Bmax;
slouken@1735
    63
slouken@1735
    64
    /* Horizontal intersection */
slouken@1735
    65
    Amin = A->x;
slouken@1735
    66
    Amax = Amin + A->w;
slouken@1735
    67
    Bmin = B->x;
slouken@1735
    68
    Bmax = Bmin + B->w;
slouken@1735
    69
    if (Bmin > Amin)
slouken@1735
    70
        Amin = Bmin;
slouken@1735
    71
    result->x = Amin;
slouken@1735
    72
    if (Bmax < Amax)
slouken@1735
    73
        Amax = Bmax;
slouken@1735
    74
    result->w = Amax - Amin;
slouken@1735
    75
slouken@1735
    76
    /* Vertical intersection */
slouken@1735
    77
    Amin = A->y;
slouken@1735
    78
    Amax = Amin + A->h;
slouken@1735
    79
    Bmin = B->y;
slouken@1735
    80
    Bmax = Bmin + B->h;
slouken@1735
    81
    if (Bmin > Amin)
slouken@1735
    82
        Amin = Bmin;
slouken@1735
    83
    result->y = Amin;
slouken@1735
    84
    if (Bmax < Amax)
slouken@1735
    85
        Amax = Bmax;
slouken@1735
    86
    result->h = Amax - Amin;
slouken@1735
    87
slouken@1735
    88
    return !SDL_RectEmpty(result);
slouken@1735
    89
}
slouken@1735
    90
slouken@1735
    91
void
slouken@1735
    92
SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1735
    93
{
slouken@1735
    94
    int Amin, Amax, Bmin, Bmax;
slouken@1735
    95
slouken@1735
    96
    /* Horizontal union */
slouken@1735
    97
    Amin = A->x;
slouken@1735
    98
    Amax = Amin + A->w;
slouken@1735
    99
    Bmin = B->x;
slouken@1735
   100
    Bmax = Bmin + B->w;
slouken@1735
   101
    if (Bmin < Amin)
slouken@1735
   102
        Amin = Bmin;
slouken@1735
   103
    result->x = Amin;
slouken@1735
   104
    if (Bmax > Amax)
slouken@1735
   105
        Amax = Bmax;
slouken@1735
   106
    result->w = Amax - Amin;
slouken@1735
   107
slouken@1735
   108
    /* Vertical intersection */
slouken@1735
   109
    Amin = A->y;
slouken@1735
   110
    Amax = Amin + A->h;
slouken@1735
   111
    Bmin = B->y;
slouken@1735
   112
    Bmax = Bmin + B->h;
slouken@1735
   113
    if (Bmin < Amin)
slouken@1735
   114
        Amin = Bmin;
slouken@1735
   115
    result->y = Amin;
slouken@1735
   116
    if (Bmax > Amax)
slouken@1735
   117
        Amax = Bmax;
slouken@1735
   118
    result->h = Amax - Amin;
slouken@1735
   119
}
slouken@1735
   120
slouken@1735
   121
void
slouken@1735
   122
SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
slouken@1735
   123
{
slouken@1735
   124
    SDL_DirtyRect *dirty;
slouken@1735
   125
    SDL_DirtyRect *check, *prev, *next;
slouken@1735
   126
slouken@1735
   127
    if (list->free) {
slouken@1735
   128
        dirty = list->free;
slouken@1735
   129
        list->free = dirty->next;
slouken@1735
   130
    } else {
slouken@1735
   131
        dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
slouken@1735
   132
        if (!dirty) {
slouken@1735
   133
            return;
slouken@1735
   134
        }
slouken@1735
   135
    }
slouken@1735
   136
    dirty->rect = *rect;
slouken@1735
   137
slouken@1735
   138
    /* FIXME: At what point is this optimization too expensive? */
slouken@1735
   139
    for (prev = NULL, check = list->list; check; check = next) {
slouken@1735
   140
        next = check->next;
slouken@1735
   141
slouken@1735
   142
        if (SDL_HasIntersection(&dirty->rect, &check->rect)) {
slouken@1735
   143
            SDL_UnionRect(&dirty->rect, &check->rect, &dirty->rect);
slouken@1735
   144
            if (prev) {
slouken@1735
   145
                prev->next = next;
slouken@1735
   146
            } else {
slouken@1735
   147
                list->list = next;
slouken@1735
   148
            }
slouken@1735
   149
            check->next = list->free;
slouken@1735
   150
            list->free = check;
slouken@1735
   151
            --list->count;
slouken@1735
   152
        } else {
slouken@1735
   153
            prev = check;
slouken@1735
   154
        }
slouken@1735
   155
    }
slouken@1735
   156
slouken@1735
   157
    dirty->next = list->list;
slouken@1735
   158
    list->list = dirty;
slouken@1735
   159
    ++list->count;
slouken@1735
   160
}
slouken@1735
   161
slouken@1735
   162
void
slouken@1735
   163
SDL_ClearDirtyRects(SDL_DirtyRectList * list)
slouken@1735
   164
{
slouken@1735
   165
    while (list->list) {
slouken@1735
   166
        SDL_DirtyRect *elem = list->list;
slouken@1735
   167
        list->list = elem->next;
slouken@1735
   168
        elem->next = list->free;
slouken@1735
   169
        list->free = elem;
slouken@1735
   170
    }
slouken@1735
   171
    list->count = 0;
slouken@1735
   172
}
slouken@1735
   173
slouken@1735
   174
void
slouken@1735
   175
SDL_FreeDirtyRects(SDL_DirtyRectList * list)
slouken@1735
   176
{
slouken@1735
   177
    while (list->list) {
slouken@1735
   178
        SDL_DirtyRect *elem = list->list;
slouken@1735
   179
        list->list = elem->next;
slouken@1735
   180
        SDL_free(elem);
slouken@1735
   181
    }
slouken@1735
   182
    while (list->free) {
slouken@1735
   183
        SDL_DirtyRect *elem = list->free;
slouken@1735
   184
        list->free = elem->next;
slouken@1735
   185
        SDL_free(elem);
slouken@1735
   186
    }
slouken@1735
   187
}
slouken@1735
   188
slouken@1735
   189
/* vi: set ts=4 sw=4 expandtab: */