Programically creating window + openGl view

Member
Posts: 153
Joined: 2004.12
Post: #1
Im creating an engine. It would be really nice if i could create some type of initialization code that creates a window and openGL view. That way i dont have to deal with creating the same stuff in interface builder over and over again every time I want to create a new app. I know that ogre3d does something like this but im guessing they use carbon.

Has anybody done this? I imagine it wouldn't be too difficult but im lost on how i would for example receive events if i cant directly add code to the view class or for that matter define a pixel format or setup a run loop.

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
read the docs for

Code:
-[NSWindow initWithContentRect:styleMask:backing:defer:]
-[NSOpenGLView initWithFrame:pixelFormat:]
-[NSView addSubview:]

You'll be subclassing NSOpenGLView, too.
Quote this message in a reply
Member
Posts: 153
Joined: 2004.12
Post: #3
Thanks for the help.

Where do you think the best place is to execute this? Should i create a delegate to NSApplication? Creating a controller in interface builder would kind of defeat the purpose.

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #4
Just make a class that does all this. When it's needed, let the developer instiate it himself.

"When you dream, there are no rules..."
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
Make a subclass of NSObject in the Nib or dont use a nib at all, make a new project as a Foundation Tool and add the Appkit frameworks, make sure you do NSApplicationLoad();, and put everything inside an Autorelease Pool.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 153
Joined: 2004.12
Post: #6
unknown Wrote:make a new project as a Foundation Tool and add the Appkit frameworks, make sure you do NSApplicationLoad();, and put everything inside an Autorelease Pool.

I ended up pretty much doing this.

My main.m consists of:

Code:
[NSApplication sharedApplication];
    [NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
    
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     Controller *nymeriaController = [[Controller alloc] init];
     [nymeriaController run];
     [pool release];
    
      [NSApp run];
      return 1;

and Controller looks like

Code:
- (void)run
{    
    [[NSApplication sharedApplication] setDelegate:self];    
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    app = [[LCApp alloc] init];
    NSSize windowSize;
    windowSize.width = 800;
    windowSize.height = 600;
    
    NSPoint windowOrigin;
    windowOrigin.x = 0;
    windowOrigin.y = 0;
    
    NSRect windowRect;
    windowRect.size = windowSize;
    windowRect.origin = windowOrigin;
    
    NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:(NSResizableWindowMask || NSClosableWindowMask || NSTitledWindowMask) backing:NSBackingStoreBuffered defer:NO];

    [app setWindow:window];
    view = [app initViewWithDefaultPixelFormat];
    
    [app setTarget:self withDrawInterval:1.0f/60.0f];
    
    scene = [[LCScene alloc] init];
    camera = [[LCCamera alloc] init];
    
    LCVisualization *vis = [[LCVisualization alloc] initWithStaticType:LC_PHYSICS_RECTANGLE_STATIC];
    [vis setSize:1:1:1];
    
    LCLight *light1 = [LCLight lightWithPosition:1:1:0:0];
    
    [scene addObject:camera];
    [scene addObject:light1];
    [scene addObject:vis];
    
    [[app eventManager] addObject:camera andAction:@selector(moveForward) forKeyPress:13];
    [[app eventManager] addObject:camera andAction:@selector(moveBackward) forKeyPress:1];
    [[app eventManager] addObject:camera andAction:@selector(strafeRight) forKeyPress:0];
    [[app eventManager] addObject:camera andAction:@selector(strafeLeft) forKeyPress:2];
    [[app eventManager] setMouseMovedObject:self andAction:@selector(mouseMoved:)];
    
}

- (void)drawFrame
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    
    [scene render];
        
    [[view openGLContext] flushBuffer];
}

- (void)mouseMoved:(NSEvent *)theEvent
{
    [camera rotateX:-[theEvent deltaY]];
    [camera rotateY:-[theEvent deltaX]];
}

It doesnt look like i cleared out all that much code, but the developer can have LCApp init everything to some type of default if they want to quickly create a working view.

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #7
Im just confused here with my own project, If im creating and deleting lots of NSWindows in an NSArray whats the correct way to dispose of them?

Code:
[window setReleasedWhenClosed:YES];
[window close];
[windowlist removeObjectAtIndex:i];
where window is the i'th object of windowlist

Is this right, am I missing somthing?
Help would be much appreciated Grin

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL and Carbon Controls in the same Window Hasty 1 2,991 Sep 1, 2006 10:19 PM
Last Post: OneSadCookie
  Mouse events for an openGL window majestik666 5 7,365 Jul 5, 2005 06:15 PM
Last Post: majestik666
  Translating a window coordinate to a view coordinate emileej 3 2,907 Jun 20, 2005 11:25 AM
Last Post: emileej
  Resizing Window and View Nick 6 3,543 Jun 7, 2005 01:43 PM
Last Post: Malarkey
  HomeBrew OpenGL view in Cocoa Feanor 4 5,836 May 14, 2002 08:16 AM
Last Post: Jeff Binder