objective-c - 在 NSWindow 中绘图

标签 objective-c cocoa objective-c++ nsopengl

我正在为图形框架开发 cocoa 包装器。

为了最终绘制这些东西,我正在这样做:

- (void)drawRect:(NSRect)rect
{
 CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
 CGContextDrawImage(ctx, NSRectToCGRect(rect), image);
}

NSView 的子类中。

现在我研究了其他框架,如 Gosu、Irrlicht 等,我发现他们总是在做复杂的 NSOpenGL 事情,例如:

// Settings, depending on fullscreen or not
NSOpenGLPixelFormatAttribute windowedAttrs[] =
{
 NSOpenGLPFADoubleBuffer,
 NSOpenGLPFAScreenMask,
 (NSOpenGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
 NSOpenGLPFADepthSize,
 (NSOpenGLPixelFormatAttribute)16,
 (NSOpenGLPixelFormatAttribute)0
};
NSOpenGLPixelFormatAttribute fullscreenAttrs[] =
{
 NSOpenGLPFADoubleBuffer,
 NSOpenGLPFAScreenMask,
 (NSOpenGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
 NSOpenGLPFAFullScreen,
 NSOpenGLPFADepthSize,
 (NSOpenGLPixelFormatAttribute)16,
 (NSOpenGLPixelFormatAttribute)0
};
NSOpenGLPixelFormatAttribute* attrs = fullscreen ? fullscreenAttrs : windowedAttrs;

// Create pixel format and OpenGL context
ObjRef<NSOpenGLPixelFormat> fmt(
  [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]);
::context = [[NSOpenGLContext alloc] initWithFormat: fmt.obj() shareContext:nil];

他们为什么要这么做?我的“简单”方式可以吗?

最佳答案

我同意 ryyst 的观点。您正在使用 CGContext,它本质上是 Quartz 2D API。 OpenGL 是另一种图形选择,特别适合复杂的 3D 渲染。

关于objective-c - 在 NSWindow 中绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5776656/

相关文章:

iphone - iPhone 上的大事件指示器

ios - 在 tableView 中检索选定的行

c++ - 使用函数创建数组 - Objective-C++

objective-c - 从 Cocoa 中的 URL 方案中捕获 URL

objective-c - NSTableView 圆角像 UITableView Groups

objective-c - 显示带有许多可点击 URL 的文本

objective-c - 在 Cocoa 中使用 Spotlight

cocoa - NSTableView:如何正确调整单元格中的文本大小

c++ -::Instance() 是什么意思?

c++ - 使用 Objective-C++ 创建一个 Metal Window Wrapper