Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 22, 2011
1 parent 443630a commit f5c24e8
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/SDL_surface.h
Expand Up @@ -351,6 +351,8 @@ extern DECLSPEC void SDLCALL SDL_GetClipRect(SDL_Surface * surface,
*/
extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
(SDL_Surface * src, SDL_PixelFormat * fmt, Uint32 flags);
extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat
(SDL_Surface * src, Uint32 pixel_format, Uint32 flags);

/**
* \brief Copy a block of pixels of one format to another format
Expand Down
15 changes: 15 additions & 0 deletions src/video/SDL_surface.c
Expand Up @@ -814,6 +814,21 @@ SDL_ConvertSurface(SDL_Surface * surface, SDL_PixelFormat * format,
return (convert);
}

SDL_Surface *
SDL_ConvertSurfaceFormat(SDL_Surface * surface, Uint32 pixel_format,
Uint32 flags)
{
SDL_PixelFormat *fmt;
SDL_Surface *convert;

fmt = SDL_AllocFormat(pixel_format);
if (fmt) {
convert = SDL_ConvertSurface(surface, fmt, flags);
SDL_FreeFormat(fmt);
}
return convert;
}

/*
* Create a surface on the stack for quick blit operations
*/
Expand Down
4 changes: 4 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -1300,6 +1300,10 @@ SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
{
CHECK_WINDOW_MAGIC(window, );

if (!icon) {
return;
}

if (_this->SetWindowIcon) {
_this->SetWindowIcon(_this, window, icon);
}
Expand Down
3 changes: 3 additions & 0 deletions src/video/cocoa/SDL_cocoavideo.h
Expand Up @@ -64,6 +64,9 @@ typedef struct SDL_VideoData
Uint32 screensaver_activity;
} SDL_VideoData;

/* Utility functions */
NSImage * Cocoa_CreateImage(SDL_Surface * surface);

#endif /* _SDL_cocoavideo_h */

/* vi: set ts=4 sw=4 expandtab: */
58 changes: 58 additions & 0 deletions src/video/cocoa/SDL_cocoavideo.m
Expand Up @@ -21,6 +21,7 @@
*/
#include "SDL_config.h"

#include "SDL_endian.h"
#include "SDL_cocoavideo.h"
#include "SDL_cocoashape.h"
#include "SDL_assert.h"
Expand Down Expand Up @@ -82,6 +83,7 @@
device->CreateWindow = Cocoa_CreateWindow;
device->CreateWindowFrom = Cocoa_CreateWindowFrom;
device->SetWindowTitle = Cocoa_SetWindowTitle;
device->SetWindowIcon = Cocoa_SetWindowIcon;
device->SetWindowPosition = Cocoa_SetWindowPosition;
device->SetWindowSize = Cocoa_SetWindowSize;
device->ShowWindow = Cocoa_ShowWindow;
Expand Down Expand Up @@ -147,6 +149,62 @@
Cocoa_QuitMouse(_this);
}

/* This function assumes that it's called from within an autorelease pool */
NSImage *
Cocoa_CreateImage(SDL_Surface * surface)
{
SDL_Surface *converted;
NSBitmapImageRep *imgrep;
Uint8 *pixels;
int i;
NSImage *img;

converted = SDL_ConvertSurfaceFormat(surface,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
SDL_PIXELFORMAT_RGBA8888,
#else
SDL_PIXELFORMAT_ABGR8888,
#endif
0);
if (!converted) {
return nil;
}

imgrep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
pixelsWide: converted->w
pixelsHigh: converted->h
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bytesPerRow: converted->pitch
bitsPerPixel: converted->format->BitsPerPixel] autorelease];
if (imgrep == nil) {
SDL_FreeSurface(converted);
return nil;
}

/* Copy the pixels */
pixels = [imgrep bitmapData];
SDL_memcpy(pixels, converted->pixels, converted->h * converted->pitch);
SDL_FreeSurface(converted);

/* Premultiply the alpha channel */
for (i = (converted->h * converted->w); i--; ) {
Uint8 alpha = pixels[3];
pixels[0] = (Uint8)(((Uint16)pixels[0] * alpha) / 255);
pixels[1] = (Uint8)(((Uint16)pixels[1] * alpha) / 255);
pixels[2] = (Uint8)(((Uint16)pixels[2] * alpha) / 255);
pixels += 4;
}

img = [[[NSImage alloc] initWithSize: NSMakeSize(surface->w, surface->h)] autorelease];
if (img != nil) {
[img addRepresentation: imgrep];
}
return img;
}

/*
* Mac OS X assertion support.
Expand Down
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoawindow.h
Expand Up @@ -96,6 +96,7 @@ extern int Cocoa_CreateWindow(_THIS, SDL_Window * window);
extern int Cocoa_CreateWindowFrom(_THIS, SDL_Window * window,
const void *data);
extern void Cocoa_SetWindowTitle(_THIS, SDL_Window * window);
extern void Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon);
extern void Cocoa_SetWindowPosition(_THIS, SDL_Window * window);
extern void Cocoa_SetWindowSize(_THIS, SDL_Window * window);
extern void Cocoa_ShowWindow(_THIS, SDL_Window * window);
Expand Down
13 changes: 13 additions & 0 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -636,6 +636,19 @@ - (void)rightMouseDown:(NSEvent *)theEvent
[pool release];
}

void
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *nsimage = Cocoa_CreateImage(icon);

if (nsimage) {
[NSApp setApplicationIconImage:nsimage];
}

[pool release];
}

void
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
{
Expand Down

0 comments on commit f5c24e8

Please sign in to comment.