Mercurial > sdl-ios-xcode
annotate src/video/SDL_surface.c @ 1312:c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 06:32:25 +0000 |
parents | 86d0d01290ea |
children | 450721ad5436 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1251
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1251
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1251
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1251
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1251
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1251
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1251
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
130
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 | |
27 #include "SDL_error.h" | |
28 #include "SDL_video.h" | |
29 #include "SDL_sysvideo.h" | |
30 #include "SDL_cursor_c.h" | |
31 #include "SDL_blit.h" | |
32 #include "SDL_RLEaccel_c.h" | |
33 #include "SDL_pixels_c.h" | |
34 #include "SDL_memops.h" | |
35 #include "SDL_leaks.h" | |
36 | |
1251
86d0d01290ea
Updated Windows CE/PocketPC support...adds GAPI driver, landscape mode,
Ryan C. Gordon <icculus@icculus.org>
parents:
1155
diff
changeset
|
37 |
0 | 38 /* Public routines */ |
39 /* | |
40 * Create an empty RGB surface of the appropriate depth | |
41 */ | |
42 SDL_Surface * SDL_CreateRGBSurface (Uint32 flags, | |
43 int width, int height, int depth, | |
44 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | |
45 { | |
46 SDL_VideoDevice *video = current_video; | |
47 SDL_VideoDevice *this = current_video; | |
48 SDL_Surface *screen; | |
49 SDL_Surface *surface; | |
50 | |
940
bb1588ebe47b
Date: Sat, 10 Jul 2004 21:02:33 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
915
diff
changeset
|
51 /* Make sure the size requested doesn't overflow our datatypes */ |
bb1588ebe47b
Date: Sat, 10 Jul 2004 21:02:33 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
915
diff
changeset
|
52 /* Next time I write a library like SDL, I'll use int for size. :) */ |
1017
c2f2370ac1e5
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
940
diff
changeset
|
53 if ( width >= 16384 || height >= 65536 ) { |
940
bb1588ebe47b
Date: Sat, 10 Jul 2004 21:02:33 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
915
diff
changeset
|
54 SDL_SetError("Width or height is too large"); |
bb1588ebe47b
Date: Sat, 10 Jul 2004 21:02:33 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
915
diff
changeset
|
55 return(NULL); |
bb1588ebe47b
Date: Sat, 10 Jul 2004 21:02:33 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
915
diff
changeset
|
56 } |
bb1588ebe47b
Date: Sat, 10 Jul 2004 21:02:33 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
915
diff
changeset
|
57 |
0 | 58 /* Check to see if we desire the surface in video memory */ |
59 if ( video ) { | |
60 screen = SDL_PublicSurface; | |
61 } else { | |
62 screen = NULL; | |
63 } | |
64 if ( screen && ((screen->flags&SDL_HWSURFACE) == SDL_HWSURFACE) ) { | |
65 if ( (flags&(SDL_SRCCOLORKEY|SDL_SRCALPHA)) != 0 ) { | |
66 flags |= SDL_HWSURFACE; | |
67 } | |
68 if ( (flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) { | |
69 if ( ! current_video->info.blit_hw_CC ) { | |
70 flags &= ~SDL_HWSURFACE; | |
71 } | |
72 } | |
73 if ( (flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { | |
74 if ( ! current_video->info.blit_hw_A ) { | |
75 flags &= ~SDL_HWSURFACE; | |
76 } | |
77 } | |
78 } else { | |
79 flags &= ~SDL_HWSURFACE; | |
80 } | |
81 | |
82 /* Allocate the surface */ | |
83 surface = (SDL_Surface *)malloc(sizeof(*surface)); | |
84 if ( surface == NULL ) { | |
85 SDL_OutOfMemory(); | |
86 return(NULL); | |
87 } | |
88 surface->flags = SDL_SWSURFACE; | |
89 if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { | |
1052
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
90 if ((Amask) && (video->displayformatalphapixel)) |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
91 { |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
92 depth = video->displayformatalphapixel->BitsPerPixel; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
93 Rmask = video->displayformatalphapixel->Rmask; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
94 Gmask = video->displayformatalphapixel->Gmask; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
95 Bmask = video->displayformatalphapixel->Bmask; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
96 Amask = video->displayformatalphapixel->Amask; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
97 } |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
98 else |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
99 { |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
100 depth = screen->format->BitsPerPixel; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
101 Rmask = screen->format->Rmask; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
102 Gmask = screen->format->Gmask; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
103 Bmask = screen->format->Bmask; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
104 Amask = screen->format->Amask; |
68f607298ca9
Some work on using accelerated alpha blits with hardware surfaces.
Ryan C. Gordon <icculus@icculus.org>
parents:
1017
diff
changeset
|
105 } |
0 | 106 } |
107 surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask); | |
108 if ( surface->format == NULL ) { | |
109 free(surface); | |
110 return(NULL); | |
111 } | |
112 if ( Amask ) { | |
113 surface->flags |= SDL_SRCALPHA; | |
114 } | |
115 surface->w = width; | |
116 surface->h = height; | |
117 surface->pitch = SDL_CalculatePitch(surface); | |
118 surface->pixels = NULL; | |
119 surface->offset = 0; | |
120 surface->hwdata = NULL; | |
121 surface->locked = 0; | |
122 surface->map = NULL; | |
441
598b25b9bffe
Zeroed out SDL_Surface::unused1 so glSDL will work on stock SDL
Sam Lantinga <slouken@libsdl.org>
parents:
431
diff
changeset
|
123 surface->unused1 = 0; |
0 | 124 SDL_SetClipRect(surface, NULL); |
845
333db1d87876
Fixed a bug in detecting surface mapping changes
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
125 SDL_FormatChanged(surface); |
0 | 126 |
127 /* Get the pixels */ | |
128 if ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) || | |
129 (video->AllocHWSurface(this, surface) < 0) ) { | |
130 if ( surface->w && surface->h ) { | |
131 surface->pixels = malloc(surface->h*surface->pitch); | |
132 if ( surface->pixels == NULL ) { | |
133 SDL_FreeSurface(surface); | |
134 SDL_OutOfMemory(); | |
135 return(NULL); | |
136 } | |
137 /* This is important for bitmaps */ | |
138 memset(surface->pixels, 0, surface->h*surface->pitch); | |
139 } | |
140 } | |
141 | |
142 /* Allocate an empty mapping */ | |
143 surface->map = SDL_AllocBlitMap(); | |
144 if ( surface->map == NULL ) { | |
145 SDL_FreeSurface(surface); | |
146 return(NULL); | |
147 } | |
148 | |
149 /* The surface is ready to go */ | |
150 surface->refcount = 1; | |
151 #ifdef CHECK_LEAKS | |
152 ++surfaces_allocated; | |
153 #endif | |
154 return(surface); | |
155 } | |
156 /* | |
157 * Create an RGB surface from an existing memory buffer | |
158 */ | |
159 SDL_Surface * SDL_CreateRGBSurfaceFrom (void *pixels, | |
160 int width, int height, int depth, int pitch, | |
161 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | |
162 { | |
163 SDL_Surface *surface; | |
164 | |
165 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, depth, | |
166 Rmask, Gmask, Bmask, Amask); | |
167 if ( surface != NULL ) { | |
168 surface->flags |= SDL_PREALLOC; | |
169 surface->pixels = pixels; | |
170 surface->w = width; | |
171 surface->h = height; | |
172 surface->pitch = pitch; | |
173 SDL_SetClipRect(surface, NULL); | |
174 } | |
175 return(surface); | |
176 } | |
177 /* | |
178 * Set the color key in a blittable surface | |
179 */ | |
180 int SDL_SetColorKey (SDL_Surface *surface, Uint32 flag, Uint32 key) | |
181 { | |
182 /* Sanity check the flag as it gets passed in */ | |
183 if ( flag & SDL_SRCCOLORKEY ) { | |
184 if ( flag & (SDL_RLEACCEL|SDL_RLEACCELOK) ) { | |
185 flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK); | |
186 } else { | |
187 flag = SDL_SRCCOLORKEY; | |
188 } | |
189 } else { | |
190 flag = 0; | |
191 } | |
192 | |
193 /* Optimize away operations that don't change anything */ | |
194 if ( (flag == (surface->flags & (SDL_SRCCOLORKEY|SDL_RLEACCELOK))) && | |
195 (key == surface->format->colorkey) ) { | |
196 return(0); | |
197 } | |
198 | |
199 /* UnRLE surfaces before we change the colorkey */ | |
200 if ( surface->flags & SDL_RLEACCEL ) { | |
201 SDL_UnRLESurface(surface, 1); | |
202 } | |
203 | |
204 if ( flag ) { | |
205 SDL_VideoDevice *video = current_video; | |
206 SDL_VideoDevice *this = current_video; | |
207 | |
208 | |
209 surface->flags |= SDL_SRCCOLORKEY; | |
210 surface->format->colorkey = key; | |
211 if ( (surface->flags & SDL_HWACCEL) == SDL_HWACCEL ) { | |
212 if ( (video->SetHWColorKey == NULL) || | |
213 (video->SetHWColorKey(this, surface, key) < 0) ) { | |
214 surface->flags &= ~SDL_HWACCEL; | |
215 } | |
216 } | |
217 if ( flag & SDL_RLEACCELOK ) { | |
218 surface->flags |= SDL_RLEACCELOK; | |
219 } else { | |
220 surface->flags &= ~SDL_RLEACCELOK; | |
221 } | |
222 } else { | |
223 surface->flags &= ~(SDL_SRCCOLORKEY|SDL_RLEACCELOK); | |
224 surface->format->colorkey = 0; | |
225 } | |
226 SDL_InvalidateMap(surface->map); | |
227 return(0); | |
228 } | |
431
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
229 /* This function sets the alpha channel of a surface */ |
0 | 230 int SDL_SetAlpha (SDL_Surface *surface, Uint32 flag, Uint8 value) |
231 { | |
232 Uint32 oldflags = surface->flags; | |
233 Uint32 oldalpha = surface->format->alpha; | |
234 | |
235 /* Sanity check the flag as it gets passed in */ | |
236 if ( flag & SDL_SRCALPHA ) { | |
237 if ( flag & (SDL_RLEACCEL|SDL_RLEACCELOK) ) { | |
238 flag = (SDL_SRCALPHA | SDL_RLEACCELOK); | |
239 } else { | |
240 flag = SDL_SRCALPHA; | |
241 } | |
242 } else { | |
243 flag = 0; | |
244 } | |
245 | |
246 /* Optimize away operations that don't change anything */ | |
247 if ( (flag == (surface->flags & (SDL_SRCALPHA|SDL_RLEACCELOK))) && | |
248 (!flag || value == oldalpha) ) { | |
249 return(0); | |
250 } | |
251 | |
252 if(!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL)) | |
253 SDL_UnRLESurface(surface, 1); | |
254 | |
255 if ( flag ) { | |
256 SDL_VideoDevice *video = current_video; | |
257 SDL_VideoDevice *this = current_video; | |
258 | |
259 surface->flags |= SDL_SRCALPHA; | |
260 surface->format->alpha = value; | |
261 if ( (surface->flags & SDL_HWACCEL) == SDL_HWACCEL ) { | |
262 if ( (video->SetHWAlpha == NULL) || | |
263 (video->SetHWAlpha(this, surface, value) < 0) ) { | |
264 surface->flags &= ~SDL_HWACCEL; | |
265 } | |
266 } | |
267 if ( flag & SDL_RLEACCELOK ) { | |
268 surface->flags |= SDL_RLEACCELOK; | |
269 } else { | |
270 surface->flags &= ~SDL_RLEACCELOK; | |
271 } | |
272 } else { | |
273 surface->flags &= ~SDL_SRCALPHA; | |
274 surface->format->alpha = SDL_ALPHA_OPAQUE; | |
275 } | |
276 /* | |
277 * The representation for software surfaces is independent of | |
278 * per-surface alpha, so no need to invalidate the blit mapping | |
279 * if just the alpha value was changed. (If either is 255, we still | |
280 * need to invalidate.) | |
281 */ | |
282 if((surface->flags & SDL_HWACCEL) == SDL_HWACCEL | |
283 || oldflags != surface->flags | |
284 || (((oldalpha + 1) ^ (value + 1)) & 0x100)) | |
285 SDL_InvalidateMap(surface->map); | |
286 return(0); | |
287 } | |
431
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
288 int SDL_SetAlphaChannel(SDL_Surface *surface, Uint8 value) |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
289 { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
290 int row, col; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
291 int offset; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
292 Uint8 *buf; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
293 |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
294 if ( (surface->format->Amask != 0xFF000000) && |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
295 (surface->format->Amask != 0x000000FF) ) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
296 SDL_SetError("Unsupported surface alpha mask format"); |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
297 return -1; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
298 } |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
299 |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
300 #if SDL_BYTEORDER == SDL_LIL_ENDIAN |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
301 if ( surface->format->Amask == 0xFF000000 ) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
302 offset = 3; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
303 } else { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
304 offset = 0; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
305 } |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
306 #else |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
307 if ( surface->format->Amask == 0xFF000000 ) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
308 offset = 0; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
309 } else { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
310 offset = 3; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
311 } |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
312 #endif /* Byte ordering */ |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
313 |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
314 /* Quickly set the alpha channel of an RGBA or ARGB surface */ |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
315 if ( SDL_MUSTLOCK(surface) ) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
316 if ( SDL_LockSurface(surface) < 0 ) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
317 return -1; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
318 } |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
319 } |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
320 row = surface->h; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
321 while (row--) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
322 col = surface->w; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
323 buf = (Uint8 *)surface->pixels + row * surface->pitch + offset; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
324 while(col--) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
325 *buf = value; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
326 buf += 4; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
327 } |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
328 } |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
329 if ( SDL_MUSTLOCK(surface) ) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
330 SDL_UnlockSurface(surface); |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
331 } |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
332 return 0; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
333 } |
0 | 334 |
335 /* | |
336 * A function to calculate the intersection of two rectangles: | |
337 * return true if the rectangles intersect, false otherwise | |
338 */ | |
339 static __inline__ | |
130
14af14ff7c19
The rectangle argument to SDL_SetClipRect is really const
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
340 SDL_bool SDL_IntersectRect(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *intersection) |
0 | 341 { |
342 int Amin, Amax, Bmin, Bmax; | |
343 | |
344 /* Horizontal intersection */ | |
345 Amin = A->x; | |
346 Amax = Amin + A->w; | |
347 Bmin = B->x; | |
348 Bmax = Bmin + B->w; | |
349 if(Bmin > Amin) | |
350 Amin = Bmin; | |
351 intersection->x = Amin; | |
352 if(Bmax < Amax) | |
353 Amax = Bmax; | |
354 intersection->w = Amax - Amin > 0 ? Amax - Amin : 0; | |
355 | |
356 /* Vertical intersection */ | |
357 Amin = A->y; | |
358 Amax = Amin + A->h; | |
359 Bmin = B->y; | |
360 Bmax = Bmin + B->h; | |
361 if(Bmin > Amin) | |
362 Amin = Bmin; | |
363 intersection->y = Amin; | |
364 if(Bmax < Amax) | |
365 Amax = Bmax; | |
366 intersection->h = Amax - Amin > 0 ? Amax - Amin : 0; | |
367 | |
368 return (intersection->w && intersection->h); | |
369 } | |
370 /* | |
371 * Set the clipping rectangle for a blittable surface | |
372 */ | |
130
14af14ff7c19
The rectangle argument to SDL_SetClipRect is really const
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
373 SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect) |
0 | 374 { |
375 SDL_Rect full_rect; | |
376 | |
377 /* Don't do anything if there's no surface to act on */ | |
378 if ( ! surface ) { | |
379 return SDL_FALSE; | |
380 } | |
381 | |
382 /* Set up the full surface rectangle */ | |
383 full_rect.x = 0; | |
384 full_rect.y = 0; | |
385 full_rect.w = surface->w; | |
386 full_rect.h = surface->h; | |
387 | |
388 /* Set the clipping rectangle */ | |
389 if ( ! rect ) { | |
390 surface->clip_rect = full_rect; | |
391 return 1; | |
392 } | |
393 return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect); | |
394 } | |
395 void SDL_GetClipRect(SDL_Surface *surface, SDL_Rect *rect) | |
396 { | |
397 if ( surface && rect ) { | |
398 *rect = surface->clip_rect; | |
399 } | |
400 } | |
401 /* | |
402 * Set up a blit between two surfaces -- split into three parts: | |
403 * The upper part, SDL_UpperBlit(), performs clipping and rectangle | |
404 * verification. The lower part is a pointer to a low level | |
405 * accelerated blitting function. | |
406 * | |
407 * These parts are separated out and each used internally by this | |
408 * library in the optimimum places. They are exported so that if | |
409 * you know exactly what you are doing, you can optimize your code | |
410 * by calling the one(s) you need. | |
411 */ | |
412 int SDL_LowerBlit (SDL_Surface *src, SDL_Rect *srcrect, | |
413 SDL_Surface *dst, SDL_Rect *dstrect) | |
414 { | |
415 SDL_blit do_blit; | |
462
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
416 SDL_Rect hw_srcrect; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
417 SDL_Rect hw_dstrect; |
0 | 418 |
419 /* Check to make sure the blit mapping is valid */ | |
420 if ( (src->map->dst != dst) || | |
421 (src->map->dst->format_version != src->map->format_version) ) { | |
422 if ( SDL_MapSurface(src, dst) < 0 ) { | |
423 return(-1); | |
424 } | |
425 } | |
426 | |
427 /* Figure out which blitter to use */ | |
428 if ( (src->flags & SDL_HWACCEL) == SDL_HWACCEL ) { | |
462
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
429 if ( src == SDL_VideoSurface ) { |
519
c7da0cd5ae5e
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
462
diff
changeset
|
430 hw_srcrect = *srcrect; |
462
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
431 hw_srcrect.x += current_video->offset_x; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
432 hw_srcrect.y += current_video->offset_y; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
433 srcrect = &hw_srcrect; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
434 } |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
435 if ( dst == SDL_VideoSurface ) { |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
436 hw_dstrect = *dstrect; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
437 hw_dstrect.x += current_video->offset_x; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
438 hw_dstrect.y += current_video->offset_y; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
439 dstrect = &hw_dstrect; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
440 } |
0 | 441 do_blit = src->map->hw_blit; |
442 } else { | |
443 do_blit = src->map->sw_blit; | |
444 } | |
445 return(do_blit(src, srcrect, dst, dstrect)); | |
446 } | |
447 | |
448 | |
449 int SDL_UpperBlit (SDL_Surface *src, SDL_Rect *srcrect, | |
450 SDL_Surface *dst, SDL_Rect *dstrect) | |
451 { | |
452 SDL_Rect fulldst; | |
453 int srcx, srcy, w, h; | |
454 | |
455 /* Make sure the surfaces aren't locked */ | |
456 if ( ! src || ! dst ) { | |
457 SDL_SetError("SDL_UpperBlit: passed a NULL surface"); | |
458 return(-1); | |
459 } | |
460 if ( src->locked || dst->locked ) { | |
461 SDL_SetError("Surfaces must not be locked during blit"); | |
462 return(-1); | |
463 } | |
464 | |
465 /* If the destination rectangle is NULL, use the entire dest surface */ | |
466 if ( dstrect == NULL ) { | |
467 fulldst.x = fulldst.y = 0; | |
468 dstrect = &fulldst; | |
469 } | |
470 | |
471 /* clip the source rectangle to the source surface */ | |
472 if(srcrect) { | |
473 int maxw, maxh; | |
474 | |
475 srcx = srcrect->x; | |
476 w = srcrect->w; | |
477 if(srcx < 0) { | |
478 w += srcx; | |
479 dstrect->x -= srcx; | |
480 srcx = 0; | |
481 } | |
482 maxw = src->w - srcx; | |
483 if(maxw < w) | |
484 w = maxw; | |
485 | |
486 srcy = srcrect->y; | |
487 h = srcrect->h; | |
488 if(srcy < 0) { | |
489 h += srcy; | |
490 dstrect->y -= srcy; | |
491 srcy = 0; | |
492 } | |
493 maxh = src->h - srcy; | |
494 if(maxh < h) | |
495 h = maxh; | |
496 | |
497 } else { | |
498 srcx = srcy = 0; | |
499 w = src->w; | |
500 h = src->h; | |
501 } | |
502 | |
503 /* clip the destination rectangle against the clip rectangle */ | |
504 { | |
505 SDL_Rect *clip = &dst->clip_rect; | |
506 int dx, dy; | |
507 | |
508 dx = clip->x - dstrect->x; | |
509 if(dx > 0) { | |
510 w -= dx; | |
511 dstrect->x += dx; | |
512 srcx += dx; | |
513 } | |
514 dx = dstrect->x + w - clip->x - clip->w; | |
515 if(dx > 0) | |
516 w -= dx; | |
517 | |
518 dy = clip->y - dstrect->y; | |
519 if(dy > 0) { | |
520 h -= dy; | |
521 dstrect->y += dy; | |
522 srcy += dy; | |
523 } | |
524 dy = dstrect->y + h - clip->y - clip->h; | |
525 if(dy > 0) | |
526 h -= dy; | |
527 } | |
528 | |
529 if(w > 0 && h > 0) { | |
530 SDL_Rect sr; | |
531 sr.x = srcx; | |
532 sr.y = srcy; | |
533 sr.w = dstrect->w = w; | |
534 sr.h = dstrect->h = h; | |
535 return SDL_LowerBlit(src, &sr, dst, dstrect); | |
536 } | |
537 dstrect->w = dstrect->h = 0; | |
538 return 0; | |
539 } | |
540 | |
532
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
541 static int SDL_FillRect1(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color) |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
542 { |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
543 /* FIXME: We have to worry about packing order.. *sigh* */ |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
544 SDL_SetError("1-bpp rect fill not yet implemented"); |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
545 return -1; |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
546 } |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
547 |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
548 static int SDL_FillRect4(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color) |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
549 { |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
550 /* FIXME: We have to worry about packing order.. *sigh* */ |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
551 SDL_SetError("4-bpp rect fill not yet implemented"); |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
552 return -1; |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
553 } |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
554 |
0 | 555 /* |
556 * This function performs a fast fill of the given rectangle with 'color' | |
557 */ | |
558 int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color) | |
559 { | |
560 SDL_VideoDevice *video = current_video; | |
561 SDL_VideoDevice *this = current_video; | |
562 int x, y; | |
563 Uint8 *row; | |
564 | |
532
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
565 /* This function doesn't work on surfaces < 8 bpp */ |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
566 if ( dst->format->BitsPerPixel < 8 ) { |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
567 switch(dst->format->BitsPerPixel) { |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
568 case 1: |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
569 return SDL_FillRect1(dst, dstrect, color); |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
570 break; |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
571 case 4: |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
572 return SDL_FillRect4(dst, dstrect, color); |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
573 break; |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
574 default: |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
575 SDL_SetError("Fill rect on unsupported surface format"); |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
576 return(-1); |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
577 break; |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
578 } |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
579 } |
058d47b7a3b4
Return an error with color fills on less than 8 bpp surfaces.
Sam Lantinga <slouken@libsdl.org>
parents:
526
diff
changeset
|
580 |
0 | 581 /* If 'dstrect' == NULL, then fill the whole surface */ |
582 if ( dstrect ) { | |
583 /* Perform clipping */ | |
584 if ( !SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect) ) { | |
585 return(0); | |
586 } | |
587 } else { | |
588 dstrect = &dst->clip_rect; | |
589 } | |
590 | |
591 /* Check for hardware acceleration */ | |
592 if ( ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) && | |
593 video->info.blit_fill ) { | |
462
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
594 SDL_Rect hw_rect; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
595 if ( dst == SDL_VideoSurface ) { |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
596 hw_rect = *dstrect; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
597 hw_rect.x += current_video->offset_x; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
598 hw_rect.y += current_video->offset_y; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
599 dstrect = &hw_rect; |
1be0cdaf8092
Fixed offset bug in hardware accelerated fills and blits
Sam Lantinga <slouken@libsdl.org>
parents:
441
diff
changeset
|
600 } |
0 | 601 return(video->FillHWRect(this, dst, dstrect, color)); |
602 } | |
603 | |
604 /* Perform software fill */ | |
605 if ( SDL_LockSurface(dst) != 0 ) { | |
606 return(-1); | |
607 } | |
608 row = (Uint8 *)dst->pixels+dstrect->y*dst->pitch+ | |
609 dstrect->x*dst->format->BytesPerPixel; | |
610 if ( dst->format->palette || (color == 0) ) { | |
611 x = dstrect->w*dst->format->BytesPerPixel; | |
612 if ( !color && !((long)row&3) && !(x&3) && !(dst->pitch&3) ) { | |
613 int n = x >> 2; | |
614 for ( y=dstrect->h; y; --y ) { | |
615 SDL_memset4(row, 0, n); | |
616 row += dst->pitch; | |
617 } | |
618 } else { | |
619 #ifdef __powerpc__ | |
620 /* | |
621 * memset() on PPC (both glibc and codewarrior) uses | |
622 * the dcbz (Data Cache Block Zero) instruction, which | |
623 * causes an alignment exception if the destination is | |
624 * uncachable, so only use it on software surfaces | |
625 */ | |
626 if((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { | |
627 if(dstrect->w >= 8) { | |
628 /* | |
629 * 64-bit stores are probably most | |
630 * efficient to uncached video memory | |
631 */ | |
632 double fill; | |
633 memset(&fill, color, (sizeof fill)); | |
634 for(y = dstrect->h; y; y--) { | |
635 Uint8 *d = row; | |
636 unsigned n = x; | |
637 unsigned nn; | |
638 Uint8 c = color; | |
639 double f = fill; | |
640 while((unsigned long)d | |
641 & (sizeof(double) - 1)) { | |
642 *d++ = c; | |
643 n--; | |
644 } | |
645 nn = n / (sizeof(double) * 4); | |
646 while(nn) { | |
647 ((double *)d)[0] = f; | |
648 ((double *)d)[1] = f; | |
649 ((double *)d)[2] = f; | |
650 ((double *)d)[3] = f; | |
651 d += 4*sizeof(double); | |
652 nn--; | |
653 } | |
654 n &= ~(sizeof(double) * 4 - 1); | |
655 nn = n / sizeof(double); | |
656 while(nn) { | |
657 *(double *)d = f; | |
658 d += sizeof(double); | |
659 nn--; | |
660 } | |
661 n &= ~(sizeof(double) - 1); | |
662 while(n) { | |
663 *d++ = c; | |
664 n--; | |
665 } | |
666 row += dst->pitch; | |
667 } | |
668 } else { | |
669 /* narrow boxes */ | |
670 for(y = dstrect->h; y; y--) { | |
671 Uint8 *d = row; | |
672 Uint8 c = color; | |
673 int n = x; | |
674 while(n) { | |
675 *d++ = c; | |
676 n--; | |
677 } | |
678 row += dst->pitch; | |
679 } | |
680 } | |
681 } else | |
682 #endif /* __powerpc__ */ | |
683 { | |
684 for(y = dstrect->h; y; y--) { | |
685 memset(row, color, x); | |
686 row += dst->pitch; | |
687 } | |
688 } | |
689 } | |
690 } else { | |
691 switch (dst->format->BytesPerPixel) { | |
692 case 2: | |
693 for ( y=dstrect->h; y; --y ) { | |
694 Uint16 *pixels = (Uint16 *)row; | |
695 Uint16 c = color; | |
696 Uint32 cc = (Uint32)c << 16 | c; | |
697 int n = dstrect->w; | |
698 if((unsigned long)pixels & 3) { | |
699 *pixels++ = c; | |
700 n--; | |
701 } | |
702 if(n >> 1) | |
703 SDL_memset4(pixels, cc, n >> 1); | |
704 if(n & 1) | |
705 pixels[n - 1] = c; | |
706 row += dst->pitch; | |
707 } | |
708 break; | |
709 | |
710 case 3: | |
1155
91569ec25acd
Fixed some compiler warnings about "unreachable code" on Watcom C.
Ryan C. Gordon <icculus@icculus.org>
parents:
1052
diff
changeset
|
711 #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
0 | 712 color <<= 8; |
1155
91569ec25acd
Fixed some compiler warnings about "unreachable code" on Watcom C.
Ryan C. Gordon <icculus@icculus.org>
parents:
1052
diff
changeset
|
713 #endif |
0 | 714 for ( y=dstrect->h; y; --y ) { |
715 Uint8 *pixels = row; | |
716 for ( x=dstrect->w; x; --x ) { | |
717 memcpy(pixels, &color, 3); | |
718 pixels += 3; | |
719 } | |
720 row += dst->pitch; | |
721 } | |
722 break; | |
723 | |
724 case 4: | |
725 for(y = dstrect->h; y; --y) { | |
726 SDL_memset4(row, color, dstrect->w); | |
727 row += dst->pitch; | |
728 } | |
729 break; | |
730 } | |
731 } | |
732 SDL_UnlockSurface(dst); | |
733 | |
734 /* We're done! */ | |
735 return(0); | |
736 } | |
737 | |
738 /* | |
739 * Lock a surface to directly access the pixels | |
740 */ | |
741 int SDL_LockSurface (SDL_Surface *surface) | |
742 { | |
743 if ( ! surface->locked ) { | |
744 /* Perform the lock */ | |
745 if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) { | |
746 SDL_VideoDevice *video = current_video; | |
747 SDL_VideoDevice *this = current_video; | |
748 if ( video->LockHWSurface(this, surface) < 0 ) { | |
749 return(-1); | |
750 } | |
751 } | |
752 if ( surface->flags & SDL_RLEACCEL ) { | |
753 SDL_UnRLESurface(surface, 1); | |
754 surface->flags |= SDL_RLEACCEL; /* save accel'd state */ | |
755 } | |
756 /* This needs to be done here in case pixels changes value */ | |
757 surface->pixels = (Uint8 *)surface->pixels + surface->offset; | |
758 } | |
759 | |
760 /* Increment the surface lock count, for recursive locks */ | |
761 ++surface->locked; | |
762 | |
763 /* Ready to go.. */ | |
764 return(0); | |
765 } | |
766 /* | |
767 * Unlock a previously locked surface | |
768 */ | |
769 void SDL_UnlockSurface (SDL_Surface *surface) | |
770 { | |
771 /* Only perform an unlock if we are locked */ | |
772 if ( ! surface->locked || (--surface->locked > 0) ) { | |
773 return; | |
774 } | |
775 | |
776 /* Perform the unlock */ | |
777 surface->pixels = (Uint8 *)surface->pixels - surface->offset; | |
778 | |
779 /* Unlock hardware or accelerated surfaces */ | |
780 if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) { | |
781 SDL_VideoDevice *video = current_video; | |
782 SDL_VideoDevice *this = current_video; | |
783 video->UnlockHWSurface(this, surface); | |
784 } else { | |
785 /* Update RLE encoded surface with new data */ | |
786 if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { | |
787 surface->flags &= ~SDL_RLEACCEL; /* stop lying */ | |
788 SDL_RLESurface(surface); | |
789 } | |
790 } | |
791 } | |
792 | |
793 /* | |
794 * Convert a surface into the specified pixel format. | |
795 */ | |
796 SDL_Surface * SDL_ConvertSurface (SDL_Surface *surface, | |
797 SDL_PixelFormat *format, Uint32 flags) | |
798 { | |
799 SDL_Surface *convert; | |
800 Uint32 colorkey = 0; | |
801 Uint8 alpha = 0; | |
802 Uint32 surface_flags; | |
803 SDL_Rect bounds; | |
804 | |
805 /* Check for empty destination palette! (results in empty image) */ | |
806 if ( format->palette != NULL ) { | |
807 int i; | |
808 for ( i=0; i<format->palette->ncolors; ++i ) { | |
809 if ( (format->palette->colors[i].r != 0) || | |
810 (format->palette->colors[i].g != 0) || | |
811 (format->palette->colors[i].b != 0) ) | |
812 break; | |
813 } | |
814 if ( i == format->palette->ncolors ) { | |
815 SDL_SetError("Empty destination palette"); | |
816 return(NULL); | |
817 } | |
818 } | |
819 | |
264
c9cd3b564e4b
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
820 /* Only create hw surfaces with alpha channel if hw alpha blits |
c9cd3b564e4b
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
821 are supported */ |
c9cd3b564e4b
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
822 if(format->Amask != 0 && (flags & SDL_HWSURFACE)) { |
c9cd3b564e4b
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
823 const SDL_VideoInfo *vi = SDL_GetVideoInfo(); |
c9cd3b564e4b
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
824 if(!vi || !vi->blit_hw_A) |
c9cd3b564e4b
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
825 flags &= ~SDL_HWSURFACE; |
c9cd3b564e4b
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
826 } |
c9cd3b564e4b
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
827 |
0 | 828 /* Create a new surface with the desired format */ |
829 convert = SDL_CreateRGBSurface(flags, | |
830 surface->w, surface->h, format->BitsPerPixel, | |
831 format->Rmask, format->Gmask, format->Bmask, format->Amask); | |
832 if ( convert == NULL ) { | |
833 return(NULL); | |
834 } | |
835 | |
836 /* Copy the palette if any */ | |
837 if ( format->palette && convert->format->palette ) { | |
838 memcpy(convert->format->palette->colors, | |
839 format->palette->colors, | |
840 format->palette->ncolors*sizeof(SDL_Color)); | |
841 convert->format->palette->ncolors = format->palette->ncolors; | |
842 } | |
843 | |
844 /* Save the original surface color key and alpha */ | |
845 surface_flags = surface->flags; | |
846 if ( (surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) { | |
847 /* Convert colourkeyed surfaces to RGBA if requested */ | |
848 if((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY | |
849 && format->Amask) { | |
850 surface_flags &= ~SDL_SRCCOLORKEY; | |
851 } else { | |
852 colorkey = surface->format->colorkey; | |
853 SDL_SetColorKey(surface, 0, 0); | |
854 } | |
855 } | |
856 if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { | |
431
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
857 /* Copy over the alpha channel to RGBA if requested */ |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
858 if ( format->Amask ) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
859 surface->flags &= ~SDL_SRCALPHA; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
860 } else { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
861 alpha = surface->format->alpha; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
862 SDL_SetAlpha(surface, 0, 0); |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
863 } |
0 | 864 } |
865 | |
866 /* Copy over the image data */ | |
867 bounds.x = 0; | |
868 bounds.y = 0; | |
869 bounds.w = surface->w; | |
870 bounds.h = surface->h; | |
871 SDL_LowerBlit(surface, &bounds, convert, &bounds); | |
872 | |
873 /* Clean up the original surface, and update converted surface */ | |
874 if ( convert != NULL ) { | |
875 SDL_SetClipRect(convert, &surface->clip_rect); | |
876 } | |
877 if ( (surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) { | |
878 Uint32 cflags = surface_flags&(SDL_SRCCOLORKEY|SDL_RLEACCELOK); | |
879 if ( convert != NULL ) { | |
880 Uint8 keyR, keyG, keyB; | |
881 | |
882 SDL_GetRGB(colorkey,surface->format,&keyR,&keyG,&keyB); | |
883 SDL_SetColorKey(convert, cflags|(flags&SDL_RLEACCELOK), | |
884 SDL_MapRGB(convert->format, keyR, keyG, keyB)); | |
885 } | |
886 SDL_SetColorKey(surface, cflags, colorkey); | |
887 } | |
888 if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { | |
889 Uint32 aflags = surface_flags&(SDL_SRCALPHA|SDL_RLEACCELOK); | |
890 if ( convert != NULL ) { | |
891 SDL_SetAlpha(convert, aflags|(flags&SDL_RLEACCELOK), | |
892 alpha); | |
893 } | |
431
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
894 if ( format->Amask ) { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
895 surface->flags |= SDL_SRCALPHA; |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
896 } else { |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
897 SDL_SetAlpha(surface, aflags, alpha); |
41cadcba32e8
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents:
422
diff
changeset
|
898 } |
0 | 899 } |
900 | |
901 /* We're ready to go! */ | |
902 return(convert); | |
903 } | |
904 | |
905 /* | |
906 * Free a surface created by the above function. | |
907 */ | |
908 void SDL_FreeSurface (SDL_Surface *surface) | |
909 { | |
910 /* Free anything that's not NULL, and not the screen surface */ | |
911 if ((surface == NULL) || | |
912 (current_video && | |
913 ((surface == SDL_ShadowSurface)||(surface == SDL_VideoSurface)))) { | |
914 return; | |
915 } | |
916 if ( --surface->refcount > 0 ) { | |
917 return; | |
918 } | |
915
01cddd0f2efb
You can't free locked surfaces!
Sam Lantinga <slouken@libsdl.org>
parents:
845
diff
changeset
|
919 while ( surface->locked > 0 ) { |
01cddd0f2efb
You can't free locked surfaces!
Sam Lantinga <slouken@libsdl.org>
parents:
845
diff
changeset
|
920 SDL_UnlockSurface(surface); |
01cddd0f2efb
You can't free locked surfaces!
Sam Lantinga <slouken@libsdl.org>
parents:
845
diff
changeset
|
921 } |
0 | 922 if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { |
923 SDL_UnRLESurface(surface, 0); | |
924 } | |
925 if ( surface->format ) { | |
926 SDL_FreeFormat(surface->format); | |
927 surface->format = NULL; | |
928 } | |
929 if ( surface->map != NULL ) { | |
930 SDL_FreeBlitMap(surface->map); | |
931 surface->map = NULL; | |
932 } | |
422
b1b9ee41be70
Memory leak fix for DirectX software surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
933 if ( surface->hwdata ) { |
0 | 934 SDL_VideoDevice *video = current_video; |
935 SDL_VideoDevice *this = current_video; | |
936 video->FreeHWSurface(this, surface); | |
937 } | |
938 if ( surface->pixels && | |
939 ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC) ) { | |
940 free(surface->pixels); | |
941 } | |
942 free(surface); | |
943 #ifdef CHECK_LEAKS | |
944 --surfaces_allocated; | |
945 #endif | |
946 } |