Artwork . Games

Making Games with Go

March 7, 2014

The 5-second pitch: ENGi is a cross platform game library that supports Mac, Linux, Windows, The Web, (soon) native Android, and (right now) iOS and Android through CocoonJS. You can check out the project on Github.

Back-story

I have been trying to create a cross-platform 2D game engine in Go for quite a while now. There have been a few good starts, but building for some important platforms (like the web and mobile) was impossible. Even building and distributing for desktop platforms has been nontrivial. All that has changed recently.

Go -> Web

If you haven’t heard of GopherJS, do yourself a favor and check it out right now. It allows you to compile just about any Go to Javascript. The js-interop is top notch, and the resulting code is efficient. There is even goroutine support, so nothing is holding it back now.

GopherJS comes with a build flag, just like every other platform Go supports. What this means is you can separate your web-specific code in a file with ‘// +build js’ and your desktop specific code in a file with ‘// +build !js’ and then share the rest of the code between the two.

I have used this feature to abstract out the windowing, rendering, and file handling for each platform so that the rest of the engine is generic. You can compile for desktop or the web, and it will behave the same.

Mobile

GopherJS also opens up the possibility to release a game on iOS and Android by using CocoonJS. ENGi is already working pretty well in their simulator. I fully intend support this option with bug fixes and performance enhancements.

Tooling

Getting started with GopherJS is as simple as ‘go get’. The build workflow is very similar to the ‘go’ tool. Each new project, though, needs an HTML file that loads the compiled javascript. Every time you make a change, you will need to recompile and reload. This process makes trying out new ideas a bit slow.

I have created a project SRVi that you can run in the same directory as your GopherJS project. If your game is in a file named ‘main.go,’ you can access http://localhost:8080/ and SRVi will compile and serve it for you. Each time you refresh the page, the utility will recompile your game. See the docs for more options.

Another major headache is with packaging up a native game for every platform. I am working on another utility BLDi to make the process as easy as possible. Right now the tool only works for OSX and needs more work.

It can generate all the required files and icons, build the .app file, copy all needed dynamic library dependencies, and rewrite the binary to point to the local libs. All resources your game uses will also be bundled up. In the future, I will support Windows and Linux as well.