How to call a function of type Ptr GLubyte -> IO() in Haskell
In the OpenGL Raw library is the following function:
glPolygonStipple :: Ptr开发者_StackOverflow 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?
You'll use mallocArray to allocate memory and pokeArray to put your list into it:
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]. Package provide rich interface for both immutable and mutable vectors so don't have to create vectors inside IO monad. Besides lists are linked list and conversion to arrays inovlve copying
You particular example could looks like
let myVector = fromList [1,2,3,4]
in unsafeWith myVector glPolygonStipple
精彩评论