{$mode objfpc}
{$modeswitch objectivec1}
uses CocoaAll, ctypes, MacOSAll;
type
TCocoaApp = objcclass (NSApplication)
procedure poll; message 'poll';
end;
TCocoaAppDelegate = objcclass (NSObject, NSApplicationDelegateProtocol)
end;
procedure TCocoaApp.poll;
var
event: NSEvent;
pool: NSAutoreleasePool;
begin
pool := NSAutoreleasePool.alloc.init;
event := nextEventMatchingMask_untilDate_inMode_dequeue(NSAnyEventMask, {NSDate.distantPast}nil, NSDefaultRunLoopMode, true);
if event <> nil then begin
sendEvent(event);
updateWindows;
end;
pool.release;
end;
procedure SetupMainMenu;
procedure AddMenu (menu: NSMenu);
var
menuItem: NSMenuItem;
begin
menuItem := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(menu.title, nil, NSSTR('')).autorelease;
menuItem.setSubmenu(menu);
NSApp.mainMenu.addItem(menuItem);
end;
var
mainMenu: NSMenu;
appleMenu: NSMenu;
windowMenu: NSMenu;
begin
// main menu
mainMenu := NSMenu.alloc.init.autorelease;
NSApp.setMainMenu(mainMenu);
// apple menu
appleMenu := NSMenu.alloc.initWithTitle(NSSTR('')).autorelease;
appleMenu.addItemWithTitle_action_keyEquivalent(NSSTR('Quit '+NSProcessInfo.processInfo.processName.UTF8String), objcselector('terminate:'), NSSTR('q'));
AddMenu(appleMenu);
// window menu
windowMenu := NSMenu.alloc.initWithTitle(NSSTR('Window')).autorelease;
windowMenu.addItemWithTitle_action_keyEquivalent(NSSTR('Minimize'), objcselector('performMiniaturize:'), NSSTR('m'));
windowMenu.addItemWithTitle_action_keyEquivalent(NSSTR('Zoom'), objcselector('performZoom:'), NSSTR(''));
AddMenu(windowMenu);
end;
procedure Cocoa_Init;
var
pool: NSAutoreleasePool;
app: TCocoaApp;
delegate: TCocoaAppDelegate;
begin
// https://hero.handmade.network/forums/code-discussion/t/1409-main_game_loop_on_os_x
pool := NSAutoreleasePool.alloc.init;
app := TCocoaApp(TCocoaApp.sharedApplication);
delegate := TCocoaAppDelegate.alloc.init;
app.setDelegate(delegate);
NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
NSApp.activateIgnoringOtherApps(true);
if NSApp.mainMenu = nil then SetupMainMenu;
app.finishLaunching;
pool.release;
end;
procedure Cocoa_PollEvents;
begin
// note: do we really need to call on main thread? isn't the first thread "main"?
TCocoaApp(TCocoaApp.sharedApplication).poll;
NSApp.performSelectorOnMainThread_withObject_waitUntilDone(objcselector('poll'), nil, true);
end;
procedure Cocoa_CreateWindow(width, height: integer; title: PChar);
const
NSWindowCollectionBehaviorFullScreenPrimary = 1 shl 7 { available in 10_7 };
var
window: NSWindow;
windowFlags: NSUInteger = 0;
begin
windowFlags := NSTitledWindowMask + NSClosableWindowMask + NSMiniaturizableWindowMask + NSResizableWindowMask;
window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(123, 123, width, height ), windowFlags, NSBackingStoreBuffered, false);
window.setTitle(NSSTR(title));
window.setCollectionBehavior(NSWindowCollectionBehaviorFullScreenPrimary);
window.setAcceptsMouseMovedEvents(true);
window.makeKeyAndOrderFront(nil);
end;
begin
Cocoa_Init;
Cocoa_CreateWindow(640,480,'test');
repeat
Cocoa_PollEvents;
until false;
end.