Search code examples
xcodemacosopenglswiftnsopenglview

Initializing NSOpenGLPixelFormat in Swift


Apparently I'm the only one to attempt this, as none of my Google searches turned up anything helpful. Assume I'm initializing an attribute array like this:

let glPFAttributes = [
    NSOpenGLPFAAccelerated,
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFAColorSize, 48,
    NSOpenGLPFAAlphaSize, 16,
    NSOpenGLPFAMultisample,
    NSOpenGLPFASampleBuffers, 1,
    NSOpenGLPFASamples, 4,
    NSOpenGLPFAMinimumPolicy,
    0
]

These things are all regular Ints, I've checked. Now if I do

let glPixelFormat = NSOpenGLPixelFormat(attributes: glPFAttributes)

the compiler gives me this error message:

'Int' is not identical to 'NSOpenGLPixelFormatAttribute'

If I made a mistake somewhere, I'm not seeing it.


Solution

  • NSOpenGLPixelFormatAttribute is typeAlias of UInt32. NSOpenGLPixelFormat intializer takes array of NSOpenGLPixelFormatAttribute so you need to make array and convert all Int to UInt32.Below code will work

    let glPFAttributes:[NSOpenGLPixelFormatAttribute] = [
        UInt32(NSOpenGLPFAAccelerated),
        UInt32(NSOpenGLPFADoubleBuffer),
        UInt32(NSOpenGLPFAColorSize), UInt32(48),
        UInt32(NSOpenGLPFAAlphaSize), UInt32(16),
        UInt32(NSOpenGLPFAMultisample),
        UInt32(NSOpenGLPFASampleBuffers), UInt32(1),
        UInt32(NSOpenGLPFASamples), UInt32(4),
        UInt32(NSOpenGLPFAMinimumPolicy),
        UInt32(0)
    ]
    
    
    let glPixelFormat = NSOpenGLPixelFormat(attributes: glPFAttributes)