# HG changeset patch # User Paul Hunkin # Date 1280258484 -7200 # Node ID faa228f7ce5b8c9bc973be5eb38219c6d4f6f1df # Parent 7bb9d3a3f2572e39be9eb99b66e892458c10e9ac - 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. diff -r 7bb9d3a3f257 -r faa228f7ce5b Makefile.android --- 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 \ diff -r 7bb9d3a3f257 -r faa228f7ce5b android/testproject/jni/app-android.cpp --- 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); +} + diff -r 7bb9d3a3f257 -r faa228f7ce5b android/testproject/jni/lesson05.c --- 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 #include +#include + #include @@ -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 */ diff -r 7bb9d3a3f257 -r faa228f7ce5b android/testproject/src/org/libsdl/android/SDLActivity.java --- 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(); diff -r 7bb9d3a3f257 -r faa228f7ce5b include/SDL_config_android.h --- 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 diff -r 7bb9d3a3f257 -r faa228f7ce5b src/audio/SDL_audio.c --- 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 + /* The general mixing thread function */ int SDLCALL SDL_RunAudio(void *devicep) diff -r 7bb9d3a3f257 -r faa228f7ce5b src/audio/android/SDL_androidaudio.o Binary file src/audio/android/SDL_androidaudio.o has changed diff -r 7bb9d3a3f257 -r faa228f7ce5b src/video/android/SDL_androidgl.c --- 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){