How to draw NSView graphic context inside CVDisplayLink callback?

I have a simple game that maps 2D graphics to a frame buffer (without using OpenGL). I was going to use CVDisplayLink to get a clean frame rate, however most of the examples on the Internet deal with OpenGL or QuickTime.

So far, I have a subclass of NSView:

@interface GameView : NSView {
@private
    CVDisplayLinkRef displayLink;
}

- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime;
@end

And I set the CVDisplayLink callback:

CVDisplayLinkSetOutputCallback(displayLink, MyDisplayLinkCallback, self);

And I have a callback function:

CVReturn MyDisplayLinkCallback (CVDisplayLinkRef displayLink,
                                const CVTimeStamp *inNow,
                                const CVTimeStamp *inOutputTime,
                                CVOptionFlags flagsIn,
                                CVOptionFlags *flagsOut,
                                void *displayLinkContext)
{
    CVReturn error = [(GameView*)displayLinkContext getFrameForTime:inOutputTime];

    return error;
}

The part where I got stuck is what I need to do in getFrameForTime:order to draw the graphics context in the GameView. My first assumption was to make a drawing in the same way as indrawRect:

- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime
{
    CGContextRef ctxCurrent = [[NSGraphicsContext currentContext] graphicsPort];

    //.. Drawing code follows
}

ctxCurrent nil, , , - , drawRect:, . , , . ?

?

+5
2

‑drawRect:, MyDisplayLinkCallback() ivar ‑display . .

‑drawRect: ivar , .

+2

-draw: MyDisplayLinkCallback(), -drawRect:.

Rob Keniger ; , -display , , , ( , ).

0

All Articles