SDL port for Android SDK/NDK 1.6

Quickly share your Android Code Snippets here...

SDL port for Android SDK/NDK 1.6

Postby pelya » Thu Nov 26, 2009 7:56 pm

This is the port of Alien Blaster game with libSDL ported to Android (includes SDL 1.2.14 and SDL 1.3, SDL_mixer 1.2.11, SDL_image 1.2.10 with jpg and png support, Tremor, SDL_ttf with freetype, and STLPort). Only SDL 1.3 has hardware acceleration currently, SDL 1.2 draws to memory surface (however you may use OpenGL ES to get HW accel with SDL 1.2).
Unlike other libSDL ports around, it does not require root access to video device, and will produce .apk package that you may publish on Android Market (and sell them, the LGPL license allows closed-source apps).

It is intended to be used as template to easily port your own C++ applications that use SDL library,
readme file describes all the steps required. No Java knowledge is necessary to use this lib.

It requires Android NDK r4 and SDK 2.2 to compile, however it will run on Android 1.6 devices. Android 1.5 devices do not have OpenGL lib, but it's still possible to port libSDL on them using software surfaces and drawing them to bitmap, however it will be slower than OpenGL (and I probably won't bother with this, only 19% of all Android devices are 1.5 or older).

To make successful port you have to ensure that your application supports native screen resolution (480x320, or 800x480 for widescreen devices), 16 bits per pixel. Alternatively you can draw to 640x480 and application screen will be resized nicely to fit device screen.
Also to make your application usable on every device around you have to ensure that it uses only arrow keys plus 4 action keys - that's Menu key mapped to Enter, Volume Up/Down keys mapped to PgUp/PgDown, and Back key mapped to Escape (which should quit application, plz don't make it game control). Arrow keys are emulated with accelerometer, touchscreen maps to mouse events.

