Programically creating window + openGl view
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.
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.
read the docs for
You'll be subclassing NSOpenGLView, too.
Code:
-[NSWindow initWithContentRect:styleMask:backing:defer:]
-[NSOpenGLView initWithFrame:pixelFormat:]
-[NSView addSubview:]You'll be subclassing NSOpenGLView, too.
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.
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.
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..."
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!
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.
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?
where window is the i'th object of windowlist
Is this right, am I missing somthing?
Help would be much appreciated
Code:
[window setReleasedWhenClosed:YES];
[window close];
[windowlist removeObjectAtIndex:i];Is this right, am I missing somthing?
Help would be much appreciated
Sir, e^iπ + 1 = 0, hence God exists; reply!
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| OpenGL and Carbon Controls in the same Window | Hasty | 1 | 2,680 |
Sep 1, 2006 10:19 PM Last Post: OneSadCookie |
|
| Mouse events for an openGL window | majestik666 | 5 | 6,814 |
Jul 5, 2005 06:15 PM Last Post: majestik666 |
|
| Translating a window coordinate to a view coordinate | emileej | 3 | 2,649 |
Jun 20, 2005 11:25 AM Last Post: emileej |
|
| Resizing Window and View | Nick | 6 | 3,248 |
Jun 7, 2005 01:43 PM Last Post: Malarkey |
|
| HomeBrew OpenGL view in Cocoa | Feanor | 4 | 5,062 |
May 14, 2002 08:16 AM Last Post: Jeff Binder |
|

