views:

213

answers:

2

I'm currently reading "the red book" for learning OpenGL properly, and in one of the first examples, the author writes a line that says "InitializeAWindowPlease();" as a place holder for the code that will make a window to draw the OpenGL content in.

Since I'm using Xcode for my programing, I know that I "get" a window to work with automatically (and that I have to make my own OpenGL view in interfacebuilder).

How can I make this with pure code?

I'm trying to learn programming, and I'm not to happy about taking "shortcuts" all the time. How can I make a window to draw my openGL stuff in?

With Objective-C and C code I would love to see it. My goal is that I can make it without opening interface builder at all:)

A: 

The way it's normally done with Cocoa is a little different, to my understanding. [NeHe's OpenGL lessons] have links to example Xcode projects at the bottom that you can ape the format of.

Chuck
A: 

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.

gavinb