src/video/cocoa/SDL_cocoashape.m
author Sam Lantinga
Fri, 27 Jan 2017 06:05:50 -0800
changeset 10856 486aa38c6a88
parent 10737 3406a0f8b041
child 11144 cccd37a9c28b
permissions -rw-r--r--
Added Thrustmaster Wheel FFB entry to the list of wheels
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 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_internal.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 {
    35     SDL_WindowData* windata = (SDL_WindowData*)window->driverdata;
    36     [windata->nswindow setOpaque:NO];
    37 
    38     [windata->nswindow setStyleMask:NSBorderlessWindowMask];
    39 
    40     SDL_WindowShaper* result = result = malloc(sizeof(SDL_WindowShaper));
    41     result->window = window;
    42     result->mode.mode = ShapeModeDefault;
    43     result->mode.parameters.binarizationCutoff = 1;
    44     result->userx = result->usery = 0;
    45     window->shaper = result;
    46 
    47     SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData));
    48     result->driverdata = data;
    49     data->context = [windata->nswindow graphicsContext];
    50     data->saved = SDL_FALSE;
    51     data->shape = NULL;
    52 
    53     int resized_properly = Cocoa_ResizeWindowShape(window);
    54     SDL_assert(resized_properly == 0);
    55     return result;
    56 }
    57 
    58 typedef struct {
    59     NSView* view;
    60     NSBezierPath* path;
    61     SDL_Window* window;
    62 } SDL_CocoaClosure;
    63 
    64 void
    65 ConvertRects(SDL_ShapeTree* tree, void* closure)
    66 {
    67     SDL_CocoaClosure* data = (SDL_CocoaClosure*)closure;
    68     if(tree->kind == OpaqueShape) {
    69         NSRect rect = NSMakeRect(tree->data.shape.x,data->window->h - tree->data.shape.y,tree->data.shape.w,tree->data.shape.h);
    70         [data->path appendBezierPathWithRect:[data->view convertRect:rect toView:nil]];
    71     }
    72 }
    73 
    74 int
    75 Cocoa_SetWindowShape(SDL_WindowShaper *shaper, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
    76 { @autoreleasepool
    77 {
    78     SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
    79     SDL_WindowData* windata = (SDL_WindowData*)shaper->window->driverdata;
    80     SDL_CocoaClosure closure;
    81     if(data->saved == SDL_TRUE) {
    82         [data->context restoreGraphicsState];
    83         data->saved = SDL_FALSE;
    84     }
    85 
    86     /*[data->context saveGraphicsState];*/
    87     /*data->saved = SDL_TRUE;*/
    88     [NSGraphicsContext setCurrentContext:data->context];
    89 
    90     [[NSColor clearColor] set];
    91     NSRectFill([[windata->nswindow contentView] frame]);
    92     data->shape = SDL_CalculateShapeTree(*shape_mode,shape);
    93 
    94     closure.view = [windata->nswindow contentView];
    95     closure.path = [NSBezierPath bezierPath];
    96     closure.window = shaper->window;
    97     SDL_TraverseShapeTree(data->shape,&ConvertRects,&closure);
    98     [closure.path addClip];
    99 
   100     return 0;
   101 }}
   102 
   103 int
   104 Cocoa_ResizeWindowShape(SDL_Window *window)
   105 {
   106     SDL_ShapeData* data = window->shaper->driverdata;
   107     SDL_assert(data != NULL);
   108     return 0;
   109 }
   110 
   111 #endif /* SDL_VIDEO_DRIVER_COCOA */
   112 
   113 /* vi: set ts=4 sw=4 expandtab: */