Handle keyboard and mouse events, adjust frameTimer scaling for macOS

This commit is contained in:
Stephen Saunders 2021-09-12 02:34:38 -04:00
parent 28d98b9ee7
commit a74afb20fc
5 changed files with 181 additions and 18 deletions

View file

@ -5,20 +5,109 @@
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <Carbon/Carbon.h>
#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) {

View file

@ -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();

View file

@ -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"

View file

@ -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