src/video/SDL_rect.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 11 Aug 2007 21:34:08 +0000
changeset 2223 175754591a13
parent 1895 c121d94672cb
child 2224 29cc9e9c76bd
permissions -rw-r--r--
Optimized dirty rect code
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 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@1895
   121
void
slouken@1895
   122
SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
slouken@1895
   123
{
slouken@1895
   124
    SDL_DirtyRect *dirty;
slouken@2223
   125
slouken@2223
   126
    /* FIXME: At what point is this optimization too expensive? */
slouken@2223
   127
    for (dirty = list->list; dirty; dirty = dirty->next) {
slouken@2223
   128
        if (SDL_HasIntersection(&dirty->rect, rect)) {
slouken@2223
   129
            SDL_UnionRect(&dirty->rect, rect, &dirty->rect);
slouken@2223
   130
            return;
slouken@2223
   131
        }
slouken@2223
   132
    }
slouken@1895
   133
slouken@1895
   134
    if (list->free) {
slouken@1895
   135
        dirty = list->free;
slouken@1895
   136
        list->free = dirty->next;
slouken@1895
   137
    } else {
slouken@1895
   138
        dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
slouken@1895
   139
        if (!dirty) {
slouken@1895
   140
            return;
slouken@1895
   141
        }
slouken@1895
   142
    }
slouken@1895
   143
    dirty->rect = *rect;
slouken@1895
   144
    dirty->next = list->list;
slouken@1895
   145
    list->list = dirty;
slouken@1895
   146
    ++list->count;
slouken@1895
   147
}
slouken@1895
   148
slouken@1895
   149
void
slouken@1895
   150
SDL_ClearDirtyRects(SDL_DirtyRectList * list)
slouken@1895
   151
{
slouken@2223
   152
    list->free = list->list;
slouken@2223
   153
    list->list = NULL;
slouken@1895
   154
    list->count = 0;
slouken@1895
   155
}
slouken@1895
   156
slouken@1895
   157
void
slouken@1895
   158
SDL_FreeDirtyRects(SDL_DirtyRectList * list)
slouken@1895
   159
{
slouken@1895
   160
    while (list->list) {
slouken@1895
   161
        SDL_DirtyRect *elem = list->list;
slouken@1895
   162
        list->list = elem->next;
slouken@1895
   163
        SDL_free(elem);
slouken@1895
   164
    }
slouken@1895
   165
    while (list->free) {
slouken@1895
   166
        SDL_DirtyRect *elem = list->free;
slouken@1895
   167
        list->free = elem->next;
slouken@1895
   168
        SDL_free(elem);
slouken@1895
   169
    }
slouken@1895
   170
}
slouken@1895
   171
slouken@1895
   172
/* vi: set ts=4 sw=4 expandtab: */