Skip to content

Commit 1b360de

Browse files
committed
Implement mac app features.
1 parent 1468c1c commit 1b360de

File tree

15 files changed

+239
-65
lines changed

15 files changed

+239
-65
lines changed

objc/08-CubeMapping/CubeMapping-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="MBEMetalViewMac">
708709
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
709710
<autoresizingMask key="autoresizingMask"/>
710711
</view>
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
//
22
// ViewController.h
3-
// CubeMapping-Mac
3+
// Mipmapping-Mac
44
//
55
// Created by Brent Gulanowski on 2018-06-19.
66
// Copyright © 2018 Metal By Example. All rights reserved.
77
//
88

9-
#import <Cocoa/Cocoa.h>
9+
@import Cocoa;
10+
11+
#import "MBEMetalView.h"
1012

1113
@interface ViewController : NSViewController
1214

15+
@property (nonatomic, readonly) MBEMetalView *metalView;
1316

1417
@end
1518

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,74 @@
11
//
22
// ViewController.m
3-
// CubeMapping-Mac
3+
// Mipmapping-Mac
44
//
55
// Created by Brent Gulanowski on 2018-06-19.
66
// Copyright © 2018 Metal By Example. All rights reserved.
77
//
88

99
#import "ViewController.h"
10+
#import "MBERenderer.h"
11+
12+
@interface ViewController ()
13+
14+
@property (nonatomic) CVDisplayLinkRef displayLink;
15+
@property (nonatomic, strong) MBERenderer *renderer;
16+
17+
- (void)draw;
18+
19+
@end
20+
21+
static CVReturn C3DViewDisplayLink(CVDisplayLinkRef displayLink,
22+
const CVTimeStamp *inNow,
23+
const CVTimeStamp *inOutputTime,
24+
CVOptionFlags flagsIn,
25+
CVOptionFlags *flagsOut,
26+
void *viewController)
27+
{
28+
@autoreleasepool {
29+
[(__bridge ViewController *)viewController draw];
30+
}
31+
32+
return kCVReturnSuccess;
33+
}
1034

1135
@implementation ViewController
1236

