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