2015-07-17 00:40:46 +00:00
|
|
|
//
|
|
|
|
// OpenGLView.m
|
|
|
|
// ElectrEm
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "OpenGLView.h"
|
|
|
|
@import CoreVideo;
|
|
|
|
|
|
|
|
@implementation CSOpenGLView {
|
|
|
|
CVDisplayLinkRef displayLink;
|
|
|
|
}
|
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
- (void)prepareOpenGL
|
2015-07-17 00:40:46 +00:00
|
|
|
{
|
2015-07-17 01:16:21 +00:00
|
|
|
// Synchronize buffer swaps with vertical refresh rate
|
|
|
|
GLint swapInt = 1;
|
|
|
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// 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);
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|