【发布时间】:2012-01-29 01:11:27
【问题描述】:
是否可以在不使用 XIB 的情况下使用 Xcode 4 在 OSX 中创建 OpenGL 窗口?
我希望这是一个静态库,否则我会很乐意使用 Interface Builder。
【问题讨论】:
是否可以在不使用 XIB 的情况下使用 Xcode 4 在 OSX 中创建 OpenGL 窗口?
我希望这是一个静态库,否则我会很乐意使用 Interface Builder。
【问题讨论】:
当然,您可以通过编程方式创建所有 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 并改用您的类...
【讨论】: