Cleanups.
authorPaul Hunkin <paul@bieh.net>
Tue, 17 Aug 2010 15:35:56 +1200
changeset 47269076cdb027af
parent 4725 4eb9d3c7fdd2
child 4727 8087bb208acf
Cleanups.

- Moved to 'project' instead of 'testproject'
- Removed extraneous .c files
- Removed the android_libs folder (that was against the NDK agreement anyway)
README.android
android/project/AndroidManifest.xml
android/project/build.properties
android/project/build.xml
android/project/default.properties
android/project/jni/Android.mk
android/project/jni/android-support.cpp
android/project/jni/lesson05.c
android/project/local.properties
android/project/res/drawable-hdpi/icon.png
android/project/res/drawable-ldpi/icon.png
android/project/res/drawable-mdpi/icon.png
android/project/res/layout/main.xml
android/project/res/values/strings.xml
android/project/src/org/libsdl/app/SDLActivity.java
android/testproject/jni/app-android.cpp
build-scripts/android_libs/libEGL.so
build-scripts/android_libs/libcutils.so
build-scripts/android_libs/libutils.so
     1.1 --- a/README.android	Sat Aug 14 16:17:17 2010 +1200
     1.2 +++ b/README.android	Tue Aug 17 15:35:56 2010 +1200
     1.3 @@ -1,5 +1,5 @@
     1.4  ==============================================================================
     1.5 -Building the Simple DirectMedia Layer for Android
     1.6 +Simple DirectMedia Layer for Android
     1.7  ==============================================================================
     1.8  
     1.9  Requirements: Android NDK r4 or later
    1.10 @@ -11,6 +11,6 @@
    1.11  4. Run 'ndk-build' (a script provided by the NDK). This compiles the C source
    1.12  4. Run 'ant' in android/testproject. This compiles the .java and eventually 
    1.13  creates a .apk with the C source embedded
    1.14 -6. 'ant install' will push the apk to the device
    1.15 +6. 'ant install' will push the apk to the device or emulator (if connected)
    1.16  
    1.17  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/android/project/AndroidManifest.xml	Tue Aug 17 15:35:56 2010 +1200
     2.3 @@ -0,0 +1,15 @@
     2.4 +<?xml version="1.0" encoding="utf-8"?>
     2.5 +<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     2.6 +      package="org.libsdl.app"
     2.7 +      android:versionCode="1"
     2.8 +      android:versionName="1.0">
     2.9 +    <application android:label="@string/app_name" android:icon="@drawable/icon">
    2.10 +        <activity android:name="SDLActivity"
    2.11 +                  android:label="@string/app_name">
    2.12 +            <intent-filter>
    2.13 +                <action android:name="android.intent.action.MAIN" />
    2.14 +                <category android:name="android.intent.category.LAUNCHER" />
    2.15 +            </intent-filter>
    2.16 +        </activity>
    2.17 +    </application>
    2.18 +</manifest> 
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/android/project/build.properties	Tue Aug 17 15:35:56 2010 +1200
     3.3 @@ -0,0 +1,17 @@
     3.4 +# This file is used to override default values used by the Ant build system.
     3.5 +# 
     3.6 +# This file must be checked in Version Control Systems, as it is
     3.7 +# integral to the build system of your project.
     3.8 +
     3.9 +# This file is only used by the Ant script.
    3.10 +
    3.11 +# You can use this to override default values such as
    3.12 +#  'source.dir' for the location of your java source folder and
    3.13 +#  'out.dir' for the location of your output folder.
    3.14 +
    3.15 +# You can also use it define how the release builds are signed by declaring
    3.16 +# the following properties:
    3.17 +#  'key.store' for the location of your keystore and
    3.18 +#  'key.alias' for the name of the key to use.
    3.19 +# The password will be asked during the build when you use the 'release' target.
    3.20 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/android/project/build.xml	Tue Aug 17 15:35:56 2010 +1200
     4.3 @@ -0,0 +1,67 @@
     4.4 +<?xml version="1.0" encoding="UTF-8"?>
     4.5 +<project name="SDLApp" default="help">
     4.6 +
     4.7 +    <!-- The local.properties file is created and updated by the 'android' tool.
     4.8 +         It contains the path to the SDK. It should *NOT* be checked in in Version
     4.9 +         Control Systems. -->
    4.10 +    <property file="local.properties" />
    4.11 +
    4.12 +    <!-- The build.properties file can be created by you and is never touched
    4.13 +         by the 'android' tool. This is the place to change some of the default property values
    4.14 +         used by the Ant rules.
    4.15 +         Here are some properties you may want to change/update:
    4.16 +
    4.17 +         application.package
    4.18 +             the name of your application package as defined in the manifest. Used by the
    4.19 +             'uninstall' rule.
    4.20 +         source.dir
    4.21 +             the name of the source directory. Default is 'src'.
    4.22 +         out.dir
    4.23 +             the name of the output directory. Default is 'bin'.
    4.24 +
    4.25 +         Properties related to the SDK location or the project target should be updated
    4.26 +          using the 'android' tool with the 'update' action.
    4.27 +
    4.28 +         This file is an integral part of the build system for your application and
    4.29 +         should be checked in in Version Control Systems.
    4.30 +
    4.31 +         -->
    4.32 +    <property file="build.properties" />
    4.33 +
    4.34 +    <!-- The default.properties file is created and updated by the 'android' tool, as well
    4.35 +         as ADT.
    4.36 +         This file is an integral part of the build system for your application and
    4.37 +         should be checked in in Version Control Systems. -->
    4.38 +    <property file="default.properties" />
    4.39 +
    4.40 +    <!-- Custom Android task to deal with the project target, and import the proper rules.
    4.41 +         This requires ant 1.6.0 or above. -->
    4.42 +    <path id="android.antlibs">
    4.43 +        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
    4.44 +        <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
    4.45 +        <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
    4.46 +        <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
    4.47 +        <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
    4.48 +    </path>
    4.49 +
    4.50 +    <taskdef name="setup"
    4.51 +        classname="com.android.ant.SetupTask"
    4.52 +        classpathref="android.antlibs" />
    4.53 +
    4.54 +    <!-- Execute the Android Setup task that will setup some properties specific to the target,
    4.55 +         and import the build rules files.
    4.56 +
    4.57 +         The rules file is imported from
    4.58 +            <SDK>/platforms/<target_platform>/templates/android_rules.xml
    4.59 +
    4.60 +         To customize some build steps for your project:
    4.61 +         - copy the content of the main node <project> from android_rules.xml
    4.62 +         - paste it in this build.xml below the <setup /> task.
    4.63 +         - disable the import by changing the setup task below to <setup import="false" />
    4.64 +
    4.65 +         This will ensure that the properties are setup correctly but that your customized
    4.66 +         build steps are used.
    4.67 +    -->
    4.68 +    <setup />
    4.69 +
    4.70 +</project>
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/android/project/default.properties	Tue Aug 17 15:35:56 2010 +1200
     5.3 @@ -0,0 +1,11 @@
     5.4 +# This file is automatically generated by Android Tools.
     5.5 +# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
     5.6 +# 
     5.7 +# This file must be checked in Version Control Systems.
     5.8 +# 
     5.9 +# To customize properties used by the Ant build system use,
    5.10 +# "build.properties", and override values to adapt the script to your
    5.11 +# project structure.
    5.12 +
    5.13 +# Project target.
    5.14 +target=android-7
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/android/project/jni/Android.mk	Tue Aug 17 15:35:56 2010 +1200
     6.3 @@ -0,0 +1,18 @@
     6.4 +LOCAL_PATH := $(call my-dir)
     6.5 +
     6.6 +include $(CLEAR_VARS)
     6.7 +
     6.8 +LOCAL_MODULE := sdlapp
     6.9 +SDL := ../../../
    6.10 +
    6.11 +LOCAL_CFLAGS := -DANDROID_NDK \
    6.12 +                -DDISABLE_IMPORTGL \
    6.13 +                -I$(SDL)/include
    6.14 +
    6.15 +LOCAL_SRC_FILES := \
    6.16 +    android-support.cpp \
    6.17 +    lesson05.c \
    6.18 +
    6.19 +LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog -lSDL -lgcc -L$(SDL)
    6.20 +
    6.21 +include $(BUILD_SHARED_LIBRARY)
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/android/project/jni/android-support.cpp	Tue Aug 17 15:35:56 2010 +1200
     7.3 @@ -0,0 +1,242 @@
     7.4 +/*******************************************************************************
     7.5 + This file links the Java side of Android with libsdl
     7.6 +*******************************************************************************/
     7.7 +#include <jni.h>
     7.8 +#include <sys/time.h>
     7.9 +#include <time.h>
    7.10 +#include <android/log.h>
    7.11 +#include <stdint.h>
    7.12 +#include <stdio.h>
    7.13 +#include <stdlib.h>
    7.14 +#include <math.h>
    7.15 +#include <pthread.h>
    7.16 +
    7.17 +#define DEBUG
    7.18 +
    7.19 +
    7.20 +/*******************************************************************************
    7.21 +                               Globals
    7.22 +*******************************************************************************/
    7.23 +static long _getTime(void){
    7.24 +	struct timeval  now;
    7.25 +	gettimeofday(&now, NULL);
    7.26 +	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
    7.27 +}
    7.28 +
    7.29 +JNIEnv* mEnv = NULL;
    7.30 +JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
    7.31 +JavaVM* mVM = NULL;
    7.32 +
    7.33 +//Main activity
    7.34 +jclass mActivityInstance;
    7.35 +
    7.36 +//method signatures
    7.37 +jmethodID midCreateGLContext;
    7.38 +jmethodID midFlipBuffers;
    7.39 +jmethodID midEnableFeature;
    7.40 +jmethodID midUpdateAudio;
    7.41 +
    7.42 +extern "C" int SDL_main();
    7.43 +extern "C" int Android_OnKeyDown(int keycode);
    7.44 +extern "C" int Android_OnKeyUp(int keycode);
    7.45 +extern "C" void Android_SetScreenResolution(int width, int height);
    7.46 +extern "C" void Android_OnResize(int width, int height, int format);
    7.47 +extern "C" int SDL_SendQuit();
    7.48 +extern "C" void Android_EnableFeature(int featureid, bool enabled);
    7.49 +
    7.50 +//If we're not the active app, don't try to render
    7.51 +bool bRenderingEnabled = false;
    7.52 +
    7.53 +//Feature IDs
    7.54 +static const int FEATURE_AUDIO = 1;
    7.55 +static const int FEATURE_ACCEL = 2;
    7.56 +
    7.57 +//Accelerometer data storage
    7.58 +float fLastAccelerometer[3];
    7.59 +
    7.60 +
    7.61 +/*******************************************************************************
    7.62 +                 Functions called by JNI
    7.63 +*******************************************************************************/	
    7.64 +
    7.65 +//Library init
    7.66 +extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){
    7.67 +
    7.68 +    JNIEnv* env = NULL;
    7.69 +    jint result = -1;
    7.70 +
    7.71 +    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
    7.72 +        return result;
    7.73 +    }
    7.74 +
    7.75 +    mEnv = env;
    7.76 +
    7.77 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
    7.78 +
    7.79 +    jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity"); 
    7.80 +    mActivityInstance = cls;
    7.81 +    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    7.82 +    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    7.83 +    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
    7.84 +    midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
    7.85 +
    7.86 +    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
    7.87 +        !midUpdateAudio){
    7.88 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    7.89 +    }else{
    7.90 +#ifdef DEBUG
    7.91 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    7.92 +#endif
    7.93 +    }
    7.94 +    
    7.95 +    return JNI_VERSION_1_4;
    7.96 +}
    7.97 +
    7.98 +//Start up the SDL app
    7.99 +extern "C" void Java_org_libsdl_app_SDLActivity_nativeInit( JNIEnv* env, 
   7.100 +                                                                jobject obj ){ 
   7.101 +                                                                   
   7.102 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
   7.103 +
   7.104 +	mEnv = env;
   7.105 +	bRenderingEnabled = true;
   7.106 +
   7.107 +	Android_EnableFeature(FEATURE_ACCEL, true);
   7.108 +
   7.109 +    SDL_main();
   7.110 +}
   7.111 +
   7.112 +//Keydown
   7.113 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env, 
   7.114 +               jobject obj, jint keycode){
   7.115 +    
   7.116 +    int r = Android_OnKeyDown(keycode);
   7.117 +#ifdef DEBUG
   7.118 +    __android_log_print(ANDROID_LOG_INFO, "SDL", 
   7.119 +                        "SDL: native key down %d, %d\n", keycode, r);
   7.120 +#endif
   7.121 +                        
   7.122 +}
   7.123 +
   7.124 +//Keyup
   7.125 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env, 
   7.126 +               jobject obj, jint keycode){
   7.127 +    
   7.128 +    int r = Android_OnKeyUp(keycode);
   7.129 +#ifdef DEBUG
   7.130 +    __android_log_print(ANDROID_LOG_INFO, "SDL", 
   7.131 +                        "SDL: native key up %d, %d\n", keycode, r);
   7.132 +#endif
   7.133 +                        
   7.134 +}
   7.135 +
   7.136 +//Touch
   7.137 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env, 
   7.138 +               jobject obj, jint action, jfloat x, jfloat y, jfloat p){
   7.139 +
   7.140 +#ifdef DEBUG
   7.141 +    __android_log_print(ANDROID_LOG_INFO, "SDL", 
   7.142 +                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
   7.143 +                        action, x, y, p);
   7.144 +#endif
   7.145 +
   7.146 +    //TODO: Pass this off to the SDL multitouch stuff
   7.147 +                        
   7.148 +}
   7.149 +
   7.150 +//Quit
   7.151 +extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv*  env, 
   7.152 +                                                                jobject obj ){    
   7.153 +
   7.154 +    //Stop rendering as we're no longer in the foreground
   7.155 +	bRenderingEnabled = false;
   7.156 +
   7.157 +    //Inject a SDL_QUIT event
   7.158 +    int r = SDL_SendQuit();
   7.159 +
   7.160 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native quit %d", r);        
   7.161 +}
   7.162 +
   7.163 +//Screen size
   7.164 +extern "C" void Java_org_libsdl_app_SDLActivity_nativeSetScreenSize(
   7.165 +                JNIEnv*  env, jobject obj, jint width, jint height){
   7.166 +
   7.167 +    __android_log_print(ANDROID_LOG_INFO, "SDL", 
   7.168 +                        "SDL: Set screen size on init: %d/%d\n", width, height);
   7.169 +    Android_SetScreenResolution(width, height);
   7.170 +                        
   7.171 +}
   7.172 +
   7.173 +//Resize
   7.174 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
   7.175 +                                        JNIEnv*  env, jobject obj, jint width, 
   7.176 +                                        jint height, jint format){
   7.177 +    Android_OnResize(width, height, format);
   7.178 +}
   7.179 +
   7.180 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
   7.181 +                                        JNIEnv*  env, jobject obj,
   7.182 +                                        jfloat x, jfloat y, jfloat z){
   7.183 +    fLastAccelerometer[0] = x;
   7.184 +    fLastAccelerometer[1] = y;
   7.185 +    fLastAccelerometer[2] = z;   
   7.186 +}
   7.187 +
   7.188 +
   7.189 +
   7.190 +/*******************************************************************************
   7.191 +             Functions called by SDL into Java
   7.192 +*******************************************************************************/
   7.193 +extern "C" void Android_CreateContext(){
   7.194 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
   7.195 +
   7.196 +	bRenderingEnabled = true;
   7.197 +
   7.198 +    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
   7.199 +}
   7.200 +
   7.201 +extern "C" void Android_Render(){
   7.202 +
   7.203 +    if(!bRenderingEnabled){
   7.204 +        return;
   7.205 +    }
   7.206 +
   7.207 +    //When we get here, we've accumulated a full frame    
   7.208 +    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
   7.209 +}
   7.210 +
   7.211 +extern "C" void Android_EnableFeature(int featureid, bool enabled){
   7.212 +
   7.213 +    mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
   7.214 +                                featureid, (int)enabled); 
   7.215 +}
   7.216 +
   7.217 +extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len){
   7.218 +
   7.219 +    //Annoyingly we can't just call into Java from any thread. Because the audio
   7.220 +    //callback is dispatched from the SDL audio thread (that wasn't made from
   7.221 +    //java, we have to do some magic here to let the JVM know about the thread.
   7.222 +    //Because everything it touches on the Java side is static anyway, it's 
   7.223 +    //not a big deal, just annoying.
   7.224 +    if(!mAudioThreadEnv){
   7.225 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
   7.226 +
   7.227 +        mVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
   7.228 +
   7.229 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
   7.230 +    }
   7.231 +    
   7.232 +    jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
   7.233 +
   7.234 +    //blah. We probably should rework this so we avoid the copy. 
   7.235 +    mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
   7.236 +    
   7.237 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
   7.238 +
   7.239 +    mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
   7.240 +                                            midUpdateAudio, arr );
   7.241 +
   7.242 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
   7.243 +    
   7.244 +}
   7.245 +
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/android/project/jni/lesson05.c	Tue Aug 17 15:35:56 2010 +1200
     8.3 @@ -0,0 +1,574 @@
     8.4 +/*
     8.5 + * This code was created by Jeff Molofee '99 
     8.6 + * (ported to Linux/SDL by Ti Leggett '01)
     8.7 + *
     8.8 + * If you've found this code useful, please let me know.
     8.9 + *
    8.10 + * Visit Jeff at http://nehe.gamedev.net/
    8.11 + * 
    8.12 + * or for port-specific comments, questions, bugreports etc. 
    8.13 + * email to leggett@eecs.tulane.edu
    8.14 + */
    8.15 + 
    8.16 +#include <stdio.h>
    8.17 +#include <stdlib.h>
    8.18 +#include <math.h>
    8.19 +
    8.20 +#include <signal.h>
    8.21 +
    8.22 +#include <android/log.h>
    8.23 +
    8.24 +
    8.25 +#ifdef ANDROID
    8.26 +#include <GLES/gl.h>
    8.27 +#else
    8.28 +#include <GL/gl.h>
    8.29 +#include <GL/glu.h>
    8.30 +#endif
    8.31 +#include "SDL.h"
    8.32 +
    8.33 +/* screen width, height, and bit depth */
    8.34 +#define SCREEN_WIDTH  320
    8.35 +#define SCREEN_HEIGHT 430
    8.36 +#define SCREEN_BPP     16
    8.37 +
    8.38 +/* Define our booleans */
    8.39 +#define TRUE  1
    8.40 +#define FALSE 0
    8.41 +
    8.42 +/* This is our SDL surface */
    8.43 +SDL_Surface *surface;
    8.44 +
    8.45 +int rotation = 0;
    8.46 +
    8.47 +
    8.48 +/**************************************
    8.49 +	gluperspective implementation
    8.50 +**************************************/
    8.51 +void gluPerspective(double fovy, double aspect, double zNear, double zFar){
    8.52 +	glMatrixMode(GL_PROJECTION);
    8.53 +	glLoadIdentity();
    8.54 +	double xmin, xmax, ymin, ymax;
    8.55 +	ymax = zNear * tan(fovy * M_PI / 360.0);
    8.56 +	ymin = -ymax;
    8.57 +	xmin = ymin * aspect;
    8.58 +	xmax = ymax * aspect;
    8.59 +	glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
    8.60 +}
    8.61 +
    8.62 +
    8.63 +/**************************************
    8.64 +	  glulookat implementation
    8.65 +**************************************/
    8.66 +void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
    8.67 +          GLfloat centerx, GLfloat centery, GLfloat centerz,
    8.68 +          GLfloat upx, GLfloat upy, GLfloat upz)
    8.69 +{
    8.70 +    GLfloat m[16];
    8.71 +    GLfloat x[3], y[3], z[3];
    8.72 +    GLfloat mag;
    8.73 +    
    8.74 +    /* Make rotation matrix */
    8.75 +    
    8.76 +    /* Z vector */
    8.77 +    z[0] = eyex - centerx;
    8.78 +    z[1] = eyey - centery;
    8.79 +    z[2] = eyez - centerz;
    8.80 +    mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
    8.81 +    if (mag) {          /* mpichler, 19950515 */
    8.82 +        z[0] /= mag;
    8.83 +        z[1] /= mag;
    8.84 +        z[2] /= mag;
    8.85 +    }
    8.86 +    
    8.87 +    /* Y vector */
    8.88 +    y[0] = upx;
    8.89 +    y[1] = upy;
    8.90 +    y[2] = upz;
    8.91 +    
    8.92 +    /* X vector = Y cross Z */
    8.93 +    x[0] = y[1] * z[2] - y[2] * z[1];
    8.94 +    x[1] = -y[0] * z[2] + y[2] * z[0];
    8.95 +    x[2] = y[0] * z[1] - y[1] * z[0];
    8.96 +    
    8.97 +    /* Recompute Y = Z cross X */
    8.98 +    y[0] = z[1] * x[2] - z[2] * x[1];
    8.99 +    y[1] = -z[0] * x[2] + z[2] * x[0];
   8.100 +    y[2] = z[0] * x[1] - z[1] * x[0];
   8.101 +    
   8.102 +    /* mpichler, 19950515 */
   8.103 +    /* cross product gives area of parallelogram, which is < 1.0 for
   8.104 +     * non-perpendicular unit-length vectors; so normalize x, y here
   8.105 +     */
   8.106 +    
   8.107 +    mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
   8.108 +    if (mag) {
   8.109 +        x[0] /= mag;
   8.110 +        x[1] /= mag;
   8.111 +        x[2] /= mag;
   8.112 +    }
   8.113 +    
   8.114 +    mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
   8.115 +    if (mag) {
   8.116 +        y[0] /= mag;
   8.117 +        y[1] /= mag;
   8.118 +        y[2] /= mag;
   8.119 +    }
   8.120 +    
   8.121 +#define M(row,col)  m[col*4+row]
   8.122 +    M(0, 0) = x[0];
   8.123 +    M(0, 1) = x[1];
   8.124 +    M(0, 2) = x[2];
   8.125 +    M(0, 3) = 0.0;
   8.126 +    M(1, 0) = y[0];
   8.127 +    M(1, 1) = y[1];
   8.128 +    M(1, 2) = y[2];
   8.129 +    M(1, 3) = 0.0;
   8.130 +    M(2, 0) = z[0];
   8.131 +    M(2, 1) = z[1];
   8.132 +    M(2, 2) = z[2];
   8.133 +    M(2, 3) = 0.0;
   8.134 +    M(3, 0) = 0.0;
   8.135 +    M(3, 1) = 0.0;
   8.136 +    M(3, 2) = 0.0;
   8.137 +    M(3, 3) = 1.0;
   8.138 +#undef M
   8.139 +    glMultMatrixf(m);
   8.140 +    
   8.141 +    /* Translate Eye to Origin */
   8.142 +    glTranslatef(-eyex, -eyey, -eyez);
   8.143 +    
   8.144 +}
   8.145 +
   8.146 +
   8.147 +
   8.148 +
   8.149 +
   8.150 +/* function to release/destroy our resources and restoring the old desktop */
   8.151 +void Quit( int returnCode )
   8.152 +{
   8.153 +    /* clean up the window */
   8.154 +    SDL_Quit( );
   8.155 +
   8.156 +    /* and exit appropriately */
   8.157 +    exit( returnCode );
   8.158 +}
   8.159 +
   8.160 +/* function to reset our viewport after a window resize */
   8.161 +int resizeWindow( int width, int height )
   8.162 +{
   8.163 +    /* Height / width ration */
   8.164 +    GLfloat ratio;
   8.165 + 
   8.166 +    /* Protect against a divide by zero */
   8.167 +   if ( height == 0 )
   8.168 +	height = 1;
   8.169 +
   8.170 +    ratio = ( GLfloat )width / ( GLfloat )height;
   8.171 +
   8.172 +    /* Setup our viewport. */
   8.173 +    glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
   8.174 +
   8.175 +    /* change to the projection matrix and set our viewing volume. */
   8.176 +    glMatrixMode( GL_PROJECTION );
   8.177 +    glLoadIdentity( );
   8.178 +
   8.179 +    /* Set our perspective */
   8.180 +    gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
   8.181 +
   8.182 +    /* Make sure we're chaning the model view and not the projection */
   8.183 +    glMatrixMode( GL_MODELVIEW );
   8.184 +
   8.185 +    /* Reset The View */
   8.186 +    glLoadIdentity( );
   8.187 +
   8.188 +    return( TRUE );
   8.189 +}
   8.190 +
   8.191 +/* function to handle key press events */
   8.192 +void handleKeyPress( SDL_keysym *keysym )
   8.193 +{
   8.194 +    switch ( keysym->sym )
   8.195 +	{
   8.196 +	case SDLK_ESCAPE:
   8.197 +	    /* ESC key was pressed */
   8.198 +	    Quit( 0 );
   8.199 +	    break;
   8.200 +	case SDLK_F1:
   8.201 +	    /* F1 key was pressed
   8.202 +	     * this toggles fullscreen mode
   8.203 +	     */
   8.204 +	    SDL_WM_ToggleFullScreen( surface );
   8.205 +	    break;
   8.206 +    case SDLK_LEFT:
   8.207 +        rotation -= 30;
   8.208 +        break;
   8.209 +
   8.210 +    case SDLK_RIGHT:
   8.211 +        rotation += 30;
   8.212 +        break;
   8.213 +        
   8.214 +	default:
   8.215 +	    break;
   8.216 +	}
   8.217 +
   8.218 +    __android_log_print(ANDROID_LOG_INFO, "SDL","Keycode: %d, %d, %d\n", keysym->sym, SDLK_LEFT, SDLK_RIGHT);
   8.219 +
   8.220 +    return;
   8.221 +}
   8.222 +
   8.223 +/* general OpenGL initialization function */
   8.224 +int initGL( GLvoid )
   8.225 +{
   8.226 +
   8.227 +    /* Enable smooth shading */
   8.228 +    glShadeModel( GL_SMOOTH );
   8.229 +
   8.230 +    /* Set the background black */
   8.231 +    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
   8.232 +
   8.233 +    /* Depth buffer setup */
   8.234 +    //glClearDepth( 1.0f );
   8.235 +
   8.236 +    /* Enables Depth Testing */
   8.237 +    glEnable( GL_DEPTH_TEST );
   8.238 +
   8.239 +    /* The Type Of Depth Test To Do */
   8.240 +    glDepthFunc( GL_LEQUAL );
   8.241 +
   8.242 +    /* Really Nice Perspective Calculations */
   8.243 +    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
   8.244 +
   8.245 +    return( TRUE );
   8.246 +}
   8.247 +
   8.248 +/* Here goes our drawing code */
   8.249 +int drawGLScene( GLvoid )
   8.250 +{
   8.251 +      
   8.252 +	static int Frames = 0;
   8.253 +	static int T0 = 0;
   8.254 +	
   8.255 +    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
   8.256 +
   8.257 +    glClearColorx(0,0,0,255);
   8.258 +    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
   8.259 +
   8.260 +    glMatrixMode(GL_PROJECTION);
   8.261 +    glLoadIdentity();
   8.262 +    gluPerspective(45, (float)SCREEN_WIDTH / SCREEN_HEIGHT, 0.5f, 150);
   8.263 +
   8.264 +    glMatrixMode(GL_MODELVIEW);
   8.265 +
   8.266 +    glLoadIdentity();
   8.267 +
   8.268 +	//Camera
   8.269 +	gluLookAt(0,0,5, 0,0,0, 0,1,0);
   8.270 +			
   8.271 +	//Draw a triangle
   8.272 +	//glRotatef(iRot, 0, 1, 0);
   8.273 +
   8.274 +	glRotatef( rotation, 0.0f, 1.0f, 0.0f );
   8.275 +
   8.276 +
   8.277 +	glEnableClientState (GL_VERTEX_ARRAY);
   8.278 +	glEnableClientState (GL_COLOR_ARRAY);
   8.279 +	
   8.280 +	/* Rotate The Triangle On The Y axis ( NEW ) */
   8.281 +    //glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
   8.282 +
   8.283 +    /* GLES variant of drawing a triangle */
   8.284 +    const GLfloat triVertices[][9] = {
   8.285 +      {     /* Front Triangle */
   8.286 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
   8.287 +	-1.0f, -1.0f,  1.0f,               /* Left Of Triangle              */
   8.288 +	 1.0f, -1.0f,  1.0f                /* Right Of Triangle             */
   8.289 +      }, {  /* Right Triangle */
   8.290 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
   8.291 +	 1.0f, -1.0f,  1.0f,               /* Left Of Triangle              */
   8.292 +	 1.0f, -1.0f, -1.0f                /* Right Of Triangle             */
   8.293 +      }, {  /* Back Triangle */
   8.294 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
   8.295 +	 1.0f, -1.0f, -1.0f,               /* Left Of Triangle              */
   8.296 +	-1.0f, -1.0f, -1.0f                /* Right Of Triangle             */
   8.297 +      }, {  /* Left Triangle */
   8.298 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
   8.299 +	-1.0f, -1.0f, -1.0f,               /* Left Of Triangle              */
   8.300 +	-1.0f, -1.0f,  1.0f                /* Right Of Triangle             */
   8.301 +      }
   8.302 +    };
   8.303 +
   8.304 +    /* unlike GL, GLES does not support RGB. We have to use RGBA instead */
   8.305 +    const GLfloat triColors[][12] = {
   8.306 +      {     /* Front triangle */
   8.307 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
   8.308 +	0.0f, 1.0f, 0.0f, 1.0f,            /* Green                         */
   8.309 +	0.0f, 0.0f, 1.0f, 1.0f             /* Blue                          */
   8.310 +      }, {  /* Right triangle */
   8.311 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
   8.312 +	0.0f, 0.0f, 1.0f, 1.0f,            /* Blue                          */
   8.313 +	0.0f, 1.0f, 0.0f, 1.0f             /* Green                         */
   8.314 +      }, {  /* Back triangle */
   8.315 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
   8.316 +	0.0f, 1.0f, 0.0f, 1.0f,            /* Green                         */
   8.317 +	0.0f, 0.0f, 1.0f, 1.0f             /* Blue                          */
   8.318 +      }, {  /* Left triangle */
   8.319 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
   8.320 +	0.0f, 0.0f, 1.0f, 1.0f,            /* Blue                          */
   8.321 +	0.0f, 1.0f, 0.0f, 1.0f             /* Green                         */
   8.322 +      }
   8.323 +    };
   8.324 +
   8.325 +    glEnableClientState(GL_COLOR_ARRAY);
   8.326 +
   8.327 +    int tri=0;
   8.328 +
   8.329 +    /* Loop through all Triangles */
   8.330 +    for(tri=0;tri<sizeof(triVertices)/(9*sizeof(GLfloat));tri++) 
   8.331 +    {
   8.332 +      glVertexPointer(3, GL_FLOAT, 0, triVertices[tri]);
   8.333 +      glColorPointer(4, GL_FLOAT, 0, triColors[tri]);
   8.334 +      
   8.335 +      glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
   8.336 +    }
   8.337 +		
   8.338 +    //__android_log_print(ANDROID_LOG_INFO, "SDL", "render %d", Frames++);
   8.339 +
   8.340 +    /* Draw it to the screen */
   8.341 +    SDL_GL_SwapBuffers( );
   8.342 +
   8.343 +    /* Gather our frames per second */
   8.344 +    Frames++;
   8.345 +    {
   8.346 +	GLint t = SDL_GetTicks();
   8.347 +	if (t - T0 >= 5000) {
   8.348 +	    GLfloat seconds = (t - T0) / 1000.0;
   8.349 +	    GLfloat fps = Frames / seconds;
   8.350 +	    __android_log_print(ANDROID_LOG_INFO, "SDL","%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
   8.351 +	    T0 = t;
   8.352 +	    Frames = 0;
   8.353 +	}
   8.354 +    }
   8.355 +
   8.356 +    rotation++;
   8.357 +
   8.358 +    return( TRUE );
   8.359 +}
   8.360 +
   8.361 +
   8.362 +struct
   8.363 +{
   8.364 +    SDL_AudioSpec spec;
   8.365 +    Uint8 *sound;               /* Pointer to wave data */
   8.366 +    Uint32 soundlen;            /* Length of wave data */
   8.367 +    int soundpos;               /* Current play position */
   8.368 +} wave;
   8.369 +
   8.370 +void SDLCALL
   8.371 +fillerup(void *unused, Uint8 * stream, int len)
   8.372 +{
   8.373 +    __android_log_print(ANDROID_LOG_INFO, "SDL","FILLERUP\n");
   8.374 +    
   8.375 +    Uint8 *waveptr;
   8.376 +    int waveleft;
   8.377 +
   8.378 +    /* Set up the pointers */
   8.379 +    waveptr = wave.sound + wave.soundpos;
   8.380 +    waveleft = wave.soundlen - wave.soundpos;
   8.381 +
   8.382 +    /* Go! */
   8.383 +    while (waveleft <= len) {
   8.384 +        SDL_memcpy(stream, waveptr, waveleft);
   8.385 +        stream += waveleft;
   8.386 +        len -= waveleft;
   8.387 +        waveptr = wave.sound;
   8.388 +        waveleft = wave.soundlen;
   8.389 +        wave.soundpos = 0;
   8.390 +    }
   8.391 +    SDL_memcpy(stream, waveptr, len);
   8.392 +    wave.soundpos += len;
   8.393 +}
   8.394 +
   8.395 +void testAudio(){
   8.396 +
   8.397 +    const char *file = "/sdcard/sample.wav";
   8.398 +
   8.399 +    /* Load the SDL library */
   8.400 +    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   8.401 +        __android_log_print(ANDROID_LOG_INFO, "SDL","Couldn't initialize SDL Audio: %s\n", SDL_GetError());
   8.402 +        return;
   8.403 +    }else{
   8.404 +        __android_log_print(ANDROID_LOG_INFO, "SDL","Init audio ok\n");
   8.405 +    }
   8.406 +
   8.407 +    /* Load the wave file into memory */
   8.408 +    if (SDL_LoadWAV(file, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
   8.409 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't load %s: %s\n", file, SDL_GetError());
   8.410 +        return;
   8.411 +    }
   8.412 +
   8.413 +    wave.spec.callback = fillerup;
   8.414 +
   8.415 +    __android_log_print(ANDROID_LOG_INFO, "SDL","Loaded: %d\n", wave.soundlen);
   8.416 +
   8.417 +
   8.418 +    /* Initialize fillerup() variables */
   8.419 +    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   8.420 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't open audio: %s\n", SDL_GetError());
   8.421 +        SDL_FreeWAV(wave.sound);
   8.422 +        return;
   8.423 +    }
   8.424 +
   8.425 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   8.426 +
   8.427 +    /* Let the audio run */
   8.428 +    SDL_PauseAudio(0);
   8.429 +
   8.430 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Playing\n");
   8.431 +    
   8.432 +    while (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING){
   8.433 +         //__android_log_print(ANDROID_LOG_INFO, "SDL","Still playing\n");
   8.434 +        SDL_Delay(100);
   8.435 +    }
   8.436 +
   8.437 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Closing down\n");
   8.438 +
   8.439 +    /* Clean up on signal */
   8.440 +    SDL_CloseAudio();
   8.441 +    SDL_FreeWAV(wave.sound);
   8.442 +}
   8.443 +
   8.444 +int SDL_main( int argc, char **argv )
   8.445 +{
   8.446 +
   8.447 +	__android_log_print(ANDROID_LOG_INFO, "SDL","entry\n");
   8.448 +
   8.449 +    /* Flags to pass to SDL_SetVideoMode */
   8.450 +    int videoFlags;
   8.451 +    /* main loop variable */
   8.452 +    int done = FALSE;
   8.453 +    /* used to collect events */
   8.454 +    SDL_Event event;
   8.455 +    /* this holds some info about our display */
   8.456 +    const SDL_VideoInfo *videoInfo;
   8.457 +    /* whether or not the window is active */
   8.458 +    int isActive = TRUE;
   8.459 +
   8.460 +    /* initialize SDL */
   8.461 +    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
   8.462 +	{
   8.463 +	    __android_log_print(ANDROID_LOG_INFO, "SDL", "Video initialization failed: %s\n",
   8.464 +		     SDL_GetError( ) );
   8.465 +	    Quit( 1 );
   8.466 +	}
   8.467 +
   8.468 +    /* Fetch the video info */
   8.469 +    videoInfo = SDL_GetVideoInfo( );
   8.470 +
   8.471 +    if ( !videoInfo )
   8.472 +	{
   8.473 +	    __android_log_print(ANDROID_LOG_INFO, "SDL", "Video query failed: %s\n",
   8.474 +		     SDL_GetError( ) );
   8.475 +	    Quit( 1 );
   8.476 +	}
   8.477 +
   8.478 +    /* the flags to pass to SDL_SetVideoMode */
   8.479 +    videoFlags  = SDL_OPENGL;          /* Enable OpenGL in SDL */
   8.480 +    videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
   8.481 +    videoFlags |= SDL_HWPALETTE;       /* Store the palette in hardware */
   8.482 +    videoFlags |= SDL_RESIZABLE;       /* Enable window resizing */
   8.483 +
   8.484 +    /* This checks to see if surfaces can be stored in memory */
   8.485 +    if ( videoInfo->hw_available )
   8.486 +	videoFlags |= SDL_HWSURFACE;
   8.487 +    else
   8.488 +	videoFlags |= SDL_SWSURFACE;
   8.489 +
   8.490 +    /* This checks if hardware blits can be done */
   8.491 +    if ( videoInfo->blit_hw )
   8.492 +	videoFlags |= SDL_HWACCEL;
   8.493 +
   8.494 +    /* Sets up OpenGL double buffering */
   8.495 +    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
   8.496 +
   8.497 +    /* get a SDL surface */
   8.498 +    surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
   8.499 +				videoFlags );
   8.500 +
   8.501 +    /* Verify there is a surface */
   8.502 +    if ( !surface )
   8.503 +	{
   8.504 +	    __android_log_print(ANDROID_LOG_INFO, "SDL",  "Video mode set failed: %s\n", SDL_GetError( ) );
   8.505 +	    Quit( 1 );
   8.506 +	}
   8.507 +
   8.508 +	__android_log_print(ANDROID_LOG_INFO, "SDL","Made a video mode!\n");
   8.509 +
   8.510 +    /* initialize OpenGL */
   8.511 +    initGL( );
   8.512 +
   8.513 +    /* resize the initial window */
   8.514 +    resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
   8.515 +
   8.516 +
   8.517 +    //testAudio();
   8.518 +
   8.519 +
   8.520 +    /* wait for events */ 
   8.521 +    while ( !done )
   8.522 +	{
   8.523 +	    /* handle the events in the queue */
   8.524 +
   8.525 +	    while ( SDL_PollEvent( &event ) )
   8.526 +		{
   8.527 +		    switch( event.type )
   8.528 +			{
   8.529 +			case SDL_ACTIVEEVENT:
   8.530 +			    /* Something's happend with our focus
   8.531 +			     * If we lost focus or we are iconified, we
   8.532 +			     * shouldn't draw the screen
   8.533 +			     */
   8.534 +			    if ( event.active.gain == 0 )
   8.535 +				isActive = FALSE;
   8.536 +			    else
   8.537 +				isActive = TRUE;
   8.538 +			    break;			    
   8.539 +			case SDL_VIDEORESIZE:
   8.540 +			    /* handle resize event */
   8.541 +			    surface = SDL_SetVideoMode( event.resize.w,
   8.542 +							event.resize.h,
   8.543 +							16, videoFlags );
   8.544 +			    if ( !surface )
   8.545 +				{
   8.546 +				    __android_log_print(ANDROID_LOG_INFO, "SDL","Could not get a surface after resize: %s\n", SDL_GetError( ) );
   8.547 +				    Quit( 1 );
   8.548 +				}
   8.549 +			    resizeWindow( event.resize.w, event.resize.h );
   8.550 +			    break;
   8.551 +			case SDL_KEYDOWN:
   8.552 +			    /* handle key presses */
   8.553 +			    handleKeyPress( &event.key.keysym );
   8.554 +			    break;
   8.555 +			case SDL_QUIT:
   8.556 +			    /* handle quit requests */
   8.557 +			    done = TRUE;
   8.558 +			    __android_log_print(ANDROID_LOG_INFO, "SDL","App is shutting down\n");
   8.559 +			    break;
   8.560 +			default:
   8.561 +			    break;
   8.562 +			}
   8.563 +		}
   8.564 +
   8.565 +	    /* draw the scene */
   8.566 +	    if ( isActive )
   8.567 +		drawGLScene( );
   8.568 +	}
   8.569 +
   8.570 +    /* clean ourselves up and exit */
   8.571 +    Quit( 0 );
   8.572 +
   8.573 +    /* Should never get here */
   8.574 +    return( 0 );
   8.575 +}
   8.576 +
   8.577 +
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/android/project/local.properties	Tue Aug 17 15:35:56 2010 +1200
     9.3 @@ -0,0 +1,10 @@
     9.4 +# This file is automatically generated by Android Tools.
     9.5 +# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
     9.6 +# 
     9.7 +# This file must *NOT* be checked in Version Control Systems,
     9.8 +# as it contains information specific to your local configuration.
     9.9 +
    9.10 +# location of the SDK. This is only used by Ant
    9.11 +# For customization when using a Version Control System, please read the
    9.12 +# header note.
    9.13 +sdk.dir=/home/paul/Projects/gsoc/sdk/android-sdk-linux_86
    10.1 Binary file android/project/res/drawable-hdpi/icon.png has changed
    11.1 Binary file android/project/res/drawable-ldpi/icon.png has changed
    12.1 Binary file android/project/res/drawable-mdpi/icon.png has changed
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/android/project/res/layout/main.xml	Tue Aug 17 15:35:56 2010 +1200
    13.3 @@ -0,0 +1,13 @@
    13.4 +<?xml version="1.0" encoding="utf-8"?>
    13.5 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    13.6 +    android:orientation="vertical"
    13.7 +    android:layout_width="fill_parent"
    13.8 +    android:layout_height="fill_parent"
    13.9 +    >
   13.10 +<TextView  
   13.11 +    android:layout_width="fill_parent" 
   13.12 +    android:layout_height="wrap_content" 
   13.13 +    android:text="Hello World, SDLActivity"
   13.14 +    />
   13.15 +</LinearLayout>
   13.16 +
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/android/project/res/values/strings.xml	Tue Aug 17 15:35:56 2010 +1200
    14.3 @@ -0,0 +1,4 @@
    14.4 +<?xml version="1.0" encoding="utf-8"?>
    14.5 +<resources>
    14.6 +    <string name="app_name">SDLActivity</string>
    14.7 +</resources>
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/android/project/src/org/libsdl/app/SDLActivity.java	Tue Aug 17 15:35:56 2010 +1200
    15.3 @@ -0,0 +1,388 @@
    15.4 +package org.libsdl.app;
    15.5 +
    15.6 +import javax.microedition.khronos.egl.EGLConfig;
    15.7 +import javax.microedition.khronos.opengles.GL10;
    15.8 +import javax.microedition.khronos.egl.*;
    15.9 +
   15.10 +import android.app.*;
   15.11 +import android.content.*;
   15.12 +import android.view.*;
   15.13 +import android.os.*;
   15.14 +import android.util.Log;
   15.15 +import android.graphics.*;
   15.16 +import android.text.method.*;
   15.17 +import android.text.*;
   15.18 +import android.media.*;
   15.19 +import android.hardware.*;
   15.20 +import android.content.*;
   15.21 +
   15.22 +import java.lang.*;
   15.23 +
   15.24 +
   15.25 +/**
   15.26 +    SDL Activity
   15.27 +*/
   15.28 +public class SDLActivity extends Activity {
   15.29 +
   15.30 +    //Main components
   15.31 +    private static SDLActivity mSingleton;
   15.32 +    private static SDLSurface mSurface;
   15.33 +
   15.34 +    //Audio
   15.35 +    private static AudioTrack mAudioTrack;
   15.36 +    private static boolean bAudioIsEnabled;
   15.37 +
   15.38 +    //Sensors
   15.39 +    private static boolean bAccelIsEnabled;
   15.40 +
   15.41 +    //feature IDs. Must match up on the C side as well.
   15.42 +    private static int FEATURE_AUDIO = 1;
   15.43 +    private static int FEATURE_ACCEL = 2;
   15.44 +
   15.45 +    //Load the .so
   15.46 +    static {
   15.47 +        System.loadLibrary("sdlapp");
   15.48 +    }
   15.49 +
   15.50 +    //Setup
   15.51 +    protected void onCreate(Bundle savedInstanceState) {
   15.52 +        super.onCreate(savedInstanceState);
   15.53 +        
   15.54 +        //So we can call stuff from static callbacks
   15.55 +        mSingleton = this;
   15.56 +
   15.57 +        //Set up the surface
   15.58 +        mSurface = new SDLSurface(getApplication());
   15.59 +        setContentView(mSurface);
   15.60 +        SurfaceHolder holder = mSurface.getHolder();
   15.61 +        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
   15.62 +        
   15.63 +    }
   15.64 +
   15.65 +    //Audio
   15.66 +    public static boolean initAudio(){        
   15.67 +
   15.68 +        //blah. Hardcoded things are bad. FIXME when we have more sound stuff
   15.69 +        //working properly. 
   15.70 +        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
   15.71 +                    11025,
   15.72 +                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
   15.73 +                    AudioFormat.ENCODING_PCM_8BIT,
   15.74 +                    2048,
   15.75 +                    AudioTrack.MODE_STREAM);   
   15.76 +        bAudioIsEnabled = true;     
   15.77 +        return true;
   15.78 +    }
   15.79 +
   15.80 +    //Accel
   15.81 +    public static boolean initAccel(){
   15.82 +        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
   15.83 +        bAccelIsEnabled = true;
   15.84 +        return true;
   15.85 +    }
   15.86 +    
   15.87 +    public static boolean closeAccel(){
   15.88 +        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false);
   15.89 +        bAccelIsEnabled = false;
   15.90 +        return true;
   15.91 +    }
   15.92 +    
   15.93 +
   15.94 +    //Events
   15.95 +    protected void onPause() {
   15.96 +        super.onPause();
   15.97 +    }
   15.98 +
   15.99 +    protected void onResume() {
  15.100 +        super.onResume();
  15.101 +    }
  15.102 +
  15.103 +    
  15.104 +
  15.105 +
  15.106 +
  15.107 +    //C functions we call
  15.108 +    public static native void nativeInit();
  15.109 +    public static native void nativeQuit();
  15.110 +    public static native void nativeSetScreenSize(int width, int height);
  15.111 +    public static native void onNativeKeyDown(int keycode);
  15.112 +    public static native void onNativeKeyUp(int keycode);
  15.113 +    public static native void onNativeTouch(int action, float x, 
  15.114 +                                            float y, float p);
  15.115 +    public static native void onNativeResize(int x, int y, int format);
  15.116 +    public static native void onNativeAccel(float x, float y, float z);
  15.117 +
  15.118 +
  15.119 +
  15.120 +    //Java functions called from C
  15.121 +    private static void createGLContext(){
  15.122 +        mSurface.initEGL();
  15.123 +    }
  15.124 +
  15.125 +    public static void flipBuffers(){
  15.126 +        mSurface.flipEGL();
  15.127 +    }
  15.128 +
  15.129 +    public static void updateAudio(byte [] buf){
  15.130 +    
  15.131 +        if(mAudioTrack == null){
  15.132 +            return;
  15.133 +        }
  15.134 +        
  15.135 +        mAudioTrack.write(buf, 0, buf.length);
  15.136 +        mAudioTrack.play();
  15.137 +        
  15.138 +        Log.v("SDL","Played some audio");
  15.139 +    }
  15.140 +
  15.141 +    public static void enableFeature(int featureid, int enabled){
  15.142 +         Log.v("SDL","Feature " + featureid + " = " + enabled);
  15.143 +
  15.144 +        //Yuck. This is all horribly inelegent. If it gets to more than a few
  15.145 +        //'features' I'll rip this out and make something nicer, I promise :)
  15.146 +        if(featureid == FEATURE_AUDIO){
  15.147 +            if(enabled == 1){
  15.148 +                initAudio();
  15.149 +            }else{
  15.150 +                //We don't have one of these yet...
  15.151 +                //closeAudio(); 
  15.152 +            }
  15.153 +        }
  15.154 +
  15.155 +        else if(featureid == FEATURE_ACCEL){
  15.156 +            if(enabled == 1){
  15.157 +                initAccel();
  15.158 +            }else{
  15.159 +                closeAccel();
  15.160 +            }
  15.161 +        }
  15.162 +    }
  15.163 +
  15.164 +
  15.165 +
  15.166 +
  15.167 +
  15.168 +    
  15.169 +    
  15.170 +}
  15.171 +
  15.172 +/**
  15.173 +    Simple nativeInit() runnable
  15.174 +*/
  15.175 +class SDLRunner implements Runnable{
  15.176 +    public void run(){
  15.177 +        //SDLActivity.initAudio();
  15.178 +        
  15.179 +        //Runs SDL_main()
  15.180 +        SDLActivity.nativeInit();
  15.181 +
  15.182 +        Log.v("SDL","SDL thread terminated");
  15.183 +    }
  15.184 +}
  15.185 +
  15.186 +
  15.187 +/**
  15.188 +    SDLSurface. This is what we draw on, so we need to know when it's created
  15.189 +    in order to do anything useful. 
  15.190 +
  15.191 +    Because of this, that's where we set up the SDL thread
  15.192 +*/
  15.193 +class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
  15.194 +    View.OnKeyListener, View.OnTouchListener, SensorEventListener  {
  15.195 +
  15.196 +    //This is what SDL runs in. It invokes SDL_main(), eventually
  15.197 +    private Thread mSDLThread;    
  15.198 +    
  15.199 +    //EGL private objects
  15.200 +    private EGLContext  mEGLContext;
  15.201 +    private EGLSurface  mEGLSurface;
  15.202 +    private EGLDisplay  mEGLDisplay;
  15.203 +
  15.204 +    //Sensors
  15.205 +    private static SensorManager mSensorManager;
  15.206 +
  15.207 +    //Startup    
  15.208 +    public SDLSurface(Context context) {
  15.209 +        super(context);
  15.210 +        getHolder().addCallback(this); 
  15.211 +    
  15.212 +        setFocusable(true);
  15.213 +        setFocusableInTouchMode(true);
  15.214 +        requestFocus();
  15.215 +        setOnKeyListener(this); 
  15.216 +        setOnTouchListener(this);   
  15.217 +        
  15.218 +        mSensorManager = (SensorManager)context.getSystemService("sensor");  
  15.219 +    }
  15.220 +
  15.221 +    //Called when we have a valid drawing surface
  15.222 +    public void surfaceCreated(SurfaceHolder holder) {
  15.223 +        Log.v("SDL","Surface created"); 
  15.224 +
  15.225 +        int width = getWidth();
  15.226 +        int height = getHeight();
  15.227 +
  15.228 +        //Set the width and height variables in C before we start SDL so we have
  15.229 +        //it available on init
  15.230 +        SDLActivity.nativeSetScreenSize(width, height);
  15.231 +
  15.232 +        //Now start up the C app thread
  15.233 +        mSDLThread = new Thread(new SDLRunner(), "SDLThread"); 
  15.234 +		mSDLThread.start();       
  15.235 +    }
  15.236 +
  15.237 +    //Called when we lose the surface
  15.238 +    public void surfaceDestroyed(SurfaceHolder holder) {
  15.239 +        Log.v("SDL","Surface destroyed");
  15.240 +        
  15.241 +        SDLActivity.nativeQuit();
  15.242 +
  15.243 +        //Now wait for the SDL thread to quit
  15.244 +        try{
  15.245 +            mSDLThread.wait();
  15.246 +        }catch(Exception e){
  15.247 +            Log.v("SDL","Problem stopping thread: " + e);
  15.248 +        }
  15.249 +    }
  15.250 +
  15.251 +    //Called when the surface is resized
  15.252 +    public void surfaceChanged(SurfaceHolder holder, int format, 
  15.253 +                                int width, int height) {
  15.254 +        Log.v("SDL","Surface resized");
  15.255 +        
  15.256 +        SDLActivity.onNativeResize(width, height, format);
  15.257 +    }
  15.258 +
  15.259 +    //unused
  15.260 +    public void onDraw(Canvas canvas) {}
  15.261 +
  15.262 +    
  15.263 +    //EGL functions
  15.264 +    public boolean initEGL(){
  15.265 +        Log.v("SDL","Starting up");
  15.266 +
  15.267 +        try{
  15.268 +
  15.269 +            EGL10 egl = (EGL10)EGLContext.getEGL();
  15.270 +
  15.271 +            EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
  15.272 +
  15.273 +            int[] version = new int[2];
  15.274 +            egl.eglInitialize(dpy, version);
  15.275 +
  15.276 +            int[] configSpec = {
  15.277 +                    //EGL10.EGL_DEPTH_SIZE,   16,
  15.278 +                    EGL10.EGL_NONE
  15.279 +            };
  15.280 +            EGLConfig[] configs = new EGLConfig[1];
  15.281 +            int[] num_config = new int[1];
  15.282 +            egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
  15.283 +            EGLConfig config = configs[0];
  15.284 +
  15.285 +            EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
  15.286 +
  15.287 +            EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
  15.288 +
  15.289 +            egl.eglMakeCurrent(dpy, surface, surface, ctx);
  15.290 +
  15.291 +            mEGLContext = ctx;
  15.292 +            mEGLDisplay = dpy;
  15.293 +            mEGLSurface = surface;
  15.294 +            
  15.295 +            
  15.296 +        }catch(Exception e){
  15.297 +            Log.v("SDL", e + "");
  15.298 +            for(StackTraceElement s : e.getStackTrace()){
  15.299 +                Log.v("SDL", s.toString());
  15.300 +            }
  15.301 +        }
  15.302 +        Log.v("SDL","Done making!");
  15.303 +
  15.304 +        return true;
  15.305 +    }
  15.306 +
  15.307 +    //EGL buffer flip
  15.308 +    public void flipEGL(){      
  15.309 +        try{
  15.310 +        
  15.311 +            EGL10 egl = (EGL10)EGLContext.getEGL();
  15.312 +            GL10 gl = (GL10)mEGLContext.getGL();
  15.313 +
  15.314 +            egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
  15.315 +
  15.316 +            //drawing here
  15.317 +
  15.318 +            egl.eglWaitGL();
  15.319 +
  15.320 +            egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
  15.321 +
  15.322 +            
  15.323 +        }catch(Exception e){
  15.324 +            Log.v("SDL", "flipEGL(): " + e);
  15.325 +
  15.326 +            for(StackTraceElement s : e.getStackTrace()){
  15.327 +                Log.v("SDL", s.toString());
  15.328 +            }
  15.329 +        }
  15.330 +    }
  15.331 +
  15.332 +
  15.333 +  
  15.334 +    //Key events
  15.335 +    public boolean onKey(View  v, int keyCode, KeyEvent event){
  15.336 +
  15.337 +        if(event.getAction() == KeyEvent.ACTION_DOWN){
  15.338 +            SDLActivity.onNativeKeyDown(keyCode);
  15.339 +            return true;
  15.340 +        }
  15.341 +        
  15.342 +        else if(event.getAction() == KeyEvent.ACTION_UP){
  15.343 +            SDLActivity.onNativeKeyUp(keyCode);
  15.344 +            return true;
  15.345 +        }
  15.346 +        
  15.347 +        return false;
  15.348 +    }
  15.349 +
  15.350 +    //Touch events
  15.351 +    public boolean onTouch(View v, MotionEvent event){
  15.352 +    
  15.353 +        int action = event.getAction();
  15.354 +        float x = event.getX();
  15.355 +        float y = event.getY();
  15.356 +        float p = event.getPressure();
  15.357 +
  15.358 +        //TODO: Anything else we need to pass?        
  15.359 +        SDLActivity.onNativeTouch(action, x, y, p);
  15.360 +        return true;
  15.361 +    }
  15.362 +
  15.363 +    //Sensor events
  15.364 +    public void enableSensor(int sensortype, boolean enabled){
  15.365 +        //TODO: This uses getDefaultSensor - what if we have >1 accels?
  15.366 +        if(enabled){
  15.367 +            mSensorManager.registerListener(this, 
  15.368 +                            mSensorManager.getDefaultSensor(sensortype), 
  15.369 +                            SensorManager.SENSOR_DELAY_GAME, null);
  15.370 +        }else{
  15.371 +            mSensorManager.unregisterListener(this, 
  15.372 +                            mSensorManager.getDefaultSensor(sensortype));
  15.373 +        }
  15.374 +    }
  15.375 +    
  15.376 +    public void onAccuracyChanged(Sensor sensor, int accuracy){
  15.377 +        //TODO
  15.378 +    }
  15.379 +
  15.380 +    public void onSensorChanged(SensorEvent event){
  15.381 +        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
  15.382 +            SDLActivity.onNativeAccel(  event.values[0],
  15.383 +                                        event.values[1],
  15.384 +                                        event.values[2] );
  15.385 +        }
  15.386 +    }
  15.387 +
  15.388 +
  15.389 +}
  15.390 +
  15.391 +
    16.1 --- a/android/testproject/jni/app-android.cpp	Sat Aug 14 16:17:17 2010 +1200
    16.2 +++ b/android/testproject/jni/app-android.cpp	Tue Aug 17 15:35:56 2010 +1200
    16.3 @@ -13,8 +13,8 @@
    16.4  
    16.5  #include <pthread.h>
    16.6  
    16.7 -#include "importgl.h"
    16.8 -#include "egl.h"
    16.9 +//#include "importgl.h"
   16.10 +//#include "egl.h"
   16.11  
   16.12  /*******************************************************************************
   16.13                                 Globals
    17.1 Binary file build-scripts/android_libs/libEGL.so has changed
    18.1 Binary file build-scripts/android_libs/libcutils.so has changed
    19.1 Binary file build-scripts/android_libs/libutils.so has changed