Skip to content

Latest commit

 

History

History
128 lines (104 loc) · 3.58 KB

SDL_cocoametalview.m

File metadata and controls

128 lines (104 loc) · 3.58 KB
 
Aug 28, 2017
Aug 28, 2017
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
Aug 28, 2017
Aug 28, 2017
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
* @author Mark Callow, www.edgewise-consulting.com.
*
* Thanks to Alex Szpakowski, @slime73 on GitHub, for his gist showing
* how to add a CAMetalLayer backed view.
*/
#import "SDL_cocoametalview.h"
Dec 31, 2017
Dec 31, 2017
31
#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL)
Aug 29, 2017
Aug 29, 2017
32
Aug 28, 2017
Aug 28, 2017
33
34
35
36
37
38
39
40
41
42
#include "SDL_assert.h"
@implementation SDL_cocoametalview
/* The synthesized getter should be called by super's viewWithTag. */
@synthesize tag = _tag;
/* Return a Metal-compatible layer. */
+ (Class)layerClass
{
Sep 1, 2017
Sep 1, 2017
43
return NSClassFromString(@"CAMetalLayer");
Aug 28, 2017
Aug 28, 2017
44
45
46
}
/* Indicate the view wants to draw using a backing layer instead of drawRect. */
Dec 31, 2017
Dec 31, 2017
47
- (BOOL)wantsUpdateLayer
Sep 1, 2017
Sep 1, 2017
48
49
50
{
return YES;
}
Aug 28, 2017
Aug 28, 2017
51
52
53
54
/* When the wantsLayer property is set to YES, this method will be invoked to
* return a layer instance.
*/
Dec 31, 2017
Dec 31, 2017
55
- (CALayer*)makeBackingLayer
Sep 1, 2017
Sep 1, 2017
56
57
58
{
return [self.class.layerClass layer];
}
Aug 28, 2017
Aug 28, 2017
59
60
61
62
- (instancetype)initWithFrame:(NSRect)frame
useHighDPI:(bool)useHighDPI
{
Sep 1, 2017
Sep 1, 2017
63
if ((self = [super initWithFrame:frame])) {
Jan 3, 2018
Jan 3, 2018
64
65
self.wantsLayer = YES;
Sep 1, 2017
Sep 1, 2017
66
67
68
/* Allow resize. */
self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
_tag = METALVIEW_TAG;
Jan 3, 2018
Jan 3, 2018
69
Sep 1, 2017
Sep 1, 2017
70
71
72
_useHighDPI = useHighDPI;
[self updateDrawableSize];
}
Aug 28, 2017
Aug 28, 2017
73
Sep 1, 2017
Sep 1, 2017
74
return self;
Aug 28, 2017
Aug 28, 2017
75
76
77
}
/* Set the size of the metal drawables when the view is resized. */
Dec 31, 2017
Dec 31, 2017
78
79
80
- (void)resizeWithOldSuperviewSize:(NSSize)oldSize
{
[super resizeWithOldSuperviewSize:oldSize];
Aug 28, 2017
Aug 28, 2017
81
82
83
84
85
[self updateDrawableSize];
}
- (void)updateDrawableSize
{
Sep 1, 2017
Sep 1, 2017
86
NSRect bounds = [self bounds];
Aug 28, 2017
Aug 28, 2017
87
if (_useHighDPI) {
Sep 1, 2017
Sep 1, 2017
88
bounds = [self convertRectToBacking:bounds];
Aug 28, 2017
Aug 28, 2017
89
}
Sep 1, 2017
Sep 1, 2017
90
((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize(bounds.size);
Aug 28, 2017
Aug 28, 2017
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
}
@end
SDL_cocoametalview*
Cocoa_Mtl_AddMetalView(SDL_Window* window)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
NSView *view = data->nswindow.contentView;
SDL_cocoametalview *metalview
= [[SDL_cocoametalview alloc] initWithFrame:view.frame
useHighDPI:(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)];
[view addSubview:metalview];
return metalview;
}
void
Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
{
SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
NSView *view = data->nswindow.contentView;
SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
if (metalview) {
CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
assert(layer != NULL);
Sep 1, 2017
Sep 1, 2017
117
if (w) {
Aug 28, 2017
Aug 28, 2017
118
*w = layer.drawableSize.width;
Sep 1, 2017
Sep 1, 2017
119
120
}
if (h) {
Aug 28, 2017
Aug 28, 2017
121
*h = layer.drawableSize.height;
Sep 1, 2017
Sep 1, 2017
122
}
Aug 28, 2017
Aug 28, 2017
123
124
125
}
}
Dec 31, 2017
Dec 31, 2017
126
#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL) */
Aug 28, 2017
Aug 28, 2017
127
Aug 29, 2017
Aug 29, 2017
128
/* vi: set ts=4 sw=4 expandtab: */