You can combine SDL and OpenGL ES, see GLXGears example in sources.
The SDL without OpenGL mode renders using OpenGL ES renderer, so it should be HW accelerated (only if you're using SDL_Texture functions from SDL 1.3, trying to use backward-compatible SDL_Surface to draw on screen will give you exactly the same software renderer as was in SDL 1.2).
There still no multitouch support for 2.0, no vibrator and no virtual on-screen keyboard.
Accelerometer currently acts as arrow keys, to support devices without keyboard (later I'll add some config page to use it as joystick etc).
Also it will crash when you put the app to background with Home key.

Get the sources from this URL . Get the compiled application here to check performance. If you want to fix some bugs you can clone my GIT repository, fix them and merge your changes (I've documented some bugs in readme).

Also notice that I'm still working on the port and breaking it from time to time - you may wish to check out some stable revision.

If you've ported any other app I can publish it for you (it's better to pay $25 for developer account yourself though, I'll demand the app to be usable on my HTC Evo without keyboard)
Published to Android Market:
Alien Blaster
Jooleem
Last edited by pelya on Mon Jul 26, 2010 11:22 am, edited 25 times in total.
pelya
Developer
Developer
 
Posts: 32
Joined: Mon Nov 23, 2009 11:31 am

Top

Re: SDL port for Android SDK/NDK 1.6

Postby atomicdryad » Mon Nov 30, 2009 8:17 pm

pelya wrote:libSDL ported to Android (includes SDL 1.2.14, SDL_mixer 1.2.11, Tremor and STLPort).
Unlike other libSDL ports around, it does not require root access to video device, and will produce .apk package that you may publish (and sell) on Android Market.

This interests me greatly as I would love to port MPlayer and use that instead of android's rather limited video player. I see that it seems to be based on http://gitorious.org/0xdroid/external_libsdl-12 , which also seems to have libalsa and esd ports.
atomicdryad
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Nov 30, 2009 8:13 pm

Re: SDL port for Android SDK/NDK 1.6

Postby pelya » Tue Dec 01, 2009 10:40 am

atomicdryad wrote:
pelya wrote:libSDL ported to Android (includes SDL 1.2.14, SDL_mixer 1.2.11, Tremor and STLPort).
Unlike other libSDL ports around, it does not require root access to video device, and will produce .apk package that you may publish (and sell) on Android Market.

This interests me greatly as I would love to port MPlayer and use that instead of android's rather limited video player. I see that it seems to be based on http://gitorious.org/0xdroid/external_libsdl-12 , which also seems to have libalsa and esd ports.


No it's not based on anything, if I'd knew someone already did some port I would tried to reuse that.
Also I should've used libsdl 1.3, because it already have GLES code I've used for drawing, and it supports hardware surfaces unlike mine.
I doubt about libalsa, because that would mean you have to install your own custom kernel on your device, and that's possible only for unlocked devices. The "correct way" sound should be implemented is by using AudioTrack Java class and providing some callbacks from C code which will feed it with data.

Seems that anddev.org won's allow anonymous file download, so here's the code repository:
http://github.com/pelya/commandergenius ... dl_android
If I'll ever implement sound support it will go there.
pelya
Developer
Developer
 
Posts: 32
Joined: Mon Nov 23, 2009 11:31 am

Postby shagrath78 » Sat Dec 05, 2009 1:17 pm

thank you very much ! I'll play with it and try to look what I can do to help for sound support
Actually I started 2 weeks ago a port of SDL1.3 with some hooks into my own GLSurfaceView implementation, from java-side. Right now I only got the spinning cube sample working (the trunk/SDL/test/testgles.c file).
It's very hacky and obviously done in the wrong way cause I'm a big newbie at SDL. (I don't really get how to mix SDL 2D routines and 3D together)

On a side note, I don't know If you have to manage the GL thread in the Java app, I didn't, and even if it's not recommended by the Android team, I guess this is the way to go for a proper SDL compatibility ?
shagrath78
Freshman
Freshman
 
Posts: 4
Joined: Sat Dec 05, 2009 1:10 pm

Postby pelya » Sun Dec 06, 2009 10:30 pm

shagrath78 wrote:On a side note, I don't know If you have to manage the GL thread in the Java app, I didn't, and even if it's not recommended by the Android team, I guess this is the way to go for a proper SDL compatibility ?


Look indo 'libsdl/src/video/android", the code is rather small (but bit messy)
In my code the Java thread that draws OpenGL calls C callback inside libSDL, which then waits until other C threads of your app draw onto the SDL "Main Video" surface (which is actually just malloc()-ed memory region), and call SDL_Flip(). Then in SDL_Flip() the pointer to Main Video surface replaced with another memory region, and OpenGL thread unlocks and copies the data from prev mem region into GL texture.
It should also work without double-buffering (not tested by me), but your code then will block in SDL_Flip() until GL thread finishes it's work. Android have only one CPU anyway, so speed should be the same.

OpenGL in SDL is also possible, but then your code should block in SDL_GL_Flip() until Java GL thread signals you that it's ready. I did not implement that, and don't have much time lately. Maybe in a few weeks...

Edit: finally got my hands on HTC dev phone, the game is playable with few fixes - you may get them here. Still no sound, sorry.
pelya
Developer
Developer
 
Posts: 32
Joined: Mon Nov 23, 2009 11:31 am

Postby pelya » Tue Dec 08, 2009 6:32 pm

Got audio working, yay!
You may check it out here
pelya
Developer
Developer
 
Posts: 32
Joined: Mon Nov 23, 2009 11:31 am

Top

Postby shagrath78 » Tue Dec 08, 2009 10:21 pm

It's working, amazing ! I had to comment the audio logs though, my logcat console was getting killed :p
It's still a little laggy, I don't know how much cpu power that game requires

Anyway It's a great work, congrats ! You should post your work on the android-ndk group ! sure some people would help

About the OpenGL implementation in SDL1.3, like I said I'm a beginner :)
I did not mess with SDL_Flip and other functions, I just made a bridge with the egl functions in Java (getcontext, swapbuffers, etc.) as they're not available in the NDK and changed the generic SDL opengl renderer
shagrath78
Freshman
Freshman
 
Posts: 4
Joined: Sat Dec 05, 2009 1:10 pm

Postby pelya » Wed Dec 09, 2009 2:54 pm

I've removed that logging, and added downloading application data from internet on the first run, so the libSDL port is complete (also updated first post).

Here is the .apk file for you to test for performance

Would be nice if someone fixed GUI elements in that game out of place, added parallax to game field (the game field is 640x480 actually, and you cannot see right part of screen), added mouse (touchscreen) support, and released it on Android market for no price :D .
pelya
Developer
Developer
 
Posts: 32
Joined: Mon Nov 23, 2009 11:31 am

Postby hqhe1982 » Sun Jan 24, 2010 3:49 pm

I am porting sdl-1.3 to android,
The audio and video subsystem can work well.
I will put some demos to this site.
hqhe1982
Freshman
Freshman
 
Posts: 3
Joined: Sun Jan 24, 2010 3:36 pm

Postby hqhe1982 » Wed Feb 10, 2010 2:01 pm

sdl-1.3 android port:
the mouse and keyboard subsystem can also work well.

now i has a problem.
when the android app hide and then show again,
opengl surface must recreate, but i can't find a method to do it.
hqhe1982
Freshman
Freshman
 
Posts: 3
Joined: Sun Jan 24, 2010 3:36 pm

Postby shagrath78 » Sat Mar 13, 2010 1:20 am

It was essentially the major issue I got when I tested my SDL 1.3 port. Samples were working but the loss of the GL context and surface when onPause() is triggered is a pain in the ass. There's obviously something to do but the amount of trickery required is not really fun.
Another way would be to reuse the full GLSurfaceView class but I don't know how to share the EGL context with my own rendering thread without break everything
shagrath78
Freshman
Freshman
 
Posts: 4
Joined: Sat Dec 05, 2009 1:10 pm

Postby hqhe1982 » Mon Mar 15, 2010 5:26 pm

To shagrath78:

Sorry, My english is so poor.

Your problem is the same as my's:
OPENGL-ES surface and context must be recreated after android app hide and reshow.

I think maybe that is a method, but i do not test it.
Calling eglMakeCurrent() and eglDestroySurface() to destroy EGL surface before android app hide,
and calling eglMakeCurrent() and eglCreateWindowSurface() to recreate EGL surface after android app reshow.
hqhe1982
Freshman
Freshman
 
Posts: 3
Joined: Sun Jan 24, 2010 3:36 pm

Postby Tiresias » Tue Mar 30, 2010 11:56 pm

Hello
i would like to port a game done using sdl/opengl on android,
i read this topic and it looks like some of you guys already ported sdl 1/3 with ES support ?? how can i use this ported lib ?
Thanks a lot for this job,
Tiresias
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Mar 30, 2010 11:53 pm

Postby shagrath78 » Thu Apr 01, 2010 5:38 pm

mine is incomplete and unstable, you don't want to use that :)
shagrath78
Freshman
Freshman
 
Posts: 4
Joined: Sat Dec 05, 2009 1:10 pm

Postby Draffodx » Thu Apr 15, 2010 11:33 am

pelya wrote:Got audio working, yay!
You may check it out here


By that do you mean your accessing Audio at the native layer?
Draffodx
Master Developer
Master Developer
 
Posts: 201
Joined: Wed Nov 12, 2008 2:31 pm

Top
Next

Return to Code Snippets for Android

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest