src/video/SDL_blendfillrect.c
changeset 3593 b931bcfd94a0
parent 3536 0267b8b1595c
child 3594 c8bed77b0386
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/SDL_blendfillrect.c	Fri Dec 18 07:03:09 2009 +0000
     1.3 @@ -0,0 +1,357 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2009 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_draw.h"
    1.29 +
    1.30 +static int
    1.31 +SDL_BlendFillRect_RGB555(SDL_Surface * dst, const SDL_Rect * rect,
    1.32 +                         int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    1.33 +{
    1.34 +    unsigned inva = 0xff - a;
    1.35 +
    1.36 +    switch (blendMode) {
    1.37 +    case SDL_BLENDMODE_BLEND:
    1.38 +        FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB555);
    1.39 +        break;
    1.40 +    case SDL_BLENDMODE_ADD:
    1.41 +        FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB555);
    1.42 +        break;
    1.43 +    case SDL_BLENDMODE_MOD:
    1.44 +        FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB555);
    1.45 +        break;
    1.46 +    default:
    1.47 +        FILLRECT(Uint16, DRAW_SETPIXEL_RGB555);
    1.48 +        break;
    1.49 +    }
    1.50 +    return 0;
    1.51 +}
    1.52 +
    1.53 +static int
    1.54 +SDL_BlendFillRect_RGB565(SDL_Surface * dst, const SDL_Rect * rect,
    1.55 +                         int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    1.56 +{
    1.57 +    unsigned inva = 0xff - a;
    1.58 +
    1.59 +    switch (blendMode) {
    1.60 +    case SDL_BLENDMODE_BLEND:
    1.61 +        FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB565);
    1.62 +        break;
    1.63 +    case SDL_BLENDMODE_ADD:
    1.64 +        FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB565);
    1.65 +        break;
    1.66 +    case SDL_BLENDMODE_MOD:
    1.67 +        FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB565);
    1.68 +        break;
    1.69 +    default:
    1.70 +        FILLRECT(Uint16, DRAW_SETPIXEL_RGB565);
    1.71 +        break;
    1.72 +    }
    1.73 +    return 0;
    1.74 +}
    1.75 +
    1.76 +static int
    1.77 +SDL_BlendFillRect_RGB888(SDL_Surface * dst, const SDL_Rect * rect,
    1.78 +                         int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    1.79 +{
    1.80 +    unsigned inva = 0xff - a;
    1.81 +
    1.82 +    switch (blendMode) {
    1.83 +    case SDL_BLENDMODE_BLEND:
    1.84 +        FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB888);
    1.85 +        break;
    1.86 +    case SDL_BLENDMODE_ADD:
    1.87 +        FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGB888);
    1.88 +        break;
    1.89 +    case SDL_BLENDMODE_MOD:
    1.90 +        FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGB888);
    1.91 +        break;
    1.92 +    default:
    1.93 +        FILLRECT(Uint32, DRAW_SETPIXEL_RGB888);
    1.94 +        break;
    1.95 +    }
    1.96 +    return 0;
    1.97 +}
    1.98 +
    1.99 +static int
   1.100 +SDL_BlendFillRect_ARGB8888(SDL_Surface * dst, const SDL_Rect * rect,
   1.101 +                           int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.102 +{
   1.103 +    unsigned inva = 0xff - a;
   1.104 +
   1.105 +    switch (blendMode) {
   1.106 +    case SDL_BLENDMODE_BLEND:
   1.107 +        FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888);
   1.108 +        break;
   1.109 +    case SDL_BLENDMODE_ADD:
   1.110 +        FILLRECT(Uint32, DRAW_SETPIXEL_ADD_ARGB8888);
   1.111 +        break;
   1.112 +    case SDL_BLENDMODE_MOD:
   1.113 +        FILLRECT(Uint32, DRAW_SETPIXEL_MOD_ARGB8888);
   1.114 +        break;
   1.115 +    default:
   1.116 +        FILLRECT(Uint32, DRAW_SETPIXEL_ARGB8888);
   1.117 +        break;
   1.118 +    }
   1.119 +    return 0;
   1.120 +}
   1.121 +
   1.122 +static int
   1.123 +SDL_BlendFillRect_RGB(SDL_Surface * dst, const SDL_Rect * rect,
   1.124 +                      int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.125 +{
   1.126 +    SDL_PixelFormat *fmt = dst->format;
   1.127 +    unsigned inva = 0xff - a;
   1.128 +
   1.129 +    switch (fmt->BytesPerPixel) {
   1.130 +    case 2:
   1.131 +        switch (blendMode) {
   1.132 +        case SDL_BLENDMODE_BLEND:
   1.133 +            FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB);
   1.134 +            break;
   1.135 +        case SDL_BLENDMODE_ADD:
   1.136 +            FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB);
   1.137 +            break;
   1.138 +        case SDL_BLENDMODE_MOD:
   1.139 +            FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB);
   1.140 +            break;
   1.141 +        default:
   1.142 +            FILLRECT(Uint16, DRAW_SETPIXEL_RGB);
   1.143 +            break;
   1.144 +        }
   1.145 +        return 0;
   1.146 +    case 4:
   1.147 +        switch (blendMode) {
   1.148 +        case SDL_BLENDMODE_BLEND:
   1.149 +            FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB);
   1.150 +            break;
   1.151 +        case SDL_BLENDMODE_ADD:
   1.152 +            FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGB);
   1.153 +            break;
   1.154 +        case SDL_BLENDMODE_MOD:
   1.155 +            FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGB);
   1.156 +            break;
   1.157 +        default:
   1.158 +            FILLRECT(Uint32, DRAW_SETPIXEL_RGB);
   1.159 +            break;
   1.160 +        }
   1.161 +        return 0;
   1.162 +    default:
   1.163 +        SDL_Unsupported();
   1.164 +        return -1;
   1.165 +    }
   1.166 +}
   1.167 +
   1.168 +static int
   1.169 +SDL_BlendFillRect_RGBA(SDL_Surface * dst, const SDL_Rect * rect,
   1.170 +                       int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.171 +{
   1.172 +    SDL_PixelFormat *fmt = dst->format;
   1.173 +    unsigned inva = 0xff - a;
   1.174 +
   1.175 +    switch (fmt->BytesPerPixel) {
   1.176 +    case 4:
   1.177 +        switch (blendMode) {
   1.178 +        case SDL_BLENDMODE_BLEND:
   1.179 +            FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGBA);
   1.180 +            break;
   1.181 +        case SDL_BLENDMODE_ADD:
   1.182 +            FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGBA);
   1.183 +            break;
   1.184 +        case SDL_BLENDMODE_MOD:
   1.185 +            FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGBA);
   1.186 +            break;
   1.187 +        default:
   1.188 +            FILLRECT(Uint32, DRAW_SETPIXEL_RGBA);
   1.189 +            break;
   1.190 +        }
   1.191 +        return 0;
   1.192 +    default:
   1.193 +        SDL_Unsupported();
   1.194 +        return -1;
   1.195 +    }
   1.196 +}
   1.197 +
   1.198 +int
   1.199 +SDL_BlendFillRect(SDL_Surface * dst, const SDL_Rect * rect,
   1.200 +                  int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.201 +{
   1.202 +    SDL_Rect clipped;
   1.203 +
   1.204 +    if (!dst) {
   1.205 +        SDL_SetError("Passed NULL destination surface");
   1.206 +        return -1;
   1.207 +    }
   1.208 +
   1.209 +    if (blendMode < SDL_BLENDMODE_BLEND) {
   1.210 +        Uint32 color = SDL_MapRGBA(dst->format, r, g, b, a);
   1.211 +        return SDL_FillRect(dst, rect, color);
   1.212 +    }
   1.213 +
   1.214 +    /* This function doesn't work on surfaces < 8 bpp */
   1.215 +    if (dst->format->BitsPerPixel < 8) {
   1.216 +        SDL_SetError("SDL_BlendFillRect(): Unsupported surface format");
   1.217 +        return -1;
   1.218 +    }
   1.219 +
   1.220 +    /* If 'rect' == NULL, then fill the whole surface */
   1.221 +    if (rect) {
   1.222 +        /* Perform clipping */
   1.223 +        if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
   1.224 +            return 0;
   1.225 +        }
   1.226 +        rect = &clipped;
   1.227 +    } else {
   1.228 +        rect = &dst->clip_rect;
   1.229 +    }
   1.230 +
   1.231 +    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
   1.232 +        r = DRAW_MUL(r, a);
   1.233 +        g = DRAW_MUL(g, a);
   1.234 +        b = DRAW_MUL(b, a);
   1.235 +    }
   1.236 +
   1.237 +    switch (dst->format->BitsPerPixel) {
   1.238 +    case 15:
   1.239 +        switch (dst->format->Rmask) {
   1.240 +        case 0x7C00:
   1.241 +            return SDL_BlendFillRect_RGB555(dst, rect, blendMode, r, g, b, a);
   1.242 +        }
   1.243 +        break;
   1.244 +    case 16:
   1.245 +        switch (dst->format->Rmask) {
   1.246 +        case 0xF800:
   1.247 +            return SDL_BlendFillRect_RGB565(dst, rect, blendMode, r, g, b, a);
   1.248 +        }
   1.249 +        break;
   1.250 +    case 32:
   1.251 +        switch (dst->format->Rmask) {
   1.252 +        case 0x00FF0000:
   1.253 +            if (!dst->format->Amask) {
   1.254 +                return SDL_BlendFillRect_RGB888(dst, rect, blendMode, r, g, b, a);
   1.255 +            } else {
   1.256 +                return SDL_BlendFillRect_ARGB8888(dst, rect, blendMode, r, g, b, a);
   1.257 +            }
   1.258 +            break;
   1.259 +        }
   1.260 +        break;
   1.261 +    default:
   1.262 +        break;
   1.263 +    }
   1.264 +
   1.265 +    if (!dst->format->Amask) {
   1.266 +        return SDL_BlendFillRect_RGB(dst, rect, blendMode, r, g, b, a);
   1.267 +    } else {
   1.268 +        return SDL_BlendFillRect_RGBA(dst, rect, blendMode, r, g, b, a);
   1.269 +    }
   1.270 +}
   1.271 +
   1.272 +int
   1.273 +SDL_BlendFillRects(SDL_Surface * dst, const SDL_Rect ** rects, int count,
   1.274 +                   int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.275 +{
   1.276 +    SDL_Rect clipped;
   1.277 +    int i;
   1.278 +    int (*func)(SDL_Surface * dst, const SDL_Rect * rect,
   1.279 +                int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a) = NULL;
   1.280 +    int status = 0;
   1.281 +
   1.282 +    if (!dst) {
   1.283 +        SDL_SetError("Passed NULL destination surface");
   1.284 +        return -1;
   1.285 +    }
   1.286 +
   1.287 +    if (blendMode < SDL_BLENDMODE_BLEND) {
   1.288 +        Uint32 color = SDL_MapRGBA(dst->format, r, g, b, a);
   1.289 +        return SDL_FillRects(dst, rects, color);
   1.290 +    }
   1.291 +
   1.292 +    /* This function doesn't work on surfaces < 8 bpp */
   1.293 +    if (dst->format->BitsPerPixel < 8) {
   1.294 +        SDL_SetError("SDL_BlendFillRects(): Unsupported surface format");
   1.295 +        return -1;
   1.296 +    }
   1.297 +
   1.298 +    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
   1.299 +        r = DRAW_MUL(r, a);
   1.300 +        g = DRAW_MUL(g, a);
   1.301 +        b = DRAW_MUL(b, a);
   1.302 +    }
   1.303 +
   1.304 +    /* FIXME: Does this function pointer slow things down significantly? */
   1.305 +    switch (dst->format->BitsPerPixel) {
   1.306 +    case 15:
   1.307 +        switch (dst->format->Rmask) {
   1.308 +        case 0x7C00:
   1.309 +            func = SDL_BlendFillRect_RGB555;
   1.310 +        }
   1.311 +        break;
   1.312 +    case 16:
   1.313 +        switch (dst->format->Rmask) {
   1.314 +        case 0xF800:
   1.315 +            func = SDL_BlendFillRect_RGB565;
   1.316 +        }
   1.317 +        break;
   1.318 +    case 32:
   1.319 +        switch (dst->format->Rmask) {
   1.320 +        case 0x00FF0000:
   1.321 +            if (!dst->format->Amask) {
   1.322 +                func = SDL_BlendFillRect_RGB888;
   1.323 +            } else {
   1.324 +                func = SDL_BlendFillRect_ARGB8888;
   1.325 +            }
   1.326 +            break;
   1.327 +        }
   1.328 +        break;
   1.329 +    default:
   1.330 +        break;
   1.331 +    }
   1.332 +
   1.333 +    if (!func) {
   1.334 +        if (!dst->format->Amask) {
   1.335 +            func = SDL_BlendFillRect_RGB;
   1.336 +        } else {
   1.337 +            func = SDL_BlendFillRect_RGBA;
   1.338 +        }
   1.339 +    }
   1.340 +
   1.341 +    for (i = 0; i < count; ++i) {
   1.342 +        const SDL_Rect * rect = rects[i];
   1.343 +
   1.344 +        /* If 'rect' == NULL, then fill the whole surface */
   1.345 +        if (rect) {
   1.346 +            /* Perform clipping */
   1.347 +            if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
   1.348 +                continue;
   1.349 +            }
   1.350 +            rect = &clipped;
   1.351 +        } else {
   1.352 +            rect = &dst->clip_rect;
   1.353 +        }
   1.354 +
   1.355 +        status = func(dst, rect, blendMode, r, g, b, a);
   1.356 +    }
   1.357 +    return status;
   1.358 +}
   1.359 +
   1.360 +/* vi: set ts=4 sw=4 expandtab: */