2017-04-14 12:00:05 -04:00
|
|
|
/*
|
2017-06-22 14:53:49 -04:00
|
|
|
* DemoViewController.mm
|
2017-04-14 12:00:05 -04:00
|
|
|
*
|
2017-06-22 14:53:49 -04:00
|
|
|
* Copyright (c) 2016-2017 The Brenwill Workshop Ltd.
|
|
|
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
2017-04-14 12:00:05 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import "DemoViewController.h"
|
|
|
|
|
#import <QuartzCore/CAMetalLayer.h>
|
|
|
|
|
|
2017-06-22 14:53:49 -04:00
|
|
|
#include "MVKExample.h"
|
2017-04-14 12:00:05 -04:00
|
|
|
|
2021-09-10 01:23:49 -04:00
|
|
|
const std::string getAssetPath() {
|
2017-04-14 12:00:05 -04:00
|
|
|
return [NSBundle.mainBundle.resourcePath stringByAppendingString: @"/data/"].UTF8String;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Rendering loop callback function for use with a CVDisplayLink. */
|
|
|
|
|
static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink,
|
|
|
|
|
const CVTimeStamp* now,
|
|
|
|
|
const CVTimeStamp* outputTime,
|
|
|
|
|
CVOptionFlags flagsIn,
|
|
|
|
|
CVOptionFlags* flagsOut,
|
|
|
|
|
void* target) {
|
2021-09-10 01:23:49 -04:00
|
|
|
//((MVKExample*)target)->renderFrame();
|
2022-05-04 00:43:14 -04:00
|
|
|
((MVKExample*)target)->displayLinkOutputCb(); // SRS - Call displayLinkOutputCb() to animate frames vs. renderFrame() for static image
|
2017-04-14 12:00:05 -04:00
|
|
|
return kCVReturnSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 02:34:38 -04:00
|
|
|
MVKExample* _mvkExample;
|
2017-04-14 12:00:05 -04:00
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
#pragma mark DemoViewController
|
|
|
|
|
|
|
|
|
|
@implementation DemoViewController {
|
|
|
|
|
CVDisplayLinkRef _displayLink;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Since this is a single-view app, initialize Vulkan during view loading. */
|
|
|
|
|
-(void) viewDidLoad {
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
|
|
|
|
|
self.view.wantsLayer = YES; // Back the view with a layer created by the makeBackingLayer method.
|
|
|
|
|
|
2017-06-22 14:53:49 -04:00
|
|
|
_mvkExample = new MVKExample(self.view);
|
2017-04-14 12:00:05 -04:00
|
|
|
|
|
|
|
|
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
|
2017-06-22 14:53:49 -04:00
|
|
|
CVDisplayLinkSetOutputCallback(_displayLink, &DisplayLinkCallback, _mvkExample);
|
2017-04-14 12:00:05 -04:00
|
|
|
CVDisplayLinkStart(_displayLink);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-13 11:58:46 -04:00
|
|
|
// SRS - Center the window and set VulkanExampleBase::refreshPeriod from the active displayLink
|
2021-09-15 16:19:34 -04:00
|
|
|
-(void) viewWillAppear {
|
|
|
|
|
[super viewWillAppear];
|
|
|
|
|
|
2022-05-04 00:43:14 -04:00
|
|
|
NSWindow* window = self.view.window;
|
|
|
|
|
[window center];
|
2021-09-12 02:34:38 -04:00
|
|
|
|
2022-05-04 00:43:14 -04:00
|
|
|
_mvkExample->setRefreshPeriod(CVDisplayLinkGetActualOutputVideoRefreshPeriod(_displayLink));
|
2021-09-12 02:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 12:00:05 -04:00
|
|
|
-(void) dealloc {
|
|
|
|
|
CVDisplayLinkRelease(_displayLink);
|
2017-06-22 14:53:49 -04:00
|
|
|
delete _mvkExample;
|
2017-04-14 12:00:05 -04:00
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
#pragma mark DemoView
|
|
|
|
|
|
|
|
|
|
@implementation DemoView
|
|
|
|
|
|
|
|
|
|
/** Indicates that the view wants to draw using the backing layer instead of using drawRect:. */
|
|
|
|
|
-(BOOL) wantsUpdateLayer { return YES; }
|
|
|
|
|
|
|
|
|
|
/** Returns a Metal-compatible layer. */
|
|
|
|
|
+(Class) layerClass { return [CAMetalLayer class]; }
|
|
|
|
|
|
|
|
|
|
/** If the wantsLayer property is set to YES, this method will be invoked to return a layer instance. */
|
2017-06-05 16:26:29 -04:00
|
|
|
-(CALayer*) makeBackingLayer {
|
|
|
|
|
CALayer* layer = [self.class.layerClass layer];
|
|
|
|
|
CGSize viewScale = [self convertSizeToBacking: CGSizeMake(1.0, 1.0)];
|
|
|
|
|
layer.contentsScale = MIN(viewScale.width, viewScale.height);
|
|
|
|
|
return layer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(BOOL) acceptsFirstResponder { return YES; }
|
2017-04-14 12:00:05 -04:00
|
|
|
|
2021-09-12 02:34:38 -04:00
|
|
|
// SRS - Handle keyboard events
|
|
|
|
|
-(void) keyDown:(NSEvent*) theEvent {
|
2022-05-13 11:58:46 -04:00
|
|
|
NSString *text = [theEvent charactersIgnoringModifiers];
|
|
|
|
|
unichar keychar = (text.length > 0) ? [text characterAtIndex: 0] : 0;
|
|
|
|
|
_mvkExample->keyDown(keychar);
|
2021-09-12 02:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) keyUp:(NSEvent*) theEvent {
|
2022-05-13 11:58:46 -04:00
|
|
|
NSString *text = [theEvent charactersIgnoringModifiers];
|
|
|
|
|
unichar keychar = (text.length > 0) ? [text characterAtIndex: 0] : 0;
|
|
|
|
|
_mvkExample->keyUp(keychar);
|
2021-09-12 02:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SRS - Handle mouse events
|
|
|
|
|
-(NSPoint) getMouseLocalPoint:(NSEvent*) theEvent {
|
|
|
|
|
NSPoint location = [theEvent locationInWindow];
|
|
|
|
|
NSPoint point = [self convertPoint:location fromView:nil];
|
|
|
|
|
point.y = self.frame.size.height - point.y;
|
|
|
|
|
return point;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) mouseDown:(NSEvent*) theEvent {
|
|
|
|
|
auto point = [self getMouseLocalPoint:theEvent];
|
|
|
|
|
_mvkExample->mouseDown(point.x, point.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) mouseUp:(NSEvent*) theEvent {
|
2022-05-13 11:58:46 -04:00
|
|
|
_mvkExample->mouseUp();
|
2021-09-12 02:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-15 16:19:34 -04:00
|
|
|
-(void) rightMouseDown:(NSEvent*) theEvent {
|
2022-05-13 11:58:46 -04:00
|
|
|
auto point = [self getMouseLocalPoint:theEvent];
|
|
|
|
|
_mvkExample->rightMouseDown(point.x, point.y);
|
2021-09-15 16:19:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) rightMouseUp:(NSEvent*) theEvent {
|
|
|
|
|
_mvkExample->rightMouseUp();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 02:34:38 -04:00
|
|
|
-(void) otherMouseDown:(NSEvent*) theEvent {
|
2022-05-13 11:58:46 -04:00
|
|
|
auto point = [self getMouseLocalPoint:theEvent];
|
|
|
|
|
_mvkExample->otherMouseDown(point.x, point.y);
|
2021-09-12 02:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) otherMouseUp:(NSEvent*) theEvent {
|
|
|
|
|
_mvkExample->otherMouseUp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) mouseDragged:(NSEvent*) theEvent {
|
|
|
|
|
auto point = [self getMouseLocalPoint:theEvent];
|
|
|
|
|
_mvkExample->mouseDragged(point.x, point.y);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 16:19:34 -04:00
|
|
|
-(void) rightMouseDragged:(NSEvent*) theEvent {
|
|
|
|
|
auto point = [self getMouseLocalPoint:theEvent];
|
|
|
|
|
_mvkExample->mouseDragged(point.x, point.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) otherMouseDragged:(NSEvent*) theEvent {
|
|
|
|
|
auto point = [self getMouseLocalPoint:theEvent];
|
|
|
|
|
_mvkExample->mouseDragged(point.x, point.y);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 02:34:38 -04:00
|
|
|
-(void) scrollWheel:(NSEvent*) theEvent {
|
|
|
|
|
short wheelDelta = [theEvent deltaY];
|
|
|
|
|
_mvkExample->scrollWheel(wheelDelta);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 12:00:05 -04:00
|
|
|
@end
|