[swift-users] Here's a multi-platform OpenGL loader for Swift.

Joe Groff jgroff at apple.com
Mon Dec 14 11:22:58 CST 2015

> On Dec 12, 2015, at 1:39 PM, David Turnbull via swift-users <swift-users at swift.org> wrote:
> It's looking like the Epoxy requirement for SwiftGL is going away real soon now. I was able to lookup the OpenGL symbols from pure Swift code. No compiled C code is needed at all. What follows is roughly how I will do it. It lazily loads the pointer the first time you use it. The Linux version is only slightly different.
> Does anyone know a way to do this without the unsafeBitCast?
> import Darwin
> let handle = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY)
> func DLOADglGetIntegerv(a:GLenum, _ b:UnsafeMutablePointer<GLint>) -> Void {
>     glGetIntegerv = unsafeBitCast(dlsym(handle, "glGetIntegerv"), glGetIntegerv.dynamicType)
>     glGetIntegerv(a,b)
> }
> public var glGetIntegerv:@convention(c) (GLenum, UnsafeMutablePointer<GLint>) -> Void = { DLOADglGetIntegerv($0,$1) }

I think the unsafeBitCast is fundamentally necessary here to coerce the void pointer you get from dlsym to a C function pointer. I don't think it's a problem. This is pretty awesome!


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151214/cdccd7e6/attachment.html>

More information about the swift-users mailing list