/* SDL - Simple DirectMedia Layer Copyright (C) 1997-2009 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Sam Lantinga slouken@libsdl.org */ #include "SDL_config.h" #include "SDL_video.h" #include "SDL_blit.h" #define MUL(_a, _b) (((Uint16)(_a)*(Uint16)(_b))/255) #define SHIFTAND(_v, _s, _a) (((_v)>>(_s)) & (_a)) #define SETPIXEL_MASK(p, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \ do { \ if (a) { \ p = (r<rmask) sr = rmask; \ if (sg>gmask) sg = gmask; \ if (sb>bmask) sb = bmask; \ p = (sr<y; \ int h = dstrect->h; \ while (h--) { \ type *pixel = (type *)(dst->pixels + y * dst->pitch + dstrect->x * dst->format->BytesPerPixel); \ int w = dstrect->w; \ while (w--) { \ op(*pixel); \ pixel++; \ } \ y++; \ } \ } while (0) int SDL_BlendRect(SDL_Surface * dst, SDL_Rect * dstrect, int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { Uint16 inva = 0xff - a; /* This function doesn't work on surfaces < 8 bpp */ if (dst->format->BitsPerPixel < 8) { SDL_SetError("SDL_BlendRect(): Unsupported surface format"); return (-1); } /* If 'dstrect' == NULL, then fill the whole surface */ if (dstrect) { /* Perform clipping */ if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) { return (0); } } else { dstrect = &dst->clip_rect; } if ((blendMode == SDL_BLENDMODE_BLEND) || (blendMode == SDL_BLENDMODE_ADD)) { r = MUL(r, a); g = MUL(g, a); b = MUL(b, a); } switch (dst->format->BitsPerPixel) { case 15: switch (blendMode) { case SDL_BLENDMODE_MASK: BLENDRECT(Uint16, SETPIXEL15_MASK); break; case SDL_BLENDMODE_BLEND: BLENDRECT(Uint16, SETPIXEL15_BLEND); break; case SDL_BLENDMODE_ADD: BLENDRECT(Uint16, SETPIXEL15_ADD); break; case SDL_BLENDMODE_MOD: BLENDRECT(Uint16, SETPIXEL15_MOD); break; } break; case 16: switch (blendMode) { case SDL_BLENDMODE_MASK: BLENDRECT(Uint16, SETPIXEL16_MASK); break; case SDL_BLENDMODE_BLEND: BLENDRECT(Uint16, SETPIXEL16_BLEND); break; case SDL_BLENDMODE_ADD: BLENDRECT(Uint16, SETPIXEL16_ADD); break; case SDL_BLENDMODE_MOD: BLENDRECT(Uint16, SETPIXEL16_MOD); break; } break; case 24: case 32: if (dst->format->BytesPerPixel != 4) { SDL_Unsupported(); return -1; } switch (blendMode) { case SDL_BLENDMODE_MASK: BLENDRECT(Uint32, SETPIXEL32_MASK); break; case SDL_BLENDMODE_BLEND: BLENDRECT(Uint32, SETPIXEL32_BLEND); break; case SDL_BLENDMODE_ADD: BLENDRECT(Uint32, SETPIXEL32_ADD); break; case SDL_BLENDMODE_MOD: BLENDRECT(Uint32, SETPIXEL32_MOD); break; } break; default: SDL_Unsupported(); return -1; } return 0; return -1; } /* vi: set ts=4 sw=4 expandtab: */