我正在为图形框架开发 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/