Last year I participated in a game development project in my spare time. The idea was to do a small, top-down RPG with roguelike elements. The game was made for Windows with C++, using the SFML library for its multimedia capabilities. It features quests, skills, items, monsters, NPCs and more.

Recently I added precompiled headers to improve build times and made an Android port for the game. Having had some issues and made some discoveries, I decided to make a blog post to share what I found.

Build times

Build times can be very frustrating, especially on template-heavy C++ projects. After getting small gains from cleaning up unused includes and forward declaring what was possible, I started to look for other alternatives.

Following this Viva64 tutorial, I implemented the precompiled headers technique. My precompiled header contains:

  • The most common standard library headers (vector, string, map, etc).
  • Third-party libraries (SFML, easylogging and nlohmann JSON).
  • Common game code classes that are used everywhere and don’t change often.

This file is parsed once and used to compile every other translation unit. This was enough to slash the compilation time by more than half, from about 45 seconds to 20 seconds.

https://xkcd.com/303/

Android port

After having the game run on Windows, I decided to challenge myself and attempt to make an Android port. I had already programmed Android apps here at Poatek, but this would require using the native NDK. I knew beforehand some of the issues I would be working on, but others took me by surprise. Here is how it went:

  • Windows filesystem is case-insensitive, but Linux is case-sensitive. I had to rename multiple files in order to conform to that.
  • Android NDK supports only the Clang compiler, but for Windows, I was using Visual Studio’s compiler.
    • Visual Studio has a few C++ non-standard extensions that were being used that Clang doesn’t support.
    • A couple of non-portable library usages to remove.
    • Multiple compiler quirks to adjust the code to.
  • The Windows version loads assets from the filesystem, but for the Android, it would need to load from the .apk itself.
  • Adapt the logging system to dispatch logs to Android’s Logcat.
  • Add touch support for the UI.
  • Add UI scaling so the UI looks good for different phone screen densities.
  • Create a virtual directional pad to move the character.

And… success! It’s not perfect, but it works.

Conclusion

C++ can be very slow to compile, but with some work, it’s possible to speed it up considerably, making great improvements to development productivity.

Porting to Android will be very dependent on the project size, as well as the portability of any third-party library being used. If your project already compiles with Clang, the porting will be much easier.

About the author

Gabriel Allegretti is a software developer at Poatek. He studies Computer Science at the Federal University of Rio Grande do Sul. He works on the frontend and backend for web applications, as well as native Android development. On his spare time, he likes to make video games, play guitar and take his dog for a walk.