Stuff i did

Android game development with SDL2 - a summary

I've been using SDL for a while and decided to create an openGL ES2 based toy on android.

As the process was not as nearly as straightforward as it could have been i've decided to write it down and maybe it can help somebody else too.

Getting started

So to start you need the android SDK - the ADT bundle - i got the 20140321 version and the android NDK - native development kit, because we mean business:)

You also need SDL2, i used the latest, 2.0.3

The idea is to use the Native C++ interface through JNI (java native interface) to start in the java world and immediately go a level below it to the native world with c++. That's where the fun is:) SDL will take care of all the bindings for us and leave us with the well thought out SDL api.

Start with the basic android project packaged with SDL (/android-project/), copy it to your workspace. Then copy or symlink the SDL directory to the project's jni folder (the idea is to have /jni/SDL/include structure)

The Android SDK

Download an SDK platform for api version 16 (or higher) and i would also recommend an emulator because they work really well. The x86 version because you will probably have an x86 machine and that should reduce the virtualization overhead. Fun fact, android compiles native code for three platforms (arm, even more arm and x86) and includes them all in the .apk. It's a fat apk:)

Run the ADT eclipse based development environment. I would recommend it because of the easy signing and running of the app on the emulation (otherwise it kinda sucks:). Add the project to your workspace and right click it, find android tools and a native nature to the project. 

In the jni/ folder you now have SDL and src, put your main.cpp file into the src folder. Fix from "yoursourcefilehere" to "main.cpp" to compile your file.

I stumbled across a horrible eclipse bug, its c++ indexer decided that it could not resolve the SDL function even if the ndk-build command ran fine. So i had to DISABLE the unresolved function error in eclipse's c++ indexer. That removed the error and enabled a slick transition from change code -> upload to emulator -> run.

So there you have it, a working empty SDL project running on android:) I came across a lot of small obstacles so i hope this eases the process for you.

Also i have a BlackBerry Z10 so you can image i made the process even more complex by connecting directly to my blackberry (which runs .apk's yaay!) but that's for another article:) 

The Basic Project

I took the android-project that ships with SDL and added a basic main.cpp file that took me some time to get together from different sources. Disinformation on the internet, go figure:)

Here it is!

This main.cpp creates a window and an OpenGL ES2 context. Sound is also started with a basic function handling it. Usually i make a basic handling object for audio and video rendering, then pass it to this basic function:

class BasicScript
	virtual void processAudio(unsigned char* stream, unsigned int len) {}
    virtual void initialize() {}
    virtual void initializeGL() {}
    virtual void paintGL() {}
    virtual void animate() {}
    virtual bool keyUp() { return false; }
    virtual bool keyDown() { return false; }
    virtual bool keyPress() { return false; }
    virtual bool event(int eventtype, const TouchEventInfo *tei) { return false; }
    virtual int resize(int width, int height) {
        windowWidth = width; windowHeight = height;
        renderableWidth = width; renderableHeight = height;
		screenRatio = 1;
		if( renderableHeight != 0 ) screenRatio = (float)renderableWidth/renderableHeight;
		if( windowHeight != 0 ) screenRatio = (float)windowWidth / windowHeight;
        return 0;
	virtual bool isQuit() { return false; }
    char* readfile(const char filename, unsigned);
	virtual float getAnimateInterval() { return 1000.0/30; }
    int renderableWidth;
    int renderableHeight;
    int windowWidth;
    int windowHeight;
	float screenRatio;

Using this template i can create anything i want within this, then easily port it to different system just by calling the appropriate functions at the right time.

The Final Development Environment

ADT seems nice, but for actual development i will use pure SDL on the desktop giving me the same environment for the code and capability to use any IDE i want to develop it. I love KDevelop and it was easy to set the project up with cmake and start coding. I actually created the files in android-project/jni/src/ and symlinked it to the kdevelop project:) So now i can develop in the comfort and the debug mode of the desktop, then just compile and run the APK on the emulator when i need to test it. Good times:)