Merged Paul's Google Summer of Code work from SDL-gsoc2010_android
authorSam Lantinga <slouken@libsdl.org>
Sun, 22 Aug 2010 12:23:55 -0700
changeset 47291f7ad083fd3c
parent 4694 c24ba2cc9583
parent 4728 431c0381c696
child 4762 833a225613e2
Merged Paul's Google Summer of Code work from SDL-gsoc2010_android
include/SDL_stdinc.h
src/SDL_compat.c
src/audio/SDL_audio.c
src/events/SDL_events.c
src/events/SDL_keyboard.c
src/video/SDL_sysvideo.h
src/video/SDL_video.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile.android	Sun Aug 22 12:23:55 2010 -0700
     1.3 @@ -0,0 +1,52 @@
     1.4 +# Makefile to build the SDL library
     1.5 +
     1.6 +include ./android/config.cfg #get ANDROID_NDK
     1.7 +
     1.8 +TOOLS_PATH=$(ANDROID_NDK)/build/prebuilt/linux-x86/arm-eabi-4.2.1/bin
     1.9 +ANDROID_INCLUDES = 	-I$(ANDROID_NDK)/build/platforms/android-4/common/include \
    1.10 +					-I$(ANDROID_NDK)/build/platforms/android-4/arch-arm/usr/include 
    1.11 +INCLUDE = -I./include
    1.12 +CFLAGS  = -g -O2 $(INCLUDE) $(ANDROID_INCLUDES) -DANDROID -DANDROID_NDK -static
    1.13 +
    1.14 +AR	= $(TOOLS_PATH)/arm-eabi-ar
    1.15 +RANLIB	= $(TOOLS_PATH)/arm-eabi-ranlib
    1.16 +CC = $(TOOLS_PATH)/arm-eabi-gcc
    1.17 +
    1.18 +
    1.19 +CONFIG_H = include/SDL_config.h
    1.20 +TARGET  = libSDL.a
    1.21 +SOURCES = \
    1.22 +	src/*.c \
    1.23 +	src/audio/*.c \
    1.24 +	src/cpuinfo/*.c \
    1.25 +	src/events/*.c \
    1.26 +	src/file/*.c \
    1.27 +	src/joystick/*.c \
    1.28 +	src/haptic/*.c \
    1.29 +	src/stdlib/*.c \
    1.30 +	src/thread/*.c \
    1.31 +	src/timer/*.c \
    1.32 +	src/video/*.c \
    1.33 +	src/power/*.c \
    1.34 +	src/audio/android/*.c \
    1.35 +	src/video/android/*.c \
    1.36 +	src/joystick/android/*.c \
    1.37 +	src/haptic/dummy/*.c \
    1.38 +	src/atomic/dummy/*.c \
    1.39 +	src/thread/pthread/*.c \
    1.40 +	src/timer/unix/*.c \
    1.41 +	src/loadso/dummy/*.c \
    1.42 +
    1.43 +OBJECTS = $(shell echo $(SOURCES) | sed -e 's,\.c,\.o,g')
    1.44 +
    1.45 +all: $(TARGET)
    1.46 +
    1.47 +$(TARGET): $(CONFIG_H) $(OBJECTS)
    1.48 +	$(AR) crv $@ $^
    1.49 +	$(RANLIB) $@
    1.50 +
    1.51 +$(CONFIG_H):
    1.52 +	cp $(CONFIG_H).default $(CONFIG_H)
    1.53 +
    1.54 +clean:
    1.55 +	rm -f $(TARGET) $(OBJECTS)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/README.android	Sun Aug 22 12:23:55 2010 -0700
     2.3 @@ -0,0 +1,45 @@
     2.4 +================================================================================
     2.5 +Simple DirectMedia Layer for Android
     2.6 +================================================================================
     2.7 +
     2.8 +Requirements: Android NDK r4 or later
     2.9 +
    2.10 +================================================================================
    2.11 + How the port works
    2.12 +================================================================================
    2.13 +
    2.14 +- Android applications are Java-based, optionally with parts written in C
    2.15 +- As SDL apps are C-based, we use a small Java shim that uses JNI to talk to 
    2.16 +the SDL library
    2.17 +- This means that your application C code must be placed inside an android 
    2.18 +Java project, along with some C support code that communicates with Java
    2.19 +- This eventually produces a standard Android .apk package
    2.20 +
    2.21 +
    2.22 +
    2.23 +
    2.24 +
    2.25 +
    2.26 +================================================================================
    2.27 + Building an app
    2.28 +================================================================================
    2.29 +
    2.30 +Instructions:
    2.31 +1. Edit android/config.cfg to point to the location of the NDK
    2.32 +2. Run 'make -f Makefile.android'. If all goes well, libsdl.a should be created
    2.33 +3. Place your application source files in android/project/jni
    2.34 +4. Edit the Android.mk to include your source files
    2.35 +5. Run 'ndk-build' (a script provided by the NDK). This compiles the C source
    2.36 +6. Run 'ant' in android/testproject. This compiles the .java and eventually 
    2.37 +creates a .apk with the C source embedded
    2.38 +7. 'ant install' will push the apk to the device or emulator (if connected)
    2.39 +
    2.40 +
    2.41 +
    2.42 +
    2.43 +================================================================================
    2.44 + Known issues
    2.45 +================================================================================
    2.46 +
    2.47 +- SDL audio (although it's mostly written, just not working properly yet)
    2.48 +- TODO. I'm sure there's a bunch more stuff I haven't thought of 
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/android/config.cfg	Sun Aug 22 12:23:55 2010 -0700
     3.3 @@ -0,0 +1,1 @@
     3.4 +ANDROID_NDK=/home/paul/Projects/gsoc/sdk/android-ndk-r4
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/android/project/AndroidManifest.xml	Sun Aug 22 12:23:55 2010 -0700
     4.3 @@ -0,0 +1,15 @@
     4.4 +<?xml version="1.0" encoding="utf-8"?>
     4.5 +<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     4.6 +      package="org.libsdl.app"
     4.7 +      android:versionCode="1"
     4.8 +      android:versionName="1.0">
     4.9 +    <application android:label="@string/app_name" android:icon="@drawable/icon">
    4.10 +        <activity android:name="SDLActivity"
    4.11 +                  android:label="@string/app_name">
    4.12 +            <intent-filter>
    4.13 +                <action android:name="android.intent.action.MAIN" />
    4.14 +                <category android:name="android.intent.category.LAUNCHER" />
    4.15 +            </intent-filter>
    4.16 +        </activity>
    4.17 +    </application>
    4.18 +</manifest> 
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/android/project/build.properties	Sun Aug 22 12:23:55 2010 -0700
     5.3 @@ -0,0 +1,17 @@
     5.4 +# This file is used to override default values used by the Ant build system.
     5.5 +# 
     5.6 +# This file must be checked in Version Control Systems, as it is
     5.7 +# integral to the build system of your project.
     5.8 +
     5.9 +# This file is only used by the Ant script.
    5.10 +
    5.11 +# You can use this to override default values such as
    5.12 +#  'source.dir' for the location of your java source folder and
    5.13 +#  'out.dir' for the location of your output folder.
    5.14 +
    5.15 +# You can also use it define how the release builds are signed by declaring
    5.16 +# the following properties:
    5.17 +#  'key.store' for the location of your keystore and
    5.18 +#  'key.alias' for the name of the key to use.
    5.19 +# The password will be asked during the build when you use the 'release' target.
    5.20 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/android/project/build.xml	Sun Aug 22 12:23:55 2010 -0700
     6.3 @@ -0,0 +1,67 @@
     6.4 +<?xml version="1.0" encoding="UTF-8"?>
     6.5 +<project name="SDLApp" default="help">
     6.6 +
     6.7 +    <!-- The local.properties file is created and updated by the 'android' tool.
     6.8 +         It contains the path to the SDK. It should *NOT* be checked in in Version
     6.9 +         Control Systems. -->
    6.10 +    <property file="local.properties" />
    6.11 +
    6.12 +    <!-- The build.properties file can be created by you and is never touched
    6.13 +         by the 'android' tool. This is the place to change some of the default property values
    6.14 +         used by the Ant rules.
    6.15 +         Here are some properties you may want to change/update:
    6.16 +
    6.17 +         application.package
    6.18 +             the name of your application package as defined in the manifest. Used by the
    6.19 +             'uninstall' rule.
    6.20 +         source.dir
    6.21 +             the name of the source directory. Default is 'src'.
    6.22 +         out.dir
    6.23 +             the name of the output directory. Default is 'bin'.
    6.24 +
    6.25 +         Properties related to the SDK location or the project target should be updated
    6.26 +          using the 'android' tool with the 'update' action.
    6.27 +
    6.28 +         This file is an integral part of the build system for your application and
    6.29 +         should be checked in in Version Control Systems.
    6.30 +
    6.31 +         -->
    6.32 +    <property file="build.properties" />
    6.33 +
    6.34 +    <!-- The default.properties file is created and updated by the 'android' tool, as well
    6.35 +         as ADT.
    6.36 +         This file is an integral part of the build system for your application and
    6.37 +         should be checked in in Version Control Systems. -->
    6.38 +    <property file="default.properties" />
    6.39 +
    6.40 +    <!-- Custom Android task to deal with the project target, and import the proper rules.
    6.41 +         This requires ant 1.6.0 or above. -->
    6.42 +    <path id="android.antlibs">
    6.43 +        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
    6.44 +        <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
    6.45 +        <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
    6.46 +        <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
    6.47 +        <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
    6.48 +    </path>
    6.49 +
    6.50 +    <taskdef name="setup"
    6.51 +        classname="com.android.ant.SetupTask"
    6.52 +        classpathref="android.antlibs" />
    6.53 +
    6.54 +    <!-- Execute the Android Setup task that will setup some properties specific to the target,
    6.55 +         and import the build rules files.
    6.56 +
    6.57 +         The rules file is imported from
    6.58 +            <SDK>/platforms/<target_platform>/templates/android_rules.xml
    6.59 +
    6.60 +         To customize some build steps for your project:
    6.61 +         - copy the content of the main node <project> from android_rules.xml
    6.62 +         - paste it in this build.xml below the <setup /> task.
    6.63 +         - disable the import by changing the setup task below to <setup import="false" />
    6.64 +
    6.65 +         This will ensure that the properties are setup correctly but that your customized
    6.66 +         build steps are used.
    6.67 +    -->
    6.68 +    <setup />
    6.69 +
    6.70 +</project>
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/android/project/default.properties	Sun Aug 22 12:23:55 2010 -0700
     7.3 @@ -0,0 +1,11 @@
     7.4 +# This file is automatically generated by Android Tools.
     7.5 +# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
     7.6 +# 
     7.7 +# This file must be checked in Version Control Systems.
     7.8 +# 
     7.9 +# To customize properties used by the Ant build system use,
    7.10 +# "build.properties", and override values to adapt the script to your
    7.11 +# project structure.
    7.12 +
    7.13 +# Project target.
    7.14 +target=android-7
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/android/project/jni/Android.mk	Sun Aug 22 12:23:55 2010 -0700
     8.3 @@ -0,0 +1,18 @@
     8.4 +LOCAL_PATH := $(call my-dir)
     8.5 +
     8.6 +include $(CLEAR_VARS)
     8.7 +
     8.8 +LOCAL_MODULE := sdlapp
     8.9 +SDL := ../../../
    8.10 +
    8.11 +LOCAL_CFLAGS := -DANDROID_NDK \
    8.12 +                -DDISABLE_IMPORTGL \
    8.13 +                -I$(SDL)/include
    8.14 +
    8.15 +LOCAL_SRC_FILES := \
    8.16 +    android-support.cpp \
    8.17 +    lesson05.c \
    8.18 +
    8.19 +LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog -lSDL -lgcc -L$(SDL)
    8.20 +
    8.21 +include $(BUILD_SHARED_LIBRARY)
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/android/project/jni/android-support.cpp	Sun Aug 22 12:23:55 2010 -0700
     9.3 @@ -0,0 +1,242 @@
     9.4 +/*******************************************************************************
     9.5 + This file links the Java side of Android with libsdl
     9.6 +*******************************************************************************/
     9.7 +#include <jni.h>
     9.8 +#include <sys/time.h>
     9.9 +#include <time.h>
    9.10 +#include <android/log.h>
    9.11 +#include <stdint.h>
    9.12 +#include <stdio.h>
    9.13 +#include <stdlib.h>
    9.14 +#include <math.h>
    9.15 +#include <pthread.h>
    9.16 +
    9.17 +#define DEBUG
    9.18 +
    9.19 +
    9.20 +/*******************************************************************************
    9.21 +                               Globals
    9.22 +*******************************************************************************/
    9.23 +static long _getTime(void){
    9.24 +	struct timeval  now;
    9.25 +	gettimeofday(&now, NULL);
    9.26 +	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
    9.27 +}
    9.28 +
    9.29 +JNIEnv* mEnv = NULL;
    9.30 +JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
    9.31 +JavaVM* mVM = NULL;
    9.32 +
    9.33 +//Main activity
    9.34 +jclass mActivityInstance;
    9.35 +
    9.36 +//method signatures
    9.37 +jmethodID midCreateGLContext;
    9.38 +jmethodID midFlipBuffers;
    9.39 +jmethodID midEnableFeature;
    9.40 +jmethodID midUpdateAudio;
    9.41 +
    9.42 +extern "C" int SDL_main();
    9.43 +extern "C" int Android_OnKeyDown(int keycode);
    9.44 +extern "C" int Android_OnKeyUp(int keycode);
    9.45 +extern "C" void Android_SetScreenResolution(int width, int height);
    9.46 +extern "C" void Android_OnResize(int width, int height, int format);
    9.47 +extern "C" int SDL_SendQuit();
    9.48 +extern "C" void Android_EnableFeature(int featureid, bool enabled);
    9.49 +
    9.50 +//If we're not the active app, don't try to render
    9.51 +bool bRenderingEnabled = false;
    9.52 +
    9.53 +//Feature IDs
    9.54 +static const int FEATURE_AUDIO = 1;
    9.55 +static const int FEATURE_ACCEL = 2;
    9.56 +
    9.57 +//Accelerometer data storage
    9.58 +float fLastAccelerometer[3];
    9.59 +
    9.60 +
    9.61 +/*******************************************************************************
    9.62 +                 Functions called by JNI
    9.63 +*******************************************************************************/	
    9.64 +
    9.65 +//Library init
    9.66 +extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){
    9.67 +
    9.68 +    JNIEnv* env = NULL;
    9.69 +    jint result = -1;
    9.70 +
    9.71 +    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
    9.72 +        return result;
    9.73 +    }
    9.74 +
    9.75 +    mEnv = env;
    9.76 +
    9.77 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
    9.78 +
    9.79 +    jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity"); 
    9.80 +    mActivityInstance = cls;
    9.81 +    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    9.82 +    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    9.83 +    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
    9.84 +    midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
    9.85 +
    9.86 +    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
    9.87 +        !midUpdateAudio){
    9.88 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    9.89 +    }else{
    9.90 +#ifdef DEBUG
    9.91 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    9.92 +#endif
    9.93 +    }
    9.94 +    
    9.95 +    return JNI_VERSION_1_4;
    9.96 +}
    9.97 +
    9.98 +//Start up the SDL app
    9.99 +extern "C" void Java_org_libsdl_app_SDLActivity_nativeInit( JNIEnv* env, 
   9.100 +                                                                jobject obj ){ 
   9.101 +                                                                   
   9.102 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native Init");
   9.103 +
   9.104 +	mEnv = env;
   9.105 +	bRenderingEnabled = true;
   9.106 +
   9.107 +	Android_EnableFeature(FEATURE_ACCEL, true);
   9.108 +
   9.109 +    SDL_main();
   9.110 +}
   9.111 +
   9.112 +//Keydown
   9.113 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env, 
   9.114 +               jobject obj, jint keycode){
   9.115 +    
   9.116 +    int r = Android_OnKeyDown(keycode);
   9.117 +#ifdef DEBUG
   9.118 +    __android_log_print(ANDROID_LOG_INFO, "SDL", 
   9.119 +                        "SDL: native key down %d, %d\n", keycode, r);
   9.120 +#endif
   9.121 +                        
   9.122 +}
   9.123 +
   9.124 +//Keyup
   9.125 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env, 
   9.126 +               jobject obj, jint keycode){
   9.127 +    
   9.128 +    int r = Android_OnKeyUp(keycode);
   9.129 +#ifdef DEBUG
   9.130 +    __android_log_print(ANDROID_LOG_INFO, "SDL", 
   9.131 +                        "SDL: native key up %d, %d\n", keycode, r);
   9.132 +#endif
   9.133 +                        
   9.134 +}
   9.135 +
   9.136 +//Touch
   9.137 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env, 
   9.138 +               jobject obj, jint action, jfloat x, jfloat y, jfloat p){
   9.139 +
   9.140 +#ifdef DEBUG
   9.141 +    __android_log_print(ANDROID_LOG_INFO, "SDL", 
   9.142 +                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
   9.143 +                        action, x, y, p);
   9.144 +#endif
   9.145 +
   9.146 +    //TODO: Pass this off to the SDL multitouch stuff
   9.147 +                        
   9.148 +}
   9.149 +
   9.150 +//Quit
   9.151 +extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv*  env, 
   9.152 +                                                                jobject obj ){    
   9.153 +
   9.154 +    //Stop rendering as we're no longer in the foreground
   9.155 +	bRenderingEnabled = false;
   9.156 +
   9.157 +    //Inject a SDL_QUIT event
   9.158 +    int r = SDL_SendQuit();
   9.159 +
   9.160 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Native quit %d", r);        
   9.161 +}
   9.162 +
   9.163 +//Screen size
   9.164 +extern "C" void Java_org_libsdl_app_SDLActivity_nativeSetScreenSize(
   9.165 +                JNIEnv*  env, jobject obj, jint width, jint height){
   9.166 +
   9.167 +    __android_log_print(ANDROID_LOG_INFO, "SDL", 
   9.168 +                        "SDL: Set screen size on init: %d/%d\n", width, height);
   9.169 +    Android_SetScreenResolution(width, height);
   9.170 +                        
   9.171 +}
   9.172 +
   9.173 +//Resize
   9.174 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
   9.175 +                                        JNIEnv*  env, jobject obj, jint width, 
   9.176 +                                        jint height, jint format){
   9.177 +    Android_OnResize(width, height, format);
   9.178 +}
   9.179 +
   9.180 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
   9.181 +                                        JNIEnv*  env, jobject obj,
   9.182 +                                        jfloat x, jfloat y, jfloat z){
   9.183 +    fLastAccelerometer[0] = x;
   9.184 +    fLastAccelerometer[1] = y;
   9.185 +    fLastAccelerometer[2] = z;   
   9.186 +}
   9.187 +
   9.188 +
   9.189 +
   9.190 +/*******************************************************************************
   9.191 +             Functions called by SDL into Java
   9.192 +*******************************************************************************/
   9.193 +extern "C" void Android_CreateContext(){
   9.194 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
   9.195 +
   9.196 +	bRenderingEnabled = true;
   9.197 +
   9.198 +    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
   9.199 +}
   9.200 +
   9.201 +extern "C" void Android_Render(){
   9.202 +
   9.203 +    if(!bRenderingEnabled){
   9.204 +        return;
   9.205 +    }
   9.206 +
   9.207 +    //When we get here, we've accumulated a full frame    
   9.208 +    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
   9.209 +}
   9.210 +
   9.211 +extern "C" void Android_EnableFeature(int featureid, bool enabled){
   9.212 +
   9.213 +    mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
   9.214 +                                featureid, (int)enabled); 
   9.215 +}
   9.216 +
   9.217 +extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len){
   9.218 +
   9.219 +    //Annoyingly we can't just call into Java from any thread. Because the audio
   9.220 +    //callback is dispatched from the SDL audio thread (that wasn't made from
   9.221 +    //java, we have to do some magic here to let the JVM know about the thread.
   9.222 +    //Because everything it touches on the Java side is static anyway, it's 
   9.223 +    //not a big deal, just annoying.
   9.224 +    if(!mAudioThreadEnv){
   9.225 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
   9.226 +
   9.227 +        mVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
   9.228 +
   9.229 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
   9.230 +    }
   9.231 +    
   9.232 +    jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
   9.233 +
   9.234 +    //blah. We probably should rework this so we avoid the copy. 
   9.235 +    mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
   9.236 +    
   9.237 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
   9.238 +
   9.239 +    mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
   9.240 +                                            midUpdateAudio, arr );
   9.241 +
   9.242 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
   9.243 +    
   9.244 +}
   9.245 +
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/android/project/jni/lesson05.c	Sun Aug 22 12:23:55 2010 -0700
    10.3 @@ -0,0 +1,574 @@
    10.4 +/*
    10.5 + * This code was created by Jeff Molofee '99 
    10.6 + * (ported to Linux/SDL by Ti Leggett '01)
    10.7 + *
    10.8 + * If you've found this code useful, please let me know.
    10.9 + *
   10.10 + * Visit Jeff at http://nehe.gamedev.net/
   10.11 + * 
   10.12 + * or for port-specific comments, questions, bugreports etc. 
   10.13 + * email to leggett@eecs.tulane.edu
   10.14 + */
   10.15 + 
   10.16 +#include <stdio.h>
   10.17 +#include <stdlib.h>
   10.18 +#include <math.h>
   10.19 +
   10.20 +#include <signal.h>
   10.21 +
   10.22 +#include <android/log.h>
   10.23 +
   10.24 +
   10.25 +#ifdef ANDROID
   10.26 +#include <GLES/gl.h>
   10.27 +#else
   10.28 +#include <GL/gl.h>
   10.29 +#include <GL/glu.h>
   10.30 +#endif
   10.31 +#include "SDL.h"
   10.32 +
   10.33 +/* screen width, height, and bit depth */
   10.34 +#define SCREEN_WIDTH  320
   10.35 +#define SCREEN_HEIGHT 430
   10.36 +#define SCREEN_BPP     16
   10.37 +
   10.38 +/* Define our booleans */
   10.39 +#define TRUE  1
   10.40 +#define FALSE 0
   10.41 +
   10.42 +/* This is our SDL surface */
   10.43 +SDL_Surface *surface;
   10.44 +
   10.45 +int rotation = 0;
   10.46 +
   10.47 +
   10.48 +/**************************************
   10.49 +	gluperspective implementation
   10.50 +**************************************/
   10.51 +void gluPerspective(double fovy, double aspect, double zNear, double zFar){
   10.52 +	glMatrixMode(GL_PROJECTION);
   10.53 +	glLoadIdentity();
   10.54 +	double xmin, xmax, ymin, ymax;
   10.55 +	ymax = zNear * tan(fovy * M_PI / 360.0);
   10.56 +	ymin = -ymax;
   10.57 +	xmin = ymin * aspect;
   10.58 +	xmax = ymax * aspect;
   10.59 +	glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
   10.60 +}
   10.61 +
   10.62 +
   10.63 +/**************************************
   10.64 +	  glulookat implementation
   10.65 +**************************************/
   10.66 +void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
   10.67 +          GLfloat centerx, GLfloat centery, GLfloat centerz,
   10.68 +          GLfloat upx, GLfloat upy, GLfloat upz)
   10.69 +{
   10.70 +    GLfloat m[16];
   10.71 +    GLfloat x[3], y[3], z[3];
   10.72 +    GLfloat mag;
   10.73 +    
   10.74 +    /* Make rotation matrix */
   10.75 +    
   10.76 +    /* Z vector */
   10.77 +    z[0] = eyex - centerx;
   10.78 +    z[1] = eyey - centery;
   10.79 +    z[2] = eyez - centerz;
   10.80 +    mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
   10.81 +    if (mag) {          /* mpichler, 19950515 */
   10.82 +        z[0] /= mag;
   10.83 +        z[1] /= mag;
   10.84 +        z[2] /= mag;
   10.85 +    }
   10.86 +    
   10.87 +    /* Y vector */
   10.88 +    y[0] = upx;
   10.89 +    y[1] = upy;
   10.90 +    y[2] = upz;
   10.91 +    
   10.92 +    /* X vector = Y cross Z */
   10.93 +    x[0] = y[1] * z[2] - y[2] * z[1];
   10.94 +    x[1] = -y[0] * z[2] + y[2] * z[0];
   10.95 +    x[2] = y[0] * z[1] - y[1] * z[0];
   10.96 +    
   10.97 +    /* Recompute Y = Z cross X */
   10.98 +    y[0] = z[1] * x[2] - z[2] * x[1];
   10.99 +    y[1] = -z[0] * x[2] + z[2] * x[0];
  10.100 +    y[2] = z[0] * x[1] - z[1] * x[0];
  10.101 +    
  10.102 +    /* mpichler, 19950515 */
  10.103 +    /* cross product gives area of parallelogram, which is < 1.0 for
  10.104 +     * non-perpendicular unit-length vectors; so normalize x, y here
  10.105 +     */
  10.106 +    
  10.107 +    mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
  10.108 +    if (mag) {
  10.109 +        x[0] /= mag;
  10.110 +        x[1] /= mag;
  10.111 +        x[2] /= mag;
  10.112 +    }
  10.113 +    
  10.114 +    mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
  10.115 +    if (mag) {
  10.116 +        y[0] /= mag;
  10.117 +        y[1] /= mag;
  10.118 +        y[2] /= mag;
  10.119 +    }
  10.120 +    
  10.121 +#define M(row,col)  m[col*4+row]
  10.122 +    M(0, 0) = x[0];
  10.123 +    M(0, 1) = x[1];
  10.124 +    M(0, 2) = x[2];
  10.125 +    M(0, 3) = 0.0;
  10.126 +    M(1, 0) = y[0];
  10.127 +    M(1, 1) = y[1];
  10.128 +    M(1, 2) = y[2];
  10.129 +    M(1, 3) = 0.0;
  10.130 +    M(2, 0) = z[0];
  10.131 +    M(2, 1) = z[1];
  10.132 +    M(2, 2) = z[2];
  10.133 +    M(2, 3) = 0.0;
  10.134 +    M(3, 0) = 0.0;
  10.135 +    M(3, 1) = 0.0;
  10.136 +    M(3, 2) = 0.0;
  10.137 +    M(3, 3) = 1.0;
  10.138 +#undef M
  10.139 +    glMultMatrixf(m);
  10.140 +    
  10.141 +    /* Translate Eye to Origin */
  10.142 +    glTranslatef(-eyex, -eyey, -eyez);
  10.143 +    
  10.144 +}
  10.145 +
  10.146 +
  10.147 +
  10.148 +
  10.149 +
  10.150 +/* function to release/destroy our resources and restoring the old desktop */
  10.151 +void Quit( int returnCode )
  10.152 +{
  10.153 +    /* clean up the window */
  10.154 +    SDL_Quit( );
  10.155 +
  10.156 +    /* and exit appropriately */
  10.157 +    exit( returnCode );
  10.158 +}
  10.159 +
  10.160 +/* function to reset our viewport after a window resize */
  10.161 +int resizeWindow( int width, int height )
  10.162 +{
  10.163 +    /* Height / width ration */
  10.164 +    GLfloat ratio;
  10.165 + 
  10.166 +    /* Protect against a divide by zero */
  10.167 +   if ( height == 0 )
  10.168 +	height = 1;
  10.169 +
  10.170 +    ratio = ( GLfloat )width / ( GLfloat )height;
  10.171 +
  10.172 +    /* Setup our viewport. */
  10.173 +    glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
  10.174 +
  10.175 +    /* change to the projection matrix and set our viewing volume. */
  10.176 +    glMatrixMode( GL_PROJECTION );
  10.177 +    glLoadIdentity( );
  10.178 +
  10.179 +    /* Set our perspective */
  10.180 +    gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
  10.181 +
  10.182 +    /* Make sure we're chaning the model view and not the projection */
  10.183 +    glMatrixMode( GL_MODELVIEW );
  10.184 +
  10.185 +    /* Reset The View */
  10.186 +    glLoadIdentity( );
  10.187 +
  10.188 +    return( TRUE );
  10.189 +}
  10.190 +
  10.191 +/* function to handle key press events */
  10.192 +void handleKeyPress( SDL_keysym *keysym )
  10.193 +{
  10.194 +    switch ( keysym->sym )
  10.195 +	{
  10.196 +	case SDLK_ESCAPE:
  10.197 +	    /* ESC key was pressed */
  10.198 +	    Quit( 0 );
  10.199 +	    break;
  10.200 +	case SDLK_F1:
  10.201 +	    /* F1 key was pressed
  10.202 +	     * this toggles fullscreen mode
  10.203 +	     */
  10.204 +	    SDL_WM_ToggleFullScreen( surface );
  10.205 +	    break;
  10.206 +    case SDLK_LEFT:
  10.207 +        rotation -= 30;
  10.208 +        break;
  10.209 +
  10.210 +    case SDLK_RIGHT:
  10.211 +        rotation += 30;
  10.212 +        break;
  10.213 +        
  10.214 +	default:
  10.215 +	    break;
  10.216 +	}
  10.217 +
  10.218 +    __android_log_print(ANDROID_LOG_INFO, "SDL","Keycode: %d, %d, %d\n", keysym->sym, SDLK_LEFT, SDLK_RIGHT);
  10.219 +
  10.220 +    return;
  10.221 +}
  10.222 +
  10.223 +/* general OpenGL initialization function */
  10.224 +int initGL( GLvoid )
  10.225 +{
  10.226 +
  10.227 +    /* Enable smooth shading */
  10.228 +    glShadeModel( GL_SMOOTH );
  10.229 +
  10.230 +    /* Set the background black */
  10.231 +    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  10.232 +
  10.233 +    /* Depth buffer setup */
  10.234 +    //glClearDepth( 1.0f );
  10.235 +
  10.236 +    /* Enables Depth Testing */
  10.237 +    glEnable( GL_DEPTH_TEST );
  10.238 +
  10.239 +    /* The Type Of Depth Test To Do */
  10.240 +    glDepthFunc( GL_LEQUAL );
  10.241 +
  10.242 +    /* Really Nice Perspective Calculations */
  10.243 +    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
  10.244 +
  10.245 +    return( TRUE );
  10.246 +}
  10.247 +
  10.248 +/* Here goes our drawing code */
  10.249 +int drawGLScene( GLvoid )
  10.250 +{
  10.251 +      
  10.252 +	static int Frames = 0;
  10.253 +	static int T0 = 0;
  10.254 +	
  10.255 +    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  10.256 +
  10.257 +    glClearColorx(0,0,0,255);
  10.258 +    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  10.259 +
  10.260 +    glMatrixMode(GL_PROJECTION);
  10.261 +    glLoadIdentity();
  10.262 +    gluPerspective(45, (float)SCREEN_WIDTH / SCREEN_HEIGHT, 0.5f, 150);
  10.263 +
  10.264 +    glMatrixMode(GL_MODELVIEW);
  10.265 +
  10.266 +    glLoadIdentity();
  10.267 +
  10.268 +	//Camera
  10.269 +	gluLookAt(0,0,5, 0,0,0, 0,1,0);
  10.270 +			
  10.271 +	//Draw a triangle
  10.272 +	//glRotatef(iRot, 0, 1, 0);
  10.273 +
  10.274 +	glRotatef( rotation, 0.0f, 1.0f, 0.0f );
  10.275 +
  10.276 +
  10.277 +	glEnableClientState (GL_VERTEX_ARRAY);
  10.278 +	glEnableClientState (GL_COLOR_ARRAY);
  10.279 +	
  10.280 +	/* Rotate The Triangle On The Y axis ( NEW ) */
  10.281 +    //glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
  10.282 +
  10.283 +    /* GLES variant of drawing a triangle */
  10.284 +    const GLfloat triVertices[][9] = {
  10.285 +      {     /* Front Triangle */
  10.286 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
  10.287 +	-1.0f, -1.0f,  1.0f,               /* Left Of Triangle              */
  10.288 +	 1.0f, -1.0f,  1.0f                /* Right Of Triangle             */
  10.289 +      }, {  /* Right Triangle */
  10.290 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
  10.291 +	 1.0f, -1.0f,  1.0f,               /* Left Of Triangle              */
  10.292 +	 1.0f, -1.0f, -1.0f                /* Right Of Triangle             */
  10.293 +      }, {  /* Back Triangle */
  10.294 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
  10.295 +	 1.0f, -1.0f, -1.0f,               /* Left Of Triangle              */
  10.296 +	-1.0f, -1.0f, -1.0f                /* Right Of Triangle             */
  10.297 +      }, {  /* Left Triangle */
  10.298 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
  10.299 +	-1.0f, -1.0f, -1.0f,               /* Left Of Triangle              */
  10.300 +	-1.0f, -1.0f,  1.0f                /* Right Of Triangle             */
  10.301 +      }
  10.302 +    };
  10.303 +
  10.304 +    /* unlike GL, GLES does not support RGB. We have to use RGBA instead */
  10.305 +    const GLfloat triColors[][12] = {
  10.306 +      {     /* Front triangle */
  10.307 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
  10.308 +	0.0f, 1.0f, 0.0f, 1.0f,            /* Green                         */
  10.309 +	0.0f, 0.0f, 1.0f, 1.0f             /* Blue                          */
  10.310 +      }, {  /* Right triangle */
  10.311 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
  10.312 +	0.0f, 0.0f, 1.0f, 1.0f,            /* Blue                          */
  10.313 +	0.0f, 1.0f, 0.0f, 1.0f             /* Green                         */
  10.314 +      }, {  /* Back triangle */
  10.315 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
  10.316 +	0.0f, 1.0f, 0.0f, 1.0f,            /* Green                         */
  10.317 +	0.0f, 0.0f, 1.0f, 1.0f             /* Blue                          */
  10.318 +      }, {  /* Left triangle */
  10.319 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
  10.320 +	0.0f, 0.0f, 1.0f, 1.0f,            /* Blue                          */
  10.321 +	0.0f, 1.0f, 0.0f, 1.0f             /* Green                         */
  10.322 +      }
  10.323 +    };
  10.324 +
  10.325 +    glEnableClientState(GL_COLOR_ARRAY);
  10.326 +
  10.327 +    int tri=0;
  10.328 +
  10.329 +    /* Loop through all Triangles */
  10.330 +    for(tri=0;tri<sizeof(triVertices)/(9*sizeof(GLfloat));tri++) 
  10.331 +    {
  10.332 +      glVertexPointer(3, GL_FLOAT, 0, triVertices[tri]);
  10.333 +      glColorPointer(4, GL_FLOAT, 0, triColors[tri]);
  10.334 +      
  10.335 +      glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
  10.336 +    }
  10.337 +		
  10.338 +    //__android_log_print(ANDROID_LOG_INFO, "SDL", "render %d", Frames++);
  10.339 +
  10.340 +    /* Draw it to the screen */
  10.341 +    SDL_GL_SwapBuffers( );
  10.342 +
  10.343 +    /* Gather our frames per second */
  10.344 +    Frames++;
  10.345 +    {
  10.346 +	GLint t = SDL_GetTicks();
  10.347 +	if (t - T0 >= 5000) {
  10.348 +	    GLfloat seconds = (t - T0) / 1000.0;
  10.349 +	    GLfloat fps = Frames / seconds;
  10.350 +	    __android_log_print(ANDROID_LOG_INFO, "SDL","%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
  10.351 +	    T0 = t;
  10.352 +	    Frames = 0;
  10.353 +	}
  10.354 +    }
  10.355 +
  10.356 +    rotation++;
  10.357 +
  10.358 +    return( TRUE );
  10.359 +}
  10.360 +
  10.361 +
  10.362 +struct
  10.363 +{
  10.364 +    SDL_AudioSpec spec;
  10.365 +    Uint8 *sound;               /* Pointer to wave data */
  10.366 +    Uint32 soundlen;            /* Length of wave data */
  10.367 +    int soundpos;               /* Current play position */
  10.368 +} wave;
  10.369 +
  10.370 +void SDLCALL
  10.371 +fillerup(void *unused, Uint8 * stream, int len)
  10.372 +{
  10.373 +    __android_log_print(ANDROID_LOG_INFO, "SDL","FILLERUP\n");
  10.374 +    
  10.375 +    Uint8 *waveptr;
  10.376 +    int waveleft;
  10.377 +
  10.378 +    /* Set up the pointers */
  10.379 +    waveptr = wave.sound + wave.soundpos;
  10.380 +    waveleft = wave.soundlen - wave.soundpos;
  10.381 +
  10.382 +    /* Go! */
  10.383 +    while (waveleft <= len) {
  10.384 +        SDL_memcpy(stream, waveptr, waveleft);
  10.385 +        stream += waveleft;
  10.386 +        len -= waveleft;
  10.387 +        waveptr = wave.sound;
  10.388 +        waveleft = wave.soundlen;
  10.389 +        wave.soundpos = 0;
  10.390 +    }
  10.391 +    SDL_memcpy(stream, waveptr, len);
  10.392 +    wave.soundpos += len;
  10.393 +}
  10.394 +
  10.395 +void testAudio(){
  10.396 +
  10.397 +    const char *file = "/sdcard/sample.wav";
  10.398 +
  10.399 +    /* Load the SDL library */
  10.400 +    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  10.401 +        __android_log_print(ANDROID_LOG_INFO, "SDL","Couldn't initialize SDL Audio: %s\n", SDL_GetError());
  10.402 +        return;
  10.403 +    }else{
  10.404 +        __android_log_print(ANDROID_LOG_INFO, "SDL","Init audio ok\n");
  10.405 +    }
  10.406 +
  10.407 +    /* Load the wave file into memory */
  10.408 +    if (SDL_LoadWAV(file, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  10.409 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't load %s: %s\n", file, SDL_GetError());
  10.410 +        return;
  10.411 +    }
  10.412 +
  10.413 +    wave.spec.callback = fillerup;
  10.414 +
  10.415 +    __android_log_print(ANDROID_LOG_INFO, "SDL","Loaded: %d\n", wave.soundlen);
  10.416 +
  10.417 +
  10.418 +    /* Initialize fillerup() variables */
  10.419 +    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
  10.420 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't open audio: %s\n", SDL_GetError());
  10.421 +        SDL_FreeWAV(wave.sound);
  10.422 +        return;
  10.423 +    }
  10.424 +
  10.425 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  10.426 +
  10.427 +    /* Let the audio run */
  10.428 +    SDL_PauseAudio(0);
  10.429 +
  10.430 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Playing\n");
  10.431 +    
  10.432 +    while (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING){
  10.433 +         //__android_log_print(ANDROID_LOG_INFO, "SDL","Still playing\n");
  10.434 +        SDL_Delay(100);
  10.435 +    }
  10.436 +
  10.437 +     __android_log_print(ANDROID_LOG_INFO, "SDL","Closing down\n");
  10.438 +
  10.439 +    /* Clean up on signal */
  10.440 +    SDL_CloseAudio();
  10.441 +    SDL_FreeWAV(wave.sound);
  10.442 +}
  10.443 +
  10.444 +int SDL_main( int argc, char **argv )
  10.445 +{
  10.446 +
  10.447 +	__android_log_print(ANDROID_LOG_INFO, "SDL","entry\n");
  10.448 +
  10.449 +    /* Flags to pass to SDL_SetVideoMode */
  10.450 +    int videoFlags;
  10.451 +    /* main loop variable */
  10.452 +    int done = FALSE;
  10.453 +    /* used to collect events */
  10.454 +    SDL_Event event;
  10.455 +    /* this holds some info about our display */
  10.456 +    const SDL_VideoInfo *videoInfo;
  10.457 +    /* whether or not the window is active */
  10.458 +    int isActive = TRUE;
  10.459 +
  10.460 +    /* initialize SDL */
  10.461 +    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  10.462 +	{
  10.463 +	    __android_log_print(ANDROID_LOG_INFO, "SDL", "Video initialization failed: %s\n",
  10.464 +		     SDL_GetError( ) );
  10.465 +	    Quit( 1 );
  10.466 +	}
  10.467 +
  10.468 +    /* Fetch the video info */
  10.469 +    videoInfo = SDL_GetVideoInfo( );
  10.470 +
  10.471 +    if ( !videoInfo )
  10.472 +	{
  10.473 +	    __android_log_print(ANDROID_LOG_INFO, "SDL", "Video query failed: %s\n",
  10.474 +		     SDL_GetError( ) );
  10.475 +	    Quit( 1 );
  10.476 +	}
  10.477 +
  10.478 +    /* the flags to pass to SDL_SetVideoMode */
  10.479 +    videoFlags  = SDL_OPENGL;          /* Enable OpenGL in SDL */
  10.480 +    videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
  10.481 +    videoFlags |= SDL_HWPALETTE;       /* Store the palette in hardware */
  10.482 +    videoFlags |= SDL_RESIZABLE;       /* Enable window resizing */
  10.483 +
  10.484 +    /* This checks to see if surfaces can be stored in memory */
  10.485 +    if ( videoInfo->hw_available )
  10.486 +	videoFlags |= SDL_HWSURFACE;
  10.487 +    else
  10.488 +	videoFlags |= SDL_SWSURFACE;
  10.489 +
  10.490 +    /* This checks if hardware blits can be done */
  10.491 +    if ( videoInfo->blit_hw )
  10.492 +	videoFlags |= SDL_HWACCEL;
  10.493 +
  10.494 +    /* Sets up OpenGL double buffering */
  10.495 +    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  10.496 +
  10.497 +    /* get a SDL surface */
  10.498 +    surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
  10.499 +				videoFlags );
  10.500 +
  10.501 +    /* Verify there is a surface */
  10.502 +    if ( !surface )
  10.503 +	{
  10.504 +	    __android_log_print(ANDROID_LOG_INFO, "SDL",  "Video mode set failed: %s\n", SDL_GetError( ) );
  10.505 +	    Quit( 1 );
  10.506 +	}
  10.507 +
  10.508 +	__android_log_print(ANDROID_LOG_INFO, "SDL","Made a video mode!\n");
  10.509 +
  10.510 +    /* initialize OpenGL */
  10.511 +    initGL( );
  10.512 +
  10.513 +    /* resize the initial window */
  10.514 +    resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
  10.515 +
  10.516 +
  10.517 +    //testAudio();
  10.518 +
  10.519 +
  10.520 +    /* wait for events */ 
  10.521 +    while ( !done )
  10.522 +	{
  10.523 +	    /* handle the events in the queue */
  10.524 +
  10.525 +	    while ( SDL_PollEvent( &event ) )
  10.526 +		{
  10.527 +		    switch( event.type )
  10.528 +			{
  10.529 +			case SDL_ACTIVEEVENT:
  10.530 +			    /* Something's happend with our focus
  10.531 +			     * If we lost focus or we are iconified, we
  10.532 +			     * shouldn't draw the screen
  10.533 +			     */
  10.534 +			    if ( event.active.gain == 0 )
  10.535 +				isActive = FALSE;
  10.536 +			    else
  10.537 +				isActive = TRUE;
  10.538 +			    break;			    
  10.539 +			case SDL_VIDEORESIZE:
  10.540 +			    /* handle resize event */
  10.541 +			    surface = SDL_SetVideoMode( event.resize.w,
  10.542 +							event.resize.h,
  10.543 +							16, videoFlags );
  10.544 +			    if ( !surface )
  10.545 +				{
  10.546 +				    __android_log_print(ANDROID_LOG_INFO, "SDL","Could not get a surface after resize: %s\n", SDL_GetError( ) );
  10.547 +				    Quit( 1 );
  10.548 +				}
  10.549 +			    resizeWindow( event.resize.w, event.resize.h );
  10.550 +			    break;
  10.551 +			case SDL_KEYDOWN:
  10.552 +			    /* handle key presses */
  10.553 +			    handleKeyPress( &event.key.keysym );
  10.554 +			    break;
  10.555 +			case SDL_QUIT:
  10.556 +			    /* handle quit requests */
  10.557 +			    done = TRUE;
  10.558 +			    __android_log_print(ANDROID_LOG_INFO, "SDL","App is shutting down\n");
  10.559 +			    break;
  10.560 +			default:
  10.561 +			    break;
  10.562 +			}
  10.563 +		}
  10.564 +
  10.565 +	    /* draw the scene */
  10.566 +	    if ( isActive )
  10.567 +		drawGLScene( );
  10.568 +	}
  10.569 +
  10.570 +    /* clean ourselves up and exit */
  10.571 +    Quit( 0 );
  10.572 +
  10.573 +    /* Should never get here */
  10.574 +    return( 0 );
  10.575 +}
  10.576 +
  10.577 +
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/android/project/local.properties	Sun Aug 22 12:23:55 2010 -0700
    11.3 @@ -0,0 +1,10 @@
    11.4 +# This file is automatically generated by Android Tools.
    11.5 +# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
    11.6 +# 
    11.7 +# This file must *NOT* be checked in Version Control Systems,
    11.8 +# as it contains information specific to your local configuration.
    11.9 +
   11.10 +# location of the SDK. This is only used by Ant
   11.11 +# For customization when using a Version Control System, please read the
   11.12 +# header note.
   11.13 +sdk.dir=/home/paul/Projects/gsoc/sdk/android-sdk-linux_86
    12.1 Binary file android/project/res/drawable-hdpi/icon.png has changed
    13.1 Binary file android/project/res/drawable-ldpi/icon.png has changed
    14.1 Binary file android/project/res/drawable-mdpi/icon.png has changed
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/android/project/res/layout/main.xml	Sun Aug 22 12:23:55 2010 -0700
    15.3 @@ -0,0 +1,13 @@
    15.4 +<?xml version="1.0" encoding="utf-8"?>
    15.5 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    15.6 +    android:orientation="vertical"
    15.7 +    android:layout_width="fill_parent"
    15.8 +    android:layout_height="fill_parent"
    15.9 +    >
   15.10 +<TextView  
   15.11 +    android:layout_width="fill_parent" 
   15.12 +    android:layout_height="wrap_content" 
   15.13 +    android:text="Hello World, SDLActivity"
   15.14 +    />
   15.15 +</LinearLayout>
   15.16 +
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/android/project/res/values/strings.xml	Sun Aug 22 12:23:55 2010 -0700
    16.3 @@ -0,0 +1,4 @@
    16.4 +<?xml version="1.0" encoding="utf-8"?>
    16.5 +<resources>
    16.6 +    <string name="app_name">SDLActivity</string>
    16.7 +</resources>
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/android/project/src/org/libsdl/app/SDLActivity.java	Sun Aug 22 12:23:55 2010 -0700
    17.3 @@ -0,0 +1,388 @@
    17.4 +package org.libsdl.app;
    17.5 +
    17.6 +import javax.microedition.khronos.egl.EGLConfig;
    17.7 +import javax.microedition.khronos.opengles.GL10;
    17.8 +import javax.microedition.khronos.egl.*;
    17.9 +
   17.10 +import android.app.*;
   17.11 +import android.content.*;
   17.12 +import android.view.*;
   17.13 +import android.os.*;
   17.14 +import android.util.Log;
   17.15 +import android.graphics.*;
   17.16 +import android.text.method.*;
   17.17 +import android.text.*;
   17.18 +import android.media.*;
   17.19 +import android.hardware.*;
   17.20 +import android.content.*;
   17.21 +
   17.22 +import java.lang.*;
   17.23 +
   17.24 +
   17.25 +/**
   17.26 +    SDL Activity
   17.27 +*/
   17.28 +public class SDLActivity extends Activity {
   17.29 +
   17.30 +    //Main components
   17.31 +    private static SDLActivity mSingleton;
   17.32 +    private static SDLSurface mSurface;
   17.33 +
   17.34 +    //Audio
   17.35 +    private static AudioTrack mAudioTrack;
   17.36 +    private static boolean bAudioIsEnabled;
   17.37 +
   17.38 +    //Sensors
   17.39 +    private static boolean bAccelIsEnabled;
   17.40 +
   17.41 +    //feature IDs. Must match up on the C side as well.
   17.42 +    private static int FEATURE_AUDIO = 1;
   17.43 +    private static int FEATURE_ACCEL = 2;
   17.44 +
   17.45 +    //Load the .so
   17.46 +    static {
   17.47 +        System.loadLibrary("sdlapp");
   17.48 +    }
   17.49 +
   17.50 +    //Setup
   17.51 +    protected void onCreate(Bundle savedInstanceState) {
   17.52 +        super.onCreate(savedInstanceState);
   17.53 +        
   17.54 +        //So we can call stuff from static callbacks
   17.55 +        mSingleton = this;
   17.56 +
   17.57 +        //Set up the surface
   17.58 +        mSurface = new SDLSurface(getApplication());
   17.59 +        setContentView(mSurface);
   17.60 +        SurfaceHolder holder = mSurface.getHolder();
   17.61 +        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
   17.62 +        
   17.63 +    }
   17.64 +
   17.65 +    //Audio
   17.66 +    public static boolean initAudio(){        
   17.67 +
   17.68 +        //blah. Hardcoded things are bad. FIXME when we have more sound stuff
   17.69 +        //working properly. 
   17.70 +        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
   17.71 +                    11025,
   17.72 +                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
   17.73 +                    AudioFormat.ENCODING_PCM_8BIT,
   17.74 +                    2048,
   17.75 +                    AudioTrack.MODE_STREAM);   
   17.76 +        bAudioIsEnabled = true;     
   17.77 +        return true;
   17.78 +    }
   17.79 +
   17.80 +    //Accel
   17.81 +    public static boolean initAccel(){
   17.82 +        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
   17.83 +        bAccelIsEnabled = true;
   17.84 +        return true;
   17.85 +    }
   17.86 +    
   17.87 +    public static boolean closeAccel(){
   17.88 +        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false);
   17.89 +        bAccelIsEnabled = false;
   17.90 +        return true;
   17.91 +    }
   17.92 +    
   17.93 +
   17.94 +    //Events
   17.95 +    protected void onPause() {
   17.96 +        super.onPause();
   17.97 +    }
   17.98 +
   17.99 +    protected void onResume() {
  17.100 +        super.onResume();
  17.101 +    }
  17.102 +
  17.103 +    
  17.104 +
  17.105 +
  17.106 +
  17.107 +    //C functions we call
  17.108 +    public static native void nativeInit();
  17.109 +    public static native void nativeQuit();
  17.110 +    public static native void nativeSetScreenSize(int width, int height);
  17.111 +    public static native void onNativeKeyDown(int keycode);
  17.112 +    public static native void onNativeKeyUp(int keycode);
  17.113 +    public static native void onNativeTouch(int action, float x, 
  17.114 +                                            float y, float p);
  17.115 +    public static native void onNativeResize(int x, int y, int format);
  17.116 +    public static native void onNativeAccel(float x, float y, float z);
  17.117 +
  17.118 +
  17.119 +
  17.120 +    //Java functions called from C
  17.121 +    private static void createGLContext(){
  17.122 +        mSurface.initEGL();
  17.123 +    }
  17.124 +
  17.125 +    public static void flipBuffers(){
  17.126 +        mSurface.flipEGL();
  17.127 +    }
  17.128 +
  17.129 +    public static void updateAudio(byte [] buf){
  17.130 +    
  17.131 +        if(mAudioTrack == null){
  17.132 +            return;
  17.133 +        }
  17.134 +        
  17.135 +        mAudioTrack.write(buf, 0, buf.length);
  17.136 +        mAudioTrack.play();
  17.137 +        
  17.138 +        Log.v("SDL","Played some audio");
  17.139 +    }
  17.140 +
  17.141 +    public static void enableFeature(int featureid, int enabled){
  17.142 +         Log.v("SDL","Feature " + featureid + " = " + enabled);
  17.143 +
  17.144 +        //Yuck. This is all horribly inelegent. If it gets to more than a few
  17.145 +        //'features' I'll rip this out and make something nicer, I promise :)
  17.146 +        if(featureid == FEATURE_AUDIO){
  17.147 +            if(enabled == 1){
  17.148 +                initAudio();
  17.149 +            }else{
  17.150 +                //We don't have one of these yet...
  17.151 +                //closeAudio(); 
  17.152 +            }
  17.153 +        }
  17.154 +
  17.155 +        else if(featureid == FEATURE_ACCEL){
  17.156 +            if(enabled == 1){
  17.157 +                initAccel();
  17.158 +            }else{
  17.159 +                closeAccel();
  17.160 +            }
  17.161 +        }
  17.162 +    }
  17.163 +
  17.164 +
  17.165 +
  17.166 +
  17.167 +
  17.168 +    
  17.169 +    
  17.170 +}
  17.171 +
  17.172 +/**
  17.173 +    Simple nativeInit() runnable
  17.174 +*/
  17.175 +class SDLRunner implements Runnable{
  17.176 +    public void run(){
  17.177 +        //SDLActivity.initAudio();
  17.178 +        
  17.179 +        //Runs SDL_main()
  17.180 +        SDLActivity.nativeInit();
  17.181 +
  17.182 +        Log.v("SDL","SDL thread terminated");
  17.183 +    }
  17.184 +}
  17.185 +
  17.186 +
  17.187 +/**
  17.188 +    SDLSurface. This is what we draw on, so we need to know when it's created
  17.189 +    in order to do anything useful. 
  17.190 +
  17.191 +    Because of this, that's where we set up the SDL thread
  17.192 +*/
  17.193 +class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
  17.194 +    View.OnKeyListener, View.OnTouchListener, SensorEventListener  {
  17.195 +
  17.196 +    //This is what SDL runs in. It invokes SDL_main(), eventually
  17.197 +    private Thread mSDLThread;    
  17.198 +    
  17.199 +    //EGL private objects
  17.200 +    private EGLContext  mEGLContext;
  17.201 +    private EGLSurface  mEGLSurface;
  17.202 +    private EGLDisplay  mEGLDisplay;
  17.203 +
  17.204 +    //Sensors
  17.205 +    private static SensorManager mSensorManager;
  17.206 +
  17.207 +    //Startup    
  17.208 +    public SDLSurface(Context context) {
  17.209 +        super(context);
  17.210 +        getHolder().addCallback(this); 
  17.211 +    
  17.212 +        setFocusable(true);
  17.213 +        setFocusableInTouchMode(true);
  17.214 +        requestFocus();
  17.215 +        setOnKeyListener(this); 
  17.216 +        setOnTouchListener(this);   
  17.217 +        
  17.218 +        mSensorManager = (SensorManager)context.getSystemService("sensor");  
  17.219 +    }
  17.220 +
  17.221 +    //Called when we have a valid drawing surface
  17.222 +    public void surfaceCreated(SurfaceHolder holder) {
  17.223 +        Log.v("SDL","Surface created"); 
  17.224 +
  17.225 +        int width = getWidth();
  17.226 +        int height = getHeight();
  17.227 +
  17.228 +        //Set the width and height variables in C before we start SDL so we have
  17.229 +        //it available on init
  17.230 +        SDLActivity.nativeSetScreenSize(width, height);
  17.231 +
  17.232 +        //Now start up the C app thread
  17.233 +        mSDLThread = new Thread(new SDLRunner(), "SDLThread"); 
  17.234 +		mSDLThread.start();       
  17.235 +    }
  17.236 +
  17.237 +    //Called when we lose the surface
  17.238 +    public void surfaceDestroyed(SurfaceHolder holder) {
  17.239 +        Log.v("SDL","Surface destroyed");
  17.240 +        
  17.241 +        SDLActivity.nativeQuit();
  17.242 +
  17.243 +        //Now wait for the SDL thread to quit
  17.244 +        try{
  17.245 +            mSDLThread.wait();
  17.246 +        }catch(Exception e){
  17.247 +            Log.v("SDL","Problem stopping thread: " + e);
  17.248 +        }
  17.249 +    }
  17.250 +
  17.251 +    //Called when the surface is resized
  17.252 +    public void surfaceChanged(SurfaceHolder holder, int format, 
  17.253 +                                int width, int height) {
  17.254 +        Log.v("SDL","Surface resized");
  17.255 +        
  17.256 +        SDLActivity.onNativeResize(width, height, format);
  17.257 +    }
  17.258 +
  17.259 +    //unused
  17.260 +    public void onDraw(Canvas canvas) {}
  17.261 +
  17.262 +    
  17.263 +    //EGL functions
  17.264 +    public boolean initEGL(){
  17.265 +        Log.v("SDL","Starting up");
  17.266 +
  17.267 +        try{
  17.268 +
  17.269 +            EGL10 egl = (EGL10)EGLContext.getEGL();
  17.270 +
  17.271 +            EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
  17.272 +
  17.273 +            int[] version = new int[2];
  17.274 +            egl.eglInitialize(dpy, version);
  17.275 +
  17.276 +            int[] configSpec = {
  17.277 +                    //EGL10.EGL_DEPTH_SIZE,   16,
  17.278 +                    EGL10.EGL_NONE
  17.279 +            };
  17.280 +            EGLConfig[] configs = new EGLConfig[1];
  17.281 +            int[] num_config = new int[1];
  17.282 +            egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
  17.283 +            EGLConfig config = configs[0];
  17.284 +
  17.285 +            EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
  17.286 +
  17.287 +            EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
  17.288 +
  17.289 +            egl.eglMakeCurrent(dpy, surface, surface, ctx);
  17.290 +
  17.291 +            mEGLContext = ctx;
  17.292 +            mEGLDisplay = dpy;
  17.293 +            mEGLSurface = surface;
  17.294 +            
  17.295 +            
  17.296 +        }catch(Exception e){
  17.297 +            Log.v("SDL", e + "");
  17.298 +            for(StackTraceElement s : e.getStackTrace()){
  17.299 +                Log.v("SDL", s.toString());
  17.300 +            }
  17.301 +        }
  17.302 +        Log.v("SDL","Done making!");
  17.303 +
  17.304 +        return true;
  17.305 +    }
  17.306 +
  17.307 +    //EGL buffer flip
  17.308 +    public void flipEGL(){      
  17.309 +        try{
  17.310 +        
  17.311 +            EGL10 egl = (EGL10)EGLContext.getEGL();
  17.312 +            GL10 gl = (GL10)mEGLContext.getGL();
  17.313 +
  17.314 +            egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
  17.315 +
  17.316 +            //drawing here
  17.317 +
  17.318 +            egl.eglWaitGL();
  17.319 +
  17.320 +            egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
  17.321 +
  17.322 +            
  17.323 +        }catch(Exception e){
  17.324 +            Log.v("SDL", "flipEGL(): " + e);
  17.325 +
  17.326 +            for(StackTraceElement s : e.getStackTrace()){
  17.327 +                Log.v("SDL", s.toString());
  17.328 +            }
  17.329 +        }
  17.330 +    }
  17.331 +
  17.332 +
  17.333 +  
  17.334 +    //Key events
  17.335 +    public boolean onKey(View  v, int keyCode, KeyEvent event){
  17.336 +
  17.337 +        if(event.getAction() == KeyEvent.ACTION_DOWN){
  17.338 +            SDLActivity.onNativeKeyDown(keyCode);
  17.339 +            return true;
  17.340 +        }
  17.341 +        
  17.342 +        else if(event.getAction() == KeyEvent.ACTION_UP){
  17.343 +            SDLActivity.onNativeKeyUp(keyCode);
  17.344 +            return true;
  17.345 +        }
  17.346 +        
  17.347 +        return false;
  17.348 +    }
  17.349 +
  17.350 +    //Touch events
  17.351 +    public boolean onTouch(View v, MotionEvent event){
  17.352 +    
  17.353 +        int action = event.getAction();
  17.354 +        float x = event.getX();
  17.355 +        float y = event.getY();
  17.356 +        float p = event.getPressure();
  17.357 +
  17.358 +        //TODO: Anything else we need to pass?        
  17.359 +        SDLActivity.onNativeTouch(action, x, y, p);
  17.360 +        return true;
  17.361 +    }
  17.362 +
  17.363 +    //Sensor events
  17.364 +    public void enableSensor(int sensortype, boolean enabled){
  17.365 +        //TODO: This uses getDefaultSensor - what if we have >1 accels?
  17.366 +        if(enabled){
  17.367 +            mSensorManager.registerListener(this, 
  17.368 +                            mSensorManager.getDefaultSensor(sensortype), 
  17.369 +                            SensorManager.SENSOR_DELAY_GAME, null);
  17.370 +        }else{
  17.371 +            mSensorManager.unregisterListener(this, 
  17.372 +                            mSensorManager.getDefaultSensor(sensortype));
  17.373 +        }
  17.374 +    }
  17.375 +    
  17.376 +    public void onAccuracyChanged(Sensor sensor, int accuracy){
  17.377 +        //TODO
  17.378 +    }
  17.379 +
  17.380 +    public void onSensorChanged(SensorEvent event){
  17.381 +        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
  17.382 +            SDLActivity.onNativeAccel(  event.values[0],
  17.383 +                                        event.values[1],
  17.384 +                                        event.values[2] );
  17.385 +        }
  17.386 +    }
  17.387 +
  17.388 +
  17.389 +}
  17.390 +
  17.391 +
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/android/scripts/acc.sh	Sun Aug 22 12:23:55 2010 -0700
    18.3 @@ -0,0 +1,15 @@
    18.4 +#!/bin/bash
    18.5 +ANDROID_NDK="/home/paul/Projects/gsoc/sdk/android-ndk-r4"
    18.6 +TOOLS_PATH="$ANDROID_NDK/build/prebuilt/linux-x86/arm-eabi-4.2.1/bin"
    18.7 +
    18.8 +export PATH=$TOOLS_PATH:$PATH
    18.9 +
   18.10 +CC="arm-eabi-gcc"
   18.11 +
   18.12 +#cflags
   18.13 +ACC_C="	-I$ANDROID_NDK/build/platforms/android-8/common/include \
   18.14 +		-I$ANDROID_NDK/build/platforms/android-8/arch-arm/usr/include \
   18.15 +		-DANDROID -DANDROID_NDK -c"
   18.16 +		
   18.17 +		
   18.18 +$CC $CFLAGS $ACC_C $@
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/android/scripts/ald.sh	Sun Aug 22 12:23:55 2010 -0700
    19.3 @@ -0,0 +1,20 @@
    19.4 +#!/bin/bash
    19.5 +ANDROID_NDK="/home/paul/Projects/gsoc/sdk/android-ndk-r4"
    19.6 +TOOLS_PATH="$ANDROID_NDK/build/prebuilt/linux-x86/arm-eabi-4.2.1/bin"
    19.7 +ADDITIONAL_LIBS=`dirname "$0"`/android_libs/
    19.8 +
    19.9 +export PATH=$TOOLS_PATH:$PATH
   19.10 +
   19.11 +LD="arm-eabi-ld"
   19.12 +
   19.13 +#ldflags
   19.14 +ACC_L="	-rpath-link=$ANDROID_NDK/build/platforms/android-8/arch-arm/usr/lib/ \
   19.15 +		-dynamic-linker=/system/bin/linker \
   19.16 +		-lc -nostdlib \
   19.17 + 		$ANDROID_NDK/build/platforms/android-8/arch-arm/usr/lib/crtbegin_static.o \
   19.18 + 		-L$ANDROID_NDK/build/platforms/android-8/arch-arm/usr/lib/ \
   19.19 + 		-L$ANDROID_NDK/build/prebuilt/linux-x86/arm-eabi-4.2.1/lib/gcc/arm-eabi/4.2.1 \
   19.20 + 		-L$ADDITIONAL_LIBS "
   19.21 +		
   19.22 +$LD $ACC_L $LDFLAGS $@ -lgcc
   19.23 +
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/include/SDL_config_android.h	Sun Aug 22 12:23:55 2010 -0700
    20.3 @@ -0,0 +1,145 @@
    20.4 +/*
    20.5 +    SDL - Simple DirectMedia Layer
    20.6 +    Copyright (C) 1997-2010 Sam Lantinga
    20.7 +
    20.8 +    This library is free software; you can redistribute it and/or
    20.9 +    modify it under the terms of the GNU Lesser General Public
   20.10 +    License as published by the Free Software Foundation; either
   20.11 +    version 2.1 of the License, or (at your option) any later version.
   20.12 +
   20.13 +    This library is distributed in the hope that it will be useful,
   20.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   20.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   20.16 +    Lesser General Public License for more details.
   20.17 +
   20.18 +    You should have received a copy of the GNU Lesser General Public
   20.19 +    License along with this library; if not, write to the Free Software
   20.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   20.21 +
   20.22 +    Sam Lantinga
   20.23 +    slouken@libsdl.org
   20.24 +*/
   20.25 +
   20.26 +#ifndef _SDL_config_android_h
   20.27 +#define _SDL_config_android_h
   20.28 +
   20.29 +#include "SDL_platform.h"
   20.30 +
   20.31 +/**
   20.32 + *  \file SDL_config_android.h
   20.33 + *  
   20.34 + *  This is a configuration that can be used to build SDL for Android
   20.35 + */
   20.36 +
   20.37 +#include <stdarg.h>
   20.38 +
   20.39 +/*
   20.40 +typedef signed char int8_t;
   20.41 +typedef unsigned char uint8_t;
   20.42 +typedef signed short int16_t;
   20.43 +typedef unsigned short uint16_t;
   20.44 +typedef signed int int32_t;
   20.45 +typedef unsigned int uint32_t;
   20.46 +*/
   20.47 +
   20.48 +
   20.49 +#define HAVE_ALLOCA_H		1
   20.50 +#define HAVE_SYS_TYPES_H	1
   20.51 +#define HAVE_STDIO_H	1
   20.52 +#define STDC_HEADERS	1
   20.53 +#define HAVE_STRING_H	1
   20.54 +#define HAVE_INTTYPES_H	1
   20.55 +#define HAVE_STDINT_H	1
   20.56 +#define HAVE_CTYPE_H	1
   20.57 +#define HAVE_MATH_H	1
   20.58 +#define HAVE_SIGNAL_H	1
   20.59 +
   20.60 +/* C library functions */
   20.61 +#define HAVE_MALLOC	1
   20.62 +#define HAVE_CALLOC	1
   20.63 +#define HAVE_REALLOC	1
   20.64 +#define HAVE_FREE	1
   20.65 +#define HAVE_ALLOCA	1
   20.66 +#define HAVE_GETENV	1
   20.67 +#define HAVE_SETENV	1
   20.68 +#define HAVE_PUTENV	1
   20.69 +#define HAVE_SETENV	1
   20.70 +#define HAVE_UNSETENV	1
   20.71 +#define HAVE_QSORT	1
   20.72 +#define HAVE_ABS	1
   20.73 +#define HAVE_BCOPY	1
   20.74 +#define HAVE_MEMSET	1
   20.75 +#define HAVE_MEMCPY	1
   20.76 +#define HAVE_MEMMOVE	1
   20.77 +#define HAVE_MEMCMP	1
   20.78 +#define HAVE_STRLEN	1
   20.79 +#define HAVE_STRLCPY	1
   20.80 +#define HAVE_STRLCAT	1
   20.81 +#define HAVE_STRDUP	1
   20.82 +#define HAVE_STRCHR	1
   20.83 +#define HAVE_STRRCHR	1
   20.84 +#define HAVE_STRSTR	1
   20.85 +#define HAVE_STRTOL	1
   20.86 +#define HAVE_STRTOUL	1
   20.87 +#define HAVE_STRTOLL	1
   20.88 +#define HAVE_STRTOULL	1
   20.89 +#define HAVE_STRTOD	1
   20.90 +#define HAVE_ATOI	1
   20.91 +#define HAVE_ATOF	1
   20.92 +#define HAVE_STRCMP	1
   20.93 +#define HAVE_STRNCMP	1
   20.94 +#define HAVE_STRCASECMP	1
   20.95 +#define HAVE_STRNCASECMP 1
   20.96 +#define HAVE_SSCANF	1
   20.97 +#define HAVE_SNPRINTF	1
   20.98 +#define HAVE_VSNPRINTF	1
   20.99 +#define HAVE_CEIL	1
  20.100 +#define HAVE_COPYSIGN	1
  20.101 +#define HAVE_COS	1
  20.102 +#define HAVE_COSF	1
  20.103 +#define HAVE_FABS	1
  20.104 +#define HAVE_FLOOR	1
  20.105 +#define HAVE_LOG	1
  20.106 +#define HAVE_POW	1
  20.107 +#define HAVE_SCALBN	1
  20.108 +#define HAVE_SIN	1
  20.109 +#define HAVE_SINF	1
  20.110 +#define HAVE_SQRT	1
  20.111 +#define HAVE_SIGACTION	1
  20.112 +#define HAVE_SETJMP	1
  20.113 +#define HAVE_NANOSLEEP	1
  20.114 +#define HAVE_SYSCONF	1
  20.115 +
  20.116 +#define SIZEOF_VOIDP 4
  20.117 +
  20.118 +typedef unsigned int size_t;
  20.119 +//typedef unsigned long uintptr_t;
  20.120 +
  20.121 +#define SDL_AUDIO_DRIVER_ANDROID	1
  20.122 +
  20.123 +#define SDL_CDROM_DISABLED 1
  20.124 +
  20.125 +#define SDL_HAPTIC_DISABLED 1
  20.126 +
  20.127 +#define SDL_JOYSTICK_ANDROID 1
  20.128 +
  20.129 +#define SDL_LOADSO_DISABLED 1
  20.130 +
  20.131 +/* Enable various threading systems */
  20.132 +#define SDL_THREAD_PTHREAD	1
  20.133 +#define SDL_THREAD_PTHREAD_RECURSIVE_MUTEX	1
  20.134 +
  20.135 +/* Enable various timer systems */
  20.136 +#define SDL_TIMER_UNIX	1
  20.137 +
  20.138 +#define SDL_VIDEO_DRIVER_ANDROID 1
  20.139 +
  20.140 +#define HAVE_STDIO_H 1
  20.141 +#define HAVE_SYS_TYPES_H 1
  20.142 +
  20.143 +#define HAVE_M_PI 1
  20.144 +
  20.145 +#define SDL_VIDEO_RENDER_OGL_ES 1
  20.146 +
  20.147 +#endif /* _SDL_config_minimal_h */
  20.148 +
    21.1 --- a/include/SDL_platform.h	Sun Aug 22 12:07:55 2010 -0700
    21.2 +++ b/include/SDL_platform.h	Sun Aug 22 12:23:55 2010 -0700
    21.3 @@ -65,6 +65,11 @@
    21.4  #undef __LINUX__
    21.5  #define __LINUX__	1
    21.6  #endif
    21.7 +#if defined(ANDROID)
    21.8 +#undef __ANDROID__
    21.9 +#undef __LINUX__ /*do we need to do this?*/
   21.10 +#define __ANDROID__ 1
   21.11 +#endif
   21.12  
   21.13  #if defined(__APPLE__)
   21.14  /* lets us know what version of Mac OS X we're compiling on */
    22.1 --- a/include/SDL_stdinc.h	Sun Aug 22 12:07:55 2010 -0700
    22.2 +++ b/include/SDL_stdinc.h	Sun Aug 22 12:23:55 2010 -0700
    22.3 @@ -174,9 +174,10 @@
    22.4  SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
    22.5  SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
    22.6  SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
    22.7 -#ifndef __NINTENDODS__          /* TODO: figure out why the following happens:
    22.8 -                                   include/SDL_stdinc.h:150: error: size of array 'SDL_dummy_uint64' is negative
    22.9 -                                   include/SDL_stdinc.h:151: error: size of array 'SDL_dummy_sint64' is negative */
   22.10 +#if !defined(__NINTENDODS__) && !defined(__ANDROID__)
   22.11 +/* TODO: figure out why the following happens:
   22.12 + include/SDL_stdinc.h:150: error: size of array 'SDL_dummy_uint64' is negative
   22.13 + include/SDL_stdinc.h:151: error: size of array 'SDL_dummy_sint64' is negative */
   22.14  SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
   22.15  SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
   22.16  #endif
   22.17 @@ -195,7 +196,8 @@
   22.18  
   22.19  /** \cond */
   22.20  #ifndef DOXYGEN_SHOULD_IGNORE_THIS
   22.21 -#ifndef __NINTENDODS__          /* TODO: include/SDL_stdinc.h:174: error: size of array 'SDL_dummy_enum' is negative */
   22.22 +#if !defined(__NINTENDODS__) && !defined(__ANDROID__) 
   22.23 +   /* TODO: include/SDL_stdinc.h:174: error: size of array 'SDL_dummy_enum' is negative */
   22.24  typedef enum
   22.25  {
   22.26      DUMMY_ENUM_VALUE
    23.1 --- a/src/SDL_compat.c	Sun Aug 22 12:07:55 2010 -0700
    23.2 +++ b/src/SDL_compat.c	Sun Aug 22 12:23:55 2010 -0700
    23.3 @@ -639,18 +639,29 @@
    23.4  
    23.5      /* If we're in OpenGL mode, just create a stub surface and we're done! */
    23.6      if (flags & SDL_OPENGL) {
    23.7 +        
    23.8 +       printf("1\n");
    23.9 +    
   23.10          SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow);
   23.11          if (!SDL_VideoContext) {
   23.12              return NULL;
   23.13          }
   23.14 +
   23.15 +        
   23.16 +    printf("2\n");
   23.17 +    
   23.18          if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) {
   23.19              return NULL;
   23.20          }
   23.21 +        
   23.22 +    printf("3\n");
   23.23          SDL_VideoSurface =
   23.24              SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
   23.25          if (!SDL_VideoSurface) {
   23.26              return NULL;
   23.27          }
   23.28 +        
   23.29 +    printf("4\n");
   23.30          SDL_VideoSurface->flags |= surface_flags;
   23.31          SDL_PublicSurface = SDL_VideoSurface;
   23.32          return SDL_PublicSurface;
    24.1 --- a/src/audio/SDL_audio.c	Sun Aug 22 12:07:55 2010 -0700
    24.2 +++ b/src/audio/SDL_audio.c	Sun Aug 22 12:23:55 2010 -0700
    24.3 @@ -69,6 +69,7 @@
    24.4  extern AudioBootStrap DART_bootstrap;
    24.5  extern AudioBootStrap NDSAUD_bootstrap;
    24.6  extern AudioBootStrap FUSIONSOUND_bootstrap;
    24.7 +extern AudioBootStrap ANDROIDAUD_bootstrap;
    24.8  
    24.9  
   24.10  /* Available audio drivers */
   24.11 @@ -137,6 +138,9 @@
   24.12  #if SDL_AUDIO_DRIVER_FUSIONSOUND
   24.13      &FUSIONSOUND_bootstrap,
   24.14  #endif
   24.15 +#if SDL_AUDIO_DRIVER_ANDROID
   24.16 +    &ANDROIDAUD_bootstrap,
   24.17 +#endif
   24.18      NULL
   24.19  };
   24.20  
   24.21 @@ -318,6 +322,8 @@
   24.22  }
   24.23  
   24.24  
   24.25 +#include <android/log.h>
   24.26 +
   24.27  /* The general mixing thread function */
   24.28  int SDLCALL
   24.29  SDL_RunAudio(void *devicep)
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/src/audio/android/SDL_androidaudio.c	Sun Aug 22 12:23:55 2010 -0700
    25.3 @@ -0,0 +1,140 @@
    25.4 +/*
    25.5 +    SDL - Simple DirectMedia Layer
    25.6 +    Copyright (C) 1997-2010 Sam Lantinga
    25.7 +
    25.8 +    This library is free software; you can redistribute it and/or
    25.9 +    modify it under the terms of the GNU Lesser General Public
   25.10 +    License as published by the Free Software Foundation; either
   25.11 +    version 2.1 of the License, or (at your option) any later version.
   25.12 +
   25.13 +    This library is distributed in the hope that it will be useful,
   25.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   25.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   25.16 +    Lesser General Public License for more details.
   25.17 +
   25.18 +    You should have received a copy of the GNU Lesser General Public
   25.19 +    License along with this library; if not, write to the Free Software
   25.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   25.21 +
   25.22 +    Sam Lantinga
   25.23 +    slouken@libsdl.org
   25.24 +
   25.25 +    This file written by Ryan C. Gordon (icculus@icculus.org)
   25.26 +*/
   25.27 +#include "SDL_config.h"
   25.28 +
   25.29 +/* Output audio to Android */
   25.30 +
   25.31 +#include "SDL_audio.h"
   25.32 +#include "../SDL_audio_c.h"
   25.33 +#include "SDL_androidaudio.h"
   25.34 +
   25.35 +extern void Android_UpdateAudioBuffer(unsigned char *buf, int len);
   25.36 +
   25.37 +#include <android/log.h>
   25.38 +
   25.39 +static int
   25.40 +AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
   25.41 +{
   25.42 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   25.43 +    int valid_datatype = 0;
   25.44 +    
   25.45 +    //TODO: Sample rates etc
   25.46 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "AndroidAudio Open\n");
   25.47 +
   25.48 +    this->hidden = SDL_malloc(sizeof(*(this->hidden)));
   25.49 +    if (!this->hidden) {
   25.50 +        SDL_OutOfMemory();
   25.51 +        return 0;
   25.52 +    }
   25.53 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   25.54 +
   25.55 +    while ((!valid_datatype) && (test_format)) {
   25.56 +        this->spec.format = test_format;
   25.57 +        switch (test_format) {
   25.58 +        case AUDIO_S8:
   25.59 +            /*case AUDIO_S16LSB: */
   25.60 +            valid_datatype = 1;
   25.61 +            break;
   25.62 +        default:
   25.63 +            test_format = SDL_NextAudioFormat();
   25.64 +            break;
   25.65 +        }
   25.66 +    }
   25.67 +    
   25.68 +    return 1;
   25.69 +}
   25.70 +
   25.71 +static void
   25.72 +AndroidAUD_PlayDevice(_THIS)
   25.73 +{
   25.74 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "AndroidAudio Play\n");
   25.75 +    
   25.76 +
   25.77 +    //playGenericSound(this->hidden->mixbuf, this->hidden->mixlen);
   25.78 +    
   25.79 +#if 0
   25.80 +
   25.81 +//    sound->rate = 22050; /* sample rate = 22050Hz */
   25.82 +//    sound->vol = 127;    /* volume [0..127] for [min..max] */
   25.83 +//    sound->pan = 64;     /* balance [0..127] for [left..right] */
   25.84 +//    sound->format = 0;   /* 0 for 16-bit, 1 for 8-bit */
   25.85 +//    playSound(sound);
   25.86 +#endif
   25.87 +}
   25.88 +
   25.89 +
   25.90 +static Uint8 *
   25.91 +AndroidAUD_GetDeviceBuf(_THIS)
   25.92 +{
   25.93 +     //__android_log_print(ANDROID_LOG_INFO, "SDL", "****** get device buf\n");
   25.94 +
   25.95 +     
   25.96 +    //    sound->data = this->hidden->mixbuf;/* pointer to raw audio data */
   25.97 +//    sound->len = this->hidden->mixlen; /* size of raw data pointed to above */
   25.98 +
   25.99 +
  25.100 +    Android_UpdateAudioBuffer(this->hidden->mixbuf, this->hidden->mixlen);
  25.101 +    
  25.102 +    return this->hidden->mixbuf;        /* is this right? */
  25.103 +}
  25.104 +
  25.105 +static void
  25.106 +AndroidAUD_WaitDevice(_THIS)
  25.107 +{
  25.108 +    /* stub */
  25.109 +     __android_log_print(ANDROID_LOG_INFO, "SDL", "****** wait device buf\n");
  25.110 +}
  25.111 +
  25.112 +static void
  25.113 +AndroidAUD_CloseDevice(_THIS)
  25.114 +{
  25.115 +    /* stub */
  25.116 +     __android_log_print(ANDROID_LOG_INFO, "SDL", "****** close device buf\n");
  25.117 +}
  25.118 +
  25.119 +static int
  25.120 +AndroidAUD_Init(SDL_AudioDriverImpl * impl)
  25.121 +{
  25.122 +    /* Set the function pointers */
  25.123 +    impl->OpenDevice = AndroidAUD_OpenDevice;
  25.124 +    impl->PlayDevice = AndroidAUD_PlayDevice;
  25.125 +    impl->WaitDevice = AndroidAUD_WaitDevice;
  25.126 +    impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf;
  25.127 +    impl->CloseDevice = AndroidAUD_CloseDevice;
  25.128 +
  25.129 +    /* and the capabilities */
  25.130 +    impl->HasCaptureSupport = 0; //TODO
  25.131 +    impl->OnlyHasDefaultOutputDevice = 1;
  25.132 +    impl->OnlyHasDefaultInputDevice = 1;
  25.133 +
  25.134 +    __android_log_print(ANDROID_LOG_INFO, "SDL","Audio init\n");
  25.135 +
  25.136 +    return 1;   /* this audio target is available. */
  25.137 +}
  25.138 +
  25.139 +AudioBootStrap ANDROIDAUD_bootstrap = {
  25.140 +    "android", "SDL Android audio driver", AndroidAUD_Init, 0       /*1? */
  25.141 +};
  25.142 +
  25.143 +/* vi: set ts=4 sw=4 expandtab: */
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/src/audio/android/SDL_androidaudio.h	Sun Aug 22 12:23:55 2010 -0700
    26.3 @@ -0,0 +1,42 @@
    26.4 +/*
    26.5 +    SDL - Simple DirectMedia Layer
    26.6 +    Copyright (C) 1997-2010 Sam Lantinga
    26.7 +
    26.8 +    This library is free software; you can redistribute it and/or
    26.9 +    modify it under the terms of the GNU Lesser General Public
   26.10 +    License as published by the Free Software Foundation; either
   26.11 +    version 2.1 of the License, or (at your option) any later version.
   26.12 +
   26.13 +    This library is distributed in the hope that it will be useful,
   26.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   26.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   26.16 +    Lesser General Public License for more details.
   26.17 +
   26.18 +    You should have received a copy of the GNU Lesser General Public
   26.19 +    License along with this library; if not, write to the Free Software
   26.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   26.21 +
   26.22 +    Sam Lantinga
   26.23 +    slouken@libsdl.org
   26.24 +*/
   26.25 +#include "SDL_config.h"
   26.26 +
   26.27 +#ifndef _SDL_androidaudio_h
   26.28 +#define _SDL_androidaudio_h
   26.29 +
   26.30 +#include "../SDL_sysaudio.h"
   26.31 +
   26.32 +/* Hidden "this" pointer for the audio functions */
   26.33 +#define _THIS	SDL_AudioDevice *this
   26.34 +
   26.35 +struct SDL_PrivateAudioData
   26.36 +{
   26.37 +    /* The file descriptor for the audio device */
   26.38 +    Uint8 *mixbuf;
   26.39 +    Uint32 mixlen;
   26.40 +    Uint32 write_delay;
   26.41 +    Uint32 initial_calls;
   26.42 +};
   26.43 +
   26.44 +#endif /* _SDL_androidaudio_h */
   26.45 +/* vi: set ts=4 sw=4 expandtab: */
    27.1 --- a/src/events/SDL_events.c	Sun Aug 22 12:07:55 2010 -0700
    27.2 +++ b/src/events/SDL_events.c	Sun Aug 22 12:23:55 2010 -0700
    27.3 @@ -92,11 +92,13 @@
    27.4  static __inline__ SDL_bool
    27.5  SDL_ShouldPollJoystick()
    27.6  {
    27.7 +#if !SDL_JOYSTICK_DISABLED
    27.8      if (SDL_numjoysticks &&
    27.9          (!SDL_disabled_events[SDL_JOYAXISMOTION >> 8] ||
   27.10           SDL_JoystickEventState(SDL_QUERY))) {
   27.11          return SDL_TRUE;
   27.12      }
   27.13 +#endif
   27.14      return SDL_FALSE;
   27.15  }
   27.16  
    28.1 --- a/src/events/SDL_keyboard.c	Sun Aug 22 12:07:55 2010 -0700
    28.2 +++ b/src/events/SDL_keyboard.c	Sun Aug 22 12:23:55 2010 -0700
    28.3 @@ -729,7 +729,7 @@
    28.4          break;
    28.5      default:
    28.6          /* Invalid state -- bail */
    28.7 -        return 0;
    28.8 +        return 2;
    28.9      }
   28.10  
   28.11      /* Drop events that don't change state */
   28.12 @@ -738,14 +738,14 @@
   28.13  #if 0
   28.14          printf("Keyboard event didn't change state - dropped!\n");
   28.15  #endif
   28.16 -        return 0;
   28.17 +        return 3;
   28.18      }
   28.19  
   28.20      /* Update internal keyboard state */
   28.21      keyboard->keystate[scancode] = state;
   28.22  
   28.23      /* Post the event, if desired */
   28.24 -    posted = 0;
   28.25 +    posted = 4;
   28.26      if (SDL_GetEventState(type) == SDL_ENABLE) {
   28.27          SDL_Event event;
   28.28          event.key.type = type;
    29.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Sun Aug 22 12:23:55 2010 -0700
    29.3 @@ -0,0 +1,106 @@
    29.4 +/*
    29.5 +    SDL - Simple DirectMedia Layer
    29.6 +    Copyright (C) 1997-2010 Sam Lantinga
    29.7 +
    29.8 +    This library is free software; you can redistribute it and/or
    29.9 +    modify it under the terms of the GNU Lesser General Public
   29.10 +    License as published by the Free Software Foundation; either
   29.11 +    version 2.1 of the License, or (at your option) any later version.
   29.12 +
   29.13 +    This library is distributed in the hope that it will be useful,
   29.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   29.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   29.16 +    Lesser General Public License for more details.
   29.17 +
   29.18 +    You should have received a copy of the GNU Lesser General Public
   29.19 +    License along with this library; if not, write to the Free Software
   29.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   29.21 +
   29.22 +    Sam Lantinga
   29.23 +    slouken@libsdl.org
   29.24 +*/
   29.25 +
   29.26 +#include "SDL_config.h"
   29.27 +
   29.28 +#ifdef SDL_JOYSTICK_ANDROID
   29.29 +
   29.30 +/* This is the system specific header for the SDL joystick API */
   29.31 +#include <stdio.h>              /* For the definition of NULL */
   29.32 +
   29.33 +#include "SDL_error.h"
   29.34 +#include "SDL_events.h"
   29.35 +#include "SDL_joystick.h"
   29.36 +#include "../SDL_sysjoystick.h"
   29.37 +#include "../SDL_joystick_c.h"
   29.38 +
   29.39 +extern float fLastAccelerometer[3];
   29.40 +
   29.41 +const char *accelerometerName = "Android accelerometer";
   29.42 +
   29.43 +/* Function to scan the system for joysticks.
   29.44 + * This function should set SDL_numjoysticks to the number of available
   29.45 + * joysticks.  Joystick 0 should be the system default joystick.
   29.46 + * It should return 0, or -1 on an unrecoverable fatal error.
   29.47 + */
   29.48 +int
   29.49 +SDL_SYS_JoystickInit(void)
   29.50 +{
   29.51 +    SDL_numjoysticks = 1;
   29.52 +    
   29.53 +return (1);
   29.54 +}
   29.55 +
   29.56 +/* Function to get the device-dependent name of a joystick */
   29.57 +const char *
   29.58 +SDL_SYS_JoystickName(int index)
   29.59 +{
   29.60 +    if (!index)
   29.61 +        return accelerometerName;
   29.62 +    SDL_SetError("No joystick available with that index");
   29.63 +    return (NULL);
   29.64 +}
   29.65 +
   29.66 +/* Function to open a joystick for use.
   29.67 +   The joystick to open is specified by the index field of the joystick.
   29.68 +   This should fill the nbuttons and naxes fields of the joystick structure.
   29.69 +   It returns 0, or -1 if there is an error.
   29.70 + */
   29.71 +int
   29.72 +SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
   29.73 +{
   29.74 +    joystick->nbuttons = 0;
   29.75 +    joystick->nhats = 0;
   29.76 +    joystick->nballs = 0;
   29.77 +    joystick->naxes = 3;
   29.78 +    joystick->name = accelerometerName;
   29.79 +    return 0;
   29.80 +}
   29.81 +
   29.82 +
   29.83 +/* Function to update the state of a joystick - called as a device poll.
   29.84 + * This function shouldn't update the joystick structure directly,
   29.85 + * but instead should call SDL_PrivateJoystick*() to deliver events
   29.86 + * and update joystick device state.
   29.87 + */
   29.88 +    void
   29.89 +SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   29.90 +{
   29.91 +    int i=0; 
   29.92 +    for(i=0;i<3;i++){
   29.93 +        SDL_PrivateJoystickAxis(joystick, i, fLastAccelerometer[i]);
   29.94 +    }
   29.95 +}
   29.96 +
   29.97 +/* Function to close a joystick after use */
   29.98 +void
   29.99 +SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  29.100 +{
  29.101 +}
  29.102 +
  29.103 +/* Function to perform any system-specific joystick related cleanup */
  29.104 +void
  29.105 +SDL_SYS_JoystickQuit(void)
  29.106 +{
  29.107 +}
  29.108 +
  29.109 +#endif /* SDL_JOYSTICK_NDS */
    30.1 --- a/src/video/SDL_sysvideo.h	Sun Aug 22 12:07:55 2010 -0700
    30.2 +++ b/src/video/SDL_sysvideo.h	Sun Aug 22 12:23:55 2010 -0700
    30.3 @@ -417,6 +417,9 @@
    30.4  #if SDL_VIDEO_DRIVER_PANDORA
    30.5  extern VideoBootStrap PND_bootstrap;
    30.6  #endif
    30.7 +#if SDL_VIDEO_DRIVER_ANDROID
    30.8 +extern VideoBootStrap Android_bootstrap;
    30.9 +#endif
   30.10  
   30.11  #define SDL_CurrentDisplay	(&_this->displays[_this->current_display])
   30.12  #define SDL_CurrentRenderer	(SDL_CurrentDisplay->current_renderer)
    31.1 --- a/src/video/SDL_video.c	Sun Aug 22 12:07:55 2010 -0700
    31.2 +++ b/src/video/SDL_video.c	Sun Aug 22 12:23:55 2010 -0700
    31.3 @@ -97,6 +97,9 @@
    31.4  #if SDL_VIDEO_DRIVER_PANDORA
    31.5      &PND_bootstrap,
    31.6  #endif
    31.7 +#if SDL_VIDEO_DRIVER_ANDROID
    31.8 +    &Android_bootstrap,
    31.9 +#endif
   31.10      NULL
   31.11  };
   31.12  
    32.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.2 +++ b/src/video/android/SDL_androidevents.c	Sun Aug 22 12:23:55 2010 -0700
    32.3 @@ -0,0 +1,85 @@
    32.4 +/*
    32.5 +    SDL - Simple DirectMedia Layer
    32.6 +    Copyright (C) 1997-2010 Sam Lantinga
    32.7 +
    32.8 +    This library is free software; you can redistribute it and/or
    32.9 +    modify it under the terms of the GNU Lesser General Public
   32.10 +    License as published by the Free Software Foundation; either
   32.11 +    version 2.1 of the License, or (at your option) any later version.
   32.12 +
   32.13 +    This library is distributed in the hope that it will be useful,
   32.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   32.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   32.16 +    Lesser General Public License for more details.
   32.17 +
   32.18 +    You should have received a copy of the GNU Lesser General Public
   32.19 +    License along with this library; if not, write to the Free Software
   32.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   32.21 +
   32.22 +    Sam Lantinga
   32.23 +    slouken@libsdl.org
   32.24 +*/
   32.25 +#include "SDL_config.h"
   32.26 +
   32.27 +/* Being a null driver, there's no event stream. We just define stubs for
   32.28 +   most of the API. */
   32.29 +
   32.30 +#include <stdio.h>
   32.31 +#include <stdlib.h>
   32.32 +
   32.33 +#include "../../events/SDL_sysevents.h"
   32.34 +#include "../../events/SDL_events_c.h"
   32.35 +
   32.36 +#include "SDL_androidevents.h"
   32.37 +
   32.38 +void Android_InitEvents(){
   32.39 +
   32.40 +    SDL_Keyboard keyboard;
   32.41 +
   32.42 +    SDL_zero(keyboard);
   32.43 +    SDL_AddKeyboard(&keyboard, -1);
   32.44 +
   32.45 +    SDLKey keymap[SDL_NUM_SCANCODES];
   32.46 +
   32.47 +    /* Add default scancode to key mapping */
   32.48 +    SDL_GetDefaultKeymap(keymap);
   32.49 +    SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
   32.50 +
   32.51 +
   32.52 +}
   32.53 +
   32.54 +void
   32.55 +Android_PumpEvents(_THIS)
   32.56 +{
   32.57 +
   32.58 +    //scanKeys();
   32.59 +    /* TODO: defer click-age */
   32.60 +    /*
   32.61 +    if (keysDown() & KEY_TOUCH) {
   32.62 +        SDL_SendMouseButton(0, SDL_PRESSED, 0);
   32.63 +    } else if (keysUp() & KEY_TOUCH) {
   32.64 +        SDL_SendMouseButton(0, SDL_RELEASED, 0);
   32.65 +    }
   32.66 +    if (keysHeld() & KEY_TOUCH) {
   32.67 +        touchPosition t = touchReadXY();
   32.68 +        SDL_SendMouseMotion(0, 0, t.px, t.py, 1);       
   32.69 +    }
   32.70 +    */
   32.71 +}
   32.72 +
   32.73 +
   32.74 +void Android_OnResize(int width, int height, int format){
   32.75 +
   32.76 +}
   32.77 +
   32.78 +int
   32.79 +Android_OnKeyDown(int keycode){
   32.80 +    return SDL_SendKeyboardKey(0, SDL_PRESSED, (SDL_scancode)keycode);
   32.81 +}
   32.82 +
   32.83 +int
   32.84 +Android_OnKeyUp(int keycode){
   32.85 +    return SDL_SendKeyboardKey(0, SDL_RELEASED, (SDL_scancode)keycode);
   32.86 +}
   32.87 +
   32.88 +/* vi: set ts=4 sw=4 expandtab: */
    33.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.2 +++ b/src/video/android/SDL_androidevents.h	Sun Aug 22 12:23:55 2010 -0700
    33.3 @@ -0,0 +1,29 @@
    33.4 +/*
    33.5 +    SDL - Simple DirectMedia Layer
    33.6 +    Copyright (C) 1997-2010 Sam Lantinga
    33.7 +
    33.8 +    This library is free software; you can redistribute it and/or
    33.9 +    modify it under the terms of the GNU Lesser General Public
   33.10 +    License as published by the Free Software Foundation; either
   33.11 +    version 2.1 of the License, or (at your option) any later version.
   33.12 +
   33.13 +    This library is distributed in the hope that it will be useful,
   33.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   33.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   33.16 +    Lesser General Public License for more details.
   33.17 +
   33.18 +    You should have received a copy of the GNU Lesser General Public
   33.19 +    License along with this library; if not, write to the Free Software
   33.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   33.21 +
   33.22 +    Sam Lantinga
   33.23 +    slouken@libsdl.org
   33.24 +*/
   33.25 +#include "SDL_config.h"
   33.26 +
   33.27 +#include "SDL_androidvideo.h"
   33.28 +
   33.29 +extern void Android_PumpEvents(_THIS);
   33.30 +extern void Android_InitEvents();
   33.31 +
   33.32 +/* vi: set ts=4 sw=4 expandtab: */
    34.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.2 +++ b/src/video/android/SDL_androidgl.c	Sun Aug 22 12:23:55 2010 -0700
    34.3 @@ -0,0 +1,96 @@
    34.4 +/*
    34.5 +    SDL - Simple DirectMedia Layer
    34.6 +    Copyright (C) 1997-2010 Sam Lantinga
    34.7 +
    34.8 +    This library is free software; you can redistribute it and/or
    34.9 +    modify it under the terms of the GNU Lesser General Public
   34.10 +    License as published by the Free Software Foundation; either
   34.11 +    version 2.1 of the License, or (at your option) any later version.
   34.12 +
   34.13 +    This library is distributed in the hope that it will be useful,
   34.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   34.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   34.16 +    Lesser General Public License for more details.
   34.17 +
   34.18 +    You should have received a copy of the GNU Lesser General Public
   34.19 +    License along with this library; if not, write to the Free Software
   34.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   34.21 +
   34.22 +    Sam Lantinga
   34.23 +    slouken@libsdl.org
   34.24 +*/
   34.25 +#include "SDL_config.h"
   34.26 +
   34.27 +/* Android SDL video driver implementation
   34.28 +*/
   34.29 +
   34.30 +#include "SDL_video.h"
   34.31 +#include "SDL_mouse.h"
   34.32 +#include "../SDL_sysvideo.h"
   34.33 +#include "../SDL_pixels_c.h"
   34.34 +#include "../../events/SDL_events_c.h"
   34.35 +
   34.36 +#include "SDL_androidvideo.h"
   34.37 +#include "SDL_androidevents.h"
   34.38 +#include "SDL_androidrender.h"
   34.39 +
   34.40 +#include <android/log.h>
   34.41 +
   34.42 +#include <pthread.h>
   34.43 +
   34.44 +/*
   34.45 +These things are in the JNI android support
   34.46 +*/
   34.47 +extern void Android_CreateContext();
   34.48 +extern void Android_Render();
   34.49 +
   34.50 +/* GL functions */
   34.51 +int Android_GL_LoadLibrary(_THIS, const char *path){
   34.52 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_LoadLibrary\n");
   34.53 +	return 0;
   34.54 +}
   34.55 +
   34.56 +void *Android_GL_GetProcAddress(_THIS, const char *proc){
   34.57 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_GetProcAddress\n");
   34.58 +	return 0;
   34.59 +}
   34.60 +
   34.61 +void Android_GL_UnloadLibrary(_THIS){
   34.62 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_UnloadLibrary\n");
   34.63 +}
   34.64 +
   34.65 +/*
   34.66 +int *Android_GL_GetVisual(_THIS, Display * display, int screen){
   34.67 +	__android_log_print(ANDROID_LOG_INFO, "SDL","[STUB] GL_GetVisual\n");
   34.68 +	return 0;
   34.69 +}
   34.70 +*/
   34.71 +
   34.72 +SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window){
   34.73 +	Android_CreateContext();
   34.74 +	return 1;
   34.75 +}
   34.76 +
   34.77 +int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
   34.78 +                              SDL_GLContext context){
   34.79 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_MakeCurrent\n");
   34.80 +	return 0;
   34.81 +}
   34.82 +
   34.83 +int Android_GL_SetSwapInterval(_THIS, int interval){
   34.84 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_SetSwapInterval\n");
   34.85 +	return 0;
   34.86 +}
   34.87 +
   34.88 +int Android_GL_GetSwapInterval(_THIS){
   34.89 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_GetSwapInterval\n");
   34.90 +	return 0;
   34.91 +}
   34.92 +
   34.93 +void Android_GL_SwapWindow(_THIS, SDL_Window * window){
   34.94 +	Android_Render();
   34.95 +}
   34.96 +
   34.97 +void Android_GL_DeleteContext(_THIS, SDL_GLContext context){
   34.98 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_DeleteContext\n");
   34.99 +}
    35.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.2 +++ b/src/video/android/SDL_androidrender.c	Sun Aug 22 12:23:55 2010 -0700
    35.3 @@ -0,0 +1,344 @@
    35.4 +/*
    35.5 +    SDL - Simple DirectMedia Layer
    35.6 +    Copyright (C) 1997-2010 Sam Lantinga
    35.7 +
    35.8 +    This library is free software; you can redistribute it and/or
    35.9 +    modify it under the terms of the GNU Lesser General Public
   35.10 +    License as published by the Free Software Foundation; either
   35.11 +    version 2.1 of the License, or (at your option) any later version.
   35.12 +
   35.13 +    This library is distributed in the hope that it will be useful,
   35.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   35.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   35.16 +    Lesser General Public License for more details.
   35.17 +
   35.18 +    You should have received a copy of the GNU Lesser General Public
   35.19 +    License along with this library; if not, write to the Free Software
   35.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   35.21 +
   35.22 +    Sam Lantinga
   35.23 +    slouken@libsdl.org
   35.24 +*/
   35.25 +#include "SDL_config.h"
   35.26 +
   35.27 +#include "SDL_video.h"
   35.28 +#include "../SDL_sysvideo.h"
   35.29 +#include "../SDL_yuv_sw_c.h"
   35.30 +#include "../SDL_renderer_sw.h"
   35.31 +
   35.32 +
   35.33 +/* SDL surface based renderer implementation */
   35.34 +
   35.35 +static SDL_Renderer *Android_CreateRenderer(SDL_Window * window,
   35.36 +                                              Uint32 flags);
   35.37 +static int Android_RenderDrawPoints(SDL_Renderer * renderer,
   35.38 +                                      const SDL_Point * points, int count);
   35.39 +static int Android_RenderDrawLines(SDL_Renderer * renderer,
   35.40 +                                     const SDL_Point * points, int count);
   35.41 +static int Android_RenderDrawRects(SDL_Renderer * renderer,
   35.42 +                                     const SDL_Rect ** rects, int count);
   35.43 +static int Android_RenderFillRects(SDL_Renderer * renderer,
   35.44 +                                     const SDL_Rect ** rects, int count);
   35.45 +static int Android_RenderCopy(SDL_Renderer * renderer,
   35.46 +                                SDL_Texture * texture,
   35.47 +                                const SDL_Rect * srcrect,
   35.48 +                                const SDL_Rect * dstrect);
   35.49 +static int Android_RenderReadPixels(SDL_Renderer * renderer,
   35.50 +                                      const SDL_Rect * rect,
   35.51 +                                      Uint32 format,
   35.52 +                                      void * pixels, int pitch);
   35.53 +static int Android_RenderWritePixels(SDL_Renderer * renderer,
   35.54 +                                       const SDL_Rect * rect,
   35.55 +                                       Uint32 format,
   35.56 +                                       const void * pixels, int pitch);
   35.57 +static void Android_RenderPresent(SDL_Renderer * renderer);
   35.58 +static void Android_DestroyRenderer(SDL_Renderer * renderer);
   35.59 +
   35.60 +
   35.61 +SDL_RenderDriver Android_RenderDriver = {
   35.62 +    Android_CreateRenderer,
   35.63 +    {
   35.64 +     "dummy",
   35.65 +     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
   35.66 +      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
   35.67 +      SDL_RENDERER_PRESENTDISCARD),
   35.68 +     }
   35.69 +};
   35.70 +
   35.71 +typedef struct
   35.72 +{
   35.73 +    int current_screen;
   35.74 +    SDL_Surface *screens[3];
   35.75 +} Android_RenderData;
   35.76 +
   35.77 +SDL_Renderer *
   35.78 +Android_CreateRenderer(SDL_Window * window, Uint32 flags)
   35.79 +{
   35.80 +    SDL_VideoDisplay *display = window->display;
   35.81 +    SDL_DisplayMode *displayMode = &display->current_mode;
   35.82 +    SDL_Renderer *renderer;
   35.83 +    Android_RenderData *data;
   35.84 +    int i, n;
   35.85 +    int bpp;
   35.86 +    Uint32 Rmask, Gmask, Bmask, Amask;
   35.87 +
   35.88 +    if (!SDL_PixelFormatEnumToMasks
   35.89 +        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   35.90 +        SDL_SetError("Unknown display format");
   35.91 +        return NULL;
   35.92 +    }
   35.93 +
   35.94 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   35.95 +    if (!renderer) {
   35.96 +        SDL_OutOfMemory();
   35.97 +        return NULL;
   35.98 +    }
   35.99 +
  35.100 +    data = (Android_RenderData *) SDL_malloc(sizeof(*data));
  35.101 +    if (!data) {
  35.102 +        Android_DestroyRenderer(renderer);
  35.103 +        SDL_OutOfMemory();
  35.104 +        return NULL;
  35.105 +    }
  35.106 +    SDL_zerop(data);
  35.107 +
  35.108 +    renderer->RenderDrawPoints = Android_RenderDrawPoints;
  35.109 +    renderer->RenderDrawLines = Android_RenderDrawLines;
  35.110 +    renderer->RenderDrawRects = Android_RenderDrawRects;
  35.111 +    renderer->RenderFillRects = Android_RenderFillRects;
  35.112 +    renderer->RenderCopy = Android_RenderCopy;
  35.113 +    renderer->RenderReadPixels = Android_RenderReadPixels;
  35.114 +    renderer->RenderWritePixels = Android_RenderWritePixels;
  35.115 +    renderer->RenderPresent = Android_RenderPresent;
  35.116 +    renderer->DestroyRenderer = Android_DestroyRenderer;
  35.117 +    renderer->info.name = Android_RenderDriver.info.name;
  35.118 +    renderer->info.flags = 0;
  35.119 +    renderer->window = window;
  35.120 +    renderer->driverdata = data;
  35.121 +    Setup_SoftwareRenderer(renderer);
  35.122 +
  35.123 +    if (flags & SDL_RENDERER_PRESENTFLIP2) {
  35.124 +        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
  35.125 +        n = 2;
  35.126 +    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
  35.127 +        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
  35.128 +        n = 3;
  35.129 +    } else {
  35.130 +        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
  35.131 +        n = 1;
  35.132 +    }
  35.133 +    for (i = 0; i < n; ++i) {
  35.134 +        data->screens[i] =
  35.135 +            SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
  35.136 +                                 Bmask, Amask);
  35.137 +        if (!data->screens[i]) {
  35.138 +            Android_DestroyRenderer(renderer);
  35.139 +            return NULL;
  35.140 +        }
  35.141 +        SDL_SetSurfacePalette(data->screens[i], display->palette);
  35.142 +    }
  35.143 +    data->current_screen = 0;
  35.144 +
  35.145 +    return renderer;
  35.146 +}
  35.147 +
  35.148 +static int
  35.149 +Android_RenderDrawPoints(SDL_Renderer * renderer,
  35.150 +                           const SDL_Point * points, int count)
  35.151 +{
  35.152 +    Android_RenderData *data =
  35.153 +        (Android_RenderData *) renderer->driverdata;
  35.154 +    SDL_Surface *target = data->screens[data->current_screen];
  35.155 +
  35.156 +    if (renderer->blendMode == SDL_BLENDMODE_NONE ||
  35.157 +        renderer->blendMode == SDL_BLENDMODE_MASK) {
  35.158 +        Uint32 color = SDL_MapRGBA(target->format,
  35.159 +                                   renderer->r, renderer->g, renderer->b,
  35.160 +                                   renderer->a);
  35.161 +
  35.162 +        return SDL_DrawPoints(target, points, count, color);
  35.163 +    } else {
  35.164 +        return SDL_BlendPoints(target, points, count, renderer->blendMode,
  35.165 +                               renderer->r, renderer->g, renderer->b,
  35.166 +                               renderer->a);
  35.167 +    }
  35.168 +}
  35.169 +
  35.170 +static int
  35.171 +Android_RenderDrawLines(SDL_Renderer * renderer,
  35.172 +                          const SDL_Point * points, int count)
  35.173 +{
  35.174 +    Android_RenderData *data =
  35.175 +        (Android_RenderData *) renderer->driverdata;
  35.176 +    SDL_Surface *target = data->screens[data->current_screen];
  35.177 +
  35.178 +    if (renderer->blendMode == SDL_BLENDMODE_NONE ||
  35.179 +        renderer->blendMode == SDL_BLENDMODE_MASK) {
  35.180 +        Uint32 color = SDL_MapRGBA(target->format,
  35.181 +                                   renderer->r, renderer->g, renderer->b,
  35.182 +                                   renderer->a);
  35.183 +
  35.184 +        return SDL_DrawLines(target, points, count, color);
  35.185 +    } else {
  35.186 +        return SDL_BlendLines(target, points, count, renderer->blendMode,
  35.187 +                              renderer->r, renderer->g, renderer->b,
  35.188 +                              renderer->a);
  35.189 +    }
  35.190 +}
  35.191 +
  35.192 +static int
  35.193 +Android_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
  35.194 +                          int count)
  35.195 +{
  35.196 +    Android_RenderData *data =
  35.197 +        (Android_RenderData *) renderer->driverdata;
  35.198 +    SDL_Surface *target = data->screens[data->current_screen];
  35.199 +
  35.200 +    if (renderer->blendMode == SDL_BLENDMODE_NONE ||
  35.201 +        renderer->blendMode == SDL_BLENDMODE_MASK) {
  35.202 +        Uint32 color = SDL_MapRGBA(target->format,
  35.203 +                                   renderer->r, renderer->g, renderer->b,
  35.204 +                                   renderer->a);
  35.205 +
  35.206 +        return SDL_DrawRects(target, rects, count, color);
  35.207 +    } else {
  35.208 +        return SDL_BlendRects(target, rects, count,
  35.209 +                              renderer->blendMode,
  35.210 +                              renderer->r, renderer->g, renderer->b,
  35.211 +                              renderer->a);
  35.212 +    }
  35.213 +}
  35.214 +
  35.215 +static int
  35.216 +Android_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
  35.217 +                          int count)
  35.218 +{
  35.219 +    Android_RenderData *data =
  35.220 +        (Android_RenderData *) renderer->driverdata;
  35.221 +    SDL_Surface *target = data->screens[data->current_screen];
  35.222 +
  35.223 +    if (renderer->blendMode == SDL_BLENDMODE_NONE ||
  35.224 +        renderer->blendMode == SDL_BLENDMODE_MASK) {
  35.225 +        Uint32 color = SDL_MapRGBA(target->format,
  35.226 +                                   renderer->r, renderer->g, renderer->b,
  35.227 +                                   renderer->a);
  35.228 +
  35.229 +        return SDL_FillRects(target, rects, count, color);
  35.230 +    } else {
  35.231 +        return SDL_BlendFillRects(target, rects, count,
  35.232 +                                  renderer->blendMode,
  35.233 +                                  renderer->r, renderer->g, renderer->b,
  35.234 +                                  renderer->a);
  35.235 +    }
  35.236 +}
  35.237 +
  35.238 +static int
  35.239 +Android_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  35.240 +                     const SDL_Rect * srcrect, const SDL_Rect * dstrect)
  35.241 +{
  35.242 +    Android_RenderData *data =
  35.243 +        (Android_RenderData *) renderer->driverdata;
  35.244 +    SDL_Window *window = renderer->window;
  35.245 +    SDL_VideoDisplay *display = window->display;
  35.246 +
  35.247 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
  35.248 +        SDL_Surface *target = data->screens[data->current_screen];
  35.249 +        void *pixels =
  35.250 +            (Uint8 *) target->pixels + dstrect->y * target->pitch +
  35.251 +            dstrect->x * target->format->BytesPerPixel;
  35.252 +        return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
  35.253 +                                   srcrect, display->current_mode.format,
  35.254 +                                   dstrect->w, dstrect->h, pixels,
  35.255 +                                   target->pitch);
  35.256 +    } else {
  35.257 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  35.258 +        SDL_Surface *target = data->screens[data->current_screen];
  35.259 +        SDL_Rect real_srcrect = *srcrect;
  35.260 +        SDL_Rect real_dstrect = *dstrect;
  35.261 +
  35.262 +        return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
  35.263 +    }
  35.264 +}
  35.265 +
  35.266 +static int
  35.267 +Android_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  35.268 +                           Uint32 format, void * pixels, int pitch)
  35.269 +{
  35.270 +    Android_RenderData *data =
  35.271 +        (Android_RenderData *) renderer->driverdata;
  35.272 +    SDL_Window *window = renderer->window;
  35.273 +    SDL_VideoDisplay *display = window->display;
  35.274 +    SDL_Surface *screen = data->screens[data->current_screen];
  35.275 +    Uint32 screen_format = display->current_mode.format;
  35.276 +    Uint8 *screen_pixels = (Uint8 *) screen->pixels +
  35.277 +                            rect->y * screen->pitch +
  35.278 +                            rect->x * screen->format->BytesPerPixel;
  35.279 +    int screen_pitch = screen->pitch;
  35.280 +
  35.281 +    return SDL_ConvertPixels(rect->w, rect->h,
  35.282 +                             screen_format, screen_pixels, screen_pitch,
  35.283 +                             format, pixels, pitch);
  35.284 +}
  35.285 +
  35.286 +static int
  35.287 +Android_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  35.288 +                            Uint32 format, const void * pixels, int pitch)
  35.289 +{
  35.290 +    Android_RenderData *data =
  35.291 +        (Android_RenderData *) renderer->driverdata;
  35.292 +    SDL_Window *window = renderer->window;
  35.293 +    SDL_VideoDisplay *display = window->display;
  35.294 +    SDL_Surface *screen = data->screens[data->current_screen];
  35.295 +    Uint32 screen_format = display->current_mode.format;
  35.296 +    Uint8 *screen_pixels = (Uint8 *) screen->pixels +
  35.297 +                            rect->y * screen->pitch +
  35.298 +                            rect->x * screen->format->BytesPerPixel;
  35.299 +    int screen_pitch = screen->pitch;
  35.300 +
  35.301 +    return SDL_ConvertPixels(rect->w, rect->h,
  35.302 +                             format, pixels, pitch,
  35.303 +                             screen_format, screen_pixels, screen_pitch);
  35.304 +}
  35.305 +
  35.306 +static void
  35.307 +Android_RenderPresent(SDL_Renderer * renderer)
  35.308 +{
  35.309 +    static int frame_number;
  35.310 +    Android_RenderData *data =
  35.311 +        (Android_RenderData *) renderer->driverdata;
  35.312 +
  35.313 +    /* Send the data to the display */
  35.314 +    if (SDL_getenv("SDL_VIDEO_DUMMY_SAVE_FRAMES")) {
  35.315 +        char file[128];
  35.316 +        SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
  35.317 +                     renderer->window->id, ++frame_number);
  35.318 +        SDL_SaveBMP(data->screens[data->current_screen], file);
  35.319 +    }
  35.320 +
  35.321 +    /* Update the flipping chain, if any */
  35.322 +    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
  35.323 +        data->current_screen = (data->current_screen + 1) % 2;
  35.324 +    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
  35.325 +        data->current_screen = (data->current_screen + 1) % 3;
  35.326 +    }
  35.327 +}
  35.328 +
  35.329 +static void
  35.330 +Android_DestroyRenderer(SDL_Renderer * renderer)
  35.331 +{
  35.332 +    Android_RenderData *data =
  35.333 +        (Android_RenderData *) renderer->driverdata;
  35.334 +    int i;
  35.335 +
  35.336 +    if (data) {
  35.337 +        for (i = 0; i < SDL_arraysize(data->screens); ++i) {
  35.338 +            if (data->screens[i]) {
  35.339 +                SDL_FreeSurface(data->screens[i]);
  35.340 +            }
  35.341 +        }
  35.342 +        SDL_free(data);
  35.343 +    }
  35.344 +    SDL_free(renderer);
  35.345 +}
  35.346 +
  35.347 +/* vi: set ts=4 sw=4 expandtab: */
    36.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    36.2 +++ b/src/video/android/SDL_androidrender.h	Sun Aug 22 12:23:55 2010 -0700
    36.3 @@ -0,0 +1,28 @@
    36.4 +/*
    36.5 +    SDL - Simple DirectMedia Layer
    36.6 +    Copyright (C) 1997-2010 Sam Lantinga
    36.7 +
    36.8 +    This library is free software; you can redistribute it and/or
    36.9 +    modify it under the terms of the GNU Lesser General Public
   36.10 +    License as published by the Free Software Foundation; either
   36.11 +    version 2.1 of the License, or (at your option) any later version.
   36.12 +
   36.13 +    This library is distributed in the hope that it will be useful,
   36.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   36.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   36.16 +    Lesser General Public License for more details.
   36.17 +
   36.18 +    You should have received a copy of the GNU Lesser General Public
   36.19 +    License along with this library; if not, write to the Free Software
   36.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   36.21 +
   36.22 +    Sam Lantinga
   36.23 +    slouken@libsdl.org
   36.24 +*/
   36.25 +#include "SDL_config.h"
   36.26 +
   36.27 +/* SDL surface based renderer implementation */
   36.28 +
   36.29 +extern SDL_RenderDriver Android_RenderDriver;
   36.30 +
   36.31 +/* vi: set ts=4 sw=4 expandtab: */
    37.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    37.2 +++ b/src/video/android/SDL_androidvideo.c	Sun Aug 22 12:23:55 2010 -0700
    37.3 @@ -0,0 +1,166 @@
    37.4 +/*
    37.5 +    SDL - Simple DirectMedia Layer
    37.6 +    Copyright (C) 1997-2010 Sam Lantinga
    37.7 +
    37.8 +    This library is free software; you can redistribute it and/or
    37.9 +    modify it under the terms of the GNU Lesser General Public
   37.10 +    License as published by the Free Software Foundation; either
   37.11 +    version 2.1 of the License, or (at your option) any later version.
   37.12 +
   37.13 +    This library is distributed in the hope that it will be useful,
   37.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   37.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   37.16 +    Lesser General Public License for more details.
   37.17 +
   37.18 +    You should have received a copy of the GNU Lesser General Public
   37.19 +    License along with this library; if not, write to the Free Software
   37.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   37.21 +
   37.22 +    Sam Lantinga
   37.23 +    slouken@libsdl.org
   37.24 +*/
   37.25 +#include "SDL_config.h"
   37.26 +
   37.27 +/* Android SDL video driver implementation
   37.28 +*/
   37.29 +
   37.30 +#include "SDL_video.h"
   37.31 +#include "SDL_mouse.h"
   37.32 +#include "../SDL_sysvideo.h"
   37.33 +#include "../SDL_pixels_c.h"
   37.34 +#include "../../events/SDL_events_c.h"
   37.35 +
   37.36 +#include "SDL_androidvideo.h"
   37.37 +#include "SDL_androidevents.h"
   37.38 +#include "SDL_androidrender.h"
   37.39 +
   37.40 +#define ANDROID_VID_DRIVER_NAME "Android"
   37.41 +
   37.42 +/* Initialization/Query functions */
   37.43 +static int Android_VideoInit(_THIS);
   37.44 +static int Android_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
   37.45 +static void Android_VideoQuit(_THIS);
   37.46 +
   37.47 +/* GL functions (SDL_androidgl.c) */
   37.48 +extern int Android_GL_LoadLibrary(_THIS, const char *path);
   37.49 +extern void *Android_GL_GetProcAddress(_THIS, const char *proc);
   37.50 +extern void Android_GL_UnloadLibrary(_THIS);
   37.51 +//extern int *Android_GL_GetVisual(_THIS, Display * display, int screen);
   37.52 +extern SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window);
   37.53 +extern int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
   37.54 +                              SDL_GLContext context);
   37.55 +extern int Android_GL_SetSwapInterval(_THIS, int interval);
   37.56 +extern int Android_GL_GetSwapInterval(_THIS);
   37.57 +extern void Android_GL_SwapWindow(_THIS, SDL_Window * window);
   37.58 +extern void Android_GL_DeleteContext(_THIS, SDL_GLContext context);
   37.59 +
   37.60 +/* Android driver bootstrap functions */
   37.61 +
   37.62 +
   37.63 +//These are filled in with real values in Android_SetScreenResolution on 
   37.64 +//init (before SDL_Main())
   37.65 +static int iScreenWidth = 320;
   37.66 +static int iScreenHeight = 240;
   37.67 +
   37.68 +
   37.69 +static int
   37.70 +Android_Available(void)
   37.71 +{
   37.72 +    return 1;
   37.73 +}
   37.74 +
   37.75 +static void
   37.76 +Android_DeleteDevice(SDL_VideoDevice * device)
   37.77 +{
   37.78 +    SDL_free(device);
   37.79 +}
   37.80 +
   37.81 +static SDL_VideoDevice *
   37.82 +Android_CreateDevice(int devindex)
   37.83 +{
   37.84 +    printf("Creating video device\n");
   37.85 +    SDL_VideoDevice *device;
   37.86 +
   37.87 +    /* Initialize all variables that we clean on shutdown */
   37.88 +    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   37.89 +    if (!device) {
   37.90 +        SDL_OutOfMemory();
   37.91 +        if (device) {
   37.92 +            SDL_free(device);
   37.93 +        }
   37.94 +        return (0);
   37.95 +    }
   37.96 +
   37.97 +    /* Set the function pointers */
   37.98 +    device->VideoInit = Android_VideoInit;
   37.99 +    device->VideoQuit = Android_VideoQuit;
  37.100 +    device->SetDisplayMode = Android_SetDisplayMode;
  37.101 +    device->PumpEvents = Android_PumpEvents;
  37.102 +   
  37.103 +    device->free = Android_DeleteDevice;
  37.104 +
  37.105 +    /* GL pointers */
  37.106 +    device->GL_LoadLibrary = Android_GL_LoadLibrary;
  37.107 +    device->GL_GetProcAddress = Android_GL_GetProcAddress;
  37.108 +    device->GL_UnloadLibrary = Android_GL_UnloadLibrary;
  37.109 +    device->GL_CreateContext = Android_GL_CreateContext;
  37.110 +    device->GL_MakeCurrent = Android_GL_MakeCurrent;
  37.111 +    device->GL_SetSwapInterval = Android_GL_SetSwapInterval;
  37.112 +    device->GL_GetSwapInterval = Android_GL_GetSwapInterval;
  37.113 +    device->GL_SwapWindow = Android_GL_SwapWindow;
  37.114 +    device->GL_DeleteContext = Android_GL_DeleteContext;
  37.115 +
  37.116 +    return device;
  37.117 +}
  37.118 +
  37.119 +VideoBootStrap Android_bootstrap = {
  37.120 +    ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
  37.121 +    Android_Available, Android_CreateDevice
  37.122 +};
  37.123 +
  37.124 +
  37.125 +int
  37.126 +Android_VideoInit(_THIS)
  37.127 +{
  37.128 +    SDL_DisplayMode mode;
  37.129 +
  37.130 +    /* Use a fake 32-bpp desktop mode */
  37.131 +    mode.format = SDL_PIXELFORMAT_RGB888;
  37.132 +    mode.w = iScreenWidth;
  37.133 +    mode.h = iScreenHeight;
  37.134 +    mode.refresh_rate = 0;
  37.135 +    mode.driverdata = NULL;
  37.136 +    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
  37.137 +        return -1;
  37.138 +    }
  37.139 +    SDL_AddRenderDriver(&_this->displays[0], &Android_RenderDriver);
  37.140 +
  37.141 +    SDL_zero(mode);
  37.142 +    SDL_AddDisplayMode(&_this->displays[0], &mode);
  37.143 +
  37.144 +    Android_InitEvents();
  37.145 +
  37.146 +    /* We're done! */
  37.147 +    return 0;
  37.148 +}
  37.149 +
  37.150 +static int
  37.151 +Android_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
  37.152 +{
  37.153 +    return 0;
  37.154 +}
  37.155 +
  37.156 +void
  37.157 +Android_VideoQuit(_THIS)
  37.158 +{
  37.159 +}
  37.160 +
  37.161 +
  37.162 +void Android_SetScreenResolution(int width, int height){
  37.163 +    iScreenWidth = width;
  37.164 +    iScreenHeight = height;   
  37.165 +}
  37.166 +
  37.167 +
  37.168 +
  37.169 +/* vi: set ts=4 sw=4 expandtab: */
    38.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    38.2 +++ b/src/video/android/SDL_androidvideo.h	Sun Aug 22 12:23:55 2010 -0700
    38.3 @@ -0,0 +1,31 @@
    38.4 +/*
    38.5 +    SDL - Simple DirectMedia Layer
    38.6 +    Copyright (C) 1997-2010 Sam Lantinga
    38.7 +
    38.8 +    This library is free software; you can redistribute it and/or
    38.9 +    modify it under the terms of the GNU Lesser General Public
   38.10 +    License as published by the Free Software Foundation; either
   38.11 +    version 2.1 of the License, or (at your option) any later version.
   38.12 +
   38.13 +    This library is distributed in the hope that it will be useful,
   38.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   38.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   38.16 +    Lesser General Public License for more details.
   38.17 +
   38.18 +    You should have received a copy of the GNU Lesser General Public
   38.19 +    License along with this library; if not, write to the Free Software
   38.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   38.21 +
   38.22 +    Sam Lantinga
   38.23 +    slouken@libsdl.org
   38.24 +*/
   38.25 +#include "SDL_config.h"
   38.26 +
   38.27 +#ifndef _SDL_androidvideo_h
   38.28 +#define _SDL_androidvideo_h
   38.29 +
   38.30 +#include "../SDL_sysvideo.h"
   38.31 +
   38.32 +#endif /* _SDL_ndsvideo_h */
   38.33 +
   38.34 +/* vi: set ts=4 sw=4 expandtab: */