src/video/cocoa/SDL_cocoashape.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8647 e2a51b0426a6
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

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