From 4496493e85df55936fbfc432ec181c82748e10c7 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 16 Jul 2015 21:16:21 -0400 Subject: [PATCH] Performed the bare necessary steps to get my little OpenGL view to create a CVDisplayLink and then repaint itself in time with the display. --- .../Base.lproj/Atari2600Document.xib | 13 ++++++- OSBindings/Mac/Clock Signal/OpenGLView.m | 35 +++++++++++++++---- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/Base.lproj/Atari2600Document.xib b/OSBindings/Mac/Clock Signal/Base.lproj/Atari2600Document.xib index 7b26bd57f..8effec6d7 100644 --- a/OSBindings/Mac/Clock Signal/Base.lproj/Atari2600Document.xib +++ b/OSBindings/Mac/Clock Signal/Base.lproj/Atari2600Document.xib @@ -17,9 +17,20 @@ - + + + + + + + + + + + + diff --git a/OSBindings/Mac/Clock Signal/OpenGLView.m b/OSBindings/Mac/Clock Signal/OpenGLView.m index e4475dee1..023353c6f 100644 --- a/OSBindings/Mac/Clock Signal/OpenGLView.m +++ b/OSBindings/Mac/Clock Signal/OpenGLView.m @@ -13,15 +13,38 @@ CVDisplayLinkRef displayLink; } -- (instancetype)initWithCoder:(nonnull NSCoder *)coder +- (void)prepareOpenGL { - self = [super initWithCoder:coder]; + // Synchronize buffer swaps with vertical refresh rate + GLint swapInt = 1; + [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; - if(self) - { - } + // Create a display link capable of being used with all active displays + CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); + + // Set the renderer output callback function + CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, (__bridge void * __nullable)(self)); + + // Set the display link for the current renderer + CGLContextObj cglContext = [[self openGLContext] CGLContextObj]; + CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj]; + CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat); + + // Activate the display link + CVDisplayLinkStart(displayLink); +} - return self; +// This is the renderer output callback function +static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext) +{ + [(__bridge CSOpenGLView *)displayLinkContext setNeedsDisplay:YES]; + return kCVReturnSuccess; +} + +- (void)dealloc +{ + // Release the display link + CVDisplayLinkRelease(displayLink); } @end