GL_EXTENSIONS and Swift
It can be useful during the development and debug of graphical code to check the OpenGL version or which extensions are available[1].
Swift being more type-sensitive than Objective-C, one sometimes needs to fiddle when combining it with OpenGL. First, enums defined in OpenGL headers will be considered as Int32
, not as GLenum
, so you'll have to cast them before calling any GL function.
Furthermore, when calling glGetString you have to manually handle the pointer, as glGetString returns a pointer to an array of UInt8
(ASCII string), whereas Swift necessarily expects a Int8
array when manipulating C-Strings. With no possible direct conversion from UnsafePointer<UInt8>
to UnsafePointer<Int8>
, you'll have to extract each character.
var extensions = glGetString(GLenum(GL_EXTENSIONS))
as UnsafePointer<UInt8>
var array : [Int8] = []
while (extensions[0] != UInt8(ascii:"\0")){
array.append(Int8(extensions[0]))
extensions = extensions.advancedBy(1)
}
array.append(Int8(0))
if let extensionsString = String.fromCString(array) {
println("Available extensions :\n--------------------------")
println(extensionsString.stringByReplacingOccurrencesOfString(" ", withString: "\n"))
}
This snippet displays all the extensions enabled on the testing device. (Pointer manipulation in Swift is not as straightforward as in C derived languages, but seems safer.)
-
for instance, the list of GL extensions for iOS devices with PowerVR GPUs in the documentation is not exhaustive. ↩