So, why would anyone want native code in the browser? Legacy code, you might think, but that’s not really it. Games is the biggest reason.

C++ is the single most important language in (professional) game development, and before that it was C. Before that various assembler dialects. See the pattern? Many game developers, especially indies, use engines where they can work in higher-level languages, but you still need to run these on everything that calls itself a gaming device. And they’re of course almost all written in C or C++. So there’s really no way around native code execution if you want games on your platform, that’s why every notable mobile platform has added them, sometimes reluctantly.

Why C++?

There are numerous reasons why most of the game industry is still using C++. One is that it’s pretty much the only language you can use on every single platform that runs games, from desktop to handheld consoles. Another is that, as opposed to desktop applications, games usually have the exact same user interface across platforms. So rewriting it in the platform’s native frameworks, as is a popular approach to mobile apps these days, does not make much sense. You usually want one code base that runs everywhere. C and C++ are the only reasonable options for that right now. And even though many atrocities have been committed with C++, you can actually write nice code in it – look to the likes of Stroustrup and Carmack for that.

Why not JavaScript?

But couldn’t people just use JavaScript and ship their game with an app that embeds WebKit? Hardly. Browsers and mobile browsers are becoming faster, but the bleeding edge is still barely able to deliver the kind of games that were running fine in Flash ten years ago. Yet current desktops and smartphones are capable of so much more than that. WebGL could arguably fix this, but even if mobile browsers weren’t so slow to implement it, and Microsoft wouldn’t refuse to adopt it, you’ll still end up with significantly more overhead and stricter hardware requirements than if you just make a native game.

I’ve thought long and hard about whether I should make web-based games. I’ve been developing web applications during my day jobs for more than five years now, and JavaScript has been my main language all the time, so it seemed like a no-brainer. But I want to use proven libraries like SDL and Box2D, I want to support older operating systems and hardware, and I don’t want to spend my time optimising every little algorithm I write (which is what I ended up doing for pretty much all browser games I worked on so far). It’s C++ for me.

So, why is asm.js a big deal?

Because the browser is suddenly a feasible platform for game developers, most of which previously ignored it. It’s not a huge investment anymore, you can still ship the same code base to desktop, mobile and consoles. You can even feasibly have a web-based demo and then deliver the full game as a native application, or make an MMORPG free to play with a limited browser client, and have people pay money for the gorgeous native client. We’ll probably see all the big third party engines compile to asm.js in no time, Unreal Engine having already taken the lead. Browsers will either support WebGL or lose users. I’m betting my money on the former. This is very likely the dawn of the browser as a proper gaming platform.

3 Responses

Absolutely excellent article. Thanks for the share. I’m a huge javascript fan and have wanted to write games in it for a long time. I’m hoping that asm.js and WebGL will allow this? I’m not sure. Does asm.js just make it so your C++ code can be run in browsers?

Well, asm.js itself is just a specification for a subset of JS that allows efficient manual memory management etc. Emscripten is a compiler that compiles C, C++ and others to it, but I guess you could compile any statically typed language that allows manual memory management to it.

You can write games in pure JS just fine, but you’re not unlikely to end up with high memory usage and GC pauses, typical issues for games in garbage collected languages. But those are solvable problems.