objective-ccocoaobjective-c++nsopengl

Drawing in a NSWindow


I'm working on a cocoa wrapper for a graphics framework.

To finally draw the stuff, I'm doing this:

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

inside a subclass of NSView.

Now I've looked at other frameworks like Gosu, Irrlicht, etc. and I saw they were always doing complicated NSOpenGL stuff like:

// 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];

Why are they doing all that? Is my "simple" way okay?


Solution

  • I agree with ryyst. You are using CGContext which is essentially the Quartz 2D API. OpenGL is another option for graphics especially good for complex 3D rendering.