简体   繁体   English

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

[英]How to call a function of type Ptr GLubyte -> IO() in Haskell

In the OpenGL Raw library is the following function:在 OpenGL 原始库中是以下 function:

glPolygonStipple :: Ptr GLubyte -> IO ()

The C counterpart to this function accepts a pointer to an array, but how can I call this function with an array/list in a Haskell program? The C counterpart to this function accepts a pointer to an array, but how can I call this function with an array/list in a Haskell program?

You'll use mallocArray to allocate memory and pokeArray to put your list into it:您将使用 mallocArray 分配 memory 和 pokeArray 将您的列表放入其中:

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

Something like:就像是:

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

Probably best way to go in this situation is storable vectors in the vector package [http://hackage.haskell.org/packages/archive/vector/0.7.1/doc/html/Data-Vector-Storable.html][1]. Probably best way to go in this situation is storable vectors in the vector package [http://hackage.haskell.org/packages/archive/vector/0.7.1/doc/html/Data-Vector-Storable.html][1 ]。 Package provide rich interface for both immutable and mutable vectors so don't have to create vectors inside IO monad. Package 为不可变和可变向量提供了丰富的接口,因此不必在 IO monad 中创建向量。 Besides lists are linked list and conversion to arrays inovlve copying除了列表是链表和转换为 arrays inovlve 复制

You particular example could looks like您的特定示例可能看起来像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM