SDL port for Android SDK/NDK 1.6

Quickly share your Android Code Snippets here...

Re: SDL port for Android SDK/NDK 1.6

Postby pelya » Wed Dec 28, 2011 5:09 pm

Beware - those zipfiles on the Github page are already more than one year old, I suggest downloading the repository using Git.
pelya
Master Developer
Master Developer
 
Posts: 323
Joined: Mon Nov 23, 2009 11:31 am

Top

Re: SDL port for Android SDK/NDK 1.6

Postby confi » Thu Dec 29, 2011 11:30 pm

I ported my sdl 2d game successfully but I noticed that the framerate is nearly twice faster if I use 16b in SDL_SetVideoMode instead of 32b, do you think it is normal?

here is how I create my surfaces :
Surf_Display = SDL_SetVideoMode(WWIDTH, WHEIGHT, BPP_DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF)

also, I would like to have sprite transparency using 255,0,255 color key but in 16b mode the transparency does not work which means I can have a laggy game but with transparent sprites or a fast but ugly game

here are my androidAppSetting, I do not understand everything in these, would it be ok for a simple 2d side scroller? :S :

LibSdlVersion=1.2
AppName="sheep"
AppFullName=confi.sheep
ScreenOrientation=h
InhibitSuspend=n
AppDataDownloadUrl="Game data is 1 Mb|sheep.zip"
VideoDepthBpp=16
NeedDepthBuffer=n
NeedStencilBuffer=n
NeedGles2=n
SwVideoMode=n
SdlVideoResize=y
SdlVideoResizeKeepAspect=n
CompatibilityHacks=n
confi
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Dec 27, 2011 11:12 pm

Re: SDL port for Android SDK/NDK 1.6

Postby confi » Fri Dec 30, 2011 8:08 am

I tried in SW and it is way faster
confi
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Dec 27, 2011 11:12 pm

Re: SDL port for Android SDK/NDK 1.6

Postby pelya » Fri Dec 30, 2011 10:59 am

You should almost always use SW video mode, if you'll supply SDL_HWSURFACE to the SDL_SetVideoMode() you'll get a big overhead when accessing individual pixels.
If you want 32bpp video mode, you should specify it in both AndroidAppSetting.cfg in the VideoDepthBpp=16, and to the SDL_SetVideoMode(), otherwise SDL will convert your 32bpp surface to the 16bpp video texture each frame.
And whatever Android device you've got, 16bpp video mode will be faster than 32bpp, just because GPU chip does less mem copying (and those chips are not like the videocard in your PC, they are usually slow).
Also using smaller video modes usually helps, like 320x240, 800x600 will render painfully slowly on the low-end devices.
pelya
Master Developer
Master Developer
 
Posts: 323
Joined: Mon Nov 23, 2009 11:31 am

Re: SDL port for Android SDK/NDK 1.6

Postby confi » Sat Dec 31, 2011 11:33 am

Is there a way to check the content of the AndroidData folder of my app once it has been pushed on the phone? To my understanding it is included in the .apk so it would require a log in debug report or something...

I am trying to had a num font to display the framerate on my app in the form of a new png image in AndroidData (myapp.zip file), and it seems my app is unable to see it once on the phone, I get the "Couldn't open font.png" error in debug report.

I also tested this : take a png image that loads fine, rename it in both my c++ code and in AndroidData, push the app on the phone => the app can't open this image anymore, so I suspect the changes I am making in AndroidData are no longer taken into account (size 64kb, so way under 1mb).

For now I trick my phone by changing the AppFullName or app version so that it does not reuse outdated data, but is there another way?
confi
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Dec 27, 2011 11:12 pm

Re: SDL port for Android SDK/NDK 1.6

Postby pelya » Sat Dec 31, 2011 3:54 pm

As long as you're re-installing your app only on your developer phone, you may just remove the file "app-data/your.app.name/libsdl-DataDownloaded0.flag" from the SD card (or remove the whole directory, it will get re-created).

If you want to force your data to be extracted - rename the .zip file inside AndroidData folder and inside AndroidAppSettings.cfg, then it will be unpacked again onto the SD card.

