Are functional programming languages suitable for graphics programming?

castiel picture castiel · Mar 18, 2012 · Viewed 15k times · Source

Just very curious about this, from my own experience , all the graphic programming seems to C or C++ related. Like the Direct10X. Does functional programming language provide some sort of graphic library to develop video game?

Answer

mikera picture mikera · Mar 18, 2012

You can use functional languages to do graphics/game programming just as in any other language.

It's only a simple game, but I wrote Ironclad: Steam Legions in Clojure as an exercise in functional programming for game development.

Here are some lessons I learnt / general observations on using Clojure for game programming:

  • You need to be careful about performance as games can be very demanding and functional languages do impose some overheads. Clojure is certainly "good enough" for most games, but you need to know the tricks to keep your code optimised. For example, functional languages can get a bit GC-heavy producing a lot of temporary objects. You need to learn the tricks to avoid this (for example, using reduce in a way that avoids creating new sequence objects, or leveraging primitive artithmetic)

  • Mutability is useful in games. For example, if you are doing anything with physics or smooth animation you often have a lot of objects with constantly changing locations. You can simulate this with functional/immutable data structures but if you care about performance it's not a good idea. Hence it's worth finding out how to get mutable data in your functional language even if it isn't idiomatic (e.g. in Clojure you will probably want to make use of Java arrays)

  • Immutable persistent data structures actually turn out to be very useful in games as well. In Ironclad, the entire game state was stored in a single immutable data structure. This allowed for some cool tricks like efficiently snapshotting the game state / instant undos / running backwards in time.

  • Clojure is awesome for game scripting. The dynamic nature coupled with runtime compilation and the ability to define arbitrary DSLs with macros is a massive win. In fact, even if I was writing a game in an OOP language like Java I would seriously consider using Clojure (or another Lisp) for scripting.

  • Clojure is awesome for interactive development. I often found myself running the game in one window while hacking the running code in a REPL alongside. It's fun to alter game data structures and immediately see the effects! This awesome video also gives you a taste of what's possible with Clojure-style development.

  • In Clojure at least you will often want to use the Java libraries for graphics, e.g. Swing for 2D or LWJGL for 3D. In some cases wrappers for these already exist, however I found it easy enough to use them directly from Clojure. After all, Java interop is as simple as (.methodName object arg1 arg2)

In conclusion, I think functional languages are perfectly good choices for game development, with the exception of very performance-intensive games where you are still likely to be better with C/C++ in order to have more direct control over the hardware.