Initialize Mac OS X Window Code

I am currently reading the red book for learning OpenGL correctly, and in one of the first examples, the author writes a line that says "InitializeAWindowPlease ();" as the owner of the place for the code that will make the drawing window for OpenGL content.

Since I am using Xcode for my programming, I know that "get" the window to work with automatically (and that I have to make my own OpenGL viewer in the interface).

How can I do this with clean code?

I am trying to learn programming and I won't be glad that I click "shortcuts" all the time. How can I make a window to draw my openGL stuff?

With Objective-C and C code, I'd love to see this. My goal is that I can do this without opening the interface constructor at all :)

+2


source to share


2 answers


Frameworks actually do a great job for you. A simple call NSApplicationMain()

to main.m

does things like starting your main application class (as defined Info.plist

), loading the main class nib

, loading the main windows and menus, setting the default autorelease pool memory, and going into a run loop (and more!).

It's good to know that the "magic" happens under the covers, but I don't see anything wrong with letting the frameworks do a lot of work for you. And IB is an especially nice UI editor, it would be a shame not to use it! Anyway, since you asked ...

You should read on the following documents from the Apple Developer Connection site:



So, at a minimum, to do all of 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 menu. This is left as an exercise for the reader! But the code below basically works as 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;
}

      

This is not a complete solution, but it should get most of the way from you. That way, you can stick with your OpenGL view contentView

(instead of text) and you're away.

+1


source


The way it is usually done with Cocoa is slightly different from my understanding. [NeHe OpenGL lessons]

have links to sample Xcode projects at the bottom that you can use in.



0


source







All Articles