OpenGL, Glut in Haskell on Windows

OpenGL, Glut in Haskell on Windows

Okay so you are feeling little adventurous and want to try out some of your functional programming skills in Game and Graphics programming. Well how about Haskell afterall it is a hardcore FP Language which can be compiled or interpreted! Lets try it out with the good old utility library Glut.

We will start with
Haskell Platform which is Haskell distribution with many useful packages of which the most useful for us are the Glut and OpenGL bindings package.
FreeGLut which is the completely open sourced version of Glut. Now we will save ourself a little time and LOT of hassle by downloading prebuild binaries instead of building ourself from Martin Payne's Windows binaries for MingW. Make sure you grab the MingW package as Haskell bindings for Glut seems to be build with MingW gcc.

Now once you have downloaded and installed Haskell Platform, and unzipped the FreeGlut binaries, take the following code and copy to a HelloWorld.hs file.
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
main :: IO ()
main = do
  (progname, _) <- getArgsAndInitialize
  createWindow "Hello World"
  displayCallback $= display
display :: IO ()
display = do
  clear [ ColorBuffer ]

On a windows machine compiling this with ghc -package GLUT HelloWorld.hs -o HelloWorld will generate a HellowWorld.exe which will probably complaint and fail with missing dll error on running. Have no worries thats what we downloaded those binaries of freeglut for. Now copy the freeglut/bin/freeglut.dll to the directory containing HelloWorld.exe and rename it to the missingg dll which haskell has been complaining about ( most probably glut32.dll ). That's it. You should be having a working program doing absolutely nothing :).

No comments: