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