I wish to call the function
glShaderSource ::
GLuint
-> GLsizei
-> GHC.Ptr.Ptr (GHC.Ptr.Ptr GLchar)
-> GHC.Ptr.Ptr GLint
-> IO ()
The third argument is the shader program, which is a Haskell string in my program. How do I convert the Haskell String into a GHC.Ptr.Ptr (GHC.Ptr.Ptr GLchar) so that I can call glShaderSource?
You can use
withCStringfromForeign.C.Stringto convert a Haskell C string to a temporary C string. The string is allocated at the beginning of the call and deallocated at the end.The cast is necessary because string marshaling functions use the
CChartype, while GL uses theGLchartype. They are both 8-bit signed integers, and I presume that neither the OpenGL library nor the FFI will change its character type in the future. If you’re concerned about pointer casting, you can write your own marshaling function.On pointer-to-pointer types:
You now have a
Ptr GLchar. The right way to make aPtr (Ptr GLchar)depends on what the OpenGL library expects. For instance, does it expect an array of pointers? Does it callfreeon some pointers? Does it write to some of those strings? The solution will probably involve some amount of memory allocation and pointer copying.