Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
authorSam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 16:45:23 -0800
changeset 537516877f74123c
parent 5374 dacb4f5e0dd3
child 5376 183ec2d4485c
Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
include/SDL_surface.h
src/video/SDL_surface.c
src/video/SDL_video.c
src/video/cocoa/SDL_cocoavideo.h
src/video/cocoa/SDL_cocoavideo.m
src/video/cocoa/SDL_cocoawindow.h
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/include/SDL_surface.h	Mon Feb 21 15:25:49 2011 -0800
     1.2 +++ b/include/SDL_surface.h	Mon Feb 21 16:45:23 2011 -0800
     1.3 @@ -351,6 +351,8 @@
     1.4   */
     1.5  extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
     1.6      (SDL_Surface * src, SDL_PixelFormat * fmt, Uint32 flags);
     1.7 +extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat
     1.8 +    (SDL_Surface * src, Uint32 pixel_format, Uint32 flags);
     1.9  
    1.10  /**
    1.11   * \brief Copy a block of pixels of one format to another format
     2.1 --- a/src/video/SDL_surface.c	Mon Feb 21 15:25:49 2011 -0800
     2.2 +++ b/src/video/SDL_surface.c	Mon Feb 21 16:45:23 2011 -0800
     2.3 @@ -814,6 +814,21 @@
     2.4      return (convert);
     2.5  }
     2.6  
     2.7 +SDL_Surface *
     2.8 +SDL_ConvertSurfaceFormat(SDL_Surface * surface, Uint32 pixel_format,
     2.9 +                         Uint32 flags)
    2.10 +{
    2.11 +    SDL_PixelFormat *fmt;
    2.12 +    SDL_Surface *convert;
    2.13 +
    2.14 +    fmt = SDL_AllocFormat(pixel_format);
    2.15 +    if (fmt) {
    2.16 +        convert = SDL_ConvertSurface(surface, fmt, flags);
    2.17 +        SDL_FreeFormat(fmt);
    2.18 +    }
    2.19 +    return convert;
    2.20 +}
    2.21 +
    2.22  /*
    2.23   * Create a surface on the stack for quick blit operations
    2.24   */
     3.1 --- a/src/video/SDL_video.c	Mon Feb 21 15:25:49 2011 -0800
     3.2 +++ b/src/video/SDL_video.c	Mon Feb 21 16:45:23 2011 -0800
     3.3 @@ -1300,6 +1300,10 @@
     3.4  {
     3.5      CHECK_WINDOW_MAGIC(window, );
     3.6  
     3.7 +    if (!icon) {
     3.8 +        return;
     3.9 +    }
    3.10 +
    3.11      if (_this->SetWindowIcon) {
    3.12          _this->SetWindowIcon(_this, window, icon);
    3.13      }
     4.1 --- a/src/video/cocoa/SDL_cocoavideo.h	Mon Feb 21 15:25:49 2011 -0800
     4.2 +++ b/src/video/cocoa/SDL_cocoavideo.h	Mon Feb 21 16:45:23 2011 -0800
     4.3 @@ -64,6 +64,9 @@
     4.4      Uint32 screensaver_activity;
     4.5  } SDL_VideoData;
     4.6  
     4.7 +/* Utility functions */
     4.8 +NSImage * Cocoa_CreateImage(SDL_Surface * surface);
     4.9 +
    4.10  #endif /* _SDL_cocoavideo_h */
    4.11  
    4.12  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/video/cocoa/SDL_cocoavideo.m	Mon Feb 21 15:25:49 2011 -0800
     5.2 +++ b/src/video/cocoa/SDL_cocoavideo.m	Mon Feb 21 16:45:23 2011 -0800
     5.3 @@ -21,6 +21,7 @@
     5.4  */
     5.5  #include "SDL_config.h"
     5.6  
     5.7 +#include "SDL_endian.h"
     5.8  #include "SDL_cocoavideo.h"
     5.9  #include "SDL_cocoashape.h"
    5.10  #include "SDL_assert.h"
    5.11 @@ -82,6 +83,7 @@
    5.12      device->CreateWindow = Cocoa_CreateWindow;
    5.13      device->CreateWindowFrom = Cocoa_CreateWindowFrom;
    5.14      device->SetWindowTitle = Cocoa_SetWindowTitle;
    5.15 +    device->SetWindowIcon = Cocoa_SetWindowIcon;
    5.16      device->SetWindowPosition = Cocoa_SetWindowPosition;
    5.17      device->SetWindowSize = Cocoa_SetWindowSize;
    5.18      device->ShowWindow = Cocoa_ShowWindow;
    5.19 @@ -147,6 +149,62 @@
    5.20      Cocoa_QuitMouse(_this);
    5.21  }
    5.22  
    5.23 +/* This function assumes that it's called from within an autorelease pool */
    5.24 +NSImage *
    5.25 +Cocoa_CreateImage(SDL_Surface * surface)
    5.26 +{
    5.27 +    SDL_Surface *converted;
    5.28 +    NSBitmapImageRep *imgrep;
    5.29 +    Uint8 *pixels;
    5.30 +    int i;
    5.31 +    NSImage *img;
    5.32 +
    5.33 +    converted = SDL_ConvertSurfaceFormat(surface, 
    5.34 +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    5.35 +                                         SDL_PIXELFORMAT_RGBA8888,
    5.36 +#else
    5.37 +                                         SDL_PIXELFORMAT_ABGR8888,
    5.38 +#endif
    5.39 +                                         0);
    5.40 +    if (!converted) {
    5.41 +        return nil;
    5.42 +    }
    5.43 +
    5.44 +    imgrep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
    5.45 +                    pixelsWide: converted->w
    5.46 +                    pixelsHigh: converted->h
    5.47 +                    bitsPerSample: 8
    5.48 +                    samplesPerPixel: 4
    5.49 +                    hasAlpha: YES
    5.50 +                    isPlanar: NO
    5.51 +                    colorSpaceName: NSDeviceRGBColorSpace
    5.52 +                    bytesPerRow: converted->pitch
    5.53 +                    bitsPerPixel: converted->format->BitsPerPixel] autorelease];
    5.54 +    if (imgrep == nil) {
    5.55 +        SDL_FreeSurface(converted);
    5.56 +        return nil;
    5.57 +    }
    5.58 +
    5.59 +    /* Copy the pixels */
    5.60 +    pixels = [imgrep bitmapData];
    5.61 +    SDL_memcpy(pixels, converted->pixels, converted->h * converted->pitch);
    5.62 +    SDL_FreeSurface(converted);
    5.63 +
    5.64 +    /* Premultiply the alpha channel */
    5.65 +    for (i = (converted->h * converted->w); i--; ) {
    5.66 +        Uint8 alpha = pixels[3];
    5.67 +        pixels[0] = (Uint8)(((Uint16)pixels[0] * alpha) / 255);
    5.68 +        pixels[1] = (Uint8)(((Uint16)pixels[1] * alpha) / 255);
    5.69 +        pixels[2] = (Uint8)(((Uint16)pixels[2] * alpha) / 255);
    5.70 +        pixels += 4;
    5.71 +    }
    5.72 +
    5.73 +    img = [[[NSImage alloc] initWithSize: NSMakeSize(surface->w, surface->h)] autorelease];
    5.74 +    if (img != nil) {
    5.75 +        [img addRepresentation: imgrep];
    5.76 +    }
    5.77 +    return img;
    5.78 +}
    5.79  
    5.80  /*
    5.81   * Mac OS X assertion support.
     6.1 --- a/src/video/cocoa/SDL_cocoawindow.h	Mon Feb 21 15:25:49 2011 -0800
     6.2 +++ b/src/video/cocoa/SDL_cocoawindow.h	Mon Feb 21 16:45:23 2011 -0800
     6.3 @@ -96,6 +96,7 @@
     6.4  extern int Cocoa_CreateWindowFrom(_THIS, SDL_Window * window,
     6.5                                    const void *data);
     6.6  extern void Cocoa_SetWindowTitle(_THIS, SDL_Window * window);
     6.7 +extern void Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon);
     6.8  extern void Cocoa_SetWindowPosition(_THIS, SDL_Window * window);
     6.9  extern void Cocoa_SetWindowSize(_THIS, SDL_Window * window);
    6.10  extern void Cocoa_ShowWindow(_THIS, SDL_Window * window);
     7.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 15:25:49 2011 -0800
     7.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 16:45:23 2011 -0800
     7.3 @@ -637,6 +637,19 @@
     7.4  }
     7.5  
     7.6  void
     7.7 +Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
     7.8 +{
     7.9 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    7.10 +    NSImage *nsimage = Cocoa_CreateImage(icon);
    7.11 +
    7.12 +    if (nsimage) {
    7.13 +        [NSApp setApplicationIconImage:nsimage];
    7.14 +    }
    7.15 +
    7.16 +    [pool release];
    7.17 +}
    7.18 +
    7.19 +void
    7.20  Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
    7.21  {
    7.22      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];