src/video/cocoa/SDL_cocoavideo.m
changeset 5375 16877f74123c
parent 5262 b530ef003506
child 5466 006883d5fa51
equal deleted inserted replaced
5374:dacb4f5e0dd3 5375:16877f74123c
    19     Sam Lantinga
    19     Sam Lantinga
    20     slouken@libsdl.org
    20     slouken@libsdl.org
    21 */
    21 */
    22 #include "SDL_config.h"
    22 #include "SDL_config.h"
    23 
    23 
       
    24 #include "SDL_endian.h"
    24 #include "SDL_cocoavideo.h"
    25 #include "SDL_cocoavideo.h"
    25 #include "SDL_cocoashape.h"
    26 #include "SDL_cocoashape.h"
    26 #include "SDL_assert.h"
    27 #include "SDL_assert.h"
    27 
    28 
    28 /* Initialization/Query functions */
    29 /* Initialization/Query functions */
    80     device->PumpEvents = Cocoa_PumpEvents;
    81     device->PumpEvents = Cocoa_PumpEvents;
    81 
    82 
    82     device->CreateWindow = Cocoa_CreateWindow;
    83     device->CreateWindow = Cocoa_CreateWindow;
    83     device->CreateWindowFrom = Cocoa_CreateWindowFrom;
    84     device->CreateWindowFrom = Cocoa_CreateWindowFrom;
    84     device->SetWindowTitle = Cocoa_SetWindowTitle;
    85     device->SetWindowTitle = Cocoa_SetWindowTitle;
       
    86     device->SetWindowIcon = Cocoa_SetWindowIcon;
    85     device->SetWindowPosition = Cocoa_SetWindowPosition;
    87     device->SetWindowPosition = Cocoa_SetWindowPosition;
    86     device->SetWindowSize = Cocoa_SetWindowSize;
    88     device->SetWindowSize = Cocoa_SetWindowSize;
    87     device->ShowWindow = Cocoa_ShowWindow;
    89     device->ShowWindow = Cocoa_ShowWindow;
    88     device->HideWindow = Cocoa_HideWindow;
    90     device->HideWindow = Cocoa_HideWindow;
    89     device->RaiseWindow = Cocoa_RaiseWindow;
    91     device->RaiseWindow = Cocoa_RaiseWindow;
   145     Cocoa_QuitModes(_this);
   147     Cocoa_QuitModes(_this);
   146     Cocoa_QuitKeyboard(_this);
   148     Cocoa_QuitKeyboard(_this);
   147     Cocoa_QuitMouse(_this);
   149     Cocoa_QuitMouse(_this);
   148 }
   150 }
   149 
   151 
       
   152 /* This function assumes that it's called from within an autorelease pool */
       
   153 NSImage *
       
   154 Cocoa_CreateImage(SDL_Surface * surface)
       
   155 {
       
   156     SDL_Surface *converted;
       
   157     NSBitmapImageRep *imgrep;
       
   158     Uint8 *pixels;
       
   159     int i;
       
   160     NSImage *img;
       
   161 
       
   162     converted = SDL_ConvertSurfaceFormat(surface, 
       
   163 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
       
   164                                          SDL_PIXELFORMAT_RGBA8888,
       
   165 #else
       
   166                                          SDL_PIXELFORMAT_ABGR8888,
       
   167 #endif
       
   168                                          0);
       
   169     if (!converted) {
       
   170         return nil;
       
   171     }
       
   172 
       
   173     imgrep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
       
   174                     pixelsWide: converted->w
       
   175                     pixelsHigh: converted->h
       
   176                     bitsPerSample: 8
       
   177                     samplesPerPixel: 4
       
   178                     hasAlpha: YES
       
   179                     isPlanar: NO
       
   180                     colorSpaceName: NSDeviceRGBColorSpace
       
   181                     bytesPerRow: converted->pitch
       
   182                     bitsPerPixel: converted->format->BitsPerPixel] autorelease];
       
   183     if (imgrep == nil) {
       
   184         SDL_FreeSurface(converted);
       
   185         return nil;
       
   186     }
       
   187 
       
   188     /* Copy the pixels */
       
   189     pixels = [imgrep bitmapData];
       
   190     SDL_memcpy(pixels, converted->pixels, converted->h * converted->pitch);
       
   191     SDL_FreeSurface(converted);
       
   192 
       
   193     /* Premultiply the alpha channel */
       
   194     for (i = (converted->h * converted->w); i--; ) {
       
   195         Uint8 alpha = pixels[3];
       
   196         pixels[0] = (Uint8)(((Uint16)pixels[0] * alpha) / 255);
       
   197         pixels[1] = (Uint8)(((Uint16)pixels[1] * alpha) / 255);
       
   198         pixels[2] = (Uint8)(((Uint16)pixels[2] * alpha) / 255);
       
   199         pixels += 4;
       
   200     }
       
   201 
       
   202     img = [[[NSImage alloc] initWithSize: NSMakeSize(surface->w, surface->h)] autorelease];
       
   203     if (img != nil) {
       
   204         [img addRepresentation: imgrep];
       
   205     }
       
   206     return img;
       
   207 }
   150 
   208 
   151 /*
   209 /*
   152  * Mac OS X assertion support.
   210  * Mac OS X assertion support.
   153  *
   211  *
   154  * This doesn't really have aything to do with the interfaces of the SDL video
   212  * This doesn't really have aything to do with the interfaces of the SDL video