I'm trying to create a minimal application in order for me to start a game engine from scratch. Here is the code:
#import <Cocoa/Cocoa.h>
int main (int argc, const char * argv[]){
NSWindow *window = [[NSWindow alloc] init];
[window makeKeyAndOrderFront:nil];
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false)
while (1);
return 0;
}
I would how to display a window without calling CFRunLoopRunInMode().
Xcode 10.1
MacOS 10.14.3
I think I have found the answer myself. The window does not appear unless it receives the nextEventMatchingMask: message. This is probably what triggers the window in a CFRunLoop and is what I wanted to know, although it would be nice if I could dig deeper. For now, I'm happy with the following solution.
#import <Cocoa/Cocoa.h>
int main (int argc, const char * argv[]){
@autoreleasepool {
// Create a default window
NSWindow *window = [[NSWindow alloc] init];
// Make it blue just for better visibility
[window setBackgroundColor:[NSColor blueColor]];
// Bring to front and make it key
[window makeKeyAndOrderFront:nil];
// Custom run loop
NSEvent* event;
while(1) {
do {
event = [window nextEventMatchingMask:NSEventMaskAny]; //window shows now
if ([event type] == NSEventTypeLeftMouseDown) {
NSLog(@"Mouse down");
}
else {
NSLog(@"Something happened");
}
} while (event != nil);
}
}
return 0;
}
I don't have a reference for that. I can only refer to this article: Handmade Hero for mac in which the window appears due to a similar method. That was not good enough for me because such a method involves NSApp, which I would like to avoid, if possible.