在Swift中初始化NSOpenGLPixelFormat

Pet*_* W. 4 opengl macos xcode nsopenglview swift

显然我是唯一一个尝试这一点的人,因为我的谷歌搜索没有任何有用的信息.假设我正在初始化一个属性数组,如下所示:

let glPFAttributes = [
    NSOpenGLPFAAccelerated,
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFAColorSize, 48,
    NSOpenGLPFAAlphaSize, 16,
    NSOpenGLPFAMultisample,
    NSOpenGLPFASampleBuffers, 1,
    NSOpenGLPFASamples, 4,
    NSOpenGLPFAMinimumPolicy,
    0
]
Run Code Online (Sandbox Code Playgroud)

这些都是常规的Ints,我已经检查过了.现在,如果我这样做

let glPixelFormat = NSOpenGLPixelFormat(attributes: glPFAttributes)
Run Code Online (Sandbox Code Playgroud)

编译器给我这个错误信息:

'Int'与'NSOpenGLPixelFormatAttribute'不同

如果我在某个地方犯了错误,我就没有看到它.

cod*_*ter 7

NSOpenGLPixelFormatAttributetypeAliasUInt32.NSOpenGLPixelFormatintializer需要数组,NSOpenGLPixelFormatAttribute所以你需要创建数组并将所有转换IntUInt32.Below代码将起作用

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)
Run Code Online (Sandbox Code Playgroud)