changeset 4722:faa228f7ce5b

- 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.
author Paul Hunkin <paul@bieh.net>
date Tue, 27 Jul 2010 21:21:24 +0200
parents 7bb9d3a3f257
children 74da47b2f5b7
files 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
diffstat 8 files changed, 174 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.android	Tue Jul 27 21:20:17 2010 +0200
+++ b/Makefile.android	Tue Jul 27 21:21:24 2010 +0200
@@ -30,7 +30,7 @@
 	src/power/*.c \
 	src/audio/android/*.c \
 	src/video/android/*.c \
-	src/joystick/dummy/*.c \
+	src/joystick/android/*.c \
 	src/haptic/dummy/*.c \
 	src/atomic/dummy/*.c \
 	src/thread/pthread/*.c \
--- a/android/testproject/jni/app-android.cpp	Tue Jul 27 21:20:17 2010 +0200
+++ b/android/testproject/jni/app-android.cpp	Tue Jul 27 21:21:24 2010 +0200
@@ -34,6 +34,7 @@
 //method signatures
 jmethodID midCreateGLContext;
 jmethodID midFlipBuffers;
+jmethodID midEnableFeature;
 
 extern "C" int SDL_main();
 extern "C" int Android_OnKeyDown(int keycode);
@@ -41,10 +42,18 @@
 extern "C" void Android_SetScreenResolution(int width, int height);
 extern "C" void Android_OnResize(int width, int height, int format);
 extern "C" int SDL_SendQuit();
+extern "C" void Android_EnableFeature(int featureid, bool enabled);
 
 //If we're not the active app, don't try to render
 bool bRenderingEnabled = false;
 
+//Feature IDs
+static const int FEATURE_SOUND = 1;
+static const int FEATURE_ACCEL = 2;
+
+//Accelerometer data storage
+float fLastAccelerometer[3];
+
 /*******************************************************************************
                  Functions called by JNI
 *******************************************************************************/	
@@ -67,8 +76,9 @@
     mActivityInstance = cls;
     midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
     midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
+    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(I, I)V");
 
-    if(!midCreateGLContext || !midFlipBuffers){
+    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
     }else{
         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
@@ -84,8 +94,9 @@
 	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
 
 	mEnv = env;
+	bRenderingEnabled = true;
 
-	bRenderingEnabled = true;
+	Android_EnableFeature(FEATURE_ACCEL, true);
 
     SDL_main();
 }
@@ -152,12 +163,20 @@
     Android_OnResize(width, height, format);
 }
 
+extern "C" void Java_org_libsdl_android_SDLActivity_onNativeAccel(
+                                        JNIEnv*  env, jobject obj,
+                                        jfloat x, jfloat y, jfloat z){
+    fLastAccelerometer[0] = x;
+    fLastAccelerometer[1] = y;
+    fLastAccelerometer[2] = z;   
+}
+
 
 
 /*******************************************************************************
-                 Functions called by SDL
+             Functions called by SDL into Java
 *******************************************************************************/
-extern "C" void sdl_create_context(){
+extern "C" void Android_CreateContext(){
 	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
 
 	bRenderingEnabled = true;
@@ -165,7 +184,7 @@
     mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
 }
 
-extern "C" void sdl_render(){
+extern "C" void Android_Render(){
 
     if(!bRenderingEnabled){
         return;
@@ -175,3 +194,9 @@
     mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
 }
 
+extern "C" void Android_EnableFeature(int featureid, bool enabled){
+
+    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers, 
+                                featureid, (int)enabled); 
+}
+
--- a/android/testproject/jni/lesson05.c	Tue Jul 27 21:20:17 2010 +0200
+++ b/android/testproject/jni/lesson05.c	Tue Jul 27 21:21:24 2010 +0200
@@ -14,6 +14,8 @@
 #include <stdlib.h>
 #include <math.h>
 
+#include <signal.h>
+
 #include <android/log.h>
 
 
@@ -353,6 +355,89 @@
     return( TRUE );
 }
 
+
+struct
+{
+    SDL_AudioSpec spec;
+    Uint8 *sound;               /* Pointer to wave data */
+    Uint32 soundlen;            /* Length of wave data */
+    int soundpos;               /* Current play position */
+} wave;
+
+void SDLCALL
+fillerup(void *unused, Uint8 * stream, int len)
+{
+    __android_log_print(ANDROID_LOG_INFO, "SDL","FILLERUP\n");
+    
+    Uint8 *waveptr;
+    int waveleft;
+
+    /* Set up the pointers */
+    waveptr = wave.sound + wave.soundpos;
+    waveleft = wave.soundlen - wave.soundpos;
+
+    /* Go! */
+    while (waveleft <= len) {
+        SDL_memcpy(stream, waveptr, waveleft);
+        stream += waveleft;
+        len -= waveleft;
+        waveptr = wave.sound;
+        waveleft = wave.soundlen;
+        wave.soundpos = 0;
+    }
+    SDL_memcpy(stream, waveptr, len);
+    wave.soundpos += len;
+}
+
+void testAudio(){
+
+    const char *file = "/sdcard/sample.wav";
+
+    /* Load the SDL library */
+    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
+        __android_log_print(ANDROID_LOG_INFO, "SDL","Couldn't initialize SDL Audio: %s\n", SDL_GetError());
+        return;
+    }else{
+        __android_log_print(ANDROID_LOG_INFO, "SDL","Init audio ok\n");
+    }
+
+    /* Load the wave file into memory */
+    if (SDL_LoadWAV(file, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
+        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't load %s: %s\n", file, SDL_GetError());
+        return;
+    }
+
+    wave.spec.callback = fillerup;
+
+    __android_log_print(ANDROID_LOG_INFO, "SDL","Loaded: %d\n", wave.soundlen);
+
+
+    /* Initialize fillerup() variables */
+    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
+        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't open audio: %s\n", SDL_GetError());
+        SDL_FreeWAV(wave.sound);
+        return;
+    }
+
+     __android_log_print(ANDROID_LOG_INFO, "SDL","Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
+
+    /* Let the audio run */
+    SDL_PauseAudio(0);
+
+     __android_log_print(ANDROID_LOG_INFO, "SDL","Playing\n");
+    
+    while (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING){
+         //__android_log_print(ANDROID_LOG_INFO, "SDL","Still playing\n");
+        //SDL_Delay(100);
+    }
+
+     __android_log_print(ANDROID_LOG_INFO, "SDL","Closing down\n");
+
+    /* Clean up on signal */
+    SDL_CloseAudio();
+    SDL_FreeWAV(wave.sound);
+}
+
 int SDL_main( int argc, char **argv )
 {
 
@@ -425,13 +510,8 @@
     /* resize the initial window */
     resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
 
-    /* Load the SDL library */
-    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
-        __android_log_print(ANDROID_LOG_INFO, "SDL","Couldn't initialize SDL Audio: %s\n", SDL_GetError());
-        return (1);
-    }else{
-        __android_log_print(ANDROID_LOG_INFO, "SDL","Init audio ok\n");
-    }
+
+    testAudio();
 
 
     /* wait for events */ 
--- a/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 21:20:17 2010 +0200
+++ b/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jul 27 21:21:24 2010 +0200
@@ -12,6 +12,7 @@
 import android.graphics.*;
 import android.text.method.*;
 import android.text.*;
+import android.media.*;
 
 import java.lang.*;
 
@@ -24,6 +25,12 @@
     //Main components
     private static SDLActivity mSingleton;
     private static SDLSurface mSurface;
+    
+    private static AudioTrack mAudioTrack;
+
+    //feature IDs. Must match up on the C side as well.
+    private static int FEATURE_SOUND = 1;
+    private static int FEATURE_ACCEL = 2;
 
     //Load the .so
     static {
@@ -41,9 +48,21 @@
         mSurface = new SDLSurface(getApplication());
         setContentView(mSurface);
         SurfaceHolder holder = mSurface.getHolder();
-        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); 
+        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
+        
+    }
+
+    public static boolean initAudio(){        
 
-        
+        //blah. Hardcoded things are bad. FIXME when we have more sound stuff
+        //working properly. 
+        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
+                    11025,
+                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
+                    AudioFormat.ENCODING_PCM_8BIT,
+                    2048,
+                    AudioTrack.MODE_STREAM);        
+        return true;
     }
 
     //Events
@@ -81,6 +100,32 @@
         mSurface.flipEGL();
     }
 
+    public static void updateAudio(byte [] buf){
+    
+        if(mAudioTrack == null){
+            return;
+        }
+        
+        mAudioTrack.write(buf, 0, buf.length);
+        mAudioTrack.play();
+        
+        Log.v("SDL","Played some audio");
+    }
+
+    public static void enableFeature(int featureid, int enabled){
+         Log.v("SDL","Feature " + featureid + " = " + enabled);
+
+        //Yuck. This is all horribly inelegent. If it gets to more than a few
+        //'features' I'll rip this out and make something nicer, I promise :)
+        if(featureid == FEATURE_SOUND){
+            if(enabled == 1){
+                initAudio();
+            }else{
+                //We don't have one of these yet...
+                //closeAudio(); 
+            }
+        }
+    }
 
 
 
@@ -95,6 +140,8 @@
 */
 class SDLRunner implements Runnable{
     public void run(){
+        //SDLActivity.initAudio();
+        
         //Runs SDL_main()
         SDLActivity.nativeInit();
 
--- a/include/SDL_config_android.h	Tue Jul 27 21:20:17 2010 +0200
+++ b/include/SDL_config_android.h	Tue Jul 27 21:21:24 2010 +0200
@@ -121,7 +121,7 @@
 
 #define SDL_HAPTIC_DISABLED 1
 
-#define SDL_JOYSTICK_DISABLED 1
+#define SDL_JOYSTICK_ANDROID 1
 
 #define SDL_LOADSO_DISABLED 1
 
--- a/src/audio/SDL_audio.c	Tue Jul 27 21:20:17 2010 +0200
+++ b/src/audio/SDL_audio.c	Tue Jul 27 21:21:24 2010 +0200
@@ -320,6 +320,8 @@
 }
 
 
+#include <android/log.h>
+
 /* The general mixing thread function */
 int SDLCALL
 SDL_RunAudio(void *devicep)
Binary file src/audio/android/SDL_androidaudio.o has changed
--- a/src/video/android/SDL_androidgl.c	Tue Jul 27 21:20:17 2010 +0200
+++ b/src/video/android/SDL_androidgl.c	Tue Jul 27 21:21:24 2010 +0200
@@ -41,8 +41,8 @@
 /*
 These things are in the JNI android support
 */
-extern void sdl_create_context();
-extern void sdl_render();
+extern void Android_CreateContext();
+extern void Android_Render();
 
 /* GL functions */
 int Android_GL_LoadLibrary(_THIS, const char *path){
@@ -67,7 +67,7 @@
 */
 
 SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window){
-	sdl_create_context();
+	Android_CreateContext();
 	return 1;
 }
 
@@ -88,7 +88,7 @@
 }
 
 void Android_GL_SwapWindow(_THIS, SDL_Window * window){
-	sdl_render();
+	Android_Render();
 }
 
 void Android_GL_DeleteContext(_THIS, SDL_GLContext context){