14

I'd like to experiment with Haskell a bit, and I'm hoping to write a small 2D arcade game (Tetris or Breakout). Can you recommend a simple graphics library that will help me to get started quickly?

Btw, I've been experimenting with SDL and wxWidgets, but haven't yet succeeded in running any samples because of dependency problems, working on it...

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
  • 1
    It's extremely important that one of the first thing you do is download Cabal Install. Without it, dependency management is horrible. – Rayne Jul 08 '09 at 17:56
  • Cabal is great -- you should definitely download it. But it doesn't solve the issue of missing underlying native libraries. A lot of libraries (e.g., wxWidgets, OpenGL, probably GTK) are simply not included in the Cabal package. – rtperson Jul 09 '09 at 12:48
  • I just took a look at Haskell's GTK2HS, and it does include the full GTK stack (which is nice). Unfortunately, it doesn't have a Cabal package (not so nice), and its installer requires GHC 6.10.3. Not "6.10.3 or higher" -- just "6.10.3." Easy enough to work around, but still... :P – rtperson Jul 30 '09 at 12:52
  • A better answer: http://stackoverflow.com/questions/5612201/haskell-library-for-2d-drawing – Don Stewart Apr 17 '11 at 21:24
  • @Don Stewart: Agreed -- the other answer is far better, to the point where it supersedes everything said here. – rtperson Jun 14 '11 at 12:28

3 Answers3

11

It's not exactly a "simple" library, but there is a lot of information online about OpenGL and GLUT, as well as some very good tutorials and a ton of example code.

The biggest issue you're up against is that the OpenGL and GLUT bindings in Haskell do not include the libraries that they bind to. (This is true for wxWidgets as well.) A lot of Linux distros come with OpenGL binaries bundled, but not Windows. The Haskell Platform was supposed to fix this, but it didn't seem to for me.

So, assuming you're installing on Windows, here's what I'd recommend you try:

  1. Follow the directions in this blog to the letter. They're complicated -- involving installs of MinGW, MSys, and hand-compilation of a GLUT project from SourceForge, but it's the only way I've gotten OpenGL to work. I've now successfully installed on three separate machines, including XP and Vista, so I can safely say that these are very good directions.
  2. Once it does work, check out these two awesome tutorials. They really opened my eyes about just how powerful Haskell can be when it comes to graphics. You'll find the code involved a lot simpler than you may have anticipated.
  3. Check out the sample games on the Haskell OpenGL page. They're very experimental -- which is good, as it means less code to wade through than you'll find in a production system -- but they're also surprisingly sophisticated. (And yes, there's already more than one bare-bones Tetris implementation, but don't let that stop you.)
  4. Another good source of sample code is Haskell's GLUT binding itself. Look for the examples directory and you'll find many ports of sample code from the OpenGL Red Book.

OpenGL is very stateful, so you may find the Haskell code a little daunting if you haven't fully grokked Monads yet. I'm using my OpenGL experiments as motivation to finally wrap my mind around the concept.

Good luck!

rtperson
  • 11,632
  • 4
  • 29
  • 36
  • OpenGL state is all in the IO monad, so practice may help with the basics of dealing with monads, but doesn't help when trying to generalize to other monads which do far more useful things than simply keeping track of the state of the world. – ephemient Jul 08 '09 at 14:40
  • I don't mean to complain too much, though; OpenGL/GLUT seems like a good start for OP, though it's not very Haskell-ish. Personally I use Gtk2Hs with its GtkGlArea bindings, as it's nicer than raw GLUT, but it doesn't matter what's handling the windows once you get down to OpenGL itself. – ephemient Jul 08 '09 at 14:44
  • I haven't played around with GTK. I got wxWidgets working first, so I did my UI experiments with it. But I will try it out now on your recommendation. I agree with you on the monad thing -- the IO monad is a pretty dull beast, but it is a place to start. My eventual goal is to work my way up to arrows so I can try some reactive programming with Yampa. Right now all the concepts are flying over my head but I'm starting to put the pieces together. – rtperson Jul 08 '09 at 15:59
  • The configure step in OpenGL's configuration give me the error "Couldn't reserve space for cygwin's heap", apparently this may be caused by anti-virus software. Disabling Avast on my computer doesn't seem to help the problem. Patience and perseverance are getting well trained in this little project... – StackedCrooked Jul 10 '09 at 18:15
  • Actually it's more likely a pathing problem, especially if you have both Cygwin and MinGW installed. For some reason, compiling the freeglut libraries doesn't work with Cygwin's gcc. Type "gcc --version" into the command line and make sure the resulting output looks something like this: "gcc.exe (GCC) 3.4.5 (mingw-vista special r3)" – rtperson Jul 10 '09 at 18:31
  • 1
    For easy setup with newer versions check http://openwires.blogspot.in/2012/10/opengl-glut-in-haskell-on-windows-okay.html – FUD Oct 18 '12 at 17:25
  • ``cabal install gloss`` and you have a simple graphics library which works on top of opengl. Allt hat mingw and cygwin talk is not necessary IMHO. Gloss works for me on windows and there is nothing complicated about installing it. It just works (on my windows 10). – BitTickler Oct 08 '18 at 00:04
4

Have you perused the following lists:

Haskell Graphics Libraries: There appears to be quite a few interfaces to OpenGL, SDL, and other graphic libs here.

Haskell GUI: There's some wxWidget libs here as well.

Tesseract
  • 526
  • 3
  • 20
Jay Atkinson
  • 3,279
  • 2
  • 27
  • 41
1

Cairo Is written in C but has haskell bindings, Perhaps trying that might be a good idea. Ive only ever used its python bindings but those seemed to work well.

Fusspawn
  • 1,405
  • 12
  • 19