Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
- Cleaned up a bunch of code
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
bieh committed Jul 27, 2010
1 parent 7cec8fc commit d269743
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile.android
Expand Up @@ -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 \
Expand Down
35 changes: 30 additions & 5 deletions android/testproject/jni/app-android.cpp
Expand Up @@ -34,17 +34,26 @@ jclass mActivityInstance;
//method signatures
jmethodID midCreateGLContext;
jmethodID midFlipBuffers;
jmethodID midEnableFeature;

extern "C" int SDL_main();
extern "C" int Android_OnKeyDown(int keycode);
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
*******************************************************************************/
Expand All @@ -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");
Expand All @@ -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();
}

Expand Down Expand Up @@ -152,20 +163,28 @@ 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;

mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext );
}

extern "C" void sdl_render(){
extern "C" void Android_Render(){

if(!bRenderingEnabled){
return;
Expand All @@ -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);
}

94 changes: 87 additions & 7 deletions android/testproject/jni/lesson05.c
Expand Up @@ -14,6 +14,8 @@
#include <stdlib.h>
#include <math.h>

#include <signal.h>

#include <android/log.h>


Expand Down Expand Up @@ -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 )
{

Expand Down Expand Up @@ -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 */
Expand Down
51 changes: 49 additions & 2 deletions android/testproject/src/org/libsdl/android/SDLActivity.java
Expand Up @@ -12,6 +12,7 @@
import android.graphics.*;
import android.text.method.*;
import android.text.*;
import android.media.*;

import java.lang.*;

Expand All @@ -24,6 +25,12 @@ public class SDLActivity extends Activity {
//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 {
Expand All @@ -41,11 +48,23 @@ protected void onCreate(Bundle savedInstanceState) {
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
protected void onPause() {
super.onPause();
Expand Down Expand Up @@ -81,6 +100,32 @@ public static void flipBuffers(){
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();
}
}
}



Expand All @@ -95,6 +140,8 @@ Simple nativeInit() runnable
*/
class SDLRunner implements Runnable{
public void run(){
//SDLActivity.initAudio();

//Runs SDL_main()
SDLActivity.nativeInit();

Expand Down
2 changes: 1 addition & 1 deletion include/SDL_config_android.h
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/audio/SDL_audio.c
Expand Up @@ -320,6 +320,8 @@ SDL_StreamDeinit(SDL_AudioStreamer * stream)
}


#include <android/log.h>

/* The general mixing thread function */
int SDLCALL
SDL_RunAudio(void *devicep)
Expand Down
Binary file modified src/audio/android/SDL_androidaudio.o
Binary file not shown.
8 changes: 4 additions & 4 deletions src/video/android/SDL_androidgl.c
Expand Up @@ -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){
Expand All @@ -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;
}

Expand All @@ -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){
Expand Down

0 comments on commit d269743

Please sign in to comment.