如何调用Ptr GLubyte类型的函数->IO()在Haskell



在OpenGL Raw库中有以下函数:

glPolygonStipple :: Ptr GLubyte -> IO ()

与此函数对应的C语言接受指向数组的指针,但是我如何在Haskell程序中使用数组/列表调用此函数?

您将使用mallocArray分配内存,并使用pokeArray将列表放入其中:

http://hackage.haskell.org/packages/archive/base/latest/doc/html/Foreign-Marshal-Array.html v: mallocArray

类似:

do
  arrayOfGLuBytes <- (mallocArray 15) :: IO (Ptr GLubyte)
  pokeArray arrayOfGLuBytes [1,2,3,4]
  glPolygonStipple arrayOfGLuBytes
  free arrayOfGLuBytes -- free from Foreign.Marshall.Alloc

在这种情况下最好的方法可能是在vector包[http://hackage.haskell.org/packages/archive/vector/0.7.1/doc/html/Data-Vector-Storable.html][1]中存储可存储的vector。包为不可变和可变向量提供了丰富的接口,因此不必在IO monad中创建向量。此外,列表是链表,转换为数组需要复制

你的例子可能看起来像

let myVector = fromList [1,2,3,4] 
in unsafeWith myVector glPolygonStipple

最新更新