Search code examples
androidc++sdl-2screen-rotationautorotate

Forcing auto-rotation to stop (Android, C++, SDL2)


I have a game and it runs well on Android through an application called C4Droid (If you don't know about C4Droid, just google it). It is written in C++ and only uses SDL2.

It runs on portrait and so, when I tilt the device with auto-rotation turned on, it gets landscape.

Now, what I wanted is to do something that avoids it to get landscape even when auto-rotation is turned on. Answer please?


Solution

  • No, there is no way to do this in SDL2 only (you can in iOS, but for some reason they made it different in Android). wadali's answer is best, here are some ways you can use it in your build environment:

    • Use a different build environment. If you build the APK on a desktop machine you can control the contents of the AndroidManifest.xml file. If you really want to do it on your phone, there are other C/C++ compilers for Android. The part you lose is C4Droid's ease of use - you will need to learn how to build the APK yourself.
    • Build it using C4Droid as usual, and then use an APK editor to extract, edit, and repackage AndroidManifest.xml. If you're comfortable copying the APK to a desktop machine you can use apktool to unpack the APK, edit the file you need, then repack it again (note: you will need to re-sign the APK after doing this before it can be used); otherwise, there seem to be a few APK editor utilities available for Android, too.

    There are Android APIs for controlling the orientation, but you can't access them from within SDL2. SDL2 creates its own Activity and doesn't have any means of either extending it or letting you provide your own activity, so you cannot expose additional Android API calls to your own code.