Mercurial > sdl-ios-xcode
comparison src/render/software/SDL_blendpoint.c @ 5166:d72793305335
Making the API simpler, moved the surface drawing functions to the software renderer.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 03 Feb 2011 02:45:29 -0800 |
parents | src/video/SDL_blendpoint.c@e743b9c3f6d6 |
children | d976b67150c5 |
comparison
equal
deleted
inserted
replaced
5165:e2b3f003e085 | 5166:d72793305335 |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997-2010 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_draw.h" | |
25 #include "SDL_blendpoint.h" | |
26 | |
27 | |
28 static int | |
29 SDL_BlendPoint_RGB555(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r, | |
30 Uint8 g, Uint8 b, Uint8 a) | |
31 { | |
32 unsigned inva = 0xff - a; | |
33 | |
34 switch (blendMode) { | |
35 case SDL_BLENDMODE_BLEND: | |
36 DRAW_SETPIXELXY_BLEND_RGB555(x, y); | |
37 break; | |
38 case SDL_BLENDMODE_ADD: | |
39 DRAW_SETPIXELXY_ADD_RGB555(x, y); | |
40 break; | |
41 default: | |
42 DRAW_SETPIXELXY_RGB555(x, y); | |
43 break; | |
44 } | |
45 return 0; | |
46 } | |
47 | |
48 static int | |
49 SDL_BlendPoint_RGB565(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r, | |
50 Uint8 g, Uint8 b, Uint8 a) | |
51 { | |
52 unsigned inva = 0xff - a; | |
53 | |
54 switch (blendMode) { | |
55 case SDL_BLENDMODE_BLEND: | |
56 DRAW_SETPIXELXY_BLEND_RGB565(x, y); | |
57 break; | |
58 case SDL_BLENDMODE_ADD: | |
59 DRAW_SETPIXELXY_ADD_RGB565(x, y); | |
60 break; | |
61 default: | |
62 DRAW_SETPIXELXY_RGB565(x, y); | |
63 break; | |
64 } | |
65 return 0; | |
66 } | |
67 | |
68 static int | |
69 SDL_BlendPoint_RGB888(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r, | |
70 Uint8 g, Uint8 b, Uint8 a) | |
71 { | |
72 unsigned inva = 0xff - a; | |
73 | |
74 switch (blendMode) { | |
75 case SDL_BLENDMODE_BLEND: | |
76 DRAW_SETPIXELXY_BLEND_RGB888(x, y); | |
77 break; | |
78 case SDL_BLENDMODE_ADD: | |
79 DRAW_SETPIXELXY_ADD_RGB888(x, y); | |
80 break; | |
81 default: | |
82 DRAW_SETPIXELXY_RGB888(x, y); | |
83 break; | |
84 } | |
85 return 0; | |
86 } | |
87 | |
88 static int | |
89 SDL_BlendPoint_ARGB8888(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, | |
90 Uint8 r, Uint8 g, Uint8 b, Uint8 a) | |
91 { | |
92 unsigned inva = 0xff - a; | |
93 | |
94 switch (blendMode) { | |
95 case SDL_BLENDMODE_BLEND: | |
96 DRAW_SETPIXELXY_BLEND_ARGB8888(x, y); | |
97 break; | |
98 case SDL_BLENDMODE_ADD: | |
99 DRAW_SETPIXELXY_ADD_ARGB8888(x, y); | |
100 break; | |
101 default: | |
102 DRAW_SETPIXELXY_ARGB8888(x, y); | |
103 break; | |
104 } | |
105 return 0; | |
106 } | |
107 | |
108 static int | |
109 SDL_BlendPoint_RGB(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r, | |
110 Uint8 g, Uint8 b, Uint8 a) | |
111 { | |
112 SDL_PixelFormat *fmt = dst->format; | |
113 unsigned inva = 0xff - a; | |
114 | |
115 switch (fmt->BytesPerPixel) { | |
116 case 2: | |
117 switch (blendMode) { | |
118 case SDL_BLENDMODE_BLEND: | |
119 DRAW_SETPIXELXY2_BLEND_RGB(x, y); | |
120 break; | |
121 case SDL_BLENDMODE_ADD: | |
122 DRAW_SETPIXELXY2_ADD_RGB(x, y); | |
123 break; | |
124 default: | |
125 DRAW_SETPIXELXY2_RGB(x, y); | |
126 break; | |
127 } | |
128 return 0; | |
129 case 4: | |
130 switch (blendMode) { | |
131 case SDL_BLENDMODE_BLEND: | |
132 DRAW_SETPIXELXY4_BLEND_RGB(x, y); | |
133 break; | |
134 case SDL_BLENDMODE_ADD: | |
135 DRAW_SETPIXELXY4_ADD_RGB(x, y); | |
136 break; | |
137 default: | |
138 DRAW_SETPIXELXY4_RGB(x, y); | |
139 break; | |
140 } | |
141 return 0; | |
142 default: | |
143 SDL_Unsupported(); | |
144 return -1; | |
145 } | |
146 } | |
147 | |
148 static int | |
149 SDL_BlendPoint_RGBA(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r, | |
150 Uint8 g, Uint8 b, Uint8 a) | |
151 { | |
152 SDL_PixelFormat *fmt = dst->format; | |
153 unsigned inva = 0xff - a; | |
154 | |
155 switch (fmt->BytesPerPixel) { | |
156 case 4: | |
157 switch (blendMode) { | |
158 case SDL_BLENDMODE_BLEND: | |
159 DRAW_SETPIXELXY4_BLEND_RGBA(x, y); | |
160 break; | |
161 case SDL_BLENDMODE_ADD: | |
162 DRAW_SETPIXELXY4_ADD_RGBA(x, y); | |
163 break; | |
164 default: | |
165 DRAW_SETPIXELXY4_RGBA(x, y); | |
166 break; | |
167 } | |
168 return 0; | |
169 default: | |
170 SDL_Unsupported(); | |
171 return -1; | |
172 } | |
173 } | |
174 | |
175 int | |
176 SDL_BlendPoint(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r, | |
177 Uint8 g, Uint8 b, Uint8 a) | |
178 { | |
179 if (!dst) { | |
180 SDL_SetError("Passed NULL destination surface"); | |
181 return -1; | |
182 } | |
183 | |
184 /* This function doesn't work on surfaces < 8 bpp */ | |
185 if (dst->format->BitsPerPixel < 8) { | |
186 SDL_SetError("SDL_BlendPoint(): Unsupported surface format"); | |
187 return -1; | |
188 } | |
189 | |
190 /* Perform clipping */ | |
191 if (x < dst->clip_rect.x || y < dst->clip_rect.y || | |
192 x >= (dst->clip_rect.x + dst->clip_rect.w) || | |
193 y >= (dst->clip_rect.y + dst->clip_rect.h)) { | |
194 return 0; | |
195 } | |
196 | |
197 if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) { | |
198 r = DRAW_MUL(r, a); | |
199 g = DRAW_MUL(g, a); | |
200 b = DRAW_MUL(b, a); | |
201 } | |
202 | |
203 switch (dst->format->BitsPerPixel) { | |
204 case 15: | |
205 switch (dst->format->Rmask) { | |
206 case 0x7C00: | |
207 return SDL_BlendPoint_RGB555(dst, x, y, blendMode, r, g, b, a); | |
208 } | |
209 break; | |
210 case 16: | |
211 switch (dst->format->Rmask) { | |
212 case 0xF800: | |
213 return SDL_BlendPoint_RGB565(dst, x, y, blendMode, r, g, b, a); | |
214 } | |
215 break; | |
216 case 32: | |
217 switch (dst->format->Rmask) { | |
218 case 0x00FF0000: | |
219 if (!dst->format->Amask) { | |
220 return SDL_BlendPoint_RGB888(dst, x, y, blendMode, r, g, b, | |
221 a); | |
222 } else { | |
223 return SDL_BlendPoint_ARGB8888(dst, x, y, blendMode, r, g, b, | |
224 a); | |
225 } | |
226 break; | |
227 } | |
228 break; | |
229 default: | |
230 break; | |
231 } | |
232 | |
233 if (!dst->format->Amask) { | |
234 return SDL_BlendPoint_RGB(dst, x, y, blendMode, r, g, b, a); | |
235 } else { | |
236 return SDL_BlendPoint_RGBA(dst, x, y, blendMode, r, g, b, a); | |
237 } | |
238 } | |
239 | |
240 int | |
241 SDL_BlendPoints(SDL_Surface * dst, const SDL_Point * points, int count, | |
242 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a) | |
243 { | |
244 int minx, miny; | |
245 int maxx, maxy; | |
246 int i; | |
247 int x, y; | |
248 int (*func)(SDL_Surface * dst, int x, int y, | |
249 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a) = NULL; | |
250 int status = 0; | |
251 | |
252 if (!dst) { | |
253 SDL_SetError("Passed NULL destination surface"); | |
254 return -1; | |
255 } | |
256 | |
257 /* This function doesn't work on surfaces < 8 bpp */ | |
258 if (dst->format->BitsPerPixel < 8) { | |
259 SDL_SetError("SDL_BlendPoints(): Unsupported surface format"); | |
260 return (-1); | |
261 } | |
262 | |
263 if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) { | |
264 r = DRAW_MUL(r, a); | |
265 g = DRAW_MUL(g, a); | |
266 b = DRAW_MUL(b, a); | |
267 } | |
268 | |
269 /* FIXME: Does this function pointer slow things down significantly? */ | |
270 switch (dst->format->BitsPerPixel) { | |
271 case 15: | |
272 switch (dst->format->Rmask) { | |
273 case 0x7C00: | |
274 func = SDL_BlendPoint_RGB555; | |
275 break; | |
276 } | |
277 break; | |
278 case 16: | |
279 switch (dst->format->Rmask) { | |
280 case 0xF800: | |
281 func = SDL_BlendPoint_RGB565; | |
282 break; | |
283 } | |
284 break; | |
285 case 32: | |
286 switch (dst->format->Rmask) { | |
287 case 0x00FF0000: | |
288 if (!dst->format->Amask) { | |
289 func = SDL_BlendPoint_RGB888; | |
290 } else { | |
291 func = SDL_BlendPoint_ARGB8888; | |
292 } | |
293 break; | |
294 } | |
295 break; | |
296 default: | |
297 break; | |
298 } | |
299 | |
300 if (!func) { | |
301 if (!dst->format->Amask) { | |
302 func = SDL_BlendPoint_RGB; | |
303 } else { | |
304 func = SDL_BlendPoint_RGBA; | |
305 } | |
306 } | |
307 | |
308 minx = dst->clip_rect.x; | |
309 maxx = dst->clip_rect.x + dst->clip_rect.w - 1; | |
310 miny = dst->clip_rect.y; | |
311 maxy = dst->clip_rect.y + dst->clip_rect.h - 1; | |
312 | |
313 for (i = 0; i < count; ++i) { | |
314 x = points[i].x; | |
315 y = points[i].y; | |
316 | |
317 if (x < minx || x > maxx || y < miny || y > maxy) { | |
318 continue; | |
319 } | |
320 status = func(dst, x, y, blendMode, r, g, b, a); | |
321 } | |
322 return status; | |
323 } | |
324 | |
325 /* vi: set ts=4 sw=4 expandtab: */ |