Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are functional programming languages suitable for graphics programming?

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?

like image 846
castiel Avatar asked Mar 18 '12 04:03

castiel


People also ask

What programming language is best for graphics?

C and C++ languages are the most interesting because they are commonly the go-to language for graphics rendering.

Which programming language is used in computer graphics?

C/C++ GUI. C and C++ are interesting because they are commonly the go-to language for graphics rendering.

What are functional programming languages good for?

Advantages Of Functional Programming It improves modularity. It allows us to implement lambda calculus in our program to solve complex problems. Some programming languages support nested functions which improve maintainability of the code. It reduces complex problems into simple pieces.

What programming languages use functional programming?

Functional programming has historically been less popular than imperative programming, but many functional languages are seeing use today in industry and education, including Common Lisp, Scheme, Clojure, Wolfram Language, Racket, Erlang, Elixir, OCaml, Haskell, and F#.


2 Answers

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.

like image 188
mikera Avatar answered Sep 30 '22 06:09

mikera


This is a good series on the topic: (4 parts) Purely Functional Retrogames. You could take this approach in Clojure and use underlying Java game libraries to manipulate the graphics.

like image 32
Bill Avatar answered Sep 30 '22 07:09

Bill