在OSX中创建OGL窗口而不使用XIB

时间:2011-12-28 09:16:14

标签: xcode macos opengl xib

是否可以使用Xcode 4在OSX中创建OpenGL窗口,而不使用XIB?

我希望这是一个静态库,否则我会很高兴使用Interface Builder。

1 个答案:

答案 0 :(得分:2)

当然,您可以通过编程方式创建所有UI元素。您只需创建一个包含NSOpenGLView内容的窗口,例如:

NSWindow *w = [[NSWindow alloc] initWithContentRect:NSMakeRect(100,100,400,300)
                                          styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask
                                            backing:NSBackingStoreBuffered
                                              defer:YES];
NSRect frame = [w contentRectForFrameRect:[w frame]];
// this is optional - request accelerated context
unsigned int attrs[] = { NSOpenGLPFAAccelerated, 0 };
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*)attrs];
NSOpenGLView  *view = [[NSOpenGLView alloc] initWithFrame:frame pixelFormat:pixelFormat];
[pixelFormat release];
// manage properties of the window as you please ...
[w setOpaque:YES];
[w setContentView:view];
[w makeFirstResponder:view];
[w setContentMinSize:NSMakeSize(150.0, 100.0)];
[w makeKeyAndOrderFront: self];

在实践中,您可能会继承NSOpenGLView并使用您的班级......

相关问题