You can specify the URL instead of the file name in the AndroidAppSettings.cfg, and it works the same way - if you change URL it will be re-downloaded. Also, if you're updating your app and want to put some new files, you may just add a second, smaller, download to the AndroidAppSettings.cfg in the form "!Main game data|http://server/file.zip^!New game files|http://server/file2.zip", and it will download only the second file, without asking user for confirmation.

Also, many older phones won't download an .apk file bigger than 10-20 Mb from the Market (that depends on the free space left on the cache partition), so it might be a good idea to put your app data onto some public HTTP server instead of the APK file (sourceforge.net works perfectly as a public file storage, if your app is open-source - just create a new dummy project with a link to your sources, new projects are getting approved automatically now).
pelya
Master Developer
Master Developer
 
Posts: 323
Joined: Mon Nov 23, 2009 11:31 am

Top

Re: SDL port for Android SDK/NDK 1.6

Postby slvn » Fri Jan 06, 2012 1:49 pm

Hello,

Just to let you know : I got this crash report (1 out of 5000),
I don't know for which cellphone, android version, and so on...
(I use the sdl 1 3 and the sdl mixer)

Code: Select all
java.lang.UnsatisfiedLinkError: nativeAudioInitJavaCallbacks
at net.sbr.balloons.AudioThread.nativeAudioInitJavaCallbacks(Native Method)
at net.sbr.balloons.AudioThread.<init>(Audio.java:54)
at net.sbr.balloons.MainActivity.onCreate(MainActivity.java:148)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)




I got also this one (1 out of 10000):

Code: Select all
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.content.ContentQueryMap.setKeepUpdated(ContentQueryMap.java:95)
at android.content.ContentQueryMap.<init>(ContentQueryMap.java:68)
at android.hardware.SensorManager.<init>(SensorManager.java:840)
at android.app.ContextImpl.getSensorManager(ContextImpl.java:1127)
at android.app.ContextImpl.getSystemService(ContextImpl.java:951)
at android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:73)
at android.app.Activity.getSystemService(Activity.java:3546)
at net.sbr.balloons.AccelerometerReader.<init>(Accelerometer.java:48)
at net.sbr.balloons.DemoRenderer.onDrawFrame(Video.java:324)
at net.sbr.balloons.GLSurfaceView_SDL$GLThread.run(GLSurfaceView_SDL.java:1000)
slvn
Developer
Developer
 
Posts: 33
Joined: Thu Jul 28, 2011 7:45 am

Re: SDL port for Android SDK/NDK 1.6

Postby confi » Wed Jan 25, 2012 10:43 pm

Hello,

Pelya, I would like to know if it could be possible to add the "zoom gesture" with 2 fingers as a SDL event (like mouse wheel maybe?)
confi
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Dec 27, 2011 11:12 pm

Re: SDL port for Android SDK/NDK 1.6

Postby pelya » Thu Jan 26, 2012 11:39 am

This feature already exists - set this line in your AndroidAppSettings.cfg:
RedefinedKeysScreenKb="A B C D E F PAGEUP PAGEDOWN G H"


This will make SDL send SDL_KEYDOWN/SDL_KEYUP event with SDL_keysym.sym == SDLK_PAGEUP when you zoom in with 2 fingers, SDLK_PAGEDOWN when you zoom out, SDLK_G and SDLK_H when you rotate fingers left and right.
pelya
Master Developer
Master Developer
 
Posts: 323
Joined: Mon Nov 23, 2009 11:31 am

Re: SDL port for Android SDK/NDK 1.6

Postby confi » Sat Jan 28, 2012 4:12 pm

ok, I see, but apart from this gestures, is it possible to use multitouch? like having mouse1 + mouse2 coordinates or something like that?
confi
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Dec 27, 2011 11:12 pm

Re: SDL port for Android SDK/NDK 1.6

Postby pelya » Sun Jan 29, 2012 1:17 pm

Yes, it's possible. With SDL 1.3 there's a special SDL_FINGERDOWN event for that, with SDL 1.2 you'll need to enable the AppUsesMultitouch flag in the AndroidAppSettings.cfg, then you'll be receiving multitouch events as a joystick events - the Ballfield example in the SDL repository supports multitouch in that way, though you'll need to modify AndroidAppSettings.cfg to enable it.
pelya
Master Developer
Master Developer
 
