src/video/cocoa/SDL_cocoashape.m
author Sam Lantinga
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 4925 514f811a4887
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 2010 Eli Gottlieb
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Eli Gottlieb
    20     eligottlieb@gmail.com
    21 */
    22 
    23 #include "SDL_cocoavideo.h"
    24 #include "SDL_shape.h"
    25 #include "SDL_cocoashape.h"
    26 #include "../SDL_sysvideo.h"
    27 
    28 SDL_WindowShaper*
    29 Cocoa_CreateShaper(SDL_Window* window) {
    30     SDL_WindowData* windata = (SDL_WindowData*)window->driverdata;
    31     [windata->nswindow setOpaque:NO];
    32 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
    33     [windata->nswindow setStyleMask:NSBorderlessWindowMask];
    34 #endif
    35     SDL_WindowShaper* result = result = malloc(sizeof(SDL_WindowShaper));
    36     result->window = window;
    37     result->mode.mode = ShapeModeDefault;
    38     result->mode.parameters.binarizationCutoff = 1;
    39     result->userx = result->usery = 0;
    40     window->shaper = result;
    41     
    42     SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData));
    43     result->driverdata = data;
    44     data->context = [windata->nswindow graphicsContext];
    45     data->saved = SDL_FALSE;
    46     data->shape = NULL;
    47     
    48     int resized_properly = Cocoa_ResizeWindowShape(window);
    49     assert(resized_properly == 0);
    50     return result;
    51 }
    52 
    53 typedef struct {
    54     NSView* view;
    55     NSBezierPath* path;
    56     SDL_Window* window;
    57 } SDL_CocoaClosure;
    58 
    59 void
    60 ConvertRects(SDL_ShapeTree* tree,void* closure) {
    61     SDL_CocoaClosure* data = (SDL_CocoaClosure*)closure;
    62     if(tree->kind == OpaqueShape) {
    63         NSRect rect = NSMakeRect(tree->data.shape.x,data->window->h - tree->data.shape.y,tree->data.shape.w,tree->data.shape.h);
    64         [data->path appendBezierPathWithRect:[data->view convertRect:rect toView:nil]];
    65     }
    66 }
    67 
    68 int
    69 Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
    70     SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
    71 	SDL_WindowData* windata = (SDL_WindowData*)shaper->window->driverdata;
    72 	SDL_CocoaClosure closure;
    73 	NSAutoreleasePool *pool = NULL;
    74     if(data->saved == SDL_TRUE) {
    75         [data->context restoreGraphicsState];
    76         data->saved = SDL_FALSE;
    77     }
    78         
    79     //[data->context saveGraphicsState];
    80     //data->saved = SDL_TRUE;
    81 	[NSGraphicsContext setCurrentContext:data->context];
    82     
    83     [[NSColor clearColor] set];
    84     NSRectFill([[windata->nswindow contentView] frame]);
    85     data->shape = SDL_CalculateShapeTree(*shape_mode,shape);
    86 	
    87 	pool = [[NSAutoreleasePool alloc] init];
    88     closure.view = [windata->nswindow contentView];
    89     closure.path = [[NSBezierPath bezierPath] autorelease];
    90 	closure.window = shaper->window;
    91     SDL_TraverseShapeTree(data->shape,&ConvertRects,&closure);
    92     [closure.path addClip];
    93 }
    94 
    95 int
    96 Cocoa_ResizeWindowShape(SDL_Window *window) {
    97     SDL_ShapeData* data = window->shaper->driverdata;
    98     assert(data != NULL);
    99     return 0;
   100 }