13-
- (void)viewDidLoad {
14-
[super viewDidLoad];
37+
- (void)dealloc
38+
{
39+
CVDisplayLinkRelease(_displayLink);
40+
_displayLink = NULL;
41+
}
1542

16-
// Do any additional setup after loading the view.
43+
- (MBEMetalView *)metalView
44+
{
45+
return (MBEMetalView *)self.view;
1746
}
1847

48+
- (void)viewDidLoad
49+
{
50+
[super viewDidLoad];
51+
52+
self.renderer = [[MBERenderer alloc] initWithLayer:self.metalView.metalLayer];
1953

20-
- (void)setRepresentedObject:(id)representedObject {
21-
[super setRepresentedObject:representedObject];
54+
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
55+
CVDisplayLinkSetOutputCallback(_displayLink, C3DViewDisplayLink, (__bridge void *)(self));
56+
}
2257

23-
// Update the view, if already loaded.
58+
- (void)viewWillAppear
59+
{
60+
CVDisplayLinkStart(_displayLink);
2461
}
2562

63+
- (void)viewWillDisappear
64+
{
65+
CVDisplayLinkStop(_displayLink);
66+
}
67+
68+
- (void)draw
69+
{
70+
// TODO: implement UI controls
71+
[self.renderer draw];
72+
}
2673

2774
@end

objc/08-CubeMapping/CubeMapping.xcodeproj/project.pbxproj

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@
3232
8451838E20D932F000BF473E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8451838D20D932F000BF473E /* Assets.xcassets */; };
3333
8451839120D932F000BF473E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8451838F20D932F000BF473E /* Main.storyboard */; };
3434
8451839420D932F000BF473E /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8451839320D932F000BF473E /* main.m */; };
35+
8451839920D9337C00BF473E /* nx.png in Resources */ = {isa = PBXBuildFile; fileRef = 8362D1DC1A0D6B6E00A6D9A8 /* nx.png */; };
36+
8451839A20D9337C00BF473E /* ny.png in Resources */ = {isa = PBXBuildFile; fileRef = 8362D1DD1A0D6B6E00A6D9A8 /* ny.png */; };
37+
8451839B20D9337C00BF473E /* nz.png in Resources */ = {isa = PBXBuildFile; fileRef = 8362D1DE1A0D6B6E00A6D9A8 /* nz.png */; };
38+
8451839C20D9337C00BF473E /* px.png in Resources */ = {isa = PBXBuildFile; fileRef = 8362D1DF1A0D6B6E00A6D9A8 /* px.png */; };
39+
8451839D20D9337C00BF473E /* py.png in Resources */ = {isa = PBXBuildFile; fileRef = 8362D1E01A0D6B6E00A6D9A8 /* py.png */; };
40+
8451839E20D9337C00BF473E /* pz.png in Resources */ = {isa = PBXBuildFile; fileRef = 8362D1E11A0D6B6E00A6D9A8 /* pz.png */; };
41+
8451839F20D9339700BF473E /* MBERenderer.m in Sources */ = {isa = PBXBuildFile; fileRef = 8362D1EC1A0D735800A6D9A8 /* MBERenderer.m */; };
42+
845183A020D9339700BF473E /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = 8362D1F01A0EA75F00A6D9A8 /* Shaders.metal */; };
43+
845183A120D9339700BF473E /* MBEMatrixUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 832493251A1196B7001E2340 /* MBEMatrixUtilities.m */; };
44+
845183A220D9339700BF473E /* MBETextureLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 8362D1E91A0D6BE500A6D9A8 /* MBETextureLoader.m */; };
45+
845183A320D9339700BF473E /* MBEMesh.m in Sources */ = {isa = PBXBuildFile; fileRef = 832493221A116424001E2340 /* MBEMesh.m */; };
46+
845183A420D9339700BF473E /* MBESkyboxMesh.m in Sources */ = {isa = PBXBuildFile; fileRef = 834B3CD51A0F1628009646DA /* MBESkyboxMesh.m */; };
47+
845183A520D9339700BF473E /* MBETorusKnotMesh.m in Sources */ = {isa = PBXBuildFile; fileRef = 8324931F1A113520001E2340 /* MBETorusKnotMesh.m */; };
48+
845183A820D934AD00BF473E /* MBEMetalViewMac.m in Sources */ = {isa = PBXBuildFile; fileRef = 845183A620D934AD00BF473E /* MBEMetalViewMac.m */; };
49+
845183AB20D9354400BF473E /* MBEMetalViewIOS.m in Sources */ = {isa = PBXBuildFile; fileRef = 845183A920D9354400BF473E /* MBEMetalViewIOS.m */; };
50+
845183AC20D9356200BF473E /* MBEMetalView.m in Sources */ = {isa = PBXBuildFile; fileRef = 8362D1CB1A0D653900A6D9A8 /* MBEMetalView.m */; };
3551
/* End PBXBuildFile section */
3652

3753
/* Begin PBXFileReference section */
@@ -77,6 +93,10 @@
7793
8451839220D932F000BF473E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7894
8451839320D932F000BF473E /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
7995
8451839520D932F000BF473E /* CubeMapping_Mac.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CubeMapping_Mac.entitlements; sourceTree = "<group>"; };
96+
845183A620D934AD00BF473E /* MBEMetalViewMac.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MBEMetalViewMac.m; path = "../../07-Mipmapping/Mipmapping-Mac/MBEMetalViewMac.m"; sourceTree = "<group>"; };
97+
845183A720D934AD00BF473E /* MBEMetalViewMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MBEMetalViewMac.h; path = "../../07-Mipmapping/Mipmapping-Mac/MBEMetalViewMac.h"; sourceTree = "<group>"; };
98+
845183A920D9354400BF473E /* MBEMetalViewIOS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBEMetalViewIOS.m; sourceTree = "<group>"; };
99+
845183AA20D9354400BF473E /* MBEMetalViewIOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MBEMetalViewIOS.h; sourceTree = "<group>"; };
80100
/* End PBXFileReference section */
81101

82102
/* Begin PBXFrameworksBuildPhase section */
@@ -134,6 +154,8 @@
134154
832493271A119747001E2340 /* MBETypes.h */,
135155
8362D1CA1A0D653900A6D9A8 /* MBEMetalView.h */,
136156
8362D1CB1A0D653900A6D9A8 /* MBEMetalView.m */,
157+
845183AA20D9354400BF473E /* MBEMetalViewIOS.h */,
158+
845183A920D9354400BF473E /* MBEMetalViewIOS.m */,
137159
8362D1AB1A0D5BEB00A6D9A8 /* MBEViewController.h */,
138160
8362D1AC1A0D5BEB00A6D9A8 /* MBEViewController.m */,
139161
8362D1EB1A0D735800A6D9A8 /* MBERenderer.h */,
@@ -198,13 +220,15 @@
198220
children = (
199221
8451838720D932F000BF473E /* AppDelegate.h */,
200222
8451838820D932F000BF473E /* AppDelegate.m */,
201-
8451838A20D932F000BF473E /* ViewController.h */,
202-
8451838B20D932F000BF473E /* ViewController.m */,
203223
8451838D20D932F000BF473E /* Assets.xcassets */,
204-
8451838F20D932F000BF473E /* Main.storyboard */,
224+
8451839520D932F000BF473E /* CubeMapping_Mac.entitlements */,
205225
8451839220D932F000BF473E /* Info.plist */,
206226
8451839320D932F000BF473E /* main.m */,
207-
8451839520D932F000BF473E /* CubeMapping_Mac.entitlements */,
227+
8451838F20D932F000BF473E /* Main.storyboard */,
228+
845183A720D934AD00BF473E /* MBEMetalViewMac.h */,
229+
845183A620D934AD00BF473E /* MBEMetalViewMac.m */,
230+
8451838A20D932F000BF473E /* ViewController.h */,
231+
8451838B20D932F000BF473E /* ViewController.m */,
208232
);
209233
path = "CubeMapping-Mac";
210234
sourceTree = "<group>";
@@ -304,6 +328,12 @@
304328
isa = PBXResourcesBuildPhase;
305329
buildActionMask = 2147483647;
306330
files = (
331+
8451839920D9337C00BF473E /* nx.png in Resources */,
332+
8451839A20D9337C00BF473E /* ny.png in Resources */,
333+
8451839B20D9337C00BF473E /* nz.png in Resources */,
334+
8451839C20D9337C00BF473E /* px.png in Resources */,
335+
8451839D20D9337C00BF473E /* py.png in Resources */,
336+
8451839E20D9337C00BF473E /* pz.png in Resources */,
307337
8451838E20D932F000BF473E /* Assets.xcassets in Resources */,
308338
8451839120D932F000BF473E /* Main.storyboard in Resources */,
309339
);
@@ -321,6 +351,7 @@
321351
832493231A116424001E2340 /* MBEMesh.m in Sources */,
322352
8362D1ED1A0D735800A6D9A8 /* MBERenderer.m in Sources */,
323353
8362D1AD1A0D5BEB00A6D9A8 /* MBEViewController.m in Sources */,
354+
845183AB20D9354400BF473E /* MBEMetalViewIOS.m in Sources */,
324355
832493261A1196B7001E2340 /* MBEMatrixUtilities.m in Sources */,
325356
8362D1AA1A0D5BEB00A6D9A8 /* AppDelegate.m in Sources */,
326357
8362D1A71A0D5BEB00A6D9A8 /* main.m in Sources */,
@@ -334,9 +365,18 @@
334365
isa = PBXSourcesBuildPhase;
335366
buildActionMask = 2147483647;
336367
files = (
368+
845183A420D9339700BF473E /* MBESkyboxMesh.m in Sources */,
337369
8451838C20D932F000BF473E /* ViewController.m in Sources */,
338370
8451839420D932F000BF473E /* main.m in Sources */,
371+
845183A120D9339700BF473E /* MBEMatrixUtilities.m in Sources */,
339372
8451838920D932F000BF473E /* AppDelegate.m in Sources */,
373+
8451839F20D9339700BF473E /* MBERenderer.m in Sources */,
374+
845183A320D9339700BF473E /* MBEMesh.m in Sources */,
375+
845183AC20D9356200BF473E /* MBEMetalView.m in Sources */,
376+
845183A520D9339700BF473E /* MBETorusKnotMesh.m in Sources */,
377+
845183A820D934AD00BF473E /* MBEMetalViewMac.m in Sources */,
378+
845183A220D9339700BF473E /* MBETextureLoader.m in Sources */,
379+
845183A020D9339700BF473E /* Shaders.metal in Sources */,
340380
);
341381
runOnlyForDeploymentPostprocessing = 0;
342382
};

objc/08-CubeMapping/CubeMapping/Base.lproj/Main.storyboard

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6250" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
36
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6244"/>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
9+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
510
</dependencies>
611
<scenes>
712
<!--View Controller-->
@@ -12,10 +17,10 @@
1217
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
1318
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1419
</layoutGuides>
15-
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC" customClass="MBEMetalView">
16-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
20+
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC" customClass="MBEMetalViewIOS">
21+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1722
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
23+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
1924
</view>
2025
</viewController>
2126
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

objc/08-CubeMapping/CubeMapping/MBEMesh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import UIKit;
1+
@import Foundation;
22
@import Metal;
33

44
@interface MBEMesh : NSObject
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
//
2+
// MBEMetalView.h
3+
// Mipmapping
4+
//
5+
// Created by Brent Gulanowski on 2018-06-19.
6+
// Copyright © 2018 Metal By Example. All rights reserved.
7+
//
8+
9+
#import <TargetConditionals.h>
10+
11+
#if TARGET_OS_IPHONE
112
@import UIKit;
13+
#define NSUIView UIView;
14+
#else
15+
@import AppKit;
16+
#define NSUIView NSView;
17+
#endif
18+
219
@import QuartzCore.CAMetalLayer;
320

4-
@interface MBEMetalView : UIView
21+
@interface MBEMetalView : NSUIView
522

623
@property (nonatomic, readonly) CAMetalLayer *metalLayer;
24+
@property (nonatomic, readonly) CGSize drawableSize;
725

826
@end
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1+
//
2+
// MBEMetalView.m
3+
// Mipmapping
4+
//
5+
// Created by Brent Gulanowski on 2018-06-19.
6+
// Copyright © 2018 Metal By Example. All rights reserved.
7+
//
8+
19
#import "MBEMetalView.h"
210

311
@implementation MBEMetalView
412

5-
+ (Class)layerClass
6-
{
7-
return [CAMetalLayer class];
8-
}
9-
10-
- (CAMetalLayer *)metalLayer
11-
{
12-
return (CAMetalLayer *)self.layer;
13-
}
13+
@dynamic metalLayer;
14+
@dynamic drawableSize;
1415

1516
- (void)setFrame:(CGRect)frame
1617
{
1718
[super setFrame:frame];
18-
19-
// During the first layout pass, we will not be in a view hierarchy, so we guess our scale
20-
CGFloat scale = [UIScreen mainScreen].scale;
21-
22-
// If we've moved to a window by the time our frame is being set, we can take its scale as our own
23-
if (self.window)
24-
{
25-
scale = self.window.screen.scale;
26-
}
27-
28-
CGSize drawableSize = self.bounds.size;
29-
30-
// Since drawable size is in pixels, we need to multiply by the scale to move from points to pixels
31-
drawableSize.width *= scale;
32-
drawableSize.height *= scale;
33-
34-
self.metalLayer.drawableSize = drawableSize;
19+
self.metalLayer.drawableSize = self.drawableSize;
3520
}
3621

3722
@end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@import UIKit;
2+
@import QuartzCore.CAMetalLayer;
3+
4+
#import "MBEMetalView.h"
5+
6+
@interface MBEMetalViewIOS : MBEMetalView
7+
8+
@end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#import "MBEMetalViewIOS.h"
2+
3+
@implementation MBEMetalViewIOS
4+
5+
+ (Class)layerClass
6+
{
7+
return [CAMetalLayer class];
8+
}
9+
10+
- (CAMetalLayer *)metalLayer
11+
{
12+
return (CAMetalLayer *)self.layer;
13+
}
14+
15+
- (CGSize)drawableSize {
16+
17+
// During the first layout pass, we will not be in a view hierarchy, so we guess our scale
18+
// If we've moved to a window by the time our frame is being set, we can take its scale as our own
19+
CGFloat scale = self.window ? self.window.screen.scale : [UIScreen mainScreen].scale;
20+
CGSize drawableSize = self.bounds.size;
21+
22+
// Since drawable size is in pixels, we need to multiply by the scale to move from points to pixels
23+
drawableSize.width *= scale;
24+
drawableSize.height *= scale;
25+
26+
return drawableSize;
27+
}
28+
29+
@end

0 commit comments

Comments
 (0)