Share code between fill and line drawing
authorSam Lantinga <slouken@libsdl.org>
Sun, 21 Dec 2008 08:28:25 +0000
changeset 2898e40448bc7727
parent 2897 8be863ef68ee
child 2899 a0c837a16e4c
Share code between fill and line drawing
Added general RGB surface format fallbacks to drawing code
Fixed issues with destination surface alpha channel
src/video/SDL_blendline.c
src/video/SDL_blendrect.c
src/video/SDL_blit.h
src/video/SDL_draw.h
src/video/SDL_drawline.c
     1.1 --- a/src/video/SDL_blendline.c	Sun Dec 21 08:24:32 2008 +0000
     1.2 +++ b/src/video/SDL_blendline.c	Sun Dec 21 08:28:25 2008 +0000
     1.3 @@ -21,120 +21,159 @@
     1.4  */
     1.5  #include "SDL_config.h"
     1.6  
     1.7 -#include "SDL_video.h"
     1.8 -#include "SDL_blit.h"
     1.9 +#include "SDL_draw.h"
    1.10 +
    1.11 +static int
    1.12 +SDL_BlendLine_RGB555(SDL_Surface * dst, int x1, int y1, int x2, int y2,
    1.13 +                     int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    1.14 +{
    1.15 +    unsigned inva = 0xff - a;
    1.16  
    1.17 -#define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
    1.18 +    switch (blendMode) {
    1.19 +    case SDL_BLENDMODE_BLEND:
    1.20 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_BLEND_RGB555);
    1.21 +        break;
    1.22 +    case SDL_BLENDMODE_ADD:
    1.23 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_ADD_RGB555);
    1.24 +        break;
    1.25 +    case SDL_BLENDMODE_MOD:
    1.26 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_MOD_RGB555);
    1.27 +        break;
    1.28 +    default:
    1.29 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_RGB555);
    1.30 +        break;
    1.31 +    }
    1.32 +    return 0;
    1.33 +}
    1.34  
    1.35 -#define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0)
    1.36 +static int
    1.37 +SDL_BlendLine_RGB565(SDL_Surface * dst, int x1, int y1, int x2, int y2,
    1.38 +                     int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    1.39 +{
    1.40 +    unsigned inva = 0xff - a;
    1.41  
    1.42 -#define BRESENHAM(x0, y0, x1, y1, op) \
    1.43 -{ \
    1.44 -    int deltax, deltay, steep, error, xstep, ystep, x, y; \
    1.45 - \
    1.46 -    deltax = ABS(x1 - x0); \
    1.47 -    deltay = ABS(y1 - y0); \
    1.48 -    steep = (deltay > deltax); \
    1.49 -    if (steep) { \
    1.50 -        SWAP(x0, y0); \
    1.51 -        SWAP(x1, y1); \
    1.52 -        SWAP(deltax, deltay); \
    1.53 -    } \
    1.54 -    error = (x1 - x0) / 2; \
    1.55 -    y = y0; \
    1.56 -    if (x0 > x1) { \
    1.57 -        xstep = -1; \
    1.58 -    } else { \
    1.59 -        xstep = 1; \
    1.60 -    } \
    1.61 -    if (y0 < y1) { \
    1.62 -        ystep = 1; \
    1.63 -    } else { \
    1.64 -        ystep = -1; \
    1.65 -    } \
    1.66 -    if (!steep) { \
    1.67 -        for (x = x0; x != x1; x += xstep) { \
    1.68 -            op(x, y); \
    1.69 -            error -= deltay; \
    1.70 -            if (error < 0) { \
    1.71 -                y += ystep; \
    1.72 -                error += deltax; \
    1.73 -            } \
    1.74 -        } \
    1.75 -    } else { \
    1.76 -        for (x = x0; x != x1; x += xstep) { \
    1.77 -            op(y, x); \
    1.78 -            error -= deltay; \
    1.79 -            if (error < 0) { \
    1.80 -                y += ystep; \
    1.81 -                error += deltax; \
    1.82 -            } \
    1.83 -        } \
    1.84 -    } \
    1.85 +    switch (blendMode) {
    1.86 +    case SDL_BLENDMODE_BLEND:
    1.87 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_BLEND_RGB565);
    1.88 +        break;
    1.89 +    case SDL_BLENDMODE_ADD:
    1.90 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_ADD_RGB565);
    1.91 +        break;
    1.92 +    case SDL_BLENDMODE_MOD:
    1.93 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_MOD_RGB565);
    1.94 +        break;
    1.95 +    default:
    1.96 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_RGB565);
    1.97 +        break;
    1.98 +    }
    1.99 +    return 0;
   1.100 +}
   1.101 +
   1.102 +static int
   1.103 +SDL_BlendLine_RGB888(SDL_Surface * dst, int x1, int y1, int x2, int y2,
   1.104 +                     int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.105 +{
   1.106 +    unsigned inva = 0xff - a;
   1.107 +
   1.108 +    switch (blendMode) {
   1.109 +    case SDL_BLENDMODE_BLEND:
   1.110 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_BLEND_RGB888);
   1.111 +        break;
   1.112 +    case SDL_BLENDMODE_ADD:
   1.113 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_ADD_RGB888);
   1.114 +        break;
   1.115 +    case SDL_BLENDMODE_MOD:
   1.116 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_MOD_RGB888);
   1.117 +        break;
   1.118 +    default:
   1.119 +        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_RGB888);
   1.120 +        break;
   1.121 +    }
   1.122 +    return 0;
   1.123  }
   1.124  
   1.125 -#define MUL(_a, _b) (((Uint16)(_a)*(Uint16)(_b))/255)
   1.126 -#define SHIFTAND(_v, _s, _a) (((_v)>>(_s)) & _a)
   1.127 -
   1.128 -#define SETPIXEL_MASK(x, y, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \
   1.129 -do { \
   1.130 -	type *pixel = (type *)(dst->pixels + y * dst->pitch + x * bpp); \
   1.131 -	if (a) { \
   1.132 -		*pixel = (r<<rshift) | (g<<gshift) | (b<<bshift); \
   1.133 -	} \
   1.134 -} while (0)
   1.135 -
   1.136 -#define SETPIXEL_BLEND(x, y, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \
   1.137 -do { \
   1.138 -	type *pixel = (type *)(dst->pixels + y * dst->pitch + x * bpp); \
   1.139 -	Uint8 sr = MUL(inva, SHIFTAND(*pixel, rshift, rmask)) + (Uint16) r; \
   1.140 -	Uint8 sg = MUL(inva, SHIFTAND(*pixel, gshift, gmask)) + (Uint16) g; \
   1.141 -	Uint8 sb = MUL(inva, SHIFTAND(*pixel, bshift, bmask)) + (Uint16) b; \
   1.142 -	*pixel = (sr<<rshift) | (sg<<gshift) | (sb<<bshift);  \
   1.143 -} while (0)
   1.144 +static int
   1.145 +SDL_BlendLine_RGB(SDL_Surface * dst, int x1, int y1, int x2, int y2,
   1.146 +                  int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.147 +{
   1.148 +    SDL_PixelFormat *fmt = dst->format;
   1.149 +    unsigned inva = 0xff - a;
   1.150  
   1.151 -#define SETPIXEL_ADD(x, y, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \
   1.152 -do { \
   1.153 -	type *pixel = (type *)(dst->pixels + y * dst->pitch + x * bpp); \
   1.154 -	Uint16 sr = SHIFTAND(*pixel, rshift, rmask) + (Uint16) r; \
   1.155 -	Uint16 sg = SHIFTAND(*pixel, gshift, gmask) + (Uint16) g; \
   1.156 -	Uint16 sb = SHIFTAND(*pixel, bshift, bmask) + (Uint16) b; \
   1.157 -	if (sr>rmask) sr = rmask;  \
   1.158 -	if (sg>gmask) sg = gmask;  \
   1.159 -	if (sb>bmask) sb = bmask;  \
   1.160 -	*pixel = (sr<<rshift) | (sg<<gshift) | (sb<<bshift);  \
   1.161 -} while (0)
   1.162 +    switch (fmt->BytesPerPixel) {
   1.163 +    case 2:
   1.164 +        switch (blendMode) {
   1.165 +        case SDL_BLENDMODE_BLEND:
   1.166 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY2_BLEND_RGB);
   1.167 +            break;
   1.168 +        case SDL_BLENDMODE_ADD:
   1.169 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY2_ADD_RGB);
   1.170 +            break;
   1.171 +        case SDL_BLENDMODE_MOD:
   1.172 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY2_MOD_RGB);
   1.173 +            break;
   1.174 +        default:
   1.175 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY2_RGB);
   1.176 +            break;
   1.177 +        }
   1.178 +        return 0;
   1.179 +    case 4:
   1.180 +        switch (blendMode) {
   1.181 +        case SDL_BLENDMODE_BLEND:
   1.182 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_BLEND_RGB);
   1.183 +            break;
   1.184 +        case SDL_BLENDMODE_ADD:
   1.185 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_ADD_RGB);
   1.186 +            break;
   1.187 +        case SDL_BLENDMODE_MOD:
   1.188 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_MOD_RGB);
   1.189 +            break;
   1.190 +        default:
   1.191 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_RGB);
   1.192 +            break;
   1.193 +        }
   1.194 +        return 0;
   1.195 +    default:
   1.196 +        SDL_Unsupported();
   1.197 +        return -1;
   1.198 +    }
   1.199 +}
   1.200  
   1.201 -#define SETPIXEL_MOD(x, y, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \
   1.202 -do { \
   1.203 -	type *pixel = (type *)(dst->pixels + y * dst->pitch + x * bpp); \
   1.204 -	Uint8 sr = MUL(SHIFTAND(*pixel, rshift, rmask), r); \
   1.205 -	Uint8 sg = MUL(SHIFTAND(*pixel, gshift, gmask), g); \
   1.206 -	Uint8 sb = MUL(SHIFTAND(*pixel, bshift, bmask), b); \
   1.207 -	*pixel = (sr<<rshift) | (sg<<gshift) | (sb<<bshift);  \
   1.208 -} while (0)
   1.209 -
   1.210 +static int
   1.211 +SDL_BlendLine_RGBA(SDL_Surface * dst, int x1, int y1, int x2, int y2,
   1.212 +                   int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.213 +{
   1.214 +    SDL_PixelFormat *fmt = dst->format;
   1.215 +    unsigned inva = 0xff - a;
   1.216  
   1.217 -#define SETPIXEL15_MASK(x, y) SETPIXEL_MASK(x, y, Uint16, 2, 10, 5, 0, 0x1f, 0x1f, 0x1f);
   1.218 -#define SETPIXEL15_BLEND(x, y) SETPIXEL_BLEND(x, y, Uint16, 2, 10, 5, 0, 0x1f, 0x1f, 0x1f);
   1.219 -#define SETPIXEL15_ADD(x, y) SETPIXEL_ADD(x, y, Uint16, 2, 10, 5, 0, 0x1f, 0x1f, 0x1f);
   1.220 -#define SETPIXEL15_MOD(x, y) SETPIXEL_MOD(x, y, Uint16, 2, 10, 5, 0, 0x1f, 0x1f, 0x1f);
   1.221 -
   1.222 -#define SETPIXEL16_MASK(x, y) SETPIXEL_MASK(x, y, Uint16, 2, 11, 5, 0, 0x1f, 0x3f, 0x1f);
   1.223 -#define SETPIXEL16_BLEND(x, y) SETPIXEL_BLEND(x, y, Uint16, 2, 11, 5, 0, 0x1f, 0x3f, 0x1f);
   1.224 -#define SETPIXEL16_ADD(x, y) SETPIXEL_ADD(x, y, Uint16, 2, 11, 5, 0, 0x1f, 0x3f, 0x1f);
   1.225 -#define SETPIXEL16_MOD(x, y) SETPIXEL_MOD(x, y, Uint16, 2, 11, 5, 0, 0x1f, 0x3f, 0x1f);
   1.226 -
   1.227 -#define SETPIXEL32_MASK(x, y) SETPIXEL_MASK(x, y, Uint32, 4, 16, 8, 0, 0xff, 0xff, 0xff);
   1.228 -#define SETPIXEL32_BLEND(x, y) SETPIXEL_BLEND(x, y, Uint32, 4, 16, 8, 0, 0xff, 0xff, 0xff);
   1.229 -#define SETPIXEL32_ADD(x, y) SETPIXEL_ADD(x, y, Uint32, 4, 16, 8, 0, 0xff, 0xff, 0xff);
   1.230 -#define SETPIXEL32_MOD(x, y) SETPIXEL_MOD(x, y, Uint32, 4, 16, 8, 0, 0xff, 0xff, 0xff);
   1.231 +    switch (fmt->BytesPerPixel) {
   1.232 +    case 4:
   1.233 +        switch (blendMode) {
   1.234 +        case SDL_BLENDMODE_BLEND:
   1.235 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_BLEND_RGBA);
   1.236 +            break;
   1.237 +        case SDL_BLENDMODE_ADD:
   1.238 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_ADD_RGBA);
   1.239 +            break;
   1.240 +        case SDL_BLENDMODE_MOD:
   1.241 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_MOD_RGBA);
   1.242 +            break;
   1.243 +        default:
   1.244 +            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_RGBA);
   1.245 +            break;
   1.246 +        }
   1.247 +        return 0;
   1.248 +    default:
   1.249 +        SDL_Unsupported();
   1.250 +        return -1;
   1.251 +    }
   1.252 +}
   1.253  
   1.254  int
   1.255  SDL_BlendLine(SDL_Surface * dst, int x1, int y1, int x2, int y2,
   1.256                int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   1.257  {
   1.258 -    Uint8 inva = 0xff - a;
   1.259 +    SDL_PixelFormat *fmt = dst->format;
   1.260 +
   1.261      /* This function doesn't work on surfaces < 8 bpp */
   1.262      if (dst->format->BitsPerPixel < 8) {
   1.263          SDL_SetError("SDL_BlendLine(): Unsupported surface format");
   1.264 @@ -150,69 +189,44 @@
   1.265  
   1.266      if ((blendMode == SDL_BLENDMODE_BLEND)
   1.267          || (blendMode == SDL_BLENDMODE_ADD)) {
   1.268 -        r = MUL(r, a);
   1.269 -        g = MUL(g, a);
   1.270 -        b = MUL(b, a);
   1.271 +        r = DRAW_MUL(r, a);
   1.272 +        g = DRAW_MUL(g, a);
   1.273 +        b = DRAW_MUL(b, a);
   1.274      }
   1.275 -    switch (dst->format->BitsPerPixel) {
   1.276 +
   1.277 +    switch (fmt->BitsPerPixel) {
   1.278      case 15:
   1.279 -        switch (blendMode) {
   1.280 -        case SDL_BLENDMODE_MASK:
   1.281 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL15_MASK);
   1.282 -            break;
   1.283 -        case SDL_BLENDMODE_BLEND:
   1.284 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL15_BLEND);
   1.285 -            break;
   1.286 -        case SDL_BLENDMODE_ADD:
   1.287 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL15_ADD);
   1.288 -            break;
   1.289 -        case SDL_BLENDMODE_MOD:
   1.290 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL15_MOD);
   1.291 -            break;
   1.292 +        switch (fmt->Rmask) {
   1.293 +        case 0x7C00:
   1.294 +            return SDL_BlendLine_RGB555(dst, x1, y1, x2, y2, blendMode, r, g,
   1.295 +                                        b, a);
   1.296          }
   1.297          break;
   1.298      case 16:
   1.299 -        switch (blendMode) {
   1.300 -        case SDL_BLENDMODE_MASK:
   1.301 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL16_MASK);
   1.302 -            break;
   1.303 -        case SDL_BLENDMODE_BLEND:
   1.304 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL16_BLEND);
   1.305 -            break;
   1.306 -        case SDL_BLENDMODE_ADD:
   1.307 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL16_ADD);
   1.308 -            break;
   1.309 -        case SDL_BLENDMODE_MOD:
   1.310 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL16_MOD);
   1.311 -            break;
   1.312 +        switch (fmt->Rmask) {
   1.313 +        case 0xF800:
   1.314 +            return SDL_BlendLine_RGB565(dst, x1, y1, x2, y2, blendMode, r, g,
   1.315 +                                        b, a);
   1.316          }
   1.317          break;
   1.318 -    case 24:
   1.319      case 32:
   1.320 -        if (dst->format->BytesPerPixel != 4) {
   1.321 -            SDL_Unsupported();
   1.322 -            return -1;
   1.323 -        }
   1.324 -        switch (blendMode) {
   1.325 -        case SDL_BLENDMODE_MASK:
   1.326 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL32_MASK);
   1.327 -            break;
   1.328 -        case SDL_BLENDMODE_BLEND:
   1.329 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL32_BLEND);
   1.330 -            break;
   1.331 -        case SDL_BLENDMODE_ADD:
   1.332 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL32_ADD);
   1.333 -            break;
   1.334 -        case SDL_BLENDMODE_MOD:
   1.335 -            BRESENHAM(x1, y1, x2, y2, SETPIXEL32_MOD);
   1.336 +        switch (fmt->Rmask) {
   1.337 +        case 0x00FF0000:
   1.338 +            if (!fmt->Amask) {
   1.339 +                return SDL_BlendLine_RGB888(dst, x1, y1, x2, y2, blendMode, r,
   1.340 +                                            g, b, a);
   1.341 +            }
   1.342              break;
   1.343          }
   1.344 +    default:
   1.345          break;
   1.346 -    default:
   1.347 -        SDL_Unsupported();
   1.348 -        return -1;
   1.349      }
   1.350 -    return 0;
   1.351 +
   1.352 +    if (!fmt->Amask) {
   1.353 +        return SDL_BlendLine_RGB(dst, x1, y1, x2, y2, blendMode, r, g, b, a);
   1.354 +    } else {
   1.355 +        return SDL_BlendLine_RGBA(dst, x1, y1, x2, y2, blendMode, r, g, b, a);
   1.356 +    }
   1.357  }
   1.358  
   1.359  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- a/src/video/SDL_blendrect.c	Sun Dec 21 08:24:32 2008 +0000
     2.2 +++ b/src/video/SDL_blendrect.c	Sun Dec 21 08:28:25 2008 +0000
     2.3 @@ -22,83 +22,161 @@
     2.4  #include "SDL_config.h"
     2.5  
     2.6  #include "SDL_video.h"
     2.7 -#include "SDL_blit.h"
     2.8 +#include "SDL_draw.h"
     2.9  
    2.10 -#define MUL(_a, _b) (((Uint16)(_a)*(Uint16)(_b))/255)
    2.11 -#define SHIFTAND(_v, _s, _a) (((_v)>>(_s)) & (_a))
    2.12 +static int
    2.13 +SDL_BlendRect_RGB555(SDL_Surface * dst, SDL_Rect * dstrect, int blendMode,
    2.14 +                     Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    2.15 +{
    2.16 +    unsigned inva = 0xff - a;
    2.17  
    2.18 -#define SETPIXEL_MASK(p, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \
    2.19 -do { \
    2.20 -	if (a) { \
    2.21 -		p = (r<<rshift) | (g<<gshift) | (b<<bshift); \
    2.22 -	} \
    2.23 -} while (0)
    2.24 +    switch (blendMode) {
    2.25 +    case SDL_BLENDMODE_BLEND:
    2.26 +        BLENDRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB555);
    2.27 +        break;
    2.28 +    case SDL_BLENDMODE_ADD:
    2.29 +        BLENDRECT(Uint16, DRAW_SETPIXEL_ADD_RGB555);
    2.30 +        break;
    2.31 +    case SDL_BLENDMODE_MOD:
    2.32 +        BLENDRECT(Uint16, DRAW_SETPIXEL_MOD_RGB555);
    2.33 +        break;
    2.34 +    default:
    2.35 +        BLENDRECT(Uint16, DRAW_SETPIXEL_RGB555);
    2.36 +        break;
    2.37 +    }
    2.38 +    return 0;
    2.39 +}
    2.40 +
    2.41 +static int
    2.42 +SDL_BlendRect_RGB565(SDL_Surface * dst, SDL_Rect * dstrect, int blendMode,
    2.43 +                     Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    2.44 +{
    2.45 +    unsigned inva = 0xff - a;
    2.46  
    2.47 -#define SETPIXEL_BLEND(p, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \
    2.48 -do { \
    2.49 -	Uint8 sr = MUL(inva, SHIFTAND(p, rshift, rmask)) + (Uint16) r; \
    2.50 -	Uint8 sg = MUL(inva, SHIFTAND(p, gshift, gmask)) + (Uint16) g; \
    2.51 -	Uint8 sb = MUL(inva, SHIFTAND(p, bshift, bmask)) + (Uint16) b; \
    2.52 -	p = (sr<<rshift) | (sg<<gshift) | (sb<<bshift);  \
    2.53 -} while (0)
    2.54 +    switch (blendMode) {
    2.55 +    case SDL_BLENDMODE_BLEND:
    2.56 +        BLENDRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB565);
    2.57 +        break;
    2.58 +    case SDL_BLENDMODE_ADD:
    2.59 +        BLENDRECT(Uint16, DRAW_SETPIXEL_ADD_RGB565);
    2.60 +        break;
    2.61 +    case SDL_BLENDMODE_MOD:
    2.62 +        BLENDRECT(Uint16, DRAW_SETPIXEL_MOD_RGB565);
    2.63 +        break;
    2.64 +    default:
    2.65 +        BLENDRECT(Uint16, DRAW_SETPIXEL_RGB565);
    2.66 +        break;
    2.67 +    }
    2.68 +    return 0;
    2.69 +}
    2.70  
    2.71 -#define SETPIXEL_ADD(p, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \
    2.72 -do { \
    2.73 -	Uint16 sr = SHIFTAND(p, rshift, rmask) + (Uint16) r; \
    2.74 -	Uint16 sg = SHIFTAND(p, gshift, gmask) + (Uint16) g; \
    2.75 -	Uint16 sb = SHIFTAND(p, bshift, bmask) + (Uint16) b; \
    2.76 -	if (sr>rmask) sr = rmask;  \
    2.77 -	if (sg>gmask) sg = gmask;  \
    2.78 -	if (sb>bmask) sb = bmask;  \
    2.79 -	p = (sr<<rshift) | (sg<<gshift) | (sb<<bshift);  \
    2.80 -} while (0)
    2.81 +static int
    2.82 +SDL_BlendRect_RGB888(SDL_Surface * dst, SDL_Rect * dstrect, int blendMode,
    2.83 +                     Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    2.84 +{
    2.85 +    unsigned inva = 0xff - a;
    2.86 +
    2.87 +    switch (blendMode) {
    2.88 +    case SDL_BLENDMODE_BLEND:
    2.89 +        BLENDRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB888);
    2.90 +        break;
    2.91 +    case SDL_BLENDMODE_ADD:
    2.92 +        BLENDRECT(Uint32, DRAW_SETPIXEL_ADD_RGB888);
    2.93 +        break;
    2.94 +    case SDL_BLENDMODE_MOD:
    2.95 +        BLENDRECT(Uint32, DRAW_SETPIXEL_MOD_RGB888);
    2.96 +        break;
    2.97 +    default:
    2.98 +        BLENDRECT(Uint32, DRAW_SETPIXEL_RGB888);
    2.99 +        break;
   2.100 +    }
   2.101 +    return 0;
   2.102 +}
   2.103  
   2.104 -#define SETPIXEL_MOD(p, type, bpp, rshift, gshift, bshift, rmask, gmask, bmask) \
   2.105 -do { \
   2.106 -	Uint8 sr = MUL(SHIFTAND(p, rshift, rmask), r); \
   2.107 -	Uint8 sg = MUL(SHIFTAND(p, gshift, gmask), g); \
   2.108 -	Uint8 sb = MUL(SHIFTAND(p, bshift, bmask), b); \
   2.109 -	p = (sr<<rshift) | (sg<<gshift) | (sb<<bshift);  \
   2.110 -} while (0)
   2.111 -
   2.112 -
   2.113 -#define SETPIXEL15_MASK(p) SETPIXEL_MASK(p, Uint16, 2, 10, 5, 0, 0x1f, 0x1f, 0x1f);
   2.114 -#define SETPIXEL15_BLEND(p) SETPIXEL_BLEND(p, Uint16, 2, 10, 5, 0, 0x1f, 0x1f, 0x1f);
   2.115 -#define SETPIXEL15_ADD(p) SETPIXEL_ADD(p, Uint16, 2, 10, 5, 0, 0x1f, 0x1f, 0x1f);
   2.116 -#define SETPIXEL15_MOD(p) SETPIXEL_MOD(p, Uint16, 2, 10, 5, 0, 0x1f, 0x1f, 0x1f);
   2.117 -
   2.118 -#define SETPIXEL16_MASK(p) SETPIXEL_MASK(p, Uint16, 2, 11, 5, 0, 0x1f, 0x3f, 0x1f);
   2.119 -#define SETPIXEL16_BLEND(p) SETPIXEL_BLEND(p, Uint16, 2, 11, 5, 0, 0x1f, 0x3f, 0x1f);
   2.120 -#define SETPIXEL16_ADD(p) SETPIXEL_ADD(p, Uint16, 2, 11, 5, 0, 0x1f, 0x3f, 0x1f);
   2.121 -#define SETPIXEL16_MOD(p) SETPIXEL_MOD(p, Uint16, 2, 11, 5, 0, 0x1f, 0x3f, 0x1f);
   2.122 +static int
   2.123 +SDL_BlendRect_RGB(SDL_Surface * dst, SDL_Rect * dstrect, int blendMode,
   2.124 +                  Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   2.125 +{
   2.126 +    SDL_PixelFormat *fmt = dst->format;
   2.127 +    unsigned inva = 0xff - a;
   2.128  
   2.129 -#define SETPIXEL32_MASK(p) SETPIXEL_MASK(p, Uint32, 4, 16, 8, 0, 0xff, 0xff, 0xff);
   2.130 -#define SETPIXEL32_BLEND(p) SETPIXEL_BLEND(p, Uint32, 4, 16, 8, 0, 0xff, 0xff, 0xff);
   2.131 -#define SETPIXEL32_ADD(p) SETPIXEL_ADD(p, Uint32, 4, 16, 8, 0, 0xff, 0xff, 0xff);
   2.132 -#define SETPIXEL32_MOD(p) SETPIXEL_MOD(p, Uint32, 4, 16, 8, 0, 0xff, 0xff, 0xff);
   2.133 +    switch (fmt->BytesPerPixel) {
   2.134 +    case 2:
   2.135 +        switch (blendMode) {
   2.136 +        case SDL_BLENDMODE_BLEND:
   2.137 +            BLENDRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB);
   2.138 +            break;
   2.139 +        case SDL_BLENDMODE_ADD:
   2.140 +            BLENDRECT(Uint16, DRAW_SETPIXEL_ADD_RGB);
   2.141 +            break;
   2.142 +        case SDL_BLENDMODE_MOD:
   2.143 +            BLENDRECT(Uint16, DRAW_SETPIXEL_MOD_RGB);
   2.144 +            break;
   2.145 +        default:
   2.146 +            BLENDRECT(Uint16, DRAW_SETPIXEL_RGB);
   2.147 +            break;
   2.148 +        }
   2.149 +        return 0;
   2.150 +    case 4:
   2.151 +        switch (blendMode) {
   2.152 +        case SDL_BLENDMODE_BLEND:
   2.153 +            BLENDRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB);
   2.154 +            break;
   2.155 +        case SDL_BLENDMODE_ADD:
   2.156 +            BLENDRECT(Uint32, DRAW_SETPIXEL_ADD_RGB);
   2.157 +            break;
   2.158 +        case SDL_BLENDMODE_MOD:
   2.159 +            BLENDRECT(Uint32, DRAW_SETPIXEL_MOD_RGB);
   2.160 +            break;
   2.161 +        default:
   2.162 +            BLENDRECT(Uint32, DRAW_SETPIXEL_RGB);
   2.163 +            break;
   2.164 +        }
   2.165 +        return 0;
   2.166 +    default:
   2.167 +        SDL_Unsupported();
   2.168 +        return -1;
   2.169 +    }
   2.170 +}
   2.171  
   2.172 -#define BLENDRECT(type, op) \
   2.173 -do { \
   2.174 -	int y = dstrect->y; \
   2.175 -    int h = dstrect->h; \
   2.176 -    while (h--) { \
   2.177 -        type *pixel = (type *)(dst->pixels + y * dst->pitch + dstrect->x * dst->format->BytesPerPixel); \
   2.178 -    	int w = dstrect->w; \
   2.179 -    	while (w--) { \
   2.180 -    		op(*pixel); \
   2.181 -    		pixel++; \
   2.182 -    	} \
   2.183 -    	y++; \
   2.184 -    } \
   2.185 -} while (0)
   2.186 +static int
   2.187 +SDL_BlendRect_RGBA(SDL_Surface * dst, SDL_Rect * dstrect, int blendMode,
   2.188 +                   Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   2.189 +{
   2.190 +    SDL_PixelFormat *fmt = dst->format;
   2.191 +    unsigned inva = 0xff - a;
   2.192 +
   2.193 +    switch (fmt->BytesPerPixel) {
   2.194 +    case 4:
   2.195 +        switch (blendMode) {
   2.196 +        case SDL_BLENDMODE_BLEND:
   2.197 +            BLENDRECT(Uint32, DRAW_SETPIXEL_BLEND_RGBA);
   2.198 +            break;
   2.199 +        case SDL_BLENDMODE_ADD:
   2.200 +            BLENDRECT(Uint32, DRAW_SETPIXEL_ADD_RGBA);
   2.201 +            break;
   2.202 +        case SDL_BLENDMODE_MOD:
   2.203 +            BLENDRECT(Uint32, DRAW_SETPIXEL_MOD_RGBA);
   2.204 +            break;
   2.205 +        default:
   2.206 +            BLENDRECT(Uint32, DRAW_SETPIXEL_RGBA);
   2.207 +            break;
   2.208 +        }
   2.209 +        return 0;
   2.210 +    default:
   2.211 +        SDL_Unsupported();
   2.212 +        return -1;
   2.213 +    }
   2.214 +}
   2.215  
   2.216  int
   2.217  SDL_BlendRect(SDL_Surface * dst, SDL_Rect * dstrect, int blendMode, Uint8 r,
   2.218                Uint8 g, Uint8 b, Uint8 a)
   2.219  {
   2.220 -    Uint16 inva = 0xff - a;
   2.221 +    SDL_PixelFormat *fmt = dst->format;
   2.222 +
   2.223      /* This function doesn't work on surfaces < 8 bpp */
   2.224 -    if (dst->format->BitsPerPixel < 8) {
   2.225 +    if (fmt->BitsPerPixel < 8) {
   2.226          SDL_SetError("SDL_BlendRect(): Unsupported surface format");
   2.227          return (-1);
   2.228      }
   2.229 @@ -115,70 +193,42 @@
   2.230  
   2.231      if ((blendMode == SDL_BLENDMODE_BLEND)
   2.232          || (blendMode == SDL_BLENDMODE_ADD)) {
   2.233 -        r = MUL(r, a);
   2.234 -        g = MUL(g, a);
   2.235 -        b = MUL(b, a);
   2.236 +        r = DRAW_MUL(r, a);
   2.237 +        g = DRAW_MUL(g, a);
   2.238 +        b = DRAW_MUL(b, a);
   2.239      }
   2.240 -    switch (dst->format->BitsPerPixel) {
   2.241 +
   2.242 +    switch (fmt->BitsPerPixel) {
   2.243      case 15:
   2.244 -        switch (blendMode) {
   2.245 -        case SDL_BLENDMODE_MASK:
   2.246 -            BLENDRECT(Uint16, SETPIXEL15_MASK);
   2.247 -            break;
   2.248 -        case SDL_BLENDMODE_BLEND:
   2.249 -            BLENDRECT(Uint16, SETPIXEL15_BLEND);
   2.250 -            break;
   2.251 -        case SDL_BLENDMODE_ADD:
   2.252 -            BLENDRECT(Uint16, SETPIXEL15_ADD);
   2.253 -            break;
   2.254 -        case SDL_BLENDMODE_MOD:
   2.255 -            BLENDRECT(Uint16, SETPIXEL15_MOD);
   2.256 -            break;
   2.257 +        switch (fmt->Rmask) {
   2.258 +        case 0x7C00:
   2.259 +            return SDL_BlendRect_RGB555(dst, dstrect, blendMode, r, g, b, a);
   2.260          }
   2.261          break;
   2.262      case 16:
   2.263 -        switch (blendMode) {
   2.264 -        case SDL_BLENDMODE_MASK:
   2.265 -            BLENDRECT(Uint16, SETPIXEL16_MASK);
   2.266 -            break;
   2.267 -        case SDL_BLENDMODE_BLEND:
   2.268 -            BLENDRECT(Uint16, SETPIXEL16_BLEND);
   2.269 -            break;
   2.270 -        case SDL_BLENDMODE_ADD:
   2.271 -            BLENDRECT(Uint16, SETPIXEL16_ADD);
   2.272 -            break;
   2.273 -        case SDL_BLENDMODE_MOD:
   2.274 -            BLENDRECT(Uint16, SETPIXEL16_MOD);
   2.275 -            break;
   2.276 +        switch (fmt->Rmask) {
   2.277 +        case 0xF800:
   2.278 +            return SDL_BlendRect_RGB565(dst, dstrect, blendMode, r, g, b, a);
   2.279          }
   2.280          break;
   2.281 -    case 24:
   2.282      case 32:
   2.283 -        if (dst->format->BytesPerPixel != 4) {
   2.284 -            SDL_Unsupported();
   2.285 -            return -1;
   2.286 -        }
   2.287 -        switch (blendMode) {
   2.288 -        case SDL_BLENDMODE_MASK:
   2.289 -            BLENDRECT(Uint32, SETPIXEL32_MASK);
   2.290 -            break;
   2.291 -        case SDL_BLENDMODE_BLEND:
   2.292 -            BLENDRECT(Uint32, SETPIXEL32_BLEND);
   2.293 -            break;
   2.294 -        case SDL_BLENDMODE_ADD:
   2.295 -            BLENDRECT(Uint32, SETPIXEL32_ADD);
   2.296 -            break;
   2.297 -        case SDL_BLENDMODE_MOD:
   2.298 -            BLENDRECT(Uint32, SETPIXEL32_MOD);
   2.299 +        switch (fmt->Rmask) {
   2.300 +        case 0x00FF0000:
   2.301 +            if (!fmt->Amask) {
   2.302 +                return SDL_BlendRect_RGB888(dst, dstrect, blendMode, r, g, b,
   2.303 +                                            a);
   2.304 +            }
   2.305              break;
   2.306          }
   2.307 +    default:
   2.308          break;
   2.309 -    default:
   2.310 -        SDL_Unsupported();
   2.311 -        return -1;
   2.312      }
   2.313 -    return 0;
   2.314 -    return -1;
   2.315 +
   2.316 +    if (!fmt->Amask) {
   2.317 +        return SDL_BlendRect_RGB(dst, dstrect, blendMode, r, g, b, a);
   2.318 +    } else {
   2.319 +        return SDL_BlendRect_RGBA(dst, dstrect, blendMode, r, g, b, a);
   2.320 +    }
   2.321  }
   2.322  
   2.323  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/SDL_blit.h	Sun Dec 21 08:24:32 2008 +0000
     3.2 +++ b/src/video/SDL_blit.h	Sun Dec 21 08:28:25 2008 +0000
     3.3 @@ -39,6 +39,7 @@
     3.4  
     3.5  #include "SDL_cpuinfo.h"
     3.6  #include "SDL_endian.h"
     3.7 +#include "SDL_video.h"
     3.8  
     3.9  /* SDL blit copy flags */
    3.10  #define SDL_COPY_MODULATE_COLOR     0x00000001
    3.11 @@ -575,4 +576,5 @@
    3.12  #endif
    3.13  
    3.14  #endif /* _SDL_blit_h */
    3.15 +
    3.16  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/SDL_draw.h	Sun Dec 21 08:28:25 2008 +0000
     4.3 @@ -0,0 +1,325 @@
     4.4 +/*
     4.5 +    SDL - Simple DirectMedia Layer
     4.6 +    Copyright (C) 1997-2009 Sam Lantinga
     4.7 +
     4.8 +    This library is free software; you can redistribute it and/or
     4.9 +    modify it under the terms of the GNU Lesser General Public
    4.10 +    License as published by the Free Software Foundation; either
    4.11 +    version 2.1 of the License, or (at your option) any later version.
    4.12 +
    4.13 +    This library is distributed in the hope that it will be useful,
    4.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.16 +    Lesser General Public License for more details.
    4.17 +
    4.18 +    You should have received a copy of the GNU Lesser General Public
    4.19 +    License along with this library; if not, write to the Free Software
    4.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    4.21 +
    4.22 +    Sam Lantinga
    4.23 +    slouken@libsdl.org
    4.24 +*/
    4.25 +#include "SDL_config.h"
    4.26 +
    4.27 +#include "SDL_blit.h"
    4.28 +
    4.29 +/* This code assumes that r, g, b, a are the source color,
    4.30 + * and in the blend and add case, the RGB values are premultiplied by a.
    4.31 + */
    4.32 +
    4.33 +#define DRAW_MUL(_a, _b) (((unsigned)(_a)*(_b))/255)
    4.34 +
    4.35 +#define DRAW_SETPIXEL(setpixel) \
    4.36 +do { \
    4.37 +    unsigned sr = r, sg = g, sb = b, sa = a; \
    4.38 +    setpixel; \
    4.39 +} while (0)
    4.40 +
    4.41 +#define DRAW_SETPIXEL_BLEND(getpixel, setpixel) \
    4.42 +do { \
    4.43 +    unsigned sr, sg, sb, sa; sa; \
    4.44 +    getpixel; \
    4.45 +    sr = DRAW_MUL(inva, sr) + r; \
    4.46 +    sg = DRAW_MUL(inva, sg) + g; \
    4.47 +    sb = DRAW_MUL(inva, sb) + b; \
    4.48 +    setpixel; \
    4.49 +} while (0)
    4.50 +
    4.51 +#define DRAW_SETPIXEL_ADD(getpixel, setpixel) \
    4.52 +do { \
    4.53 +    unsigned sr, sg, sb, sa; sa; \
    4.54 +    getpixel; \
    4.55 +    sr += r; if (sr > 0xff) sr = 0xff; \
    4.56 +    sg += g; if (sg > 0xff) sg = 0xff; \
    4.57 +    sb += b; if (sb > 0xff) sb = 0xff; \
    4.58 +    setpixel; \
    4.59 +} while (0)
    4.60 +
    4.61 +#define DRAW_SETPIXEL_MOD(getpixel, setpixel) \
    4.62 +do { \
    4.63 +    unsigned sr, sg, sb, sa; sa; \
    4.64 +    getpixel; \
    4.65 +    sr = DRAW_MUL(sr, r); \
    4.66 +    sg = DRAW_MUL(sg, g); \
    4.67 +    sb = DRAW_MUL(sb, b); \
    4.68 +    setpixel; \
    4.69 +} while (0)
    4.70 +
    4.71 +#define DRAW_SETPIXELXY(x, y, type, bpp, op) \
    4.72 +do { \
    4.73 +    type *pixel = (type *)(dst->pixels + y * dst->pitch + x * bpp); \
    4.74 +    op; \
    4.75 +} while (0)
    4.76 +
    4.77 +/*
    4.78 + * Define draw operators for RGB555
    4.79 + */
    4.80 +
    4.81 +#define DRAW_SETPIXEL_RGB555 \
    4.82 +    DRAW_SETPIXEL(RGB555_FROM_RGB(*pixel, sr, sg, sb))
    4.83 +
    4.84 +#define DRAW_SETPIXEL_BLEND_RGB555 \
    4.85 +    DRAW_SETPIXEL_BLEND(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
    4.86 +                        RGB555_FROM_RGB(*pixel, sr, sg, sb))
    4.87 +
    4.88 +#define DRAW_SETPIXEL_ADD_RGB555 \
    4.89 +    DRAW_SETPIXEL_ADD(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
    4.90 +                      RGB555_FROM_RGB(*pixel, sr, sg, sb))
    4.91 +
    4.92 +#define DRAW_SETPIXEL_MOD_RGB555 \
    4.93 +    DRAW_SETPIXEL_MOD(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
    4.94 +                      RGB555_FROM_RGB(*pixel, sr, sg, sb))
    4.95 +
    4.96 +#define DRAW_SETPIXELXY_RGB555(x, y) \
    4.97 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB555)
    4.98 +
    4.99 +#define DRAW_SETPIXELXY_BLEND_RGB555(x, y) \
   4.100 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB555)
   4.101 +
   4.102 +#define DRAW_SETPIXELXY_ADD_RGB555(x, y) \
   4.103 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB555)
   4.104 +
   4.105 +#define DRAW_SETPIXELXY_MOD_RGB555(x, y) \
   4.106 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB555)
   4.107 +
   4.108 +/*
   4.109 + * Define draw operators for RGB565
   4.110 + */
   4.111 +
   4.112 +#define DRAW_SETPIXEL_RGB565 \
   4.113 +    DRAW_SETPIXEL(RGB565_FROM_RGB(*pixel, sr, sg, sb))
   4.114 +
   4.115 +#define DRAW_SETPIXEL_BLEND_RGB565 \
   4.116 +    DRAW_SETPIXEL_BLEND(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
   4.117 +                        RGB565_FROM_RGB(*pixel, sr, sg, sb))
   4.118 +
   4.119 +#define DRAW_SETPIXEL_ADD_RGB565 \
   4.120 +    DRAW_SETPIXEL_ADD(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
   4.121 +                      RGB565_FROM_RGB(*pixel, sr, sg, sb))
   4.122 +
   4.123 +#define DRAW_SETPIXEL_MOD_RGB565 \
   4.124 +    DRAW_SETPIXEL_MOD(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
   4.125 +                      RGB565_FROM_RGB(*pixel, sr, sg, sb))
   4.126 +
   4.127 +#define DRAW_SETPIXELXY_RGB565(x, y) \
   4.128 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB565)
   4.129 +
   4.130 +#define DRAW_SETPIXELXY_BLEND_RGB565(x, y) \
   4.131 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB565)
   4.132 +
   4.133 +#define DRAW_SETPIXELXY_ADD_RGB565(x, y) \
   4.134 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB565)
   4.135 +
   4.136 +#define DRAW_SETPIXELXY_MOD_RGB565(x, y) \
   4.137 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB565)
   4.138 +
   4.139 +/*
   4.140 + * Define draw operators for RGB888
   4.141 + */
   4.142 +
   4.143 +#define DRAW_SETPIXEL_RGB888 \
   4.144 +    DRAW_SETPIXEL(RGB888_FROM_RGB(*pixel, sr, sg, sb))
   4.145 +
   4.146 +#define DRAW_SETPIXEL_BLEND_RGB888 \
   4.147 +    DRAW_SETPIXEL_BLEND(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
   4.148 +                        RGB888_FROM_RGB(*pixel, sr, sg, sb))
   4.149 +
   4.150 +#define DRAW_SETPIXEL_ADD_RGB888 \
   4.151 +    DRAW_SETPIXEL_ADD(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
   4.152 +                      RGB888_FROM_RGB(*pixel, sr, sg, sb))
   4.153 +
   4.154 +#define DRAW_SETPIXEL_MOD_RGB888 \
   4.155 +    DRAW_SETPIXEL_MOD(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
   4.156 +                      RGB888_FROM_RGB(*pixel, sr, sg, sb))
   4.157 +
   4.158 +#define DRAW_SETPIXELXY_RGB888(x, y) \
   4.159 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB888)
   4.160 +
   4.161 +#define DRAW_SETPIXELXY_BLEND_RGB888(x, y) \
   4.162 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB888)
   4.163 +
   4.164 +#define DRAW_SETPIXELXY_ADD_RGB888(x, y) \
   4.165 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB888)
   4.166 +
   4.167 +#define DRAW_SETPIXELXY_MOD_RGB888(x, y) \
   4.168 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB888)
   4.169 +
   4.170 +/*
   4.171 + * Define draw operators for general RGB
   4.172 + */
   4.173 +
   4.174 +#define DRAW_SETPIXEL_RGB \
   4.175 +    DRAW_SETPIXEL(PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
   4.176 +
   4.177 +#define DRAW_SETPIXEL_BLEND_RGB \
   4.178 +    DRAW_SETPIXEL_BLEND(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
   4.179 +                        PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
   4.180 +
   4.181 +#define DRAW_SETPIXEL_ADD_RGB \
   4.182 +    DRAW_SETPIXEL_ADD(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
   4.183 +                      PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
   4.184 +
   4.185 +#define DRAW_SETPIXEL_MOD_RGB \
   4.186 +    DRAW_SETPIXEL_MOD(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
   4.187 +                      PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
   4.188 +
   4.189 +#define DRAW_SETPIXELXY2_RGB(x, y) \
   4.190 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB)
   4.191 +
   4.192 +#define DRAW_SETPIXELXY4_RGB(x, y) \
   4.193 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB)
   4.194 +
   4.195 +#define DRAW_SETPIXELXY2_BLEND_RGB(x, y) \
   4.196 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB)
   4.197 +
   4.198 +#define DRAW_SETPIXELXY4_BLEND_RGB(x, y) \
   4.199 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB)
   4.200 +
   4.201 +#define DRAW_SETPIXELXY2_ADD_RGB(x, y) \
   4.202 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB)
   4.203 +
   4.204 +#define DRAW_SETPIXELXY4_ADD_RGB(x, y) \
   4.205 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB)
   4.206 +
   4.207 +#define DRAW_SETPIXELXY2_MOD_RGB(x, y) \
   4.208 +    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB)
   4.209 +
   4.210 +#define DRAW_SETPIXELXY4_MOD_RGB(x, y) \
   4.211 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB)
   4.212 +
   4.213 +
   4.214 +/*
   4.215 + * Define draw operators for general RGBA
   4.216 + */
   4.217 +
   4.218 +#define DRAW_SETPIXEL_RGBA \
   4.219 +    DRAW_SETPIXEL(PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
   4.220 +
   4.221 +#define DRAW_SETPIXEL_BLEND_RGBA \
   4.222 +    DRAW_SETPIXEL_BLEND(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
   4.223 +                        PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
   4.224 +
   4.225 +#define DRAW_SETPIXEL_ADD_RGBA \
   4.226 +    DRAW_SETPIXEL_ADD(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
   4.227 +                      PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
   4.228 +
   4.229 +#define DRAW_SETPIXEL_MOD_RGBA \
   4.230 +    DRAW_SETPIXEL_MOD(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
   4.231 +                      PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
   4.232 +
   4.233 +#define DRAW_SETPIXELXY4_RGBA(x, y) \
   4.234 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGBA)
   4.235 +
   4.236 +#define DRAW_SETPIXELXY4_BLEND_RGBA(x, y) \
   4.237 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGBA)
   4.238 +
   4.239 +#define DRAW_SETPIXELXY4_ADD_RGBA(x, y) \
   4.240 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGBA)
   4.241 +
   4.242 +#define DRAW_SETPIXELXY4_MOD_RGBA(x, y) \
   4.243 +    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGBA)
   4.244 +
   4.245 +/*
   4.246 + * Define line drawing macro
   4.247 + */
   4.248 +
   4.249 +#define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
   4.250 +
   4.251 +#define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0)
   4.252 +
   4.253 +#define BRESENHAM(x0, y0, x1, y1, op) \
   4.254 +{ \
   4.255 +    int deltax, deltay, steep, error, xstep, ystep, x, y; \
   4.256 + \
   4.257 +    deltax = ABS(x1 - x0); \
   4.258 +    deltay = ABS(y1 - y0); \
   4.259 +    steep = (deltay > deltax); \
   4.260 +    if (steep) { \
   4.261 +        SWAP(x0, y0); \
   4.262 +        SWAP(x1, y1); \
   4.263 +        SWAP(deltax, deltay); \
   4.264 +    } \
   4.265 +    error = (x1 - x0) / 2; \
   4.266 +    y = y0; \
   4.267 +    if (x0 > x1) { \
   4.268 +        xstep = -1; \
   4.269 +    } else { \
   4.270 +        xstep = 1; \
   4.271 +    } \
   4.272 +    if (y0 < y1) { \
   4.273 +        ystep = 1; \
   4.274 +    } else { \
   4.275 +        ystep = -1; \
   4.276 +    } \
   4.277 +    if (!steep) { \
   4.278 +        for (x = x0; x != x1; x += xstep) { \
   4.279 +            op(x, y); \
   4.280 +            error -= deltay; \
   4.281 +            if (error < 0) { \
   4.282 +                y += ystep; \
   4.283 +                error += deltax; \
   4.284 +            } \
   4.285 +        } \
   4.286 +    } else { \
   4.287 +        for (x = x0; x != x1; x += xstep) { \
   4.288 +            op(y, x); \
   4.289 +            error -= deltay; \
   4.290 +            if (error < 0) { \
   4.291 +                y += ystep; \
   4.292 +                error += deltax; \
   4.293 +            } \
   4.294 +        } \
   4.295 +    } \
   4.296 +}
   4.297 +
   4.298 +/*
   4.299 + * Define blend fill macro
   4.300 + */
   4.301 +
   4.302 +#define BLENDRECT(type, op) \
   4.303 +do { \
   4.304 +    int w; \
   4.305 +    int width = dstrect->w; \
   4.306 +    int height = dstrect->h; \
   4.307 +    int pitch = (dst->pitch / dst->format->BytesPerPixel); \
   4.308 +    int skip = pitch - width; \
   4.309 +    type *pixel = (type *)dst->pixels + dstrect->y * pitch + dstrect->x; \
   4.310 +    while (height--) { \
   4.311 +        { int n = (width+3)/4; \
   4.312 +            switch (width & 3) { \
   4.313 +            case 0: do {   op; pixel++; \
   4.314 +            case 3:        op; pixel++; \
   4.315 +            case 2:        op; pixel++; \
   4.316 +            case 1:        op; pixel++; \
   4.317 +                    } while ( --n > 0 ); \
   4.318 +            } \
   4.319 +        } \
   4.320 +        pixel += skip; \
   4.321 +    } \
   4.322 +} while (0)
   4.323 +
   4.324 +/*
   4.325 + * Define blend line macro
   4.326 + */
   4.327 +
   4.328 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/video/SDL_drawline.c	Sun Dec 21 08:24:32 2008 +0000
     5.2 +++ b/src/video/SDL_drawline.c	Sun Dec 21 08:28:25 2008 +0000
     5.3 @@ -21,64 +21,14 @@
     5.4  */
     5.5  #include "SDL_config.h"
     5.6  
     5.7 -#include "SDL_video.h"
     5.8 -#include "SDL_blit.h"
     5.9 -
    5.10 -#define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
    5.11 -
    5.12 -#define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0)
    5.13 -
    5.14 -#define BRESENHAM(x0, y0, x1, y1, op, color) \
    5.15 -{ \
    5.16 -    int deltax, deltay, steep, error, xstep, ystep, x, y; \
    5.17 - \
    5.18 -    deltax = ABS(x1 - x0); \
    5.19 -    deltay = ABS(y1 - y0); \
    5.20 -    steep = (deltay > deltax); \
    5.21 -    if (steep) { \
    5.22 -        SWAP(x0, y0); \
    5.23 -        SWAP(x1, y1); \
    5.24 -        SWAP(deltax, deltay); \
    5.25 -    } \
    5.26 -    error = (x1 - x0) / 2; \
    5.27 -    y = y0; \
    5.28 -    if (x0 > x1) { \
    5.29 -        xstep = -1; \
    5.30 -    } else { \
    5.31 -        xstep = 1; \
    5.32 -    } \
    5.33 -    if (y0 < y1) { \
    5.34 -        ystep = 1; \
    5.35 -    } else { \
    5.36 -        ystep = -1; \
    5.37 -    } \
    5.38 -    if (!steep) { \
    5.39 -        for (x = x0; x != x1; x += xstep) { \
    5.40 -            op(x, y, color); \
    5.41 -            error -= deltay; \
    5.42 -            if (error < 0) { \
    5.43 -                y += ystep; \
    5.44 -                error += deltax; \
    5.45 -            } \
    5.46 -        } \
    5.47 -    } else { \
    5.48 -        for (x = x0; x != x1; x += xstep) { \
    5.49 -            op(y, x, color); \
    5.50 -            error -= deltay; \
    5.51 -            if (error < 0) { \
    5.52 -                y += ystep; \
    5.53 -                error += deltax; \
    5.54 -            } \
    5.55 -        } \
    5.56 -    } \
    5.57 -}
    5.58 +#include "SDL_draw.h"
    5.59  
    5.60  #define SETPIXEL(x, y, type, bpp, color) \
    5.61      *(type *)(dst->pixels + y * dst->pitch + x * bpp) = (type) color
    5.62  
    5.63 -#define SETPIXEL1(x, y, color) SETPIXEL(x, y, Uint8, 1, color);
    5.64 -#define SETPIXEL2(x, y, color) SETPIXEL(x, y, Uint16, 2, color);
    5.65 -#define SETPIXEL4(x, y, color) SETPIXEL(x, y, Uint32, 4, color);
    5.66 +#define SETPIXEL1(x, y) SETPIXEL(x, y, Uint8, 1, color);
    5.67 +#define SETPIXEL2(x, y) SETPIXEL(x, y, Uint16, 2, color);
    5.68 +#define SETPIXEL4(x, y) SETPIXEL(x, y, Uint32, 4, color);
    5.69  
    5.70  SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
    5.71  {
    5.72 @@ -97,16 +47,16 @@
    5.73  
    5.74      switch (dst->format->BytesPerPixel) {
    5.75      case 1:
    5.76 -        BRESENHAM(x1, y1, x2, y2, SETPIXEL1, color);
    5.77 +        BRESENHAM(x1, y1, x2, y2, SETPIXEL1);
    5.78          break;
    5.79      case 2:
    5.80 -        BRESENHAM(x1, y1, x2, y2, SETPIXEL2, color);
    5.81 +        BRESENHAM(x1, y1, x2, y2, SETPIXEL2);
    5.82          break;
    5.83      case 3:
    5.84          SDL_Unsupported();
    5.85          return -1;
    5.86      case 4:
    5.87 -        BRESENHAM(x1, y1, x2, y2, SETPIXEL4, color);
    5.88 +        BRESENHAM(x1, y1, x2, y2, SETPIXEL4);
    5.89          break;
    5.90      }
    5.91      return 0;