Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
100 lines (80 loc) · 3.38 KB

SDL_cocoashape.m

File metadata and controls

100 lines (80 loc) · 3.38 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
*/
Aug 3, 2010
Aug 3, 2010
23
#include "SDL_stdinc.h"
Jul 27, 2010
Jul 27, 2010
24
#include "SDL_cocoavideo.h"
25
#include "SDL_shape.h"
Jul 27, 2010
Jul 27, 2010
26
#include "SDL_cocoashape.h"
Aug 4, 2010
Aug 4, 2010
27
#include "../SDL_sysvideo.h"
Jun 5, 2010
Jun 5, 2010
28
Jul 27, 2010
Jul 27, 2010
29
30
31
32
SDL_WindowShaper* Cocoa_CreateShaper(SDL_Window* window) {
SDL_WindowData* data = (SDL_WindowData*)window->driverdata;
[data->nswindow setAlpha:1.0];
[data->nswindow setOpaque:YES];
Jul 27, 2010
Jul 27, 2010
33
[data->nswindow setStyleMask:NSBorderlessWindowMask];
Aug 3, 2010
Aug 3, 2010
34
SDL_WindowShaper* result = SDL_malloc(sizeof(SDL_WindowShaper));
Jul 27, 2010
Jul 27, 2010
35
36
37
38
39
result->window = window;
result->mode.mode = ShapeModeDefault;
result->mode.parameters.binarizationCutoff = 1;
result->usershownflag = 0;
window->shaper = result;
Jul 27, 2010
Jul 27, 2010
40
Aug 3, 2010
Aug 3, 2010
41
42
43
44
45
SDL_ShapeData* shape_data = SDL_malloc(sizeof(SDL_ShapeData));
result->driverdata = shape_data;
shape_data->context = [data->nswindow graphicsContext];
shape_data->saved = SDL_FALSE;
shape_data->shape = NULL;
Jul 27, 2010
Jul 27, 2010
46
Jul 27, 2010
Jul 27, 2010
47
48
49
50
51
int resized_properly = Cocoa_ResizeWindowShape(window);
assert(resized_properly == 0);
return result;
}
Aug 4, 2010
Aug 4, 2010
52
53
54
55
56
57
58
59
60
61
typedef struct {
NSBezierPath* clipPath;
SDL_Window* window;
} SDL_PathConglomeration;
NSRect convert_rect(SDL_Rect rect,SDL_Window* window) {
NSRect nsrect = NSMakeRect(rect.x,window->h-(rect.y+rect.h),rect.w,rect.h);
return [[((SDL_WindowData*)window->driverdata)->nswindow contentView] convertRectFromBase:nsrect];
}
Aug 4, 2010
Aug 4, 2010
62
void ConglomerateShapeTree(SDL_ShapeTree* tree,SDL_PathConglomeration* cong) {
Aug 4, 2010
Aug 4, 2010
63
64
65
66
67
68
if(tree->kind == OpaqueShape) {
NSRect rect = convert_rect(tree->data.shape,cong->window);
[cong->clipPath appendBezierPathWithRect:rect];
}
}
Jul 27, 2010
Jul 27, 2010
69
int Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
Aug 3, 2010
Aug 3, 2010
70
71
SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
if(data->saved == SDL_TRUE) {
Jul 27, 2010
Jul 27, 2010
72
[data->context restoreGraphicsState];
Aug 3, 2010
Aug 3, 2010
73
data->saved = SDL_FALSE;
Jul 27, 2010
Jul 27, 2010
74
75
76
}
[data->context saveGraphicsState];
Aug 3, 2010
Aug 3, 2010
77
data->saved = SDL_TRUE;
Jul 27, 2010
Jul 27, 2010
78
Aug 4, 2010
Aug 4, 2010
79
80
//[[NSColor clearColor] set];
//NSRectFill([[((SDL_WindowData*)shaper->window->driverdata)->nswindow contentView] frame]);
Jul 27, 2010
Jul 27, 2010
81
82
/* TODO: It looks like Cocoa can set a clipping path based on a list of rectangles. That's what we get from the
Windoze shape-calculation code: a list of rectangles. This will work... I think. */
Aug 4, 2010
Aug 4, 2010
83
84
85
86
NSBezierPath* clipPath = [NSBezierPath bezierPath];
SDL_PathConglomeration cong = {clipPath,shaper->window};
Aug 4, 2010
Aug 4, 2010
87
SDL_TraverseShapeTree(data->shape,(SDL_TraversalFunction)&ConglomerateShapeTree,(void*)&cong);
Aug 4, 2010
Aug 4, 2010
88
89
[clipPath addClip];
Jul 27, 2010
Jul 27, 2010
90
91
92
93
94
}
int Cocoa_ResizeWindowShape(SDL_Window *window) {
SDL_ShapeData* data = window->shaper->driverdata;
assert(data != NULL);
Aug 3, 2010
Aug 3, 2010
95
96
97
98
if(data->shape != NULL)
SDL_FreeShapeTree(&data->shape);
Jul 27, 2010
Jul 27, 2010
99
100
return 0;
}