- 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.
This commit is contained in:
parent
9ac4bd2a17
commit
ef47472280
8 changed files with 174 additions and 20 deletions
|
@ -30,7 +30,7 @@ SOURCES = \
|
|||
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 \
|
||||
|
|
|
@ -34,6 +34,7 @@ jclass mActivityInstance;
|
|||
//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" int Android_OnKeyUp(int keycode);
|
|||
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 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){
|
|||
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,9 +94,10 @@ extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv* env,
|
|||
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
|
||||
|
||||
mEnv = env;
|
||||
|
||||
bRenderingEnabled = true;
|
||||
|
||||
Android_EnableFeature(FEATURE_ACCEL, true);
|
||||
|
||||
SDL_main();
|
||||
}
|
||||
|
||||
|
@ -152,12 +163,20 @@ extern "C" void Java_org_libsdl_android_SDLActivity_onNativeResize(
|
|||
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 @@ extern "C" void sdl_create_context(){
|
|||
mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
|
||||
}
|
||||
|
||||
extern "C" void sdl_render(){
|
||||
extern "C" void Android_Render(){
|
||||
|
||||
if(!bRenderingEnabled){
|
||||
return;
|
||||
|
@ -175,3 +194,9 @@ extern "C" void sdl_render(){
|
|||
mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers );
|
||||
}
|
||||
|
||||
extern "C" void Android_EnableFeature(int featureid, bool enabled){
|
||||
|
||||
mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers,
|
||||
featureid, (int)enabled);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#include <android/log.h>
|
||||
|
||||
|
||||
|
@ -353,6 +355,89 @@ int drawGLScene( GLvoid )
|
|||
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 @@ int SDL_main( int argc, char **argv )
|
|||
/* 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 */
|
||||
|
|
|
@ -12,6 +12,7 @@ import android.util.Log;
|
|||
import android.graphics.*;
|
||||
import android.text.method.*;
|
||||
import android.text.*;
|
||||
import android.media.*;
|
||||
|
||||
import java.lang.*;
|
||||
|
||||
|
@ -25,6 +26,12 @@ public class SDLActivity extends Activity {
|
|||
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 {
|
||||
System.loadLibrary("sdltest");
|
||||
|
@ -43,7 +50,19 @@ public class SDLActivity extends Activity {
|
|||
SurfaceHolder holder = mSurface.getHolder();
|
||||
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 @@ public class SDLActivity extends Activity {
|
|||
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 @@ public class SDLActivity extends Activity {
|
|||
*/
|
||||
class SDLRunner implements Runnable{
|
||||
public void run(){
|
||||
//SDLActivity.initAudio();
|
||||
|
||||
//Runs SDL_main()
|
||||
SDLActivity.nativeInit();
|
||||
|
||||
|
|
|
@ -121,7 +121,7 @@ typedef unsigned int size_t;
|
|||
|
||||
#define SDL_HAPTIC_DISABLED 1
|
||||
|
||||
#define SDL_JOYSTICK_DISABLED 1
|
||||
#define SDL_JOYSTICK_ANDROID 1
|
||||
|
||||
#define SDL_LOADSO_DISABLED 1
|
||||
|
||||
|
|
|
@ -320,6 +320,8 @@ SDL_StreamDeinit(SDL_AudioStreamer * stream)
|
|||
}
|
||||
|
||||
|
||||
#include <android/log.h>
|
||||
|
||||
/* The general mixing thread function */
|
||||
int SDLCALL
|
||||
SDL_RunAudio(void *devicep)
|
||||
|
|
Binary file not shown.
|
@ -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 @@ int *Android_GL_GetVisual(_THIS, Display * display, int screen){
|
|||
*/
|
||||
|
||||
SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window){
|
||||
sdl_create_context();
|
||||
Android_CreateContext();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -88,7 +88,7 @@ int Android_GL_GetSwapInterval(_THIS){
|
|||
}
|
||||
|
||||
void Android_GL_SwapWindow(_THIS, SDL_Window * window){
|
||||
sdl_render();
|
||||
Android_Render();
|
||||
}
|
||||
|
||||
void Android_GL_DeleteContext(_THIS, SDL_GLContext context){
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue