Updated SDL 2 to the lastest revision ( the RC ). Applied the new modifications to eepp.

This commit is contained in:
Martín Lucas Golini
2013-05-28 01:23:21 -03:00
parent a65ef102f2
commit 7ff310f944
523 changed files with 28934 additions and 19527 deletions

View File

@@ -3,8 +3,8 @@ package org.libsdl.app;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.*;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import android.app.*;
import android.content.*;
@@ -17,52 +17,47 @@ import android.widget.AbsoluteLayout;
import android.os.*;
import android.util.Log;
import android.graphics.*;
import android.text.method.*;
import android.text.*;
import android.media.*;
import android.hardware.*;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import java.lang.*;
/**
SDL Activity
*/
public class SDLActivity extends Activity {
private static final String TAG = "SDL";
// Keep track of the paused state
public static boolean mIsPaused;
public static boolean mIsPaused = false;
// Main components
private static SDLActivity mSingleton;
private static SDLSurface mSurface;
private static View mTextEdit;
private static ViewGroup mLayout;
protected static SDLActivity mSingleton;
protected static SDLSurface mSurface;
protected static View mTextEdit;
protected static ViewGroup mLayout;
// This is what SDL runs in. It invokes SDL_main(), eventually
private static Thread mSDLThread;
protected static Thread mSDLThread;
// Audio
private static Thread mAudioThread;
private static AudioTrack mAudioTrack;
protected static Thread mAudioThread;
protected static AudioTrack mAudioTrack;
// EGL private objects
private static EGLContext mEGLContext;
private static EGLSurface mEGLSurface;
private static EGLDisplay mEGLDisplay;
private static EGLConfig mEGLConfig;
private static int mGLMajor, mGLMinor;
// EGL objects
protected static EGLContext mEGLContext;
protected static EGLSurface mEGLSurface;
protected static EGLDisplay mEGLDisplay;
protected static EGLConfig mEGLConfig;
protected static int mGLMajor, mGLMinor;
// Load the .so
static {
System.loadLibrary("openal");
System.loadLibrary("openal");
System.loadLibrary("main");
}
// Setup
@Override
protected void onCreate(Bundle savedInstanceState) {
//Log.v("SDL", "onCreate()");
super.onCreate(savedInstanceState);
@@ -70,9 +65,6 @@ public class SDLActivity extends Activity {
// So we can call stuff from static callbacks
mSingleton = this;
// Keep track of the paused state
mIsPaused = false;
// Set up the surface
mSurface = new SDLSurface(getApplication());
@@ -80,23 +72,31 @@ public class SDLActivity extends Activity {
mLayout.addView(mSurface);
setContentView(mLayout);
SurfaceHolder holder = mSurface.getHolder();
}
// Events
/*protected void onPause() {
@Override
protected void onPause() {
Log.v("SDL", "onPause()");
super.onPause();
// Don't call SDLActivity.nativePause(); here, it will be called by SDLSurface::surfaceDestroyed
}
@Override
protected void onResume() {
Log.v("SDL", "onResume()");
super.onResume();
// Don't call SDLActivity.nativeResume(); here, it will be called via SDLSurface::surfaceChanged->SDLActivity::startApp
}*/
}
@Override
public void onLowMemory() {
Log.v("SDL", "onLowMemory()");
super.onLowMemory();
SDLActivity.nativeLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("SDL", "onDestroy()");
@@ -121,36 +121,72 @@ public class SDLActivity extends Activity {
static final int COMMAND_UNUSED = 2;
static final int COMMAND_TEXTEDIT_HIDE = 3;
// Handler for the messages
Handler commandHandler = new Handler() {
protected static final int COMMAND_USER = 0x8000;
/**
* This method is called by SDL if SDL did not handle a message itself.
* This happens if a received message contains an unsupported command.
* Method can be overwritten to handle Messages in a different class.
* @param command the command of the message.
* @param param the parameter of the message. May be null.
* @return if the message was handled in overridden method.
*/
protected boolean onUnhandledMessage(int command, Object param) {
return false;
}
/**
* A Handler class for Messages from native SDL applications.
* It uses current Activities as target (e.g. for the title).
* static to prevent implicit references to enclosing object.
*/
protected static class SDLCommandHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Context context = getContext();
if (context == null) {
Log.e(TAG, "error handling message, getContext() returned null");
return;
}
switch (msg.arg1) {
case COMMAND_CHANGE_TITLE:
setTitle((String)msg.obj);
if (context instanceof Activity) {
((Activity) context).setTitle((String)msg.obj);
} else {
Log.e(TAG, "error handling message, getContext() returned no Activity");
}
break;
case COMMAND_TEXTEDIT_HIDE:
if (mTextEdit != null) {
mTextEdit.setVisibility(View.GONE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mTextEdit.getWindowToken(), 0);
}
break;
default:
if ((context instanceof SDLActivity) && !((SDLActivity) context).onUnhandledMessage(msg.arg1, msg.obj)) {
Log.e(TAG, "error handling message, command is " + msg.arg1);
}
}
}
};
}
// Handler for the messages
Handler commandHandler = new SDLCommandHandler();
// Send a message from the SDLMain thread
void sendCommand(int command, Object data) {
boolean sendCommand(int command, Object data) {
Message msg = commandHandler.obtainMessage();
msg.arg1 = command;
msg.obj = data;
commandHandler.sendMessage(msg);
return commandHandler.sendMessage(msg);
}
// C functions we call
public static native void nativeInit(String apkPath);
public static native void nativeInit();
public static native void nativeLowMemory();
public static native void nativeQuit();
public static native void nativePause();
public static native void nativeResume();
@@ -166,21 +202,21 @@ public class SDLActivity extends Activity {
// Java functions called from C
public static boolean createGLContext(int majorVersion, int minorVersion) {
return initEGL(majorVersion, minorVersion);
public static boolean createGLContext(int majorVersion, int minorVersion, int[] attribs) {
return initEGL(majorVersion, minorVersion, attribs);
}
public static void flipBuffers() {
flipEGL();
}
public static void setActivityTitle(String title) {
public static boolean setActivityTitle(String title) {
// Called from SDLMain() thread and can't directly affect the view
mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
return mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
}
public static void sendMessage(int command, int param) {
mSingleton.sendCommand(command, Integer.valueOf(param));
public static boolean sendMessage(int command, int param) {
return mSingleton.sendCommand(command, Integer.valueOf(param));
}
public static Context getContext() {
@@ -205,7 +241,7 @@ public class SDLActivity extends Activity {
}
}
static class ShowTextInputHandler implements Runnable {
static class ShowTextInputTask implements Runnable {
/*
* This is used to regulate the pan&scan method to have some offset from
* the bottom edge of the input region and the top edge of an input
@@ -215,13 +251,14 @@ public class SDLActivity extends Activity {
public int x, y, w, h;
public ShowTextInputHandler(int x, int y, int w, int h) {
public ShowTextInputTask(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
@Override
public void run() {
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
w, h + HEIGHT_PADDING, x, y);
@@ -240,17 +277,16 @@ public class SDLActivity extends Activity {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mTextEdit, 0);
}
}
public static void showTextInput(int x, int y, int w, int h) {
public static boolean showTextInput(int x, int y, int w, int h) {
// Transfer the task to the main thread as a Runnable
mSingleton.commandHandler.post(new ShowTextInputHandler(x, y, w, h));
return mSingleton.commandHandler.post(new ShowTextInputTask(x, y, w, h));
}
// EGL functions
public static boolean initEGL(int majorVersion, int minorVersion) {
public static boolean initEGL(int majorVersion, int minorVersion, int[] attribs) {
try {
if (SDLActivity.mEGLDisplay == null) {
Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
@@ -262,22 +298,9 @@ public class SDLActivity extends Activity {
int[] version = new int[2];
egl.eglInitialize(dpy, version);
int EGL_OPENGL_ES_BIT = 1;
int EGL_OPENGL_ES2_BIT = 4;
int renderableType = 0;
if (majorVersion == 2) {
renderableType = EGL_OPENGL_ES2_BIT;
} else if (majorVersion == 1) {
renderableType = EGL_OPENGL_ES_BIT;
}
int[] configSpec = {
//EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_RENDERABLE_TYPE, renderableType,
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
if (!egl.eglChooseConfig(dpy, attribs, configs, 1, num_config) || num_config[0] == 0) {
Log.e("SDL", "No EGL config available");
return false;
}
@@ -365,14 +388,12 @@ public class SDLActivity extends Activity {
}
// Audio
private static Object buf;
public static Object audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
public static void audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + ((float)sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + (sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
// Let the user pick a larger buffer if they really want -- but ye
// gods they probably shouldn't, the minimums are horrifyingly high
@@ -384,18 +405,12 @@ public class SDLActivity extends Activity {
audioStartThread();
Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + ((float)mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
if (is16Bit) {
buf = new short[desiredFrames * (isStereo ? 2 : 1)];
} else {
buf = new byte[desiredFrames * (isStereo ? 2 : 1)];
}
return buf;
Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + (mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
}
public static void audioStartThread() {
mAudioThread = new Thread(new Runnable() {
@Override
public void run() {
mAudioTrack.play();
nativeRunAudioThread();
@@ -437,7 +452,7 @@ public class SDLActivity extends Activity {
// Nom nom
}
} else {
Log.w("SDL", "SDL audio: error return from write(short)");
Log.w("SDL", "SDL audio: error return from write(byte)");
return;
}
}
@@ -466,20 +481,10 @@ public class SDLActivity extends Activity {
Simple nativeInit() runnable
*/
class SDLMain implements Runnable {
@Override
public void run() {
// Runs SDL_main()
// return apk file path (or null on error)
String apkFilePath = null;
ApplicationInfo appInfo = null;
PackageManager packMgmr = SDLActivity.getContext().getPackageManager();
try {
appInfo = packMgmr.getApplicationInfo("org.libsdl.app", 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Unable to locate assets, aborting...");
}
apkFilePath = appInfo.sourceDir;
SDLActivity.nativeInit(apkFilePath);
SDLActivity.nativeInit();
//Log.v("SDL", "SDL thread terminated");
}
@@ -496,10 +501,10 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
View.OnKeyListener, View.OnTouchListener, SensorEventListener {
// Sensors
private static SensorManager mSensorManager;
protected static SensorManager mSensorManager;
// Keep track of the surface size to normalize touch events
private static float mWidth, mHeight;
protected static float mWidth, mHeight;
// Startup
public SDLSurface(Context context) {
@@ -512,7 +517,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
setOnKeyListener(this);
setOnTouchListener(this);
mSensorManager = (SensorManager)context.getSystemService("sensor");
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
// Some arbitrary defaults to avoid a potential division by zero
mWidth = 1.0f;
@@ -520,6 +525,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
}
// Called when we have a valid drawing surface
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.v("SDL", "surfaceCreated()");
holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
@@ -527,6 +533,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
}
// Called when we lose the surface
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.v("SDL", "surfaceDestroyed()");
if (!SDLActivity.mIsPaused) {
@@ -537,11 +544,12 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
}
// Called when the surface is resized
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
Log.v("SDL", "surfaceChanged()");
int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
int sdlFormat = 0x15151002; // SDL_PIXELFORMAT_RGB565 by default
switch (format) {
case PixelFormat.A_8:
Log.v("SDL", "pixel format A_8");
@@ -554,40 +562,40 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
break;
case PixelFormat.RGBA_4444:
Log.v("SDL", "pixel format RGBA_4444");
sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
sdlFormat = 0x15421002; // SDL_PIXELFORMAT_RGBA4444
break;
case PixelFormat.RGBA_5551:
Log.v("SDL", "pixel format RGBA_5551");
sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
sdlFormat = 0x15441002; // SDL_PIXELFORMAT_RGBA5551
break;
case PixelFormat.RGBA_8888:
Log.v("SDL", "pixel format RGBA_8888");
sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
sdlFormat = 0x16462004; // SDL_PIXELFORMAT_RGBA8888
break;
case PixelFormat.RGBX_8888:
Log.v("SDL", "pixel format RGBX_8888");
sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
sdlFormat = 0x16261804; // SDL_PIXELFORMAT_RGBX8888
break;
case PixelFormat.RGB_332:
Log.v("SDL", "pixel format RGB_332");
sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
sdlFormat = 0x14110801; // SDL_PIXELFORMAT_RGB332
break;
case PixelFormat.RGB_565:
Log.v("SDL", "pixel format RGB_565");
sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
sdlFormat = 0x15151002; // SDL_PIXELFORMAT_RGB565
break;
case PixelFormat.RGB_888:
Log.v("SDL", "pixel format RGB_888");
// Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
sdlFormat = 0x16161804; // SDL_PIXELFORMAT_RGB888
break;
default:
Log.v("SDL", "pixel format unknown " + format);
break;
}
mWidth = (float) width;
mHeight = (float) height;
mWidth = width;
mHeight = height;
SDLActivity.onNativeResize(width, height, sdlFormat);
Log.v("SDL", "Window size:" + width + "x"+height);
@@ -595,12 +603,12 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
}
// unused
@Override
public void onDraw(Canvas canvas) {}
// Key events
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
@@ -618,12 +626,12 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
}
// Touch events
@Override
public boolean onTouch(View v, MotionEvent event) {
{
final int touchDevId = event.getDeviceId();
final int pointerCount = event.getPointerCount();
// touchId, pointerId, action, x, y, pressure
int actionPointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent. ACTION_POINTER_ID_SHIFT; /* API 8: event.getActionIndex(); */
int actionPointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; /* API 8: event.getActionIndex(); */
int pointerFingerId = event.getPointerId(actionPointerIndex);
int action = (event.getAction() & MotionEvent.ACTION_MASK); /* API 8: event.getActionMasked(); */
@@ -644,7 +652,6 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
} else {
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
}
}
return true;
}
@@ -661,10 +668,12 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
@@ -693,6 +702,7 @@ class DummyEdit extends View implements View.OnKeyListener {
return true;
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// This handles the hardware keyboard input
@@ -741,7 +751,9 @@ class SDLInputConnection extends BaseInputConnection {
*/
int keyCode = event.getKeyCode();
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (event.isPrintingKey()) {
commitText(String.valueOf((char) event.getUnicodeChar()), 1);
}
SDLActivity.onNativeKeyDown(keyCode);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {