Tech Focus: Porting Sorcery! to Android

The dream of building games simultaneously for iOS and Android always seemed like a goal just outside of our reach. Unfortunately, writing code for Android requires learning an entirely different set of tools than for iOS, and with a core team of just two, we wanted to play to our strengths. All our apps so far have been built in native iOS code.

Sorcery! for Android

Therefore, the decision to port Sorcery! to Android was a question of who? and how?

For who, we knew we needed someone well-versed in Android, passionate about interactive fiction, with a flair for polish but also able to deal with a highly technical challenge. Luckily, we knew a guy: Iain Merrick (@iainmerrick), who after several years at Google was looking to go independent and do his own coding. Iain's been a friend for a long time: Jon originally met him through the indie-before-indie-was-cool text-based gaming scene in 2000.

The timing seemed perfect, and Iain was keen to have a go, so we started to think about the technical question: "how?" do we port? Broadly, there were two options:

  • Re-build the game from scratch in Java or C++, the primary language choices for building Android apps
  • Try to re-use some of our iOS code

As any software engineer will tell you, code reuse is Good! Also, we'd just been tipped off (thanks for your email, @apiemont!) about a new technology from a company called Apportable.

Here's Apportable's summary from their website:

Write your apps entirely in Objective-C. Share the same code on iOS and Android.

So, theoretically, we could simply compile the code we already had on iOS, and building directly for Android. A few tweaks might be necessary here and there: Android devices include many different screen sizes and resolutions, and they include features like the OS-level back button that we would want to support.

How do Apportable do it?

The software running on any computer is engineered in layers: from low level, which includes basic numerical and text manipulation functionality, up to the highest level that includes specific user interface features and finally, the application code. For an iOS app's code to run on an Android device, the full underlying stack has to be supported, so that Sorcery! can run on top.

iOS software stack

Apportable's technique is to support Apple's full software stack on Android. Easier said than done! That's a lot of software to support. Thankfully, a lot of the lower level foundation code already exists in the open source community. But then they have to re-create all of Apple's higher level functionality, including their user interface elements.

And that's where the trouble began. Sorcery! relies heavily on highly customised versions of Apple's UI components. Apportable supports many of them, but there were two major problems.

Bumps in the road

When it works, Apportable is magic. And it was incredible to get a build up and running in just a couple of days. But the build had screens in it like this one:

Early screenshot

Note the little character just at the top there...

Of course, it would be somewhat unrealistic to expect Apportable to recreate everything that Apple's teams have with perfect parity. But even basic features like re-flowing the interface when the device orientation changed turned out to be buggy. For us, these bugs meant extra time spent working around the limitations of Apportable's implementation.

The second big issue was performance. Sorcery! layers up its user interface with lots of textures and transparency, and on iOS this is accelerated on the graphics hardware using OpenGL. Unfortunately, Apportable built their user interface on top of Android's default software-based user interface layer.

We had Sorcery!'s prologue running within the first week or so - but it was very slow: clouds, crawling up the screen pixel-by-pixel. When we asked them, Apportable told us that hardware acceleration was coming, but for us it was too late.

Our solution

Thankfully for us, Iain isn't just good, but very good. And, as became apparent, he's ambitious too. What he did was write his very own hardware accelerated version of Apple's core user interface libraries. In just three months. The clear benefit was that he only had to support the exact subset of features that Sorcery! required - no more, no less. Whereas Apportable has to support everything that their clients need. Still though, it's a huge achievement.

Of course, he didn't stop there. He'd built the majority of the higher level UI features that we needed, and highly performant ones at that. What was Apportable providing that we couldn't get for free in the open source community? (And note, Iain intends to release a lot of his work back into that community.) We resolved to jump ship. Although scary, it's proved to be a great decision.

The extra benefit of moving away from Apportable was that we had full access to the source code of every level of the software stack, and that meant being able to fix bugs at every level.

I should point out for the sake of fairness to Apportable: what they have achieved is incredible, allowing many games to be ported much more easily than in our example. If Sorcery! had relied purely on traditional OpenGL graphics or one of the game libraries out there such as Cocos2D, then it would have been an excellent, and almost magical solution.

Coming to life...

The strange thing about a low-level port like this is that for the first six months there's nothing to see: all the developers are buried deep inside the code, away from the eyes of a passing user. At the start of December, Iain showed us his latest build - it had the prologue of the game, running at a good speed, with layered, semi-transparent clouds animating on top of one of another. It didn't have much else!

By January, we had text on those animating screens as well, though with a minor bug that was quickly fixed:


Then, suddenly, by the last week of January, we had a complete build - the game was playable, the map in full 3D, with panning and zooming, with combat and the 3D spell-casting globe. Everything came together in a rush. Right now, we're down to polishing up and final layout bugs.

And, of course, this will make porting the next games much, much easier, because 99% of the code features we need are already there. (For a one-off project, it's probably easier just to rewrite the game in Java.)

Coming on the 12th March

Sorcery! for Android has been a longer time coming than we hoped, but the result of Iain's labours has been that we're in a truly great position going forwards. Although we add unique new features in each part of Sorcery!, the majority of the code is shared, and now that includes the Android port.

Our dream is to be able to release our games simultaneously on iOS and Android in future. With this new shared codebase, that dream could be becoming a tangible reality.

But you'll be able to see it for yourself on the 12th March.

comments powered by Disqus