From 0a38abe5abcf9c256795b6f872b37ebddfd642c4 Mon Sep 17 00:00:00 2001 From: Palana Date: Mon, 14 Apr 2014 21:24:57 +0200 Subject: [PATCH] Make OSX test application more idiomatic and add rendering callback --- test/osx/test.mm | 194 +++++++++++++++++++++-------------------------- 1 file changed, 86 insertions(+), 108 deletions(-) diff --git a/test/osx/test.mm b/test/osx/test.mm index d2abfc925..bc346d538 100644 --- a/test/osx/test.mm +++ b/test/osx/test.mm @@ -14,15 +14,13 @@ static const int cx = 800; static const int cy = 600; - - /* --------------------------------------------------- */ template struct OBSUniqueHandle : std::unique_ptr> { using base = std::unique_ptr>; - explicit OBSUniqueHandle(T *obj) : base(obj, D) {} + explicit OBSUniqueHandle(T *obj=nullptr) : base(obj, D) {} operator T*() { return base::get(); } }; @@ -38,7 +36,7 @@ using SceneContext = OBSUniqueHandle +{ + NSWindow *win; + NSView *view; + SceneContext scene; +} +- (void)applicationDidFinishLaunching:(NSNotification*)notification; +- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app; +- (void)windowWillClose:(NSNotification*)notification; @end -@implementation window_closer - -+(id)window_closer +@implementation OBSTest +- (void)applicationDidFinishLaunching:(NSNotification*)notification { - return [[window_closer alloc] init]; -} + UNUSED_PARAMETER(notification); --(void)windowWillClose:(NSNotification *)notification -{ - (void)notification; - [NSApp stop:self]; -} -@end - -static NSWindow *CreateTestWindow() -{ - [NSApplication sharedApplication]; - - ProcessSerialNumber psn = {0, kCurrentProcess}; - TransformProcessType(&psn, kProcessTransformToForegroundApplication); -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - SetFrontProcess(&psn); -#pragma clang diagnostic pop - - NSRect content_rect = NSMakeRect(0, 0, cx, cy); - NSWindow *win = [[NSWindow alloc] - initWithContentRect:content_rect - styleMask:NSTitledWindowMask | NSClosableWindowMask - backing:NSBackingStoreBuffered - defer:NO]; - if(!win) - return nil; - - [win orderFrontRegardless]; - - NSView *view = [[NSView alloc] initWithFrame:content_rect]; - if(!view) - return nil; - - - [win setContentView:view]; - [win setTitle:@"foo"]; - [win center]; - [win makeMainWindow]; - [win setDelegate:[window_closer window_closer]]; - return win; -} - -static void test() -{ try { - static NSWindow *win = CreateTestWindow(); + NSRect content_rect = NSMakeRect(0, 0, cx, cy); + win = [[NSWindow alloc] + initWithContentRect:content_rect + styleMask:NSTitledWindowMask | NSClosableWindowMask + backing:NSBackingStoreBuffered + defer:NO]; if (!win) - throw "Couldn't create main window"; + throw "Could not create window"; - CreateOBS(win); + view = [[NSView alloc] initWithFrame:content_rect]; + if (!view) + throw "Could not create view"; - /* ------------------------------------------------------ */ - /* load module */ - if (obs_load_module("test-input") != 0) - throw "Couldn't load module"; + win.title = @"foo"; + win.delegate = self; + win.contentView = view; - /* ------------------------------------------------------ */ - /* create source */ - SourceContext source{obs_source_create(OBS_SOURCE_TYPE_INPUT, - "random", "a test source", NULL)}; - if (!source) - throw "Couldn't create random test source"; + [win orderFrontRegardless]; + [win center]; + [win makeMainWindow]; - /* ------------------------------------------------------ */ - /* create filter */ - SourceContext filter{obs_source_create(OBS_SOURCE_TYPE_FILTER, - "test", "a test filter", NULL)}; - if (!filter) - throw "Couldn't create test filter"; - obs_source_filter_add(source, filter); + CreateOBS(view); - /* ------------------------------------------------------ */ - /* create scene and add source to scene (twice) */ - SceneContext scene{obs_scene_create("test scene")}; - if (!scene) - throw "Couldn't create scene"; + scene = SetupScene(); - AddTestItems(scene, source); - - /* ------------------------------------------------------ */ - /* set the scene as the primary draw source and go */ - obs_set_output_source(0, obs_scene_getsource(scene)); - - [NSApp run]; - - obs_set_output_source(0, NULL); + obs_add_draw_callback( + [](void *, uint32_t, uint32_t) { + obs_render_main_view(); + }, nullptr); } catch (char const *error) { - printf("%s\n", error); + NSLog(@"%s\n", error); + + [NSApp terminate:nil]; } +} + +- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app +{ + UNUSED_PARAMETER(app); + + return YES; +} + +- (void)windowWillClose:(NSNotification*)notification +{ + UNUSED_PARAMETER(notification); + + obs_set_output_source(0, nullptr); + scene.reset(); obs_shutdown(); - - blog(LOG_INFO, "Number of memory leaks: %lu", bnum_allocs()); + NSLog(@"Number of memory leaks: %lu", bnum_allocs()); } +@end /* --------------------------------------------------- */ int main() { @autoreleasepool { - test(); + [NSApplication sharedApplication]; + OBSTest *test = [[OBSTest alloc] init]; + [NSApp setDelegate:test]; + + [NSApp run]; } return 0;