SDL  2.0
SDL_cocoashape.m
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2016 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 
34 {
35  SDL_WindowData* windata = (SDL_WindowData*)window->driverdata;
36  [windata->nswindow setOpaque:NO];
37 
38  if ([windata->nswindow respondsToSelector:@selector(setStyleMask:)]) {
39  [windata->nswindow setStyleMask:NSBorderlessWindowMask];
40  }
41 
42  SDL_WindowShaper* result = result = malloc(sizeof(SDL_WindowShaper));
43  result->window = window;
44  result->mode.mode = ShapeModeDefault;
46  result->userx = result->usery = 0;
47  window->shaper = result;
48 
50  result->driverdata = data;
51  data->context = [windata->nswindow graphicsContext];
52  data->saved = SDL_FALSE;
53  data->shape = NULL;
54 
55  int resized_properly = Cocoa_ResizeWindowShape(window);
56  SDL_assert(resized_properly == 0);
57  return result;
58 }
59 
60 typedef struct {
61  NSView* view;
62  NSBezierPath* path;
64 } SDL_CocoaClosure;
65 
66 void
67 ConvertRects(SDL_ShapeTree* tree, void* closure)
68 {
69  SDL_CocoaClosure* data = (SDL_CocoaClosure*)closure;
70  if(tree->kind == OpaqueShape) {
71  NSRect rect = NSMakeRect(tree->data.shape.x,data->window->h - tree->data.shape.y,tree->data.shape.w,tree->data.shape.h);
72  [data->path appendBezierPathWithRect:[data->view convertRect:rect toView:nil]];
73  }
74 }
75 
76 int
78 { @autoreleasepool
79 {
80  SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
81  SDL_WindowData* windata = (SDL_WindowData*)shaper->window->driverdata;
82  SDL_CocoaClosure closure;
83  if(data->saved == SDL_TRUE) {
84  [data->context restoreGraphicsState];
85  data->saved = SDL_FALSE;
86  }
87 
88  /*[data->context saveGraphicsState];*/
89  /*data->saved = SDL_TRUE;*/
90  [NSGraphicsContext setCurrentContext:data->context];
91 
92  [[NSColor clearColor] set];
93  NSRectFill([[windata->nswindow contentView] frame]);
94  data->shape = SDL_CalculateShapeTree(*shape_mode,shape);
95 
96  closure.view = [windata->nswindow contentView];
97  closure.path = [NSBezierPath bezierPath];
98  closure.window = shaper->window;
99  SDL_TraverseShapeTree(data->shape,&ConvertRects,&closure);
100  [closure.path addClip];
101 
102  return 0;
103 }}
104 
105 int
107 {
108  SDL_ShapeData* data = window->shaper->driverdata;
109  SDL_assert(data != NULL);
110  return 0;
111 }
112 
113 #endif /* SDL_VIDEO_DRIVER_COCOA */
114 
115 /* vi: set ts=4 sw=4 expandtab: */
SDL_ShapeTree * SDL_CalculateShapeTree(SDL_WindowShapeMode mode, SDL_Surface *shape)
Definition: SDL_shape.c:207
GLuint64EXT * result
SDL_Rect rect
Definition: testrelative.c:27
SDL_Window * window
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
SDL_ShapeUnion data
int Cocoa_SetWindowShape(SDL_WindowShaper *shaper, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
SDL_WindowShapeMode mode
Definition: SDL_sysvideo.h:47
SDL_WindowShaper * shaper
Definition: SDL_sysvideo.h:99
SDL_Window * window
Definition: SDL_sysvideo.h:41
int frame
Definition: teststreaming.c:60
SDL_WindowShaper * Cocoa_CreateShaper(SDL_Window *window)
The default mode, a binarized alpha cutoff of 1.
Definition: SDL_shape.h:81
SDL_ShapeTree * shape
SDL_WindowShapeParams parameters
Window-shape parameters.
Definition: SDL_shape.h:104
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
NSWindow * nswindow
#define SDL_assert(condition)
Definition: SDL_assert.h:167
SDL_bool saved
#define NULL
Definition: begin_code.h:143
#define malloc
Definition: SDL_malloc.c:641
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:71
void SDL_TraverseShapeTree(SDL_ShapeTree *tree, SDL_TraversalFunction function, void *closure)
Definition: SDL_shape.c:220
int Cocoa_ResizeWindowShape(SDL_Window *window)
NSGraphicsContext * context
Uint8 binarizationCutoff
a cutoff alpha value for binarization of the window shape&#39;s alpha channel.
Definition: SDL_shape.h:95
A struct that tags the SDL_WindowShapeParams union with an enum describing the type of its contents...
Definition: SDL_shape.h:100
GLsizei const GLchar *const * path
void * driverdata
Definition: SDL_sysvideo.h:106
SDL_ShapeKind kind
WindowShapeMode mode
The mode of these window-shape parameters.
Definition: SDL_shape.h:102
int y
Definition: SDL_rect.h:66