comparison src/render/software/SDL_blendfillrect.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
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_blendfillrect.h"
26
27
28 static int
29 SDL_BlendFillRect_RGB555(SDL_Surface * dst, const SDL_Rect * rect,
30 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
31 {
32 unsigned inva = 0xff - a;
33
34 switch (blendMode) {
35 case SDL_BLENDMODE_BLEND:
36 FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB555);
37 break;
38 case SDL_BLENDMODE_ADD:
39 FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB555);
40 break;
41 default:
42 FILLRECT(Uint16, DRAW_SETPIXEL_RGB555);
43 break;
44 }
45 return 0;
46 }
47
48 static int
49 SDL_BlendFillRect_RGB565(SDL_Surface * dst, const SDL_Rect * rect,
50 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
51 {
52 unsigned inva = 0xff - a;
53
54 switch (blendMode) {
55 case SDL_BLENDMODE_BLEND:
56 FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB565);
57 break;
58 case SDL_BLENDMODE_ADD:
59 FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB565);
60 break;
61 default:
62 FILLRECT(Uint16, DRAW_SETPIXEL_RGB565);
63 break;
64 }
65 return 0;
66 }
67
68 static int
69 SDL_BlendFillRect_RGB888(SDL_Surface * dst, const SDL_Rect * rect,
70 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
71 {
72 unsigned inva = 0xff - a;
73
74 switch (blendMode) {
75 case SDL_BLENDMODE_BLEND:
76 FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB888);
77 break;
78 case SDL_BLENDMODE_ADD:
79 FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGB888);
80 break;
81 default:
82 FILLRECT(Uint32, DRAW_SETPIXEL_RGB888);
83 break;
84 }
85 return 0;
86 }
87
88 static int
89 SDL_BlendFillRect_ARGB8888(SDL_Surface * dst, const SDL_Rect * rect,
90 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
91 {
92 unsigned inva = 0xff - a;
93
94 switch (blendMode) {
95 case SDL_BLENDMODE_BLEND:
96 FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888);
97 break;
98 case SDL_BLENDMODE_ADD:
99 FILLRECT(Uint32, DRAW_SETPIXEL_ADD_ARGB8888);
100 break;
101 default:
102 FILLRECT(Uint32, DRAW_SETPIXEL_ARGB8888);
103 break;
104 }
105 return 0;
106 }
107
108 static int
109 SDL_BlendFillRect_RGB(SDL_Surface * dst, const SDL_Rect * rect,
110 SDL_BlendMode blendMode, Uint8 r, 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 FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB);
120 break;
121 case SDL_BLENDMODE_ADD:
122 FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB);
123 break;
124 default:
125 FILLRECT(Uint16, DRAW_SETPIXEL_RGB);
126 break;
127 }
128 return 0;
129 case 4:
130 switch (blendMode) {
131 case SDL_BLENDMODE_BLEND:
132 FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB);
133 break;
134 case SDL_BLENDMODE_ADD:
135 FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGB);
136 break;
137 default:
138 FILLRECT(Uint32, DRAW_SETPIXEL_RGB);
139 break;
140 }
141 return 0;
142 default:
143 SDL_Unsupported();
144 return -1;
145 }
146 }
147
148 static int
149 SDL_BlendFillRect_RGBA(SDL_Surface * dst, const SDL_Rect * rect,
150 SDL_BlendMode blendMode, Uint8 r, 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 FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGBA);
160 break;
161 case SDL_BLENDMODE_ADD:
162 FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGBA);
163 break;
164 default:
165 FILLRECT(Uint32, DRAW_SETPIXEL_RGBA);
166 break;
167 }
168 return 0;
169 default:
170 SDL_Unsupported();
171 return -1;
172 }
173 }
174
175 int
176 SDL_BlendFillRect(SDL_Surface * dst, const SDL_Rect * rect,
177 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
178 {
179 SDL_Rect clipped;
180
181 if (!dst) {
182 SDL_SetError("Passed NULL destination surface");
183 return -1;
184 }
185
186 /* This function doesn't work on surfaces < 8 bpp */
187 if (dst->format->BitsPerPixel < 8) {
188 SDL_SetError("SDL_BlendFillRect(): Unsupported surface format");
189 return -1;
190 }
191
192 /* If 'rect' == NULL, then fill the whole surface */
193 if (rect) {
194 /* Perform clipping */
195 if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
196 return 0;
197 }
198 rect = &clipped;
199 } else {
200 rect = &dst->clip_rect;
201 }
202
203 if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
204 r = DRAW_MUL(r, a);
205 g = DRAW_MUL(g, a);
206 b = DRAW_MUL(b, a);
207 }
208
209 switch (dst->format->BitsPerPixel) {
210 case 15:
211 switch (dst->format->Rmask) {
212 case 0x7C00:
213 return SDL_BlendFillRect_RGB555(dst, rect, blendMode, r, g, b, a);
214 }
215 break;
216 case 16:
217 switch (dst->format->Rmask) {
218 case 0xF800:
219 return SDL_BlendFillRect_RGB565(dst, rect, blendMode, r, g, b, a);
220 }
221 break;
222 case 32:
223 switch (dst->format->Rmask) {
224 case 0x00FF0000:
225 if (!dst->format->Amask) {
226 return SDL_BlendFillRect_RGB888(dst, rect, blendMode, r, g, b, a);
227 } else {
228 return SDL_BlendFillRect_ARGB8888(dst, rect, blendMode, r, g, b, a);
229 }
230 break;
231 }
232 break;
233 default:
234 break;
235 }
236
237 if (!dst->format->Amask) {
238 return SDL_BlendFillRect_RGB(dst, rect, blendMode, r, g, b, a);
239 } else {
240 return SDL_BlendFillRect_RGBA(dst, rect, blendMode, r, g, b, a);
241 }
242 }
243
244 int
245 SDL_BlendFillRects(SDL_Surface * dst, const SDL_Rect ** rects, int count,
246 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
247 {
248 SDL_Rect clipped;
249 int i;
250 int (*func)(SDL_Surface * dst, const SDL_Rect * rect,
251 SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a) = NULL;
252 int status = 0;
253
254 if (!dst) {
255 SDL_SetError("Passed NULL destination surface");
256 return -1;
257 }
258
259 /* This function doesn't work on surfaces < 8 bpp */
260 if (dst->format->BitsPerPixel < 8) {
261 SDL_SetError("SDL_BlendFillRects(): Unsupported surface format");
262 return -1;
263 }
264
265 if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
266 r = DRAW_MUL(r, a);
267 g = DRAW_MUL(g, a);
268 b = DRAW_MUL(b, a);
269 }
270
271 /* FIXME: Does this function pointer slow things down significantly? */
272 switch (dst->format->BitsPerPixel) {
273 case 15:
274 switch (dst->format->Rmask) {
275 case 0x7C00:
276 func = SDL_BlendFillRect_RGB555;
277 }
278 break;
279 case 16:
280 switch (dst->format->Rmask) {
281 case 0xF800:
282 func = SDL_BlendFillRect_RGB565;
283 }
284 break;
285 case 32:
286 switch (dst->format->Rmask) {
287 case 0x00FF0000:
288 if (!dst->format->Amask) {
289 func = SDL_BlendFillRect_RGB888;
290 } else {
291 func = SDL_BlendFillRect_ARGB8888;
292 }
293 break;
294 }
295 break;
296 default:
297 break;
298 }
299
300 if (!func) {
301 if (!dst->format->Amask) {
302 func = SDL_BlendFillRect_RGB;
303 } else {
304 func = SDL_BlendFillRect_RGBA;
305 }
306 }
307
308 for (i = 0; i < count; ++i) {
309 const SDL_Rect * rect = rects[i];
310
311 /* If 'rect' == NULL, then fill the whole surface */
312 if (rect) {
313 /* Perform clipping */
314 if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
315 continue;
316 }
317 rect = &clipped;
318 } else {
319 rect = &dst->clip_rect;
320 }
321
322 status = func(dst, rect, blendMode, r, g, b, a);
323 }
324 return status;
325 }
326
327 /* vi: set ts=4 sw=4 expandtab: */