Я пытаюсь создать основной цикл рендеринга для приложения OpenGL Swift OS X, но я не могу найти никаких примеров в Интернете и не могу понять взаимодействие с Objective C API.
Здесь код во время инициализации моего подкласса NSOpenGLView
:
var udl : Unmanaged<CVDisplayLink>?
CVDisplayLinkCreateWithActiveCGDisplays(&udl)
var displayLink: CVDisplayLink = udl!.takeRetainedValue() // I guess
// The two following lines give errors that the type isn't convertible
// to the declared type:
let cb: CVDisplayLinkOutputCallback = dlCallback // ERROR: type not convertible
let sp: UnsafeMutablePointer<Void> = &self // ERROR: type not convertible
CVDisplayLinkSetOutputCallback(displayLink, cb, sp)
let cglContext = openGLContext.CGLContextObj
let cglPixelFormat = pixelFormat.CGLPixelFormatObj
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat)
CVDisplayLinkStart(displayLink)
и здесь моя функция обратного вызова. Я не знаю, как извлечь указатель обратно в класс вида из непрозрачного указателя, который я передал в CVDisplayLinkSetOutputCallback
(или попытался).
func dlCallback(displayLink: CVDisplayLink!,
inNow: UnsafePointer<CVTimeStamp>,
inOutputTime: UnsafePointer<CVTimeStamp>,
flagsIn: CVOptionFlags,
flagsOut: UnsafeMutablePointer<CVOptionFlags>,
context: UnsafeMutablePointer<Void>) -> CVReturn {
let that = UnsafeMutablePointer<MyView>(context) // Just a guess, but no
that.render() // ERROR: no such method
}
Я хотел бы понять, как это сделать.
Если я должен использовать какой-то другой основной цикл рендеринга вместо этого, на основе таймеров, я думаю, что я могу это сделать.