From 6149e30699b36901715d46a5cef8959625ef399b Mon Sep 17 00:00:00 2001 From: Jake Turner Date: Tue, 18 May 2021 05:55:51 +0100 Subject: Trigger demo quit from applicationWillTerminate Allows for a clean shutdown of the rendering thread (CVDisplayLink) whilst the main thread is exiting --- cube/macOS/cube/AppDelegate.m | 11 ++++++++++- cube/macOS/cube/DemoViewController.h | 3 +++ cube/macOS/cube/DemoViewController.m | 8 ++++++-- cube/macOS/cubepp/AppDelegate.mm | 11 ++++++++++- cube/macOS/cubepp/DemoViewController.h | 3 +++ cube/macOS/cubepp/DemoViewController.mm | 8 ++++++-- 6 files changed, 38 insertions(+), 6 deletions(-) (limited to 'cube/macOS') diff --git a/cube/macOS/cube/AppDelegate.m b/cube/macOS/cube/AppDelegate.m index 2a7bcaff..c1ee96b4 100644 --- a/cube/macOS/cube/AppDelegate.m +++ b/cube/macOS/cube/AppDelegate.m @@ -17,6 +17,7 @@ */ #import "AppDelegate.h" +#import "DemoViewController.h" @interface AppDelegate () @@ -29,7 +30,15 @@ } - (void)applicationWillTerminate:(NSNotification *)aNotification { - // Insert code here to tear down your application + NSApplication *app = aNotification.object; + NSArray *windows = app.windows; + for (NSUInteger i = 0; i < windows.count; ++i) { + NSViewController *viewController = windows[i].contentViewController; + if ([viewController isKindOfClass:[DemoViewController class]]) { + [(DemoViewController *)viewController quit]; + break; + } + } } - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { diff --git a/cube/macOS/cube/DemoViewController.h b/cube/macOS/cube/DemoViewController.h index 7f90cc7e..6d7ca215 100644 --- a/cube/macOS/cube/DemoViewController.h +++ b/cube/macOS/cube/DemoViewController.h @@ -23,6 +23,9 @@ /** The main view controller for the demo storyboard. */ @interface DemoViewController : NSViewController + +- (void)quit; + @end #pragma mark - diff --git a/cube/macOS/cube/DemoViewController.m b/cube/macOS/cube/DemoViewController.m index c6e229d3..058547d9 100644 --- a/cube/macOS/cube/DemoViewController.m +++ b/cube/macOS/cube/DemoViewController.m @@ -33,11 +33,15 @@ } - (void)dealloc { - demo_cleanup(&demo); - CVDisplayLinkRelease(_displayLink); + [self quit]; [super dealloc]; } +- (void)quit { + CVDisplayLinkRelease(_displayLink); + demo_cleanup(&demo); +} + /** Since this is a single-view app, initialize Vulkan during view loading. */ - (void)viewDidLoad { [super viewDidLoad]; diff --git a/cube/macOS/cubepp/AppDelegate.mm b/cube/macOS/cubepp/AppDelegate.mm index 2a7bcaff..c1ee96b4 100644 --- a/cube/macOS/cubepp/AppDelegate.mm +++ b/cube/macOS/cubepp/AppDelegate.mm @@ -17,6 +17,7 @@ */ #import "AppDelegate.h" +#import "DemoViewController.h" @interface AppDelegate () @@ -29,7 +30,15 @@ } - (void)applicationWillTerminate:(NSNotification *)aNotification { - // Insert code here to tear down your application + NSApplication *app = aNotification.object; + NSArray *windows = app.windows; + for (NSUInteger i = 0; i < windows.count; ++i) { + NSViewController *viewController = windows[i].contentViewController; + if ([viewController isKindOfClass:[DemoViewController class]]) { + [(DemoViewController *)viewController quit]; + break; + } + } } - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { diff --git a/cube/macOS/cubepp/DemoViewController.h b/cube/macOS/cubepp/DemoViewController.h index 7f90cc7e..6d7ca215 100644 --- a/cube/macOS/cubepp/DemoViewController.h +++ b/cube/macOS/cubepp/DemoViewController.h @@ -23,6 +23,9 @@ /** The main view controller for the demo storyboard. */ @interface DemoViewController : NSViewController + +- (void)quit; + @end #pragma mark - diff --git a/cube/macOS/cubepp/DemoViewController.mm b/cube/macOS/cubepp/DemoViewController.mm index 9e003b9b..7bc4681f 100644 --- a/cube/macOS/cubepp/DemoViewController.mm +++ b/cube/macOS/cubepp/DemoViewController.mm @@ -33,11 +33,15 @@ } - (void)dealloc { - demo.cleanup(); - CVDisplayLinkRelease(_displayLink); + [self quit]; [super dealloc]; } +- (void)quit { + CVDisplayLinkRelease(_displayLink); + demo.cleanup(); +} + /** Since this is a single-view app, initialize Vulkan during view loading. */ - (void)viewDidLoad { [super viewDidLoad]; -- cgit v1.2.3