- Cleaned up a bunch of code
authorPaul Hunkin <paul@bieh.net>
Tue, 27 Jul 2010 21:21:24 +0200
changeset 4722faa228f7ce5b
parent 4721 7bb9d3a3f257
child 4723 74da47b2f5b7
- Cleaned up a bunch of code
- Added 'feature' enable/disable so we're not running accel/sound/whatever in Java when we don't need to be
- More work on the sound system. But it still crashes pretty horribly, not sure why yet.
Makefile.android
android/testproject/jni/app-android.cpp
android/testproject/jni/lesson05.c
android/testproject/src/org/libsdl/android/SDLActivity.java
include/SDL_config_android.h
src/audio/SDL_audio.c
src/audio/android/SDL_androidaudio.o
src/video/android/SDL_androidgl.c
     1.1 --- a/Makefile.android	Tue Jul 27 21:20:17 2010 +0200
     1.2 +++ b/Makefile.android	Tue Jul 27 21:21:24 2010 +0200
     1.3 @@ -30,7 +30,7 @@
     1.4  	src/power/*.c \
     1.5  	src/audio/android/*.c \
     1.6  	src/video/android/*.c \
     1.7 -	src/joystick/dummy/*.c \
     1.8 +	src/joystick/android/*.c \
     1.9  	src/haptic/dummy/*.c \
    1.10  	src/atomic/dummy/*.c \
    1.11  	src/thread/pthread/*.c \
     2.1 --- a/android/testproject/jni/app-android.cpp	Tue Jul 27 21:20:17 2010 +0200
     2.2 +++ b/android/testproject/jni/app-android.cpp	Tue Jul 27 21:21:24 2010 +0200
     2.3 @@ -34,6 +34,7 @@
     2.4  //method signatures
     2.5  jmethodID midCreateGLContext;
     2.6  jmethodID midFlipBuffers;
     2.7 +jmethodID midEnableFeature;
     2.8  
     2.9  extern "C" int SDL_main();
    2.10  extern "C" int Android_OnKeyDown(int keycode);
    2.11 @@ -41,10 +42,18 @@
    2.12  extern "C" void Android_SetScreenResolution(int width, int height);
    2.13  extern "C" void Android_OnResize(int width, int height, int format);
    2.14  extern "C" int SDL_SendQuit();
    2.15 +extern "C" void Android_EnableFeature(int featureid, bool enabled);
    2.16  
    2.17  //If we're not the active app, don't try to render
    2.18  bool bRenderingEnabled = false;
    2.19  
    2.20 +//Feature IDs
    2.21 +static const int FEATURE_SOUND = 1;
    2.22 +static const int FEATURE_ACCEL = 2;
    2.23 +
    2.24 +//Accelerometer data storage
    2.25 +float fLastAccelerometer[3];
    2.26 +
    2.27  /*******************************************************************************
    2.28                   Functions called by JNI
    2.29  *******************************************************************************/	
    2.30 @@ -67,8 +76,9 @@
    2.31      mActivityInstance = cls;
    2.32      midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    2.33      midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    2.34 +    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(I, I)V");
    2.35  
    2.36 -    if(!midCreateGLContext || !midFlipBuffers){
    2.37 +    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
    2.38          __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    2.39      }else{
    2.40          __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    2.41 @@ -84,8 +94,9 @@
    2.42  	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
    2.43  
    2.44  	mEnv = env;
    2.45 +	bRenderingEnabled = true;
    2.46  
    2.47 -	bRenderingEnabled = true;
    2.48 +	Android_EnableFeature(FEATURE_ACCEL, true);
    2.49  
    2.50      SDL_main();
    2.51  }
    2.52 @@ -152,12 +163,20 @@
    2.53      Android_OnResize(width, height, format);
    2.54  }
    2.55  
    2.56 +extern "C" void Java_org_libsdl_android_SDLActivity_onNativeAccel(
    2.57 +                                        JNIEnv*  env, jobject obj,
    2.58 +                                        jfloat x, jfloat y, jfloat z){
    2.59 +    fLastAccelerometer[0] = x;
    2.60 +    fLastAccelerometer[1] = y;
    2.61 +    fLastAccelerometer[2] = z;   
    2.62 +}
    2.63 +
    2.64  
    2.65  
    2.66  /*******************************************************************************
    2.67 -                 Functions called by SDL
    2.68 +             Functions called by SDL into Java
    2.69  *******************************************************************************/
    2.70 -extern "C" void sdl_create_context(){
    2.71 +extern "C" void Android_CreateContext(){
    2.72  	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
    2.73  
    2.74  	bRenderingEnabled = true;
    2.75 @@ -165,7 +184,7 @@
    2.76      mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
    2.77  }
    2.78  
    2.79 -extern "C" void sdl_render(){
    2.80 +extern "C" void Android_Render(){
    2.81  
    2.82      if(!bRenderingEnabled){
    2.83          return;
    2.84 @@ -175,3 +194,9 @@
    2.85      mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
    2.86  }
    2.87  
    2.88 +extern "C" void Android_EnableFeature(int featureid, bool enabled){
    2.89 +
    2.90 +    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers, 
    2.91 +                                featureid, (int)enabled); 
    2.92 +}
    2.93 +
     3.1 --- a/android/testproject/jni/lesson05.c	Tue Jul 27 21:20:17 2010 +0200
     3.2 +++ b/android/testproject/jni/lesson05.c	Tue Jul 27 21:21:24 2010 +0200
     3.3 @@ -14,6 +14,8 @@
     3.4  #include <stdlib.h>
     3.5  #include <math.h>
     3.6  
     3.7 +#include <signal.h>
     3.8 +
     3.9  #include <android/log.h>
    3.10  
    3.11  
    3.12 @@ -353,6 +355,89 @@
    3.13      return( TRUE );
    3.14  }
    3.15  
    3.16 +
    3.17 +struct
    3.18 +{
    3.19 +    SDL_AudioSpec spec;
    3.20 +    Uint8 *sound;               /* Pointer to wave data */
    3.21 +    Uint32 soundlen;            /* Length of wave data */
    3.22 +    int soundpos;               /* Current play position */
    3.23 +} wave;
    3.24 +
    3.25 +void SDLCALL
    3.26 +fillerup(void *unused, Uint8 * stream, int len)
    3.27 +{
    3.28 +    __android_log_print(ANDROID_LOG_INFO, "SDL","FILLERUP\n");
    3.29 +    
    3.30 +    Uint8 *waveptr;
    3.31 +    int waveleft;
    3.32 +
    3.33 +    /* Set up the pointers */
    3.34 +    waveptr = wave.sound + wave.soundpos;
    3.35 +    waveleft = wave.soundlen - wave.soundpos;
    3.36 +
    3.37 +    /* Go! */
    3.38 +    while (waveleft <= len) {
    3.39 +        SDL_memcpy(stream, waveptr, waveleft);
    3.40 +        stream += waveleft;
    3.41 +        len -= waveleft;
    3.42 +        waveptr = wave.sound;
    3.43 +        waveleft = wave.soundlen;
    3.44 +        wave.soundpos = 0;
    3.45 +    }
    3.46 +    SDL_memcpy(stream, waveptr, len);
    3.47 +    wave.soundpos += len;
    3.48 +}
    3.49 +
    3.50 +void testAudio(){
    3.51 +
    3.52 +    const char *file = "/sdcard/sample.wav";
    3.53 +
    3.54 +    /* Load the SDL library */
    3.55 +    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    3.56 +        __android_log_print(ANDROID_LOG_INFO, "SDL","Couldn't initialize SDL Audio: %s\n", SDL_GetError());
    3.57 +        return;
    3.58 +    }else{
    3.59 +        __android_log_print(ANDROID_LOG_INFO, "SDL","Init audio ok\n");
    3.60 +    }
    3.61 +
    3.62 +    /* Load the wave file into memory */
    3.63 +    if (SDL_LoadWAV(file, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    3.64 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't load %s: %s\n", file, SDL_GetError());
    3.65 +        return;
    3.66 +    }
    3.67 +
    3.68 +    wave.spec.callback = fillerup;
    3.69 +
    3.70 +    __android_log_print(ANDROID_LOG_INFO, "SDL","Loaded: %d\n", wave.soundlen);
    3.71 +
    3.72 +
    3.73 +    /* Initialize fillerup() variables */
    3.74 +    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
    3.75 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't open audio: %s\n", SDL_GetError());
    3.76 +        SDL_FreeWAV(wave.sound);
    3.77 +        return;
    3.78 +    }
    3.79 +
    3.80 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
    3.81 +
    3.82 +    /* Let the audio run */
    3.83 +    SDL_PauseAudio(0);
    3.84 +
    3.85 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Playing\n");
    3.86 +    
    3.87 +    while (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING){
    3.88 +         //__android_log_print(ANDROID_LOG_INFO, "SDL","Still playing\n");
    3.89 +        //SDL_Delay(100);
    3.90 +    }
    3.91 +
    3.92 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Closing down\n");
    3.93 +
    3.94 +    /* Clean up on signal */
    3.95 +    SDL_CloseAudio();
    3.96 +    SDL_FreeWAV(wave.sound);
    3.97 +}
    3.98 +
    3.99  int SDL_main( int argc, char **argv )
   3.100  {
   3.101  
   3.102 @@ -425,13 +510,8 @@
   3.103      /* resize the initial window */
   3.104      resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
   3.105  
   3.106 -    /* Load the SDL library */
   3.107 -    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   3.108 -        __android_log_print(ANDROID_LOG_INFO, "SDL","Couldn't initialize SDL Audio: %s\n", SDL_GetError());
   3.109 -        return (1);
   3.110 -    }else{
   3.111 -        __android_log_print(ANDROID_LOG_INFO, "SDL","Init audio ok\n");
   3.112 -    }
   3.113 +
   3.114 +    testAudio();
   3.115  
   3.116  
   3.117      /* wait for events */ 
     4.1 --- a/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 21:20:17 2010 +0200
     4.2 +++ b/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 21:21:24 2010 +0200
     4.3 @@ -12,6 +12,7 @@
     4.4  import android.graphics.*;
     4.5  import android.text.method.*;
     4.6  import android.text.*;
     4.7 +import android.media.*;
     4.8  
     4.9  import java.lang.*;
    4.10  
    4.11 @@ -24,6 +25,12 @@
    4.12      //Main components
    4.13      private static SDLActivity mSingleton;
    4.14      private static SDLSurface mSurface;
    4.15 +    
    4.16 +    private static AudioTrack mAudioTrack;
    4.17 +
    4.18 +    //feature IDs. Must match up on the C side as well.
    4.19 +    private static int FEATURE_SOUND = 1;
    4.20 +    private static int FEATURE_ACCEL = 2;
    4.21  
    4.22      //Load the .so
    4.23      static {
    4.24 @@ -41,9 +48,21 @@
    4.25          mSurface = new SDLSurface(getApplication());
    4.26          setContentView(mSurface);
    4.27          SurfaceHolder holder = mSurface.getHolder();
    4.28 -        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); 
    4.29 +        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    4.30 +        
    4.31 +    }
    4.32  
    4.33 -        
    4.34 +    public static boolean initAudio(){        
    4.35 +
    4.36 +        //blah. Hardcoded things are bad. FIXME when we have more sound stuff
    4.37 +        //working properly. 
    4.38 +        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
    4.39 +                    11025,
    4.40 +                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
    4.41 +                    AudioFormat.ENCODING_PCM_8BIT,
    4.42 +                    2048,
    4.43 +                    AudioTrack.MODE_STREAM);        
    4.44 +        return true;
    4.45      }
    4.46  
    4.47      //Events
    4.48 @@ -81,6 +100,32 @@
    4.49          mSurface.flipEGL();
    4.50      }
    4.51  
    4.52 +    public static void updateAudio(byte [] buf){
    4.53 +    
    4.54 +        if(mAudioTrack == null){
    4.55 +            return;
    4.56 +        }
    4.57 +        
    4.58 +        mAudioTrack.write(buf, 0, buf.length);
    4.59 +        mAudioTrack.play();
    4.60 +        
    4.61 +        Log.v("SDL","Played some audio");
    4.62 +    }
    4.63 +
    4.64 +    public static void enableFeature(int featureid, int enabled){
    4.65 +         Log.v("SDL","Feature " + featureid + " = " + enabled);
    4.66 +
    4.67 +        //Yuck. This is all horribly inelegent. If it gets to more than a few
    4.68 +        //'features' I'll rip this out and make something nicer, I promise :)
    4.69 +        if(featureid == FEATURE_SOUND){
    4.70 +            if(enabled == 1){
    4.71 +                initAudio();
    4.72 +            }else{
    4.73 +                //We don't have one of these yet...
    4.74 +                //closeAudio(); 
    4.75 +            }
    4.76 +        }
    4.77 +    }
    4.78  
    4.79  
    4.80  
    4.81 @@ -95,6 +140,8 @@
    4.82  */
    4.83  class SDLRunner implements Runnable{
    4.84      public void run(){
    4.85 +        //SDLActivity.initAudio();
    4.86 +        
    4.87          //Runs SDL_main()
    4.88          SDLActivity.nativeInit();
    4.89  
     5.1 --- a/include/SDL_config_android.h	Tue Jul 27 21:20:17 2010 +0200
     5.2 +++ b/include/SDL_config_android.h	Tue Jul 27 21:21:24 2010 +0200
     5.3 @@ -121,7 +121,7 @@
     5.4  
     5.5  #define SDL_HAPTIC_DISABLED 1
     5.6  
     5.7 -#define SDL_JOYSTICK_DISABLED 1
     5.8 +#define SDL_JOYSTICK_ANDROID 1
     5.9  
    5.10  #define SDL_LOADSO_DISABLED 1
    5.11  
     6.1 --- a/src/audio/SDL_audio.c	Tue Jul 27 21:20:17 2010 +0200
     6.2 +++ b/src/audio/SDL_audio.c	Tue Jul 27 21:21:24 2010 +0200
     6.3 @@ -320,6 +320,8 @@
     6.4  }
     6.5  
     6.6  
     6.7 +#include <android/log.h>
     6.8 +
     6.9  /* The general mixing thread function */
    6.10  int SDLCALL
    6.11  SDL_RunAudio(void *devicep)
     7.1 Binary file src/audio/android/SDL_androidaudio.o has changed
     8.1 --- a/src/video/android/SDL_androidgl.c	Tue Jul 27 21:20:17 2010 +0200
     8.2 +++ b/src/video/android/SDL_androidgl.c	Tue Jul 27 21:21:24 2010 +0200
     8.3 @@ -41,8 +41,8 @@
     8.4  /*
     8.5  These things are in the JNI android support
     8.6  */
     8.7 -extern void sdl_create_context();
     8.8 -extern void sdl_render();
     8.9 +extern void Android_CreateContext();
    8.10 +extern void Android_Render();
    8.11  
    8.12  /* GL functions */
    8.13  int Android_GL_LoadLibrary(_THIS, const char *path){
    8.14 @@ -67,7 +67,7 @@
    8.15  */
    8.16  
    8.17  SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window){
    8.18 -	sdl_create_context();
    8.19 +	Android_CreateContext();
    8.20  	return 1;
    8.21  }
    8.22  
    8.23 @@ -88,7 +88,7 @@
    8.24  }
    8.25  
    8.26  void Android_GL_SwapWindow(_THIS, SDL_Window * window){
    8.27 -	sdl_render();
    8.28 +	Android_Render();
    8.29  }
    8.30  
    8.31  void Android_GL_DeleteContext(_THIS, SDL_GLContext context){