Note: you can read part 1 of this article here.
In this final part, I will talk about my struggle and non-struggle porting the game to Android. What is initially a straightforward thing, and for the most part it is, ended up having a few problems that I needed to address manually. I will also finally reveal the game with some screenshots.
Once the bare basics of the game were made, I realized that the game only really used one click of the mouse to work, so it would be pretty straightforward to port it to Android by replacing the click with a tap and SDL2 is after all available for Android. Android applications are however written in Java, that crappy programming language I would never use unless I get paid for it, so to port C programs to Android we have to use the Java Native Interface (JNI), and to do that you have to compile your application as a library along with SDL2 (you can have SDL2 be a shared library – .so – or statically linked into your main application library, which is what I did) and then write a main Java class that loads this library and calls the main library function.
Thankfully the folks working on SDL2 already did all this *** for those of us who hate Java to the bone, but there was still a few things I had to change. On my device, when I paused the game by going back to the launcher and resumed it, one assumes that the game will go back to normal, just like when you sleep your PSP or go back to the live area of the vita and returned to the game, however in my case the screen went black but the game did resume, there was background music and the game responded to my taps it was just the screen that messed up. I googled around and found that you had to reload ALL your *** textures due to them being invalid as you loose the OpenGL context when you go back to the launcher or changed application (on Android, SDL2 is an OpenGL wrapper).
I tried to override the onPause and onResume functions of SDL2’s main class to have it call some function that reloaded all textures, this meant that I had to keep track of all the loaded textures and add a new method call “reload” to the image wrapper class. However, this didn’t do anything, I still had a black screen when resuming the game. So how did I fix it? By overriding the onPause method of the main Java class to have it call a C function I created that saved the state of the game (how the map was laid out, the current score, what characters and items there were and where they were) to a file, then the game exited completely rather than pausing and when launching the game again it recreated the game’s state with that file, it is of course needless to say that if you were not playing (the game was in the main menu for instance) then there was no need to save the current game state.
Damn, that was a lot of talk so I had to split what was initially only one post into two. Anyways, the screenshots of the game:
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
The game has a lot of weapons and items that are randomly placed on the map, and you always have the same startup weapons and items. There’s also cows that you can control, they eat grass and either reveal mines or safe spots, cows never die or are harmed, they are invincible and only get bored after eating 5 times or so and go away somewhere else.
Although the code itself is pretty much done, some of the graphics still need to be finished. And it should be possible and easy to port to something like the PSP, but for now there’s no incentive to do this. I already have a friend with an Android developer account that agreed to upload the game to the store once it’s finished. The game will be free, but I want to work on the sequel (which will be bigger, I already have some ideas that will be fun) and all this requires some funding so it will most likely have ads at least at startup (I want to stress out that I don’t like day-one DLC or microtransactions so that’s out of the question) and I will be accepting donations for those who want to help with this project but don’t have technical or artistic knowledge.
Btw if anyone has (good) knowledge in making sound effects and music and would like to contribute to the project, send me a PM in /talk.
The post My experience with SDL2 and game development, part 2 appeared first on Wololo.net.