src/video/cocoa/SDL_cocoashape.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 11 Feb 2013 17:39:52 -0800
changeset 6848 478ecc8a58b3
parent 6832 156e608ec4ef
child 6885 700f1b25f77f
permissions -rw-r--r--
Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include "SDL_config.h"
    23 
    24 #if SDL_VIDEO_DRIVER_COCOA
    25 
    26 #include "SDL_cocoavideo.h"
    27 #include "SDL_shape.h"
    28 #include "SDL_cocoashape.h"
    29 #include "../SDL_sysvideo.h"
    30 #include "SDL_assert.h"
    31 
    32 SDL_WindowShaper*
    33 Cocoa_CreateShaper(SDL_Window* window) {
    34     SDL_WindowData* windata = (SDL_WindowData*)window->driverdata;
    35     [windata->nswindow setOpaque:NO];
    36 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
    37     [windata->nswindow setStyleMask:NSBorderlessWindowMask];
    38 #endif
    39     SDL_WindowShaper* result = result = malloc(sizeof(SDL_WindowShaper));
    40     result->window = window;
    41     result->mode.mode = ShapeModeDefault;
    42     result->mode.parameters.binarizationCutoff = 1;
    43     result->userx = result->usery = 0;
    44     window->shaper = result;
    45     
    46     SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData));
    47     result->driverdata = data;
    48     data->context = [windata->nswindow graphicsContext];
    49     data->saved = SDL_FALSE;
    50     data->shape = NULL;
    51     
    52     int resized_properly = Cocoa_ResizeWindowShape(window);
    53     SDL_assert(resized_properly == 0);
    54     return result;
    55 }
    56 
    57 typedef struct {
    58     NSView* view;
    59     NSBezierPath* path;
    60     SDL_Window* window;
    61 } SDL_CocoaClosure;
    62 
    63 void
    64 ConvertRects(SDL_ShapeTree* tree,void* closure) {
    65     SDL_CocoaClosure* data = (SDL_CocoaClosure*)closure;
    66     if(tree->kind == OpaqueShape) {
    67         NSRect rect = NSMakeRect(tree->data.shape.x,data->window->h - tree->data.shape.y,tree->data.shape.w,tree->data.shape.h);
    68         [data->path appendBezierPathWithRect:[data->view convertRect:rect toView:nil]];
    69     }
    70 }
    71 
    72 int
    73 Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
    74     SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
    75 	SDL_WindowData* windata = (SDL_WindowData*)shaper->window->driverdata;
    76 	SDL_CocoaClosure closure;
    77 	NSAutoreleasePool *pool = NULL;
    78     if(data->saved == SDL_TRUE) {
    79         [data->context restoreGraphicsState];
    80         data->saved = SDL_FALSE;
    81     }
    82         
    83     //[data->context saveGraphicsState];
    84     //data->saved = SDL_TRUE;
    85 	[NSGraphicsContext setCurrentContext:data->context];
    86     
    87     [[NSColor clearColor] set];
    88     NSRectFill([[windata->nswindow contentView] frame]);
    89     data->shape = SDL_CalculateShapeTree(*shape_mode,shape);
    90 	
    91 	pool = [[NSAutoreleasePool alloc] init];
    92     closure.view = [windata->nswindow contentView];
    93     closure.path = [[NSBezierPath bezierPath] autorelease];
    94 	closure.window = shaper->window;
    95     SDL_TraverseShapeTree(data->shape,&ConvertRects,&closure);
    96     [closure.path addClip];
    97 
    98     return 0;
    99 }
   100 
   101 int
   102 Cocoa_ResizeWindowShape(SDL_Window *window) {
   103     SDL_ShapeData* data = window->shaper->driverdata;
   104     SDL_assert(data != NULL);
   105     return 0;
   106 }
   107 
   108 #endif /* SDL_VIDEO_DRIVER_COCOA */
   109 
   110 /* vi: set ts=4 sw=4 expandtab: */