Mercurial > sdl-ios-xcode
comparison src/video/quartz/SDL_QuartzWM.m @ 1812:9c882e94b545
Fixed bug #208
So, here's a patch with a reimplementation of QZ_SetIcon() that does what I
described above. I apologize for the delay, I've been quite busy in the last
few days.
It appears to work here on 10.4.5 PPC in the limited testing that I've done;
I'll try to test it on 10.3.9 and 10.2.8 as well, but that might take another
week or so. Please test on i386.
Regarding alpha channels, per-surface alpha, and color keys, the same semantics
as for regular blits to an RGB surface should apply (for the final icon
composited onto the dock), unless I made a mistake - except in one pathological
case: if the icon surface has an alpha channel, its SDL_SRCALPHA flag is not
set (i.e. it has been explicitly cleared, since it's on by default for RGBA
surfaces), and it has a color key, plus an explicit mask was specified (instead
of the one autogenerated from the colorkey), then the color-keyed areas appear
black instead of transparent. I found no elegant way of fixing this, was too
lazy to implement the inelegant one, and decided that it isn't worth the effort
(but if someone disagrees, I can do it).
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 11 May 2006 03:45:55 +0000 |
parents | eed7a3f396ce |
children | 2780f547f5e7 |
comparison
equal
deleted
inserted
replaced
1811:e6de7e5fd451 | 1812:9c882e94b545 |
---|---|
260 void QZ_SetIcon (_THIS, SDL_Surface *icon, Uint8 *mask) | 260 void QZ_SetIcon (_THIS, SDL_Surface *icon, Uint8 *mask) |
261 { | 261 { |
262 NSBitmapImageRep *imgrep; | 262 NSBitmapImageRep *imgrep; |
263 NSImage *img; | 263 NSImage *img; |
264 SDL_Surface *mergedSurface; | 264 SDL_Surface *mergedSurface; |
265 int i,j; | |
266 NSAutoreleasePool *pool; | 265 NSAutoreleasePool *pool; |
267 SDL_Rect rrect; | 266 Uint8 *pixels; |
268 NSSize imgSize = {icon->w, icon->h}; | 267 SDL_bool iconSrcAlpha; |
268 Uint8 iconAlphaValue; | |
269 int i, j, maskPitch, index; | |
269 | 270 |
270 pool = [ [ NSAutoreleasePool alloc ] init ]; | 271 pool = [ [ NSAutoreleasePool alloc ] init ]; |
271 SDL_GetClipRect(icon, &rrect); | 272 |
272 | 273 imgrep = [ [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes: NULL pixelsWide: icon->w pixelsHigh: icon->h bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES isPlanar: NO colorSpaceName: NSDeviceRGBColorSpace bytesPerRow: 4*icon->w bitsPerPixel: 32 ] autorelease ]; |
273 /* create a big endian RGBA surface */ | 274 if (imgrep == nil) goto freePool; |
274 mergedSurface = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCALPHA, | 275 pixels = [ imgrep bitmapData ]; |
275 icon->w, icon->h, 32, 0xff<<24, 0xff<<16, 0xff<<8, 0xff<<0); | 276 SDL_memset(pixels, 0, 4*icon->w*icon->h); /* make the background, which will survive in colorkeyed areas, completely transparent */ |
276 if (mergedSurface==NULL) { | 277 |
277 NSLog(@"Error creating surface for merge"); | 278 #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
278 goto freePool; | 279 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
279 } | 280 #else |
280 | 281 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
281 if (mergedSurface->pitch != | 282 #endif |
282 mergedSurface->format->BytesPerPixel * mergedSurface->w) { | 283 mergedSurface = SDL_CreateRGBSurfaceFrom(pixels, icon->w, icon->h, 32, 4*icon->w, BYTEORDER_DEPENDENT_RGBA_MASKS); |
283 SDL_SetError ("merged surface has wrong format"); | 284 if (mergedSurface == NULL) goto freePool; |
284 SDL_FreeSurface (mergedSurface); | 285 |
285 goto freePool; | 286 /* blit, with temporarily cleared SRCALPHA flag because we want to copy, not alpha-blend */ |
286 } | 287 iconSrcAlpha = ((icon->flags & SDL_SRCALPHA) != 0); |
287 | 288 iconAlphaValue = icon->format->alpha; |
288 if (SDL_BlitSurface(icon,&rrect,mergedSurface,&rrect)) { | 289 SDL_SetAlpha(icon, 0, 255); |
289 NSLog(@"Error blitting to mergedSurface"); | 290 SDL_BlitSurface(icon, NULL, mergedSurface, NULL); |
290 goto freePool; | 291 if (iconSrcAlpha) SDL_SetAlpha(icon, SDL_SRCALPHA, iconAlphaValue); |
291 } | 292 |
292 | 293 SDL_FreeSurface(mergedSurface); |
293 if (mask) { | 294 |
294 | 295 /* apply mask, source alpha, and premultiply color values by alpha */ |
295 Uint32 *pixels = mergedSurface->pixels; | 296 maskPitch = (icon->w+7)/8; |
296 for (i = 0; i < mergedSurface->h; i++) { | 297 for (i = 0; i < icon->h; i++) { |
297 for (j = 0; j < mergedSurface->w; j++) { | 298 for (j = 0; j < icon->w; j++) { |
298 | 299 index = i*4*icon->w + j*4; |
299 int index = i * mergedSurface->w + j; | 300 if (!(mask[i*maskPitch + j/8] & (128 >> j%8))) { |
300 int mindex = index >> 3; | 301 pixels[index + 3] = 0; |
301 int bindex = 7 - (index & 0x7); | |
302 | |
303 if (mask[mindex] & (1 << bindex)) | |
304 pixels[index] |= 0x000000FF; | |
305 else | |
306 pixels[index] &= 0xFFFFFF00; | |
307 } | 302 } |
308 } | 303 else { |
309 } | 304 if (iconSrcAlpha) { |
310 | 305 if (icon->format->Amask == 0) pixels[index + 3] = icon->format->alpha; |
311 imgrep = [ [ NSBitmapImageRep alloc] | 306 } |
312 initWithBitmapDataPlanes:(unsigned char **)&mergedSurface->pixels | 307 else { |
313 pixelsWide:icon->w pixelsHigh:icon->h bitsPerSample:8 samplesPerPixel:4 | 308 pixels[index + 3] = 255; |
314 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace | 309 } |
315 bytesPerRow:icon->w<<2 bitsPerPixel:32 ]; | 310 } |
316 | 311 if (pixels[index + 3] < 255) { |
317 img = [ [ NSImage alloc ] initWithSize:imgSize ]; | 312 pixels[index + 0] = (Uint16)pixels[index + 0]*pixels[index + 3]/255; |
318 | 313 pixels[index + 1] = (Uint16)pixels[index + 1]*pixels[index + 3]/255; |
314 pixels[index + 2] = (Uint16)pixels[index + 2]*pixels[index + 3]/255; | |
315 } | |
316 } | |
317 } | |
318 | |
319 img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(icon->w, icon->h) ] autorelease ]; | |
320 if (img == nil) goto freePool; | |
319 [ img addRepresentation: imgrep ]; | 321 [ img addRepresentation: imgrep ]; |
320 [ NSApp setApplicationIconImage:img ]; | 322 [ NSApp setApplicationIconImage:img ]; |
321 | 323 |
322 [ img release ]; | |
323 [ imgrep release ]; | |
324 SDL_FreeSurface(mergedSurface); | |
325 freePool: | 324 freePool: |
326 [pool release]; | 325 [ pool release ]; |
327 } | 326 } |
328 | 327 |
329 int QZ_IconifyWindow (_THIS) { | 328 int QZ_IconifyWindow (_THIS) { |
330 | 329 |
331 if ( ! [ qz_window isMiniaturized ] ) { | 330 if ( ! [ qz_window isMiniaturized ] ) { |