【问题标题】:Creating an OGL Window in OSX without using an XIB在不使用 XIB 的情况下在 OSX 中创建 OGL 窗口
【发布时间】:2012-01-29 01:11:27
【问题描述】:

是否可以在不使用 XIB 的情况下使用 Xcode 4 在 OSX 中创建 OpenGL 窗口?

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

【问题讨论】:

    标签: xcode macos opengl xib


    【解决方案1】:

    当然,您可以通过编程方式创建所有 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 并改用您的类...

    【讨论】:

    • 很难找到一个最新的指南,但是这很好用,谢谢西蒙!
    猜你喜欢
    • 2012-05-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    相关资源
    最近更新 更多