Skip to content

Commit c09c593

Browse files
committed
Initial working macOS version of view.
1 parent 7ad250f commit c09c593

File tree

4 files changed

+100
-12
lines changed

4 files changed

+100
-12
lines changed

objc/03-DrawingIn2D/DrawingIn2D-Mac/Base.lproj/Main.storyboard

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11134"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14113"/>
5+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
56
</dependencies>
67
<scenes>
78
<!--Application-->
@@ -673,7 +674,7 @@
673674
<outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
674675
</connections>
675676
</application>
676-
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModuleProvider=""/>
677+
<customObject id="Voe-Tx-rLC" customClass="AppDelegate"/>
677678
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
678679
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
679680
</objects>
@@ -688,7 +689,7 @@
688689
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
689690
<rect key="contentRect" x="196" y="240" width="480" height="270"/>
690691
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
691-
<connections>
692+
<connections>
692693
<outlet property="delegate" destination="B8D-0N-5wS" id="98r-iN-zZc"/>
693694
</connections>
694695
</window>
@@ -703,8 +704,8 @@
703704
<!--View Controller-->
704705
<scene sceneID="hIz-AP-VOD">
705706
<objects>
706-
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModuleProvider="" sceneMemberID="viewController">
707-
<view key="view" wantsLayer="YES" id="m2S-Jp-Qdl">
707+
<viewController id="XfG-lQ-9wD" customClass="ViewController" sceneMemberID="viewController">
708+
<view key="view" wantsLayer="YES" id="m2S-Jp-Qdl" customClass="MBEMetalView">
708709
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
709710
<autoresizingMask key="autoresizingMask"/>
710711
</view>

objc/03-DrawingIn2D/DrawingIn2D.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
845182CB20D808C800BF473E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 845182CA20D808C800BF473E /* Assets.xcassets */; };
2121
845182CE20D808C800BF473E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 845182CC20D808C800BF473E /* Main.storyboard */; };
2222
845182D120D808C800BF473E /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 845182D020D808C800BF473E /* main.m */; };
23+
845182D620D8096300BF473E /* MBEMetalView.m in Sources */ = {isa = PBXBuildFile; fileRef = 83E2D77F1BD188710006DDD8 /* MBEMetalView.m */; };
24+
845182DC20D80E3E00BF473E /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = 83E2D7801BD188710006DDD8 /* Shaders.metal */; };
2325
/* End PBXBuildFile section */
2426

2527
/* Begin PBXFileReference section */
@@ -243,6 +245,8 @@
243245
845182C920D808C700BF473E /* ViewController.m in Sources */,
244246
845182D120D808C800BF473E /* main.m in Sources */,
245247
845182C620D808C700BF473E /* AppDelegate.m in Sources */,
248+
845182D620D8096300BF473E /* MBEMetalView.m in Sources */,
249+
845182DC20D80E3E00BF473E /* Shaders.metal in Sources */,
246250
);
247251
runOnlyForDeploymentPostprocessing = 0;
248252
};

objc/03-DrawingIn2D/DrawingIn2D/MBEMetalView.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
#import <TargetConditionals.h>
2+
3+
#if TARGET_OS_IPHONE
14
@import UIKit;
5+
#define NSUIView UIView
6+
#else
7+
@import AppKit;
8+
@import QuartzCore;
9+
#define NSUIView NSView
10+
#endif
211

3-
@interface MBEMetalView : UIView
12+
@interface MBEMetalView : NSUIView
413

514
@property (readonly) CAMetalLayer *metalLayer;
615

objc/03-DrawingIn2D/DrawingIn2D/MBEMetalView.m

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import "MBEMetalView.h"
22
@import Metal;
33
@import simd;
4+
@import CoreVideo;
45

56
typedef struct
67
{
@@ -9,17 +10,44 @@
910
} MBEVertex;
1011

1112
@interface MBEMetalView ()
13+
#if TARGET_OS_IPHONE
1214
@property (nonatomic, strong) CADisplayLink *displayLink;
15+
#else
16+
@property (nonatomic) CVDisplayLinkRef displayLink;
17+
@property (nonatomic, strong) CAMetalLayer *metalLayer;
18+
#endif
1319
@property (nonatomic, strong) id<MTLDevice> device;
1420
@property (nonatomic, strong) id<MTLRenderPipelineState> pipeline;
1521
@property (nonatomic, strong) id<MTLCommandQueue> commandQueue;
1622
@property (nonatomic, strong) id<MTLBuffer> vertexBuffer;
23+
24+
- (void)redraw;
1725
@end
1826

27+
#if TARGET_OS_MAC
28+
//static NSTimeInterval C3DTimeIntervalFromTimeStamp(const CVTimeStamp *timeStamp) {
29+
// return 1.0 / (timeStamp->rateScalar * (double)timeStamp->videoTimeScale / (double)timeStamp->videoRefreshPeriod);
30+
//}
31+
32+
static CVReturn C3DViewDisplayLink(CVDisplayLinkRef displayLink,
33+
const CVTimeStamp *inNow,
34+
const CVTimeStamp *inOutputTime,
35+
CVOptionFlags flagsIn,
36+
CVOptionFlags *flagsOut,
37+
void *view) {
38+
@autoreleasepool {
39+
[(__bridge MBEMetalView *)view redraw];
40+
}
41+
42+
return kCVReturnSuccess;
43+
}
44+
#endif
45+
1946
@implementation MBEMetalView
2047

2148
@synthesize device=device;
2249

50+
#if TARGET_OS_IPHONE
2351
+ (Class)layerClass
2452
{
2553
return [CAMetalLayer class];
@@ -56,11 +84,49 @@ - (void)didMoveToSuperview
5684
self.displayLink = nil;
5785
}
5886
}
87+
#else
88+
89+
- (CALayer *)makeBackingLayer
90+
{
91+
CAMetalLayer *layer = [[CAMetalLayer alloc] init];
92+
_metalLayer = layer;
93+
return layer;
94+
}
95+
96+
- (void)viewDidMoveToSuperview
97+
{
98+
[super viewDidMoveToSuperview];
99+
100+
if (!self.device) {
101+
[self makeDevice];
102+
[self makeBuffers];
103+
[self makePipeline];
104+
}
105+
106+
if (self.superview) {
107+
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
108+
CVDisplayLinkSetOutputCallback(_displayLink, C3DViewDisplayLink, (__bridge void *)(self));
109+
CVDisplayLinkStart(_displayLink);
110+
}
111+
else {
112+
CVDisplayLinkStop(_displayLink);
113+
CVDisplayLinkRelease(_displayLink);
114+
_displayLink = NULL;
115+
}
116+
}
117+
#endif
59118

60119
- (void)setFrame:(CGRect)frame
61120
{
62121
[super setFrame:frame];
122+
self.metalLayer.drawableSize = self.drawableSize;
123+
}
124+
125+
#if TARGET_OS_IPHONE
126+
- (CGSize)drawableSize {
63127

128+
CGSize drawableSize = self.bounds.size;
129+
64130
// During the first layout pass, we will not be in a view hierarchy, so we guess our scale
65131
CGFloat scale = [UIScreen mainScreen].scale;
66132

@@ -70,17 +136,23 @@ - (void)setFrame:(CGRect)frame
70136
scale = self.window.screen.scale;
71137
}
72138

73-
CGSize drawableSize = self.bounds.size;
74-
75139
// Since drawable size is in pixels, we need to multiply by the scale to move from points to pixels
76140
drawableSize.width *= scale;
77141
drawableSize.height *= scale;
78-
79-
self.metalLayer.drawableSize = drawableSize;
80142
}
81143

144+
#else
145+
- (CGSize)drawableSize {
146+
return self.bounds.size;
147+
}
148+
#endif
149+
82150
- (CAMetalLayer *)metalLayer {
151+
#if TARGET_OS_IPHONE
83152
return (CAMetalLayer *)self.layer;
153+
#else
154+
return _metalLayer;
155+
#endif
84156
}
85157

86158
- (void)makeDevice
@@ -154,9 +226,11 @@ - (void)redraw
154226
}
155227
}
156228

229+
#if TARGET_OS_IPHONE
157230
- (void)displayLinkDidFire:(CADisplayLink *)displayLink
158231
{
159232
[self redraw];
160233
}
234+
#endif
161235

162236
@end

0 commit comments

Comments
 (0)