Mercurial > sdl-ios-xcode
comparison src/video/SDL_draw.h @ 2898:e40448bc7727
Share code between fill and line drawing
Added general RGB surface format fallbacks to drawing code
Fixed issues with destination surface alpha channel
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 21 Dec 2008 08:28:25 +0000 |
parents | |
children | a0c837a16e4c |
comparison
equal
deleted
inserted
replaced
2897:8be863ef68ee | 2898:e40448bc7727 |
---|---|
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_SETPIXEL(setpixel) \ | |
33 do { \ | |
34 unsigned sr = r, sg = g, sb = b, sa = a; \ | |
35 setpixel; \ | |
36 } while (0) | |
37 | |
38 #define DRAW_SETPIXEL_BLEND(getpixel, setpixel) \ | |
39 do { \ | |
40 unsigned sr, sg, sb, sa; sa; \ | |
41 getpixel; \ | |
42 sr = DRAW_MUL(inva, sr) + r; \ | |
43 sg = DRAW_MUL(inva, sg) + g; \ | |
44 sb = DRAW_MUL(inva, sb) + b; \ | |
45 setpixel; \ | |
46 } while (0) | |
47 | |
48 #define DRAW_SETPIXEL_ADD(getpixel, setpixel) \ | |
49 do { \ | |
50 unsigned sr, sg, sb, sa; sa; \ | |
51 getpixel; \ | |
52 sr += r; if (sr > 0xff) sr = 0xff; \ | |
53 sg += g; if (sg > 0xff) sg = 0xff; \ | |
54 sb += b; if (sb > 0xff) sb = 0xff; \ | |
55 setpixel; \ | |
56 } while (0) | |
57 | |
58 #define DRAW_SETPIXEL_MOD(getpixel, setpixel) \ | |
59 do { \ | |
60 unsigned sr, sg, sb, sa; sa; \ | |
61 getpixel; \ | |
62 sr = DRAW_MUL(sr, r); \ | |
63 sg = DRAW_MUL(sg, g); \ | |
64 sb = DRAW_MUL(sb, b); \ | |
65 setpixel; \ | |
66 } while (0) | |
67 | |
68 #define DRAW_SETPIXELXY(x, y, type, bpp, op) \ | |
69 do { \ | |
70 type *pixel = (type *)(dst->pixels + y * dst->pitch + x * bpp); \ | |
71 op; \ | |
72 } while (0) | |
73 | |
74 /* | |
75 * Define draw operators for RGB555 | |
76 */ | |
77 | |
78 #define DRAW_SETPIXEL_RGB555 \ | |
79 DRAW_SETPIXEL(RGB555_FROM_RGB(*pixel, sr, sg, sb)) | |
80 | |
81 #define DRAW_SETPIXEL_BLEND_RGB555 \ | |
82 DRAW_SETPIXEL_BLEND(RGB_FROM_RGB555(*pixel, sr, sg, sb), \ | |
83 RGB555_FROM_RGB(*pixel, sr, sg, sb)) | |
84 | |
85 #define DRAW_SETPIXEL_ADD_RGB555 \ | |
86 DRAW_SETPIXEL_ADD(RGB_FROM_RGB555(*pixel, sr, sg, sb), \ | |
87 RGB555_FROM_RGB(*pixel, sr, sg, sb)) | |
88 | |
89 #define DRAW_SETPIXEL_MOD_RGB555 \ | |
90 DRAW_SETPIXEL_MOD(RGB_FROM_RGB555(*pixel, sr, sg, sb), \ | |
91 RGB555_FROM_RGB(*pixel, sr, sg, sb)) | |
92 | |
93 #define DRAW_SETPIXELXY_RGB555(x, y) \ | |
94 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB555) | |
95 | |
96 #define DRAW_SETPIXELXY_BLEND_RGB555(x, y) \ | |
97 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB555) | |
98 | |
99 #define DRAW_SETPIXELXY_ADD_RGB555(x, y) \ | |
100 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB555) | |
101 | |
102 #define DRAW_SETPIXELXY_MOD_RGB555(x, y) \ | |
103 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB555) | |
104 | |
105 /* | |
106 * Define draw operators for RGB565 | |
107 */ | |
108 | |
109 #define DRAW_SETPIXEL_RGB565 \ | |
110 DRAW_SETPIXEL(RGB565_FROM_RGB(*pixel, sr, sg, sb)) | |
111 | |
112 #define DRAW_SETPIXEL_BLEND_RGB565 \ | |
113 DRAW_SETPIXEL_BLEND(RGB_FROM_RGB565(*pixel, sr, sg, sb), \ | |
114 RGB565_FROM_RGB(*pixel, sr, sg, sb)) | |
115 | |
116 #define DRAW_SETPIXEL_ADD_RGB565 \ | |
117 DRAW_SETPIXEL_ADD(RGB_FROM_RGB565(*pixel, sr, sg, sb), \ | |
118 RGB565_FROM_RGB(*pixel, sr, sg, sb)) | |
119 | |
120 #define DRAW_SETPIXEL_MOD_RGB565 \ | |
121 DRAW_SETPIXEL_MOD(RGB_FROM_RGB565(*pixel, sr, sg, sb), \ | |
122 RGB565_FROM_RGB(*pixel, sr, sg, sb)) | |
123 | |
124 #define DRAW_SETPIXELXY_RGB565(x, y) \ | |
125 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB565) | |
126 | |
127 #define DRAW_SETPIXELXY_BLEND_RGB565(x, y) \ | |
128 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB565) | |
129 | |
130 #define DRAW_SETPIXELXY_ADD_RGB565(x, y) \ | |
131 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB565) | |
132 | |
133 #define DRAW_SETPIXELXY_MOD_RGB565(x, y) \ | |
134 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB565) | |
135 | |
136 /* | |
137 * Define draw operators for RGB888 | |
138 */ | |
139 | |
140 #define DRAW_SETPIXEL_RGB888 \ | |
141 DRAW_SETPIXEL(RGB888_FROM_RGB(*pixel, sr, sg, sb)) | |
142 | |
143 #define DRAW_SETPIXEL_BLEND_RGB888 \ | |
144 DRAW_SETPIXEL_BLEND(RGB_FROM_RGB888(*pixel, sr, sg, sb), \ | |
145 RGB888_FROM_RGB(*pixel, sr, sg, sb)) | |
146 | |
147 #define DRAW_SETPIXEL_ADD_RGB888 \ | |
148 DRAW_SETPIXEL_ADD(RGB_FROM_RGB888(*pixel, sr, sg, sb), \ | |
149 RGB888_FROM_RGB(*pixel, sr, sg, sb)) | |
150 | |
151 #define DRAW_SETPIXEL_MOD_RGB888 \ | |
152 DRAW_SETPIXEL_MOD(RGB_FROM_RGB888(*pixel, sr, sg, sb), \ | |
153 RGB888_FROM_RGB(*pixel, sr, sg, sb)) | |
154 | |
155 #define DRAW_SETPIXELXY_RGB888(x, y) \ | |
156 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB888) | |
157 | |
158 #define DRAW_SETPIXELXY_BLEND_RGB888(x, y) \ | |
159 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB888) | |
160 | |
161 #define DRAW_SETPIXELXY_ADD_RGB888(x, y) \ | |
162 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB888) | |
163 | |
164 #define DRAW_SETPIXELXY_MOD_RGB888(x, y) \ | |
165 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB888) | |
166 | |
167 /* | |
168 * Define draw operators for general RGB | |
169 */ | |
170 | |
171 #define DRAW_SETPIXEL_RGB \ | |
172 DRAW_SETPIXEL(PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb)) | |
173 | |
174 #define DRAW_SETPIXEL_BLEND_RGB \ | |
175 DRAW_SETPIXEL_BLEND(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \ | |
176 PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb)) | |
177 | |
178 #define DRAW_SETPIXEL_ADD_RGB \ | |
179 DRAW_SETPIXEL_ADD(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \ | |
180 PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb)) | |
181 | |
182 #define DRAW_SETPIXEL_MOD_RGB \ | |
183 DRAW_SETPIXEL_MOD(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \ | |
184 PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb)) | |
185 | |
186 #define DRAW_SETPIXELXY2_RGB(x, y) \ | |
187 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB) | |
188 | |
189 #define DRAW_SETPIXELXY4_RGB(x, y) \ | |
190 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB) | |
191 | |
192 #define DRAW_SETPIXELXY2_BLEND_RGB(x, y) \ | |
193 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB) | |
194 | |
195 #define DRAW_SETPIXELXY4_BLEND_RGB(x, y) \ | |
196 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB) | |
197 | |
198 #define DRAW_SETPIXELXY2_ADD_RGB(x, y) \ | |
199 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB) | |
200 | |
201 #define DRAW_SETPIXELXY4_ADD_RGB(x, y) \ | |
202 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB) | |
203 | |
204 #define DRAW_SETPIXELXY2_MOD_RGB(x, y) \ | |
205 DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB) | |
206 | |
207 #define DRAW_SETPIXELXY4_MOD_RGB(x, y) \ | |
208 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB) | |
209 | |
210 | |
211 /* | |
212 * Define draw operators for general RGBA | |
213 */ | |
214 | |
215 #define DRAW_SETPIXEL_RGBA \ | |
216 DRAW_SETPIXEL(PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa)) | |
217 | |
218 #define DRAW_SETPIXEL_BLEND_RGBA \ | |
219 DRAW_SETPIXEL_BLEND(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \ | |
220 PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa)) | |
221 | |
222 #define DRAW_SETPIXEL_ADD_RGBA \ | |
223 DRAW_SETPIXEL_ADD(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \ | |
224 PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa)) | |
225 | |
226 #define DRAW_SETPIXEL_MOD_RGBA \ | |
227 DRAW_SETPIXEL_MOD(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \ | |
228 PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa)) | |
229 | |
230 #define DRAW_SETPIXELXY4_RGBA(x, y) \ | |
231 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGBA) | |
232 | |
233 #define DRAW_SETPIXELXY4_BLEND_RGBA(x, y) \ | |
234 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGBA) | |
235 | |
236 #define DRAW_SETPIXELXY4_ADD_RGBA(x, y) \ | |
237 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGBA) | |
238 | |
239 #define DRAW_SETPIXELXY4_MOD_RGBA(x, y) \ | |
240 DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGBA) | |
241 | |
242 /* | |
243 * Define line drawing macro | |
244 */ | |
245 | |
246 #define ABS(_x) ((_x) < 0 ? -(_x) : (_x)) | |
247 | |
248 #define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0) | |
249 | |
250 #define BRESENHAM(x0, y0, x1, y1, op) \ | |
251 { \ | |
252 int deltax, deltay, steep, error, xstep, ystep, x, y; \ | |
253 \ | |
254 deltax = ABS(x1 - x0); \ | |
255 deltay = ABS(y1 - y0); \ | |
256 steep = (deltay > deltax); \ | |
257 if (steep) { \ | |
258 SWAP(x0, y0); \ | |
259 SWAP(x1, y1); \ | |
260 SWAP(deltax, deltay); \ | |
261 } \ | |
262 error = (x1 - x0) / 2; \ | |
263 y = y0; \ | |
264 if (x0 > x1) { \ | |
265 xstep = -1; \ | |
266 } else { \ | |
267 xstep = 1; \ | |
268 } \ | |
269 if (y0 < y1) { \ | |
270 ystep = 1; \ | |
271 } else { \ | |
272 ystep = -1; \ | |
273 } \ | |
274 if (!steep) { \ | |
275 for (x = x0; x != x1; x += xstep) { \ | |
276 op(x, y); \ | |
277 error -= deltay; \ | |
278 if (error < 0) { \ | |
279 y += ystep; \ | |
280 error += deltax; \ | |
281 } \ | |
282 } \ | |
283 } else { \ | |
284 for (x = x0; x != x1; x += xstep) { \ | |
285 op(y, x); \ | |
286 error -= deltay; \ | |
287 if (error < 0) { \ | |
288 y += ystep; \ | |
289 error += deltax; \ | |
290 } \ | |
291 } \ | |
292 } \ | |
293 } | |
294 | |
295 /* | |
296 * Define blend fill macro | |
297 */ | |
298 | |
299 #define BLENDRECT(type, op) \ | |
300 do { \ | |
301 int w; \ | |
302 int width = dstrect->w; \ | |
303 int height = dstrect->h; \ | |
304 int pitch = (dst->pitch / dst->format->BytesPerPixel); \ | |
305 int skip = pitch - width; \ | |
306 type *pixel = (type *)dst->pixels + dstrect->y * pitch + dstrect->x; \ | |
307 while (height--) { \ | |
308 { int n = (width+3)/4; \ | |
309 switch (width & 3) { \ | |
310 case 0: do { op; pixel++; \ | |
311 case 3: op; pixel++; \ | |
312 case 2: op; pixel++; \ | |
313 case 1: op; pixel++; \ | |
314 } while ( --n > 0 ); \ | |
315 } \ | |
316 } \ | |
317 pixel += skip; \ | |
318 } \ | |
319 } while (0) | |
320 | |
321 /* | |
322 * Define blend line macro | |
323 */ | |
324 | |
325 /* vi: set ts=4 sw=4 expandtab: */ |