Posts: 323
Joined: Mon Nov 23, 2009 11:31 am

Re: SDL port for Android SDK/NDK 1.6

Postby confi » Tue Jan 31, 2012 10:29 pm

I tried this with SDL1.2, on my phone I can see that SDL detects a 3 axis joystick but when I try to touch the screen with 2 fingers, no joystick event is registered (while on pc if I plug a joystick they are registered)

I added AppUsesMultitouch=y in my config, don't know if some other setting is required?
confi
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Dec 27, 2011 11:12 pm

Re: SDL port for Android SDK/NDK 1.6

Postby pelya » Wed Feb 01, 2012 12:14 pm

I've fixed the Ballfield example, it now shows multiple touch events.
You should open not just the first joystick, but the other 16 joysticks as well. The first joystick will return you an accelerometer input, if you'll set AppUsesJoystick=y in AndroidAppSettings.cfg
Here's the code snippet for a multitouch input:
Code: Select all

#include "SDL.h"

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
    SDL_Surface * screen = SDL_SetVideoMode(640, 480, 16, 0);

    enum { MAX_POINTERS = 16, PTR_PRESSED = 4 };
    int touchPointers[MAX_POINTERS][5];
    int i;

    memset(touchPointers, 0, sizeof(touchPointers));
    SDL_Joystick * joysticks[MAX_POINTERS+1];
    for(i=0; i<MAX_POINTERS; i++)
        joysticks[i] = SDL_JoystickOpen(i);

    while(1)
    {
        SDL_Event evt;
        while( SDL_PollEvent(&evt) )
        {
            if( evt.type == SDL_JOYAXISMOTION )
            {
                if( evt.jaxis.which == 0 ) // 0 = The accelerometer
                    continue;
                int joyid = evt.jaxis.which - 1;
                touchPointers[joyid][evt.jaxis.axis] = evt.jaxis.value; // Axis 0 and 1 are coordinates, 2 and 3 are pressure and touch point radius
            }
            if( evt.type == SDL_JOYBUTTONDOWN || evt.type == SDL_JOYBUTTONUP )
            {
                if( evt.jbutton.which == 0 ) // 0 = The accelerometer
                    continue;
                int joyid = evt.jbutton.which - 1;
                touchPointers[joyid][PTR_PRESSED] = (evt.jbutton.state == SDL_PRESSED);
            }
            if(evt.type == SDL_KEYDOWN && evt.key.keysym.sym == SDLK_ESCAPE)
                return 0;
        }
        SDL_FillRect(screen, 0, 0);
        SDL_Rect r;
        for(i=0; i<MAX_POINTERS; i++)
        {
            if( !touchPointers[i][PTR_PRESSED] )
                continue;
            r.x = touchPointers[i][0];
            r.y = touchPointers[i][1];
            r.w = 80 + touchPointers[i][2] / 10; // Pressure
            r.h = 80 + touchPointers[i][3] / 10; // Touch point size
            r.x -= r.w/2;
            r.y -= r.h/2;
            SDL_FillRect(screen, &r, 0xffffff);
        }
        SDL_Flip(screen);
    }
    return 0;
}

pelya
Master Developer
Master Developer
 
Posts: 323
Joined: Mon Nov 23, 2009 11:31 am

Re: SDL port for Android SDK/NDK 1.6

Postby confi » Wed Feb 01, 2012 8:49 pm

I initialized 16 joysticks as you said and now it works :)
Thanks a lot for your help, again :D
confi
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Dec 27, 2011 11:12 pm

Re: SDL port for Android SDK/NDK 1.6

Postby confi » Sat Feb 04, 2012 7:21 pm

the multitouch works fine when the touching points are moving, but is it possible to detect taps of the fingers on the screen? for instance if I just touch the screen with two fingers without moving them relatively to the plan of the screen (perpendicular touch on the screen if you prefer) only one of the touch is detected as a mouse event

at first I thought it was due to the implementation in my game that does not use SDL_JOYBUTTON events so I added them and it still the same issue
I also tried your new version of ballfield and it is the same behavior on it
confi
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Dec 27, 2011 11:12 pm

Top
PreviousNext

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests