1

I am currently programming an application using glfw in MacOS X. My only problem is that the application doesn't use an AppDelegate, but does all the initialization in the main.cpp file like a command line utility. I specifically want to implement the function

- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;

and I already registered the file extensions in the plist file and when I try to open them I get an error message saying "The document "Doc.xxx" could not be opened. MyApp cannot open files in the "xxx" format.

I attempted to create a singleton that sets itself as the delegate but it just doesn't call the function. Can anyone comment on the possibility of creating a class and implementing the NSApplicationDelegate functions?

Andres Bucci
  • 947
  • 1
  • 9
  • 37
  • Why not use NSOpenGLView inside a normal Cocoa application? – BlamKiwi Aug 28 '14 at 11:31
  • The application is multiplatform, and it was built using glfw. If I alter this flow I have to maintain a separate version from the rest of the developers on my team. I know how to do it in pure objective-C – Andres Bucci Aug 28 '14 at 14:01

1 Answers1

1

Basically there is nothing in your code to receive events from the OS and to pump the events received.

The solution, according to another stack overflow Q/A, appears to be to invoke NSApplication manually. You still need to decide where to hook your OpenGL render loop into as [NSApp run] doesn't return until the app finishes. Another solution could be to make a custom NSApplication replacement. http://www.cocoawithlove.com/2009/01/demystifying-nsapplication-by.html

Copied pretty much verbatim from this answer taking into account the modifications for ARC: https://stackoverflow.com/a/2154666/1874323

@autoreleasepool { 
    AppDelegate * delegate = [[AppDelegate alloc] init];

    NSApplication * application = [NSApplication sharedApplication];
    [application setDelegate:delegate];
    [NSApp run]; // Note this never returns until the app finishes, so you need to decide where to hook your code into
}

As always you can find documentation from the Apple website: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/Reference/Reference.html

Community
  • 1
  • 1
BlamKiwi
  • 2,093
  • 2
  • 19
  • 30
  • This is as far as I've gotten it myself, but (As per your comment) [NSApp run] doesn't return so the rest of the code never runs. – Andres Bucci Aug 28 '14 at 14:02
  • I know some developers use an NSTimer or CVDisplayLink trick to get their game loop to happen. NSTimer will be driven by the main thread event queue whereas CVDisplayLink is linked to the hardware of the machine. https://developer.apple.com/library/mac/qa/qa1385/_index.html – BlamKiwi Aug 28 '14 at 22:16
  • I ended up removing glfw and implementing it like this, it breaks compatibility for other platforms since my main is different, but It's the only way to get application delegate methods working. – Andres Bucci Sep 01 '14 at 09:55