src/SDL_android.cpp
author Sam Lantinga <slouken@libsdl.org>
Thu, 06 Jan 2011 16:11:21 -0800
changeset 4964 6c645018741e
child 4980 d9fdff945ec9
permissions -rw-r--r--
Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
slouken@4964
     1
/*
slouken@4964
     2
    SDL - Simple DirectMedia Layer
slouken@4964
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@4964
     4
slouken@4964
     5
    This library is free software; you can redistribute it and/or
slouken@4964
     6
    modify it under the terms of the GNU Lesser General Public
slouken@4964
     7
    License as published by the Free Software Foundation; either
slouken@4964
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@4964
     9
slouken@4964
    10
    This library is distributed in the hope that it will be useful,
slouken@4964
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@4964
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@4964
    13
    Lesser General Public License for more details.
slouken@4964
    14
slouken@4964
    15
    You should have received a copy of the GNU Lesser General Public
slouken@4964
    16
    License along with this library; if not, write to the Free Software
slouken@4964
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@4964
    18
slouken@4964
    19
    Sam Lantinga
slouken@4964
    20
    slouken@libsdl.org
slouken@4964
    21
*/
slouken@4964
    22
#include "SDL_config.h"
slouken@4964
    23
slouken@4964
    24
/*******************************************************************************
slouken@4964
    25
 This file links the Java side of Android with libsdl
slouken@4964
    26
*******************************************************************************/
slouken@4964
    27
#include <jni.h>
slouken@4964
    28
#include <android/log.h>
slouken@4964
    29
slouken@4964
    30
slouken@4964
    31
/*******************************************************************************
slouken@4964
    32
                               Globals
slouken@4964
    33
*******************************************************************************/
slouken@4964
    34
JavaVM* mVM = NULL;
slouken@4964
    35
JNIEnv* mEnv = NULL;
slouken@4964
    36
JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
slouken@4964
    37
slouken@4964
    38
//Main activity
slouken@4964
    39
jclass mActivityInstance;
slouken@4964
    40
slouken@4964
    41
//method signatures
slouken@4964
    42
jmethodID midCreateGLContext;
slouken@4964
    43
jmethodID midFlipBuffers;
slouken@4964
    44
jmethodID midEnableFeature;
slouken@4964
    45
jmethodID midUpdateAudio;
slouken@4964
    46
slouken@4964
    47
extern "C" int Android_OnKeyDown(int keycode);
slouken@4964
    48
extern "C" int Android_OnKeyUp(int keycode);
slouken@4964
    49
extern "C" void Android_SetScreenResolution(int width, int height);
slouken@4964
    50
extern "C" void Android_OnResize(int width, int height, int format);
slouken@4964
    51
extern "C" int SDL_SendQuit();
slouken@4964
    52
extern "C" void Android_EnableFeature(int featureid, bool enabled);
slouken@4964
    53
slouken@4964
    54
//If we're not the active app, don't try to render
slouken@4964
    55
bool bRenderingEnabled = false;
slouken@4964
    56
slouken@4964
    57
//Feature IDs
slouken@4964
    58
static const int FEATURE_AUDIO = 1;
slouken@4964
    59
static const int FEATURE_ACCEL = 2;
slouken@4964
    60
slouken@4964
    61
//Accelerometer data storage
slouken@4964
    62
float fLastAccelerometer[3];
slouken@4964
    63
slouken@4964
    64
slouken@4964
    65
/*******************************************************************************
slouken@4964
    66
                 Functions called by JNI
slouken@4964
    67
*******************************************************************************/
slouken@4964
    68
slouken@4964
    69
// Library init
slouken@4964
    70
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
    71
{
slouken@4964
    72
    mVM = vm;
slouken@4964
    73
slouken@4964
    74
    return JNI_VERSION_1_4;
slouken@4964
    75
}
slouken@4964
    76
slouken@4964
    77
// Called before SDL_main() to initialize JNI bindings
slouken@4964
    78
extern "C" void SDL_Android_Init(JNIEnv* env)
slouken@4964
    79
{
slouken@4964
    80
    mEnv = env;
slouken@4964
    81
slouken@4964
    82
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
    83
slouken@4964
    84
    jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity"); 
slouken@4964
    85
    mActivityInstance = cls;
slouken@4964
    86
    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
slouken@4964
    87
    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
slouken@4964
    88
    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
slouken@4964
    89
    midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
slouken@4964
    90
slouken@4964
    91
    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
slouken@4964
    92
        !midUpdateAudio) {
slouken@4964
    93
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
slouken@4964
    94
    } else {
slouken@4964
    95
#ifdef DEBUG
slouken@4964
    96
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
slouken@4964
    97
#endif
slouken@4964
    98
    }
slouken@4964
    99
}
slouken@4964
   100
slouken@4964
   101
// Keydown
slouken@4964
   102
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env, 
slouken@4964
   103
               jobject obj, jint keycode)
slouken@4964
   104
{
slouken@4964
   105
    int r = Android_OnKeyDown(keycode);
slouken@4964
   106
#ifdef DEBUG
slouken@4964
   107
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   108
                        "SDL: native key down %d, %d\n", keycode, r);
slouken@4964
   109
#endif
slouken@4964
   110
}
slouken@4964
   111
slouken@4964
   112
