src/video/SDL_draw.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3536 0267b8b1595c
child 3593 b931bcfd94a0
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_blit.h"
    25 
    26 /* This code assumes that r, g, b, a are the source color,
    27  * and in the blend and add case, the RGB values are premultiplied by a.
    28  */
    29 
    30 #define DRAW_MUL(_a, _b) (((unsigned)(_a)*(_b))/255)
    31 
    32 #define DRAW_FASTSETPIXEL(x, y, type, bpp, color) \
    33     *(type *)((Uint8 *)dst->pixels + y * dst->pitch + x * bpp) = (type) color
    34 
    35 #define DRAW_FASTSETPIXEL1(x, y) DRAW_FASTSETPIXEL(x, y, Uint8, 1, color);
    36 #define DRAW_FASTSETPIXEL2(x, y) DRAW_FASTSETPIXEL(x, y, Uint16, 2, color);
    37 #define DRAW_FASTSETPIXEL4(x, y) DRAW_FASTSETPIXEL(x, y, Uint32, 4, color);
    38 
    39 #define DRAW_SETPIXEL(setpixel) \
    40 do { \
    41     unsigned sr = r, sg = g, sb = b, sa = a; \
    42     setpixel; \
    43 } while (0)
    44 
    45 #define DRAW_SETPIXEL_BLEND(getpixel, setpixel) \
    46 do { \
    47     unsigned sr, sg, sb, sa; sa; \
    48     getpixel; \
    49     sr = DRAW_MUL(inva, sr) + r; \
    50     sg = DRAW_MUL(inva, sg) + g; \
    51     sb = DRAW_MUL(inva, sb) + b; \
    52     setpixel; \
    53 } while (0)
    54 
    55 #define DRAW_SETPIXEL_ADD(getpixel, setpixel) \
    56 do { \
    57     unsigned sr, sg, sb, sa; sa; \
    58     getpixel; \
    59     sr += r; if (sr > 0xff) sr = 0xff; \
    60     sg += g; if (sg > 0xff) sg = 0xff; \
    61     sb += b; if (sb > 0xff) sb = 0xff; \
    62     setpixel; \
    63 } while (0)
    64 
    65 #define DRAW_SETPIXEL_MOD(getpixel, setpixel) \
    66 do { \
    67     unsigned sr, sg, sb, sa; sa; \
    68     getpixel; \
    69     sr = DRAW_MUL(sr, r); \
    70     sg = DRAW_MUL(sg, g); \
    71     sb = DRAW_MUL(sb, b); \
    72     setpixel; \
    73 } while (0)
    74 
    75 #define DRAW_SETPIXELXY(x, y, type, bpp, op) \
    76 do { \
    77     type *pixel = (type *)((Uint8 *)dst->pixels + y * dst->pitch + x * bpp); \
    78     op; \
    79 } while (0)
    80 
    81 /*
    82  * Define draw operators for RGB555
    83  */
    84 
    85 #define DRAW_SETPIXEL_RGB555 \
    86     DRAW_SETPIXEL(RGB555_FROM_RGB(*pixel, sr, sg, sb))
    87 
    88 #define DRAW_SETPIXEL_BLEND_RGB555 \
    89     DRAW_SETPIXEL_BLEND(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
    90                         RGB555_FROM_RGB(*pixel, sr, sg, sb))
    91 
    92 #define DRAW_SETPIXEL_ADD_RGB555 \
    93     DRAW_SETPIXEL_ADD(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
    94                       RGB555_FROM_RGB(*pixel, sr, sg, sb))
    95 
    96 #define DRAW_SETPIXEL_MOD_RGB555 \
    97     DRAW_SETPIXEL_MOD(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
    98                       RGB555_FROM_RGB(*pixel, sr, sg, sb))
    99 
   100 #define DRAW_SETPIXELXY_RGB555(x, y) \
   101     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB555)
   102 
   103 #define DRAW_SETPIXELXY_BLEND_RGB555(x, y) \
   104     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB555)
   105 
   106 #define DRAW_SETPIXELXY_ADD_RGB555(x, y) \
   107     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB555)
   108 
   109 #define DRAW_SETPIXELXY_MOD_RGB555(x, y) \
   110     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB555)
   111 
   112 /*
   113  * Define draw operators for RGB565
   114  */
   115 
   116 #define DRAW_SETPIXEL_RGB565 \
   117     DRAW_SETPIXEL(RGB565_FROM_RGB(*pixel, sr, sg, sb))
   118 
   119 #define DRAW_SETPIXEL_BLEND_RGB565 \
   120     DRAW_SETPIXEL_BLEND(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
   121                         RGB565_FROM_RGB(*pixel, sr, sg, sb))
   122 
   123 #define DRAW_SETPIXEL_ADD_RGB565 \
   124     DRAW_SETPIXEL_ADD(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
   125                       RGB565_FROM_RGB(*pixel, sr, sg, sb))
   126 
   127 #define DRAW_SETPIXEL_MOD_RGB565 \
   128     DRAW_SETPIXEL_MOD(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
   129                       RGB565_FROM_RGB(*pixel, sr, sg, sb))
   130 
   131 #define DRAW_SETPIXELXY_RGB565(x, y) \
   132     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB565)
   133 
   134 #define DRAW_SETPIXELXY_BLEND_RGB565(x, y) \
   135     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB565)
   136 
   137 #define DRAW_SETPIXELXY_ADD_RGB565(x, y) \
   138     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB565)
   139 
   140 #define DRAW_SETPIXELXY_MOD_RGB565(x, y) \
   141     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB565)
   142 
   143 /*
   144  * Define draw operators for RGB888
   145  */
   146 
   147 #define DRAW_SETPIXEL_RGB888 \
   148     DRAW_SETPIXEL(RGB888_FROM_RGB(*pixel, sr, sg, sb))
   149 
   150 #define DRAW_SETPIXEL_BLEND_RGB888 \
   151     DRAW_SETPIXEL_BLEND(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
   152                         RGB888_FROM_RGB(*pixel, sr, sg, sb))
   153 
   154 #define DRAW_SETPIXEL_ADD_RGB888 \
   155     DRAW_SETPIXEL_ADD(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
   156                       RGB888_FROM_RGB(*pixel, sr, sg, sb))
   157 
   158 #define DRAW_SETPIXEL_MOD_RGB888 \
   159     DRAW_SETPIXEL_MOD(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
   160                       RGB888_FROM_RGB(*pixel, sr, sg, sb))
   161 
   162 #define DRAW_SETPIXELXY_RGB888(x, y) \
   163     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB888)
   164 
   165 #define DRAW_SETPIXELXY_BLEND_RGB888(x, y) \
   166     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB888)
   167 
   168 #define DRAW_SETPIXELXY_ADD_RGB888(x, y) \
   169     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB888)
   170 
   171 #define DRAW_SETPIXELXY_MOD_RGB888(x, y) \
   172     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB888)
   173 
   174 /*
   175  * Define draw operators for ARGB8888
   176  */
   177 
   178 #define DRAW_SETPIXEL_ARGB8888 \
   179     DRAW_SETPIXEL(ARGB8888_FROM_RGBA(*pixel, sr, sg, sb, sa))
   180 
   181 #define DRAW_SETPIXEL_BLEND_ARGB8888 \
   182     DRAW_SETPIXEL_BLEND(RGBA_FROM_ARGB8888(*pixel, sr, sg, sb, sa), \
   183                         ARGB8888_FROM_RGBA(*pixel, sr, sg, sb, sa))
   184 
   185 #define DRAW_SETPIXEL_ADD_ARGB8888 \
   186     DRAW_SETPIXEL_ADD(RGBA_FROM_ARGB8888(*pixel, sr, sg, sb, sa), \
   187                       ARGB8888_FROM_RGBA(*pixel, sr, sg, sb, sa))
   188 
   189 #define DRAW_SETPIXEL_MOD_ARGB8888 \
   190     DRAW_SETPIXEL_MOD(RGBA_FROM_ARGB8888(*pixel, sr, sg, sb, sa), \
   191                       ARGB8888_FROM_RGBA(*pixel, sr, sg, sb, sa))
   192 
   193 #define DRAW_SETPIXELXY_ARGB8888(x, y) \
   194     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ARGB8888)
   195 
   196 #define DRAW_SETPIXELXY_BLEND_ARGB8888(x, y) \
   197     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_ARGB8888)
   198 
   199 #define DRAW_SETPIXELXY_ADD_ARGB8888(x, y) \
   200     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_ARGB8888)
   201 
   202 #define DRAW_SETPIXELXY_MOD_ARGB8888(x, y) \
   203     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_ARGB8888)
   204 
   205 /*
   206  * Define draw operators for general RGB
   207  */
   208 
   209 #define DRAW_SETPIXEL_RGB \
   210     DRAW_SETPIXEL(PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
   211 
   212 #define DRAW_SETPIXEL_BLEND_RGB \
   213     DRAW_SETPIXEL_BLEND(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
   214                         PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
   215 
   216 #define DRAW_SETPIXEL_ADD_RGB \
   217     DRAW_SETPIXEL_ADD(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
   218                       PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
   219 
   220 #define DRAW_SETPIXEL_MOD_RGB \
   221     DRAW_SETPIXEL_MOD(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
   222                       PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
   223 
   224 #define DRAW_SETPIXELXY2_RGB(x, y) \
   225     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB)
   226 
   227 #define DRAW_SETPIXELXY4_RGB(x, y) \
   228     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB)
   229 
   230 #define DRAW_SETPIXELXY2_BLEND_RGB(x, y) \
   231     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB)
   232 
   233 #define DRAW_SETPIXELXY4_BLEND_RGB(x, y) \
   234     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB)
   235 
   236 #define DRAW_SETPIXELXY2_ADD_RGB(x, y) \
   237     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB)
   238 
   239 #define DRAW_SETPIXELXY4_ADD_RGB(x, y) \
   240     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB)
   241 
   242 #define DRAW_SETPIXELXY2_MOD_RGB(x, y) \
   243     DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB)
   244 
   245 #define DRAW_SETPIXELXY4_MOD_RGB(x, y) \
   246     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB)
   247 
   248 
   249 /*
   250  * Define draw operators for general RGBA
   251  */
   252 
   253 #define DRAW_SETPIXEL_RGBA \
   254     DRAW_SETPIXEL(PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
   255 
   256 #define DRAW_SETPIXEL_BLEND_RGBA \
   257     DRAW_SETPIXEL_BLEND(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
   258                         PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
   259 
   260 #define DRAW_SETPIXEL_ADD_RGBA \
   261     DRAW_SETPIXEL_ADD(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
   262                       PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
   263 
   264 #define DRAW_SETPIXEL_MOD_RGBA \
   265     DRAW_SETPIXEL_MOD(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
   266                       PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
   267 
   268 #define DRAW_SETPIXELXY4_RGBA(x, y) \
   269     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGBA)
   270 
   271 #define DRAW_SETPIXELXY4_BLEND_RGBA(x, y) \
   272     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGBA)
   273 
   274 #define DRAW_SETPIXELXY4_ADD_RGBA(x, y) \
   275     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGBA)
   276 
   277 #define DRAW_SETPIXELXY4_MOD_RGBA(x, y) \
   278     DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGBA)
   279 
   280 /*
   281  * Define line drawing macro
   282  */
   283 
   284 #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
   285 
   286 #define BRESENHAM(x1, y1, x2, y2, op) \
   287 { \
   288     int i, deltax, deltay, numpixels; \
   289     int d, dinc1, dinc2; \
   290     int x, xinc1, xinc2; \
   291     int y, yinc1, yinc2; \
   292  \
   293     deltax = ABS(x2 - x1); \
   294     deltay = ABS(y2 - y1); \
   295  \
   296     if (deltax >= deltay) { \
   297         numpixels = deltax + 1; \
   298         d = (2 * deltay) - deltax; \
   299         dinc1 = deltay * 2; \
   300         dinc2 = (deltay - deltax) * 2; \
   301         xinc1 = 1; \
   302         xinc2 = 1; \
   303         yinc1 = 0; \
   304         yinc2 = 1; \
   305     } else { \
   306         numpixels = deltay + 1; \
   307         d = (2 * deltax) - deltay; \
   308         dinc1 = deltax * 2; \
   309         dinc2 = (deltax - deltay) * 2; \
   310         xinc1 = 0; \
   311         xinc2 = 1; \
   312         yinc1 = 1; \
   313         yinc2 = 1; \
   314     } \
   315  \
   316     if (x1 > x2) { \
   317         xinc1 = -xinc1; \
   318         xinc2 = -xinc2; \
   319     } \
   320     if (y1 > y2) { \
   321         yinc1 = -yinc1; \
   322         yinc2 = -yinc2; \
   323     } \
   324  \
   325     x = x1; \
   326     y = y1; \
   327  \
   328     for (i = 0; i < numpixels; ++i) { \
   329         op(x, y); \
   330         if (d < 0) { \
   331             d += dinc1; \
   332             x += xinc1; \
   333             y += yinc1; \
   334         } else { \
   335             d += dinc2; \
   336             x += xinc2; \
   337             y += yinc2; \
   338         } \
   339     } \
   340 }
   341 #define DRAWLINE(x0, y0, x1, y1, op)	BRESENHAM(x0, y0, x1, y1, op)
   342 
   343 /*
   344  * Define blend fill macro
   345  */
   346 
   347 #define FILLRECT(type, op) \
   348 do { \
   349     int width = rect->w; \
   350     int height = rect->h; \
   351     int pitch = (dst->pitch / dst->format->BytesPerPixel); \
   352     int skip = pitch - width; \
   353     type *pixel = (type *)dst->pixels + rect->y * pitch + rect->x; \
   354     while (height--) { \
   355         { int n = (width+3)/4; \
   356             switch (width & 3) { \
   357             case 0: do {   op; pixel++; \
   358             case 3:        op; pixel++; \
   359             case 2:        op; pixel++; \
   360             case 1:        op; pixel++; \
   361                     } while ( --n > 0 ); \
   362             } \
   363         } \
   364         pixel += skip; \
   365     } \
   366 } while (0)
   367 
   368 /*
   369  * Define blend line macro
   370  */
   371 
   372 /* vi: set ts=4 sw=4 expandtab: */