Search code examples
androidc++android-ndklogcatsdl-2

Android SDL2 App black screen then exits


I have a working SDL2 demo running in Windows, but when I port this code to Android the finished app will display a black screen for few seconds and then quietly exit (no errors, nothing), rather than display the pretty test graphics within a game-loop.

If I add a call to SDL_ShowSimpleMessageBox at the start of my main, nothing happens (I've since learned that it's not implemented for Android yet - grrr), but if I comment-out my main code, ndk-build complains that it's missing, so it's certainly being included in the build, but doesn't appear to get called.

I've followed the steps in README-android.txt. After about 200 hours of solving problems over the past 2 months I eventually produced an apk. Have the following fixes I made maybe broken something?

The first problem is NDK-BUILD failing to find SDL_config.h, because of the instructions at step #2 in README-android.txt being wrong. Fixed restructuring the directories, or editing paths in Android.mk.

Second problem is NDK-BUILD failing to find EGL/eglplatform.h. Fixed by adding APP_PLATFORM := android-9 to Application.mk.

Third problem is NDK-BUILD not recognising C++11, so I added APP_CPPFLAGS += -std=c++11 to Application.mk.

Fourth problem is NDK-BUILD not finding #include <cstdarg> (used for va_list and va_start). Including <SDL.h> instead fixed this.

Fith problem is ant build failing at [aapt] Generating resource IDs. I fixed this in Android SDK Manager by deleting build-tools verion 21.1.1 and installing version 20 instead.

This is the first time I've posted for help on here because I'm desperate. I'm a veteran C++ coder but a complete novice when it comes to java. I'm using C++ in eclipse for the Windows SDL2, and I built the apk entirely from the command-line.

Tried on several different devices. Looking at the logcat, there's a signal 7 SIGBUS error:

V/SurfaceView( 3497): Layout: x=0 y=0 w=1280 h=720, frame=Rect(0, 0 - 1280, 720)
F/libc    ( 3497): Fatal signal 7 (SIGBUS) at 0x00000000 (code=128)
I/ActivityManager(  162): Displayed org.libsdl.app/.SDLActivity: +416ms
V/SDL     ( 3497): onWindowFocusChanged(): true
W/InputManagerService(  162): Starting input on non-focused client
com.android.internal.view.IInputMethodClient$StubProxy@410b72d8 (uid=10021 pid=331)

Solution

  • I've found the problem. Feel free to slap my wrists.

    I've always coded with the assumption that I can address memory with 8-bit aligned pointers. This has worked for me for the past 25 years, until now. My code fails when it's targeting Android because whatever CPU it's using refuses to address memory with pointers that are 8-bit aligned. So as soon as I move a pointer along a byte-aligned butter and cast that pointer to an object and reference that pointer, BAM, android quietly exits the app.

    Now the question is what to do with this question? It logs all the problems and solutions I've battled with over the past 2 months, so must be useful to someone?