diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index c6a23bfd..028920df 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -242,7 +242,11 @@ void VulkanExampleBase::nextFrame() frameCounter++; auto tEnd = std::chrono::high_resolution_clock::now(); auto tDiff = std::chrono::duration(tEnd - tStart).count(); +#if defined(VK_USE_PLATFORM_MACOS_MVK) + frameTimer = (float)tDiff / 100.0f; // SRS - Divide by 100 for macOS - perhaps shorter tDiff due to background rendering? +#else frameTimer = (float)tDiff / 1000.0f; +#endif camera.update(frameTimer); if (camera.moving()) { diff --git a/xcode/MVKExample.cpp b/xcode/MVKExample.cpp index 4ecf8f3e..07bf519d 100644 --- a/xcode/MVKExample.cpp +++ b/xcode/MVKExample.cpp @@ -5,20 +5,109 @@ * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ +#include #include "MVKExample.h" #include "examples.h" -void MVKExample::renderFrame() { +/* +void MVKExample::renderFrame() { // SRS - don't need to expose VulkanExampleBase::renderFrame() to DemoViewController _vulkanExample->renderFrame(); } +*/ void MVKExample::displayLinkOutputCb() { // SRS - expose VulkanExampleBase::displayLinkOutputCb() to DemoViewController _vulkanExample->displayLinkOutputCb(); } -void MVKExample::keyPressed(uint32_t keyCode) { - _vulkanExample->keyPressed(keyCode); +void MVKExample::windowWillResize(float x, float y) +{ + _vulkanExample->windowWillResize(x, y); +} + +void MVKExample::windowDidResize() +{ + _vulkanExample->windowDidResize(); +} + +void MVKExample::keyDown(uint32_t keyCode) { + switch (keyCode) + { + case kVK_ANSI_P: + _vulkanExample->paused = !_vulkanExample->paused; + break; + case kVK_ANSI_W: + _vulkanExample->camera.keys.up = true; + break; + case kVK_ANSI_S: + _vulkanExample->camera.keys.down = true; + break; + case kVK_ANSI_A: + _vulkanExample->camera.keys.left = true; + break; + case kVK_ANSI_D: + _vulkanExample->camera.keys.right = true; + break; + default: + break; + } +} + +void MVKExample::keyUp(uint32_t keyCode) { + switch (keyCode) + { + case kVK_ANSI_W: + _vulkanExample->camera.keys.up = false; + break; + case kVK_ANSI_S: + _vulkanExample->camera.keys.down = false; + break; + case kVK_ANSI_A: + _vulkanExample->camera.keys.left = false; + break; + case kVK_ANSI_D: + _vulkanExample->camera.keys.right = false; + break; + default: + break; + } +} + +void MVKExample::mouseDown(double x, double y) +{ + _vulkanExample->mousePos = glm::vec2(x, y); + _vulkanExample->mouseButtons.left = true; +} + +void MVKExample::mouseUp(double x, double y) +{ + _vulkanExample->mousePos = glm::vec2(x, y); + _vulkanExample->mouseButtons.left = false; +} + +void MVKExample::otherMouseDown() +{ + _vulkanExample->mouseButtons.right = true; +} + +void MVKExample::otherMouseUp() +{ + _vulkanExample->mouseButtons.right = false; +} + +void MVKExample::mouseDragged(double x, double y) +{ + _vulkanExample->mouseDragged(x, y); +} + +void MVKExample::mouseMoved(double x, double y) +{ + _vulkanExample->mouseDragged(x, y); +} + +void MVKExample::scrollWheel(short wheelDelta) +{ + _vulkanExample->camera.translate(glm::vec3(0.0f, 0.0f, wheelDelta * 0.05f * _vulkanExample->camera.movementSpeed)); } MVKExample::MVKExample(void* view) { diff --git a/xcode/MVKExample.h b/xcode/MVKExample.h index f926d529..64a4407d 100644 --- a/xcode/MVKExample.h +++ b/xcode/MVKExample.h @@ -13,9 +13,22 @@ class MVKExample { public: - void renderFrame(); + //void renderFrame(); // SRS - don't need to expose VulkanExampleBase::renderFrame() to DemoViewController void displayLinkOutputCb(); // SRS - expose VulkanExampleBase::displayLinkOutputCb() to DemoViewController - void keyPressed(uint32_t keyCode); + + void windowWillResize(float x, float y); // SRS - expose window resize events to DemoViewController + void windowDidResize(); + + void keyDown(uint32_t keyCode); // SRS - expose keyboard events to DemoViewController + void keyUp(uint32_t keyCode); + + void mouseDown(double x, double y); // SRS - expose mouse events to DemoViewController + void mouseUp(double x, double y); + void otherMouseDown(); + void otherMouseUp(); + void mouseDragged(double x, double y); + void mouseMoved(double x, double y); + void scrollWheel(short wheelDelta); MVKExample(void* view); ~MVKExample(); diff --git a/xcode/examples.h b/xcode/examples.h index 94c7f59a..975f6f87 100644 --- a/xcode/examples.h +++ b/xcode/examples.h @@ -39,7 +39,7 @@ # include "../examples/texture/texture.cpp" #endif -// Does not run. Metal does not support passing matrices between shader stages. +// Does not run. Metal does not support passing matrices between shader stages. Update: runs on macOS Big Sur with Vulksn SDK 1.2.189.0 #ifdef MVK_texturecubemap # include "../examples/texturecubemap/texturecubemap.cpp" #endif @@ -58,7 +58,7 @@ # include "../examples/dynamicuniformbuffer/dynamicuniformbuffer.cpp" #endif -// Does not run. Metal does not support passing arrays between shader stages. +// Does not run. Metal does not support passing arrays between shader stages. Update: runs on macOS Big Sur with Vulksn SDK 1.2.189.0 #ifdef MVK_pushconstants # include "../examples/pushconstants/pushconstants.cpp" #endif @@ -102,7 +102,7 @@ # include "../examples/indirectdraw/indirectdraw.cpp" #endif -// Does not run. Metal does not support passing matrices between shader stages. +// Does not run. Metal does not support passing matrices between shader stages. Update: runs on macOS Big Sur with Vulksn SDK 1.2.189.0 #ifdef MVK_hdr # include "../examples/hdr/hdr.cpp" #endif @@ -111,7 +111,7 @@ # include "../examples/occlusionquery/occlusionquery.cpp" #endif -// Does not run. Sampler arrays require Metal 2. +// Does not run. Sampler arrays require Metal 2. Update: runs on macOS Big Sur with Vulksn SDK 1.2.189.0 #ifdef MVK_texturemipmapgen # include "../examples/texturemipmapgen/texturemipmapgen.cpp" #endif @@ -128,9 +128,9 @@ # include "../examples/shadowmappingomni/shadowmappingomni.cpp" #endif -#ifdef MVK_skeletalanimation -# include "../examples/skeletalanimation/skeletalanimation.cpp" -#endif +//#ifdef MVK_skeletalanimation +//# include "../examples/skeletalanimation/skeletalanimation.cpp" +//#endif #ifdef MVK_bloom # include "../examples/bloom/bloom.cpp" diff --git a/xcode/macos/DemoViewController.mm b/xcode/macos/DemoViewController.mm index 15f7dbc0..3f1939ce 100644 --- a/xcode/macos/DemoViewController.mm +++ b/xcode/macos/DemoViewController.mm @@ -26,12 +26,12 @@ static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, return kCVReturnSuccess; } +MVKExample* _mvkExample; #pragma mark - #pragma mark DemoViewController @implementation DemoViewController { - MVKExample* _mvkExample; CVDisplayLinkRef _displayLink; } @@ -48,17 +48,24 @@ static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, CVDisplayLinkStart(_displayLink); } +// SRS - Handle window resize events +-(NSSize) windowWillResize:(NSWindow*) sender toSize:(NSSize)frameSize { + CVDisplayLinkStop(_displayLink); + _mvkExample->windowWillResize(frameSize.width, frameSize.height); + return frameSize; +} + +-(void) windowDidResize:(NSNotification*) notification { + _mvkExample->windowDidResize(); + CVDisplayLinkStart(_displayLink); +} + -(void) dealloc { CVDisplayLinkRelease(_displayLink); delete _mvkExample; [super dealloc]; } -// Handle keyboard input --(void) keyDown:(NSEvent*) theEvent { - _mvkExample->keyPressed(theEvent.keyCode); -} - @end @@ -83,4 +90,54 @@ static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, -(BOOL) acceptsFirstResponder { return YES; } +// SRS - Handle keyboard events +-(void) keyDown:(NSEvent*) theEvent { + _mvkExample->keyDown(theEvent.keyCode); +} + +-(void) keyUp:(NSEvent*) theEvent { + _mvkExample->keyUp(theEvent.keyCode); +} + +// 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 { + auto point = [self getMouseLocalPoint:theEvent]; + _mvkExample->mouseUp(point.x, point.y); +} + +-(void) otherMouseDown:(NSEvent*) theEvent { + _mvkExample->otherMouseDown(); +} + +-(void) otherMouseUp:(NSEvent*) theEvent { + _mvkExample->otherMouseUp(); +} + +-(void) mouseDragged:(NSEvent*) theEvent { + auto point = [self getMouseLocalPoint:theEvent]; + _mvkExample->mouseDragged(point.x, point.y); +} + +-(void) mouseMoved:(NSEvent*) theEvent { + auto point = [self getMouseLocalPoint:theEvent]; + _mvkExample->mouseDragged(point.x, point.y); +} + +-(void) scrollWheel:(NSEvent*) theEvent { + short wheelDelta = [theEvent deltaY]; + _mvkExample->scrollWheel(wheelDelta); +} + @end