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