sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_cocoashape.m (3590B)


      1 /*
      2   Simple DirectMedia Layer
      3   Copyright (C) 1997-2020 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 #include "../../SDL_internal.h"
     22 
     23 #if SDL_VIDEO_DRIVER_COCOA
     24 
     25 #include "SDL_cocoavideo.h"
     26 #include "SDL_shape.h"
     27 #include "SDL_cocoashape.h"
     28 #include "../SDL_sysvideo.h"
     29 
     30 SDL_WindowShaper*
     31 Cocoa_CreateShaper(SDL_Window* window)
     32 {
     33     SDL_WindowData* windata = (SDL_WindowData*)window->driverdata;
     34     [windata->nswindow setOpaque:NO];
     35 
     36     [windata->nswindow setStyleMask:NSWindowStyleMaskBorderless];
     37 
     38     SDL_WindowShaper* result = (SDL_WindowShaper *)SDL_malloc(sizeof(SDL_WindowShaper));
     39     result->window = window;
     40     result->mode.mode = ShapeModeDefault;
     41     result->mode.parameters.binarizationCutoff = 1;
     42     result->userx = result->usery = 0;
     43     window->shaper = result;
     44 
     45     SDL_ShapeData* data = (SDL_ShapeData *)SDL_malloc(sizeof(SDL_ShapeData));
     46     result->driverdata = data;
     47     data->context = [windata->nswindow graphicsContext];
     48     data->saved = SDL_FALSE;
     49     data->shape = NULL;
     50 
     51     int resized_properly = Cocoa_ResizeWindowShape(window);
     52     SDL_assert(resized_properly == 0);
     53     return result;
     54 }
     55 
     56 typedef struct {
     57     NSView* view;
     58     NSBezierPath* path;
     59     SDL_Window* window;
     60 } SDL_CocoaClosure;
     61 
     62 void
     63 ConvertRects(SDL_ShapeTree* tree, void* closure)
     64 {
     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 { @autoreleasepool
     75 {
     76     SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
     77     SDL_WindowData* windata = (SDL_WindowData*)shaper->window->driverdata;
     78     SDL_CocoaClosure closure;
     79     if(data->saved == SDL_TRUE) {
     80         [data->context restoreGraphicsState];
     81         data->saved = SDL_FALSE;
     82     }
     83 
     84     /*[data->context saveGraphicsState];*/
     85     /*data->saved = SDL_TRUE;*/
     86     [NSGraphicsContext setCurrentContext:data->context];
     87 
     88     [[NSColor clearColor] set];
     89     NSRectFill([windata->sdlContentView frame]);
     90     data->shape = SDL_CalculateShapeTree(*shape_mode,shape);
     91 
     92     closure.view = windata->sdlContentView;
     93     closure.path = [NSBezierPath bezierPath];
     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 {
    104     SDL_ShapeData* data = window->shaper->driverdata;
    105     SDL_assert(data != NULL);
    106     return 0;
    107 }
    108 
    109 #endif /* SDL_VIDEO_DRIVER_COCOA */
    110 
    111 /* vi: set ts=4 sw=4 expandtab: */