The frameworks actually do a great deal of work for you. The simple call to NSApplicationMain()
in your main.m
does things such as fire up your main application class (as determined by the Info.plist
), load your main nib
, load main windows and menus, set up a default memory autorelease pool, and enters the run loop (and more!).
It's good to understand what "magic" is going on underneath the covers, but I don't see anything wrong with letting the frameworks do a lot of the grunt work for you. And IB is a n especially nice UI editor, it would be a shame not to use it! Anyway, since you asked...
You should read up on the following docs from Apple's Developer Connection site:
So at a minimum, to do all this programmatically, you would need to do something like the following code. Note that this is a rough start, and does not create an application delegate, or add anything to the menus. This is left as an exercise to the reader! But the code below does basically work in the manner you describe, without using IB.
// main.m
//
// NoIBApp - Create Application without IB
//
// Created by Gavin Baker on 23/01/10.
//
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[])
{
// Autorelease Pool
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// Create shared app instance
[NSApplication sharedApplication];
// Main window
NSUInteger windowStyle = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
NSRect windowRect = NSMakeRect(100, 100, 400, 400);
NSWindow* window = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:windowStyle
backing:NSBackingStoreBuffered
defer:NO];
// Test content
NSTextView* textView = [[NSTextView alloc] initWithFrame:windowRect];
[window setContentView:textView];
// Window controller
NSWindowController* windowController = [[NSWindowController alloc] initWithWindow:window];
// @todo Create app delegate
// @todo Create menus (especially Quit!)
// Show window and run event loop
[window orderFrontRegardless];
[NSApp run];
return 0;
}
It's not a complete solution, but it should get you most of the way there. So you can stick your OpenGL view in as the contentView
(instead of the text view) and away you go.