The R Programming Language Is Now Fast Enough To Run Games On Linux With Nara

By

You may (or may not) be familiar with the existence of the R programming language. This is one very approachable language used mainly in the data science field and by more traditional statisticians as well. We have been using it extensively on Boiling Steam for numerous articles up until now. There’s a good chance that most of the charts you come across on our pages were made with R. Or even videos.

Why are we talking about R today? Well, since the release of the new package called Nara (for NAtive RAster, not the ancient japanese city that you should definitely visit if you are in Japan), R is now capable of rendering graphics very fast. Don’t expect it to reach thousands of frames per second, but compared to libraries like ggplot2, used for charting, which take several seconds to generate vector graphics, Nara can easily produce graphics at above 30 fps. The author of the package has released two demos to showcase what it can actually do: a non-interactive version of Pacman, and a port of Another World on R.

{Nara}

What does Nara bring on the table exactly that was not supported by R so far?

Nara is an off-screen rendering buffer. It uses in-place operations to avoid memory allocations (which are typically slow in R). It is focused on rendering discrete pixels (without anti-aliasing) and all dimensions are rounded to integer values prior to rendering.

The problem with graphics operations in R is that typical data structures do not correspond well to what is used in graphics devices. For example, a matrix in R is organized from columns to rows, while graphics devices usually expect row to column ordering matrices.

Another challenge is that the way color is typically encoded in R is using hexadecimal values. Graphics devices once again prefer to store RGB values in different values.

All these conversions that happen between R and graphics devices make drawing on screen much slower than it should be. This is where Nara comes in. Nara uses the built-in nativeRaster datatype (hence the name), which is an integer matrix where each value represents the RGBA color values of a single pixel - the information is encoded in four channels (RGBA) with 8 bits each. This data structure enables a more direct path to render pixels on screen - which is why it’s much faster.

Another World

Making Another world run in R is not just a matter of having a fast graphics device. We are talking about a full port in the R language. It’s possible in R to use shorcuts and program some functions in C for higher speed, but the author insisted in using only R code to create this port.

The port also required another package, eventloop, to make it possible to provide real time input in-game. R is not typically used for that kind of interation, so this was another building block needed to make a game. And let’s not forget sound support, too. I made a short video of the port running on R, on Linux.

It’s very impressive to see R being used in this way, since it was never designed to produce such applications.

But… is there really a point?

Making games in R?

Since it was mostly impossible until now to do any kind of real-time graphics with R, this changes everything. Of course, you won’t be able to write your next AAA game or even any good looking indie-game with it, but for testing ideas or even making some small prototypes, this could become an interesting option from now on.

I don’t think we are going to go beyond game prototypes anytime soon - R is not the fastest language around for this kind of things, and it lacks integration with many system libraries, controller input, or even proper GPU acceleration. Additionally, there is no good way to package a R program into an executable right now, which makes the distribution of binaries a real problem to solve.

However this does open the door for emulators to run within R, provided they are ported.

Another promising aspect when it comes to game prototypes is the combination of statistical and machine learning tools within games. Other languages don’t have such tools built-in and it would be trivial to run complex regression or classification models in the context of a game. You could imagine an action game that analyses your behavior patterns using PCA, and then throws specific enemies at you to exploit your identified weaknesses.

The FOSS aspect

One of the strength of the R language is its extraordinary community support. This is language that has by far surpassed all the commercial alternatives like SPSS because of its extreme extensibility and the explosion of packages available to use with R. All of it being under FOSS licenses. This is why this is a big thing in itself: making something possible in R will without doubt lead to more developers around the world to experiment with these new capabilities.

You only need three of four very talented people with new ideas to make waves.

References