src/video/SDL_rect.c
branchSDL-1.3
changeset 1735 8dd28c4ef746
child 2223 175754591a13
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/SDL_rect.c	Mon Jul 10 07:34:50 2006 +0000
     1.3 @@ -0,0 +1,189 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include "SDL_video.h"
    1.28 +#include "SDL_rect_c.h"
    1.29 +
    1.30 +SDL_bool
    1.31 +SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
    1.32 +{
    1.33 +    int Amin, Amax, Bmin, Bmax;
    1.34 +
    1.35 +    /* Horizontal intersection */
    1.36 +    Amin = A->x;
    1.37 +    Amax = Amin + A->w;
    1.38 +    Bmin = B->x;
    1.39 +    Bmax = Bmin + B->w;
    1.40 +    if (Bmin > Amin)
    1.41 +        Amin = Bmin;
    1.42 +    if (Bmax < Amax)
    1.43 +        Amax = Bmax;
    1.44 +    if (Amax <= Amin)
    1.45 +        return SDL_FALSE;
    1.46 +
    1.47 +    /* Vertical intersection */
    1.48 +    Amin = A->y;
    1.49 +    Amax = Amin + A->h;
    1.50 +    Bmin = B->y;
    1.51 +    Bmax = Bmin + B->h;
    1.52 +    if (Bmin > Amin)
    1.53 +        Amin = Bmin;
    1.54 +    if (Bmax < Amax)
    1.55 +        Amax = Bmax;
    1.56 +    if (Amax <= Amin)
    1.57 +        return SDL_FALSE;
    1.58 +
    1.59 +    return SDL_TRUE;
    1.60 +}
    1.61 +
    1.62 +SDL_bool
    1.63 +SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
    1.64 +{
    1.65 +    int Amin, Amax, Bmin, Bmax;
    1.66 +
    1.67 +    /* Horizontal intersection */
    1.68 +    Amin = A->x;
    1.69 +    Amax = Amin + A->w;
    1.70 +    Bmin = B->x;
    1.71 +    Bmax = Bmin + B->w;
    1.72 +    if (Bmin > Amin)
    1.73 +        Amin = Bmin;
    1.74 +    result->x = Amin;
    1.75 +    if (Bmax < Amax)
    1.76 +        Amax = Bmax;
    1.77 +    result->w = Amax - Amin;
    1.78 +
    1.79 +    /* Vertical intersection */
    1.80 +    Amin = A->y;
    1.81 +    Amax = Amin + A->h;
    1.82 +    Bmin = B->y;
    1.83 +    Bmax = Bmin + B->h;
    1.84 +    if (Bmin > Amin)
    1.85 +        Amin = Bmin;
    1.86 +    result->y = Amin;
    1.87 +    if (Bmax < Amax)
    1.88 +        Amax = Bmax;
    1.89 +    result->h = Amax - Amin;
    1.90 +
    1.91 +    return !SDL_RectEmpty(result);
    1.92 +}
    1.93 +
    1.94 +void
    1.95 +SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
    1.96 +{
    1.97 +    int Amin, Amax, Bmin, Bmax;
    1.98 +
    1.99 +    /* Horizontal union */
   1.100 +    Amin = A->x;
   1.101 +    Amax = Amin + A->w;
   1.102 +    Bmin = B->x;
   1.103 +    Bmax = Bmin + B->w;
   1.104 +    if (Bmin < Amin)
   1.105 +        Amin = Bmin;
   1.106 +    result->x = Amin;
   1.107 +    if (Bmax > Amax)
   1.108 +        Amax = Bmax;
   1.109 +    result->w = Amax - Amin;
   1.110 +
   1.111 +    /* Vertical intersection */
   1.112 +    Amin = A->y;
   1.113 +    Amax = Amin + A->h;
   1.114 +    Bmin = B->y;
   1.115 +    Bmax = Bmin + B->h;
   1.116 +    if (Bmin < Amin)
   1.117 +        Amin = Bmin;
   1.118 +    result->y = Amin;
   1.119 +    if (Bmax > Amax)
   1.120 +        Amax = Bmax;
   1.121 +    result->h = Amax - Amin;
   1.122 +}
   1.123 +
   1.124 +void
   1.125 +SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
   1.126 +{
   1.127 +    SDL_DirtyRect *dirty;
   1.128 +    SDL_DirtyRect *check, *prev, *next;
   1.129 +
   1.130 +    if (list->free) {
   1.131 +        dirty = list->free;
   1.132 +        list->free = dirty->next;
   1.133 +    } else {
   1.134 +        dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
   1.135 +        if (!dirty) {
   1.136 +            return;
   1.137 +        }
   1.138 +    }
   1.139 +    dirty->rect = *rect;
   1.140 +
   1.141 +    /* FIXME: At what point is this optimization too expensive? */
   1.142 +    for (prev = NULL, check = list->list; check; check = next) {
   1.143 +        next = check->next;
   1.144 +
   1.145 +        if (SDL_HasIntersection(&dirty->rect, &check->rect)) {
   1.146 +            SDL_UnionRect(&dirty->rect, &check->rect, &dirty->rect);
   1.147 +            if (prev) {
   1.148 +                prev->next = next;
   1.149 +            } else {
   1.150 +                list->list = next;
   1.151 +            }
   1.152 +            check->next = list->free;
   1.153 +            list->free = check;
   1.154 +            --list->count;
   1.155 +        } else {
   1.156 +            prev = check;
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    dirty->next = list->list;
   1.161 +    list->list = dirty;
   1.162 +    ++list->count;
   1.163 +}
   1.164 +
   1.165 +void
   1.166 +SDL_ClearDirtyRects(SDL_DirtyRectList * list)
   1.167 +{
   1.168 +    while (list->list) {
   1.169 +        SDL_DirtyRect *elem = list->list;
   1.170 +        list->list = elem->next;
   1.171 +        elem->next = list->free;
   1.172 +        list->free = elem;
   1.173 +    }
   1.174 +    list->count = 0;
   1.175 +}
   1.176 +
   1.177 +void
   1.178 +SDL_FreeDirtyRects(SDL_DirtyRectList * list)
   1.179 +{
   1.180 +    while (list->list) {
   1.181 +        SDL_DirtyRect *elem = list->list;
   1.182 +        list->list = elem->next;
   1.183 +        SDL_free(elem);
   1.184 +    }
   1.185 +    while (list->free) {
   1.186 +        SDL_DirtyRect *elem = list->free;
   1.187 +        list->free = elem->next;
   1.188 +        SDL_free(elem);
   1.189 +    }
   1.190 +}
   1.191 +
   1.192 +/* vi: set ts=4 sw=4 expandtab: */