From 206b1c54f7ff7ec339854c8ec7b0dfd87c2bbea0 Mon Sep 17 00:00:00 2001 From: Karl Schultz Date: Fri, 13 Apr 2018 18:02:07 -0600 Subject: macos: Pass argc/argv through to main cube code This allows users to specify arguments like "--c 100" --- demos/macOS/cube/DemoViewController.m | 37 +++++++++++++++++++++++++++++--- demos/macOS/cubepp/DemoViewController.mm | 36 ++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 6 deletions(-) (limited to 'demos/macOS') diff --git a/demos/macOS/cube/DemoViewController.m b/demos/macOS/cube/DemoViewController.m index a5fe6905..eb28ad57 100644 --- a/demos/macOS/cube/DemoViewController.m +++ b/demos/macOS/cube/DemoViewController.m @@ -29,6 +29,7 @@ @implementation DemoViewController { CVDisplayLinkRef _displayLink; struct demo demo; + NSTimer* _timer; } - (void)dealloc { @@ -43,19 +44,49 @@ self.view.wantsLayer = YES; // Back the view with a layer created by the makeBackingLayer method. - demo_main(&demo, self.view); + // Convert incoming args to "C" argc/argv strings + NSArray *args = [[NSProcessInfo processInfo] arguments]; + const char** argv = (const char**) alloca(sizeof(char*) * args.count); + for(unsigned int i = 0; i < args.count; i++) { + NSString *s = args[i]; + argv[i] = s.UTF8String; + } + demo_main(&demo, self.view, args.count, argv); + + // Monitor the rendering loop for a quit condition + _timer = [NSTimer scheduledTimerWithTimeInterval: 0.2 + target: self + selector: @selector(onTick:) + userInfo: self + repeats: YES]; + + // Start the rendering loop CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); CVDisplayLinkSetOutputCallback(_displayLink, &DisplayLinkCallback, &demo); CVDisplayLinkStart(_displayLink); + +} + +// Close the window if the demo is in a Quit state +-(void)onTick:(NSTimer*)timer { + if (demo.quit) { + [[[self view] window] close]; + } } #pragma mark Display loop callback function /** Rendering loop callback function for use with a CVDisplayLink. */ -static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, +static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, + const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* target) { - demo_draw((struct demo*)target); + struct demo* demo = (struct demo*)target; + demo_run(demo); + if (demo->quit) { + CVDisplayLinkStop(displayLink); + CVDisplayLinkRelease(displayLink); + } return kCVReturnSuccess; } diff --git a/demos/macOS/cubepp/DemoViewController.mm b/demos/macOS/cubepp/DemoViewController.mm index a0933592..049ec6b6 100644 --- a/demos/macOS/cubepp/DemoViewController.mm +++ b/demos/macOS/cubepp/DemoViewController.mm @@ -29,6 +29,7 @@ @implementation DemoViewController { CVDisplayLinkRef _displayLink; struct Demo demo; + NSTimer* _timer; } - (void)dealloc { @@ -43,19 +44,48 @@ self.view.wantsLayer = YES; // Back the view with a layer created by the makeBackingLayer method. - demo_main(demo, self.view); + // Convert incoming args to "C" argc/argv strings + NSArray *args = [[NSProcessInfo processInfo] arguments]; + const char** argv = (const char**) alloca(sizeof(char*) * args.count); + for(unsigned int i = 0; i < args.count; i++) { + NSString *s = args[i]; + argv[i] = s.UTF8String; + } + demo_main(demo, self.view, args.count, argv); + + // Monitor the rendering loop for a quit condition + _timer = [NSTimer scheduledTimerWithTimeInterval: 0.2 + target: self + selector: @selector(onTick:) + userInfo: self + repeats: YES]; + + // Start the rendering loop CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); CVDisplayLinkSetOutputCallback(_displayLink, &DisplayLinkCallback, &demo); CVDisplayLinkStart(_displayLink); } +// Close the window if the demo is in a Quit state +-(void)onTick:(NSTimer*)timer { + if (demo.quit) { + [[[self view] window] close]; + } +} + #pragma mark Display loop callback function /** Rendering loop callback function for use with a CVDisplayLink. */ -static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, +static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, + const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* target) { - ((struct Demo*)target)->draw(); + struct Demo* demo = (struct Demo*)target; + demo->run(); + if (demo->quit) { + CVDisplayLinkStop(displayLink); + CVDisplayLinkRelease(displayLink); + } return kCVReturnSuccess; } -- cgit v1.2.3