// Keyup
slouken@4964
   113
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env, 
slouken@4964
   114
               jobject obj, jint keycode)
slouken@4964
   115
{
slouken@4964
   116
    int r = Android_OnKeyUp(keycode);
slouken@4964
   117
#ifdef DEBUG
slouken@4964
   118
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   119
                        "SDL: native key up %d, %d\n", keycode, r);
slouken@4964
   120
#endif
slouken@4964
   121
}
slouken@4964
   122
slouken@4964
   123
// Touch
slouken@4964
   124
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env, 
slouken@4964
   125
               jobject obj, jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   126
{
slouken@4964
   127
#ifdef DEBUG
slouken@4964
   128
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   129
                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
slouken@4964
   130
                        action, x, y, p);
slouken@4964
   131
#endif
slouken@4964
   132
slouken@4964
   133
    //TODO: Pass this off to the SDL multitouch stuff
slouken@4964
   134
}
slouken@4964
   135
slouken@4964
   136
// Quit
slouken@4964
   137
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv*  env, 
slouken@4964
   138
                                                                jobject obj )
slouken@4964
   139
{    
slouken@4964
   140
    // Stop rendering as we're no longer in the foreground
slouken@4964
   141
    bRenderingEnabled = false;
slouken@4964
   142
slouken@4964
   143
    // Inject a SDL_QUIT event
slouken@4964
   144
    SDL_SendQuit();
slouken@4964
   145
}
slouken@4964
   146
slouken@4964
   147
// Screen size
slouken@4964
   148
extern "C" void Java_org_libsdl_app_SDLActivity_nativeSetScreenSize(
slouken@4964
   149
                JNIEnv*  env, jobject obj, jint width, jint height)
slouken@4964
   150
{
slouken@4964
   151
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   152
                        "SDL: Set screen size on init: %d/%d\n", width, height);
slouken@4964
   153
    Android_SetScreenResolution(width, height);
slouken@4964
   154
}
slouken@4964
   155
slouken@4964
   156
// Resize
slouken@4964
   157
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4964
   158
                                        JNIEnv*  env, jobject obj, jint width, 
slouken@4964
   159
                                        jint height, jint format)
slouken@4964
   160
{
slouken@4964
   161
    Android_OnResize(width, height, format);
slouken@4964
   162
}
slouken@4964
   163
slouken@4964
   164
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4964
   165
                                        JNIEnv*  env, jobject obj,
slouken@4964
   166
                                        jfloat x, jfloat y, jfloat z)
slouken@4964
   167
{
slouken@4964
   168
    fLastAccelerometer[0] = x;
slouken@4964
   169
    fLastAccelerometer[1] = y;
slouken@4964
   170
    fLastAccelerometer[2] = z;   
slouken@4964
   171
}
slouken@4964
   172
slouken@4964
   173
slouken@4964
   174
slouken@4964
   175
/*******************************************************************************
slouken@4964
   176
             Functions called by SDL into Java
slouken@4964
   177
*******************************************************************************/
slouken@4964
   178
extern "C" void Android_CreateContext()
slouken@4964
   179
{
slouken@4964
   180
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
slouken@4964
   181
slouken@4964
   182
    bRenderingEnabled = true;
slouken@4964
   183
slouken@4964
   184
    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
slouken@4964
   185
}
slouken@4964
   186
slouken@4964
   187
extern "C" void Android_Render()
slouken@4964
   188
{
slouken@4964
   189
    if (!bRenderingEnabled) {
slouken@4964
   190
        return;
slouken@4964
   191
    }
slouken@4964
   192
slouken@4964
   193
    // When we get here, we've accumulated a full frame    
slouken@4964
   194
    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers); 
slouken@4964
   195
}
slouken@4964
   196
slouken@4964
   197
extern "C" void Android_EnableFeature(int featureid, bool enabled)
slouken@4964
   198
{
slouken@4964
   199
    mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
slouken@4964
   200
                                featureid, (int)enabled); 
slouken@4964
   201
}
slouken@4964
   202
slouken@4964
   203
extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len)
slouken@4964
   204
{
slouken@4964
   205
    //Annoyingly we can't just call into Java from any thread. Because the audio
slouken@4964
   206
    //callback is dispatched from the SDL audio thread (that wasn't made from
slouken@4964
   207
    //java, we have to do some magic here to let the JVM know about the thread.
slouken@4964
   208
    //Because everything it touches on the Java side is static anyway, it's 
slouken@4964
   209
    //not a big deal, just annoying.
slouken@4964
   210
    if(!mAudioThreadEnv) {
slouken@4964
   211
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
slouken@4964
   212
slouken@4964
   213
        mVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
slouken@4964
   214
slouken@4964
   215
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
slouken@4964
   216
    }
slouken@4964
   217
    
slouken@4964
   218
    jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
slouken@4964
   219
slouken@4964
   220
    //blah. We probably should rework this so we avoid the copy. 
slouken@4964
   221
    mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
slouken@4964
   222
    
slouken@4964
   223
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
slouken@4964
   224
slouken@4964
   225
    mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
slouken@4964
   226
                                            midUpdateAudio, arr );
slouken@4964
   227
slouken@4964
   228
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
slouken@4964
   229
    
slouken@4964
   230
}
slouken@4964
   231