This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_blendfillrect.c
348 lines (313 loc) · 9.4 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2010 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
24
#include "SDL_draw.h"
25
26
#include "SDL_blendfillrect.h"
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
33
34
35
unsigned inva = 0xff - a;
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
36
FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB555);
37
38
break;
case SDL_BLENDMODE_ADD:
39
FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB555);
40
break;
41
42
43
case SDL_BLENDMODE_MOD:
FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB555);
break;
44
default:
45
FILLRECT(Uint16, DRAW_SETPIXEL_RGB555);
46
break;
47
}
48
49
return 0;
}
50
51
static int
52
SDL_BlendFillRect_RGB565(SDL_Surface * dst, const SDL_Rect * rect,
53
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
54
55
56
57
58
{
unsigned inva = 0xff - a;
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
59
FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB565);
60
61
break;
case SDL_BLENDMODE_ADD:
62
FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB565);
63
break;
64
65
66
case SDL_BLENDMODE_MOD:
FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB565);
break;
67
default:
68
FILLRECT(Uint16, DRAW_SETPIXEL_RGB565);
69
break;
70
}
71
72
return 0;
}
73
74
static int
75
SDL_BlendFillRect_RGB888(SDL_Surface * dst, const SDL_Rect * rect,
76
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
77
78
79
80
81
{
unsigned inva = 0xff - a;
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
82
FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB888);
83
84
break;
case SDL_BLENDMODE_ADD:
85
FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGB888);
86
break;
87
88
89
case SDL_BLENDMODE_MOD:
FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGB888);
break;
90
default:
91
FILLRECT(Uint32, DRAW_SETPIXEL_RGB888);
92
break;
93
}
94
95
96
return 0;
}
97
static int
98
SDL_BlendFillRect_ARGB8888(SDL_Surface * dst, const SDL_Rect * rect,
99
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
100
101
102
103
104
{
unsigned inva = 0xff - a;
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
105
FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888);
106
107
break;
case SDL_BLENDMODE_ADD:
108
FILLRECT(Uint32, DRAW_SETPIXEL_ADD_ARGB8888);
109
break;
110
111
112
case SDL_BLENDMODE_MOD:
FILLRECT(Uint32, DRAW_SETPIXEL_MOD_ARGB8888);
break;
113
default:
114
FILLRECT(Uint32, DRAW_SETPIXEL_ARGB8888);
115
116
117
118
119
break;
}
return 0;
}
120
static int
121
SDL_BlendFillRect_RGB(SDL_Surface * dst, const SDL_Rect * rect,
122
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
123
124
125
126
127
128
{
SDL_PixelFormat *fmt = dst->format;
unsigned inva = 0xff - a;
switch (fmt->BytesPerPixel) {
case 2:
129
130
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
131
FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB);
132
133
break;
case SDL_BLENDMODE_ADD:
134
FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB);
135
break;
136
137
138
case SDL_BLENDMODE_MOD:
FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB);
break;
139
default:
140
FILLRECT(Uint16, DRAW_SETPIXEL_RGB);
141
142
break;
}
143
144
return 0;
case 4:
145
146
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
147
FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB);
148
149
break;
case SDL_BLENDMODE_ADD:
150
FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGB);
151
break;
152
153
154
case SDL_BLENDMODE_MOD:
FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGB);
break;
155
default:
156
FILLRECT(Uint32, DRAW_SETPIXEL_RGB);
157
158
break;
}
159
160
161
162
163
164
165
166
return 0;
default:
SDL_Unsupported();
return -1;
}
}
static int
167
SDL_BlendFillRect_RGBA(SDL_Surface * dst, const SDL_Rect * rect,
168
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
169
170
171
172
173
174
{
SDL_PixelFormat *fmt = dst->format;
unsigned inva = 0xff - a;
switch (fmt->BytesPerPixel) {
case 4:
175
176
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
177
FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGBA);
178
179
break;
case SDL_BLENDMODE_ADD:
180
FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGBA);
181
break;
182
183
184
case SDL_BLENDMODE_MOD:
FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGBA);
break;
185
default:
186
FILLRECT(Uint32, DRAW_SETPIXEL_RGBA);
187
188
break;
}
189
return 0;
190
191
192
193
default:
SDL_Unsupported();
return -1;
}
194
195
196
}
int
197
SDL_BlendFillRect(SDL_Surface * dst, const SDL_Rect * rect,
198
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
199
{
200
201
202
203
204
205
SDL_Rect clipped;
if (!dst) {
SDL_SetError("Passed NULL destination surface");
return -1;
}
206
207
/* This function doesn't work on surfaces < 8 bpp */
208
if (dst->format->BitsPerPixel < 8) {
209
SDL_SetError("SDL_BlendFillRect(): Unsupported surface format");
210
return -1;
211
212
}
213
214
/* If 'rect' == NULL, then fill the whole surface */
if (rect) {
215
/* Perform clipping */
216
217
if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
return 0;
218
}
219
rect = &clipped;
220
} else {
221
rect = &dst->clip_rect;
222
223
}
224
if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
225
226
227
228
229
r = DRAW_MUL(r, a);
g = DRAW_MUL(g, a);
b = DRAW_MUL(b, a);
}
230
switch (dst->format->BitsPerPixel) {
231
case 15:
232
switch (dst->format->Rmask) {
233
case 0x7C00:
234
return SDL_BlendFillRect_RGB555(dst, rect, blendMode, r, g, b, a);
235
236
237
}
break;
case 16:
238
switch (dst->format->Rmask) {
239
case 0xF800:
240
return SDL_BlendFillRect_RGB565(dst, rect, blendMode, r, g, b, a);
241
242
243
}
break;
case 32:
244
switch (dst->format->Rmask) {
245
case 0x00FF0000:
246
if (!dst->format->Amask) {
247
return SDL_BlendFillRect_RGB888(dst, rect, blendMode, r, g, b, a);
248
} else {
249
return SDL_BlendFillRect_ARGB8888(dst, rect, blendMode, r, g, b, a);
250
251
252
}
break;
}
253
break;
254
255
256
257
default:
break;
}
258
if (!dst->format->Amask) {
259
return SDL_BlendFillRect_RGB(dst, rect, blendMode, r, g, b, a);
260
} else {
261
return SDL_BlendFillRect_RGBA(dst, rect, blendMode, r, g, b, a);
262
263
264
265
}
}
int
266
SDL_BlendFillRects(SDL_Surface * dst, const SDL_Rect ** rects, int count,
267
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
268
269
270
271
{
SDL_Rect clipped;
int i;
int (*func)(SDL_Surface * dst, const SDL_Rect * rect,
272
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a) = NULL;
273
274
275
276
277
278
279
280
281
int status = 0;
if (!dst) {
SDL_SetError("Passed NULL destination surface");
return -1;
}
/* This function doesn't work on surfaces < 8 bpp */
if (dst->format->BitsPerPixel < 8) {
282
SDL_SetError("SDL_BlendFillRects(): Unsupported surface format");
283
284
285
286
287
288
289
290
291
292
293
294
295
296
return -1;
}
if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
r = DRAW_MUL(r, a);
g = DRAW_MUL(g, a);
b = DRAW_MUL(b, a);
}
/* FIXME: Does this function pointer slow things down significantly? */
switch (dst->format->BitsPerPixel) {
case 15:
switch (dst->format->Rmask) {
case 0x7C00:
297
func = SDL_BlendFillRect_RGB555;
298
299
300
301
302
}
break;
case 16:
switch (dst->format->Rmask) {
case 0xF800:
303
func = SDL_BlendFillRect_RGB565;
304
305
306
307
308
309
}
break;
case 32:
switch (dst->format->Rmask) {
case 0x00FF0000:
if (!dst->format->Amask) {
310
func = SDL_BlendFillRect_RGB888;
311
} else {
312
func = SDL_BlendFillRect_ARGB8888;
313
314
315
316
317
318
319
320
321
322
}
break;
}
break;
default:
break;
}
if (!func) {
if (!dst->format->Amask) {
323
func = SDL_BlendFillRect_RGB;
324
} else {
325
func = SDL_BlendFillRect_RGBA;
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
}
}
for (i = 0; i < count; ++i) {
const SDL_Rect * rect = rects[i];
/* If 'rect' == NULL, then fill the whole surface */
if (rect) {
/* Perform clipping */
if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
continue;
}
rect = &clipped;
} else {
rect = &dst->clip_rect;
}
status = func(dst, rect, blendMode, r, g, b, a);
344
}
345
return status;
346
347
348
}
/* vi: set ts=4 sw=4 expandtab: */