Update to SDL dev 22a0b777d7cd

This commit is contained in:
Joshua Granick
2017-05-04 15:15:29 -07:00
parent 45f923360a
commit ac13182fd0
2 changed files with 101 additions and 68 deletions

View File

@@ -36,8 +36,16 @@ import android.content.pm.ActivityInfo;
public class SDLActivity extends Activity { public class SDLActivity extends Activity {
private static final String TAG = "SDL"; private static final String TAG = "SDL";
// Keep track of the paused state public static boolean mIsResumedCalled, mIsSurfaceReady, mHasFocus;
public static boolean mIsPaused, mIsSurfaceReady, mHasFocus;
// Handle the state of the native layer
public enum NativeState {
INIT, RESUMED, PAUSED
}
public static NativeState mNextNativeState;
public static NativeState mCurrentNativeState;
public static boolean mExitCalledFromJava; public static boolean mExitCalledFromJava;
/** If shared libraries (e.g. SDL or the native application) could not be loaded. */ /** If shared libraries (e.g. SDL or the native application) could not be loaded. */
@@ -110,9 +118,11 @@ public class SDLActivity extends Activity {
mAudioRecord = null; mAudioRecord = null;
mExitCalledFromJava = false; mExitCalledFromJava = false;
mBrokenLibraries = false; mBrokenLibraries = false;
mIsPaused = false; mIsResumedCalled = false;
mIsSurfaceReady = false; mIsSurfaceReady = false;
mHasFocus = true; mHasFocus = true;
mNextNativeState = NativeState.INIT;
mCurrentNativeState = NativeState.INIT;
} }
// Setup // Setup
@@ -195,24 +205,28 @@ public class SDLActivity extends Activity {
protected void onPause() { protected void onPause() {
Log.v(TAG, "onPause()"); Log.v(TAG, "onPause()");
super.onPause(); super.onPause();
mNextNativeState = NativeState.PAUSED;
mIsResumedCalled = false;
if (SDLActivity.mBrokenLibraries) { if (SDLActivity.mBrokenLibraries) {
return; return;
} }
SDLActivity.handlePause(); SDLActivity.handleNativeState();
} }
@Override @Override
protected void onResume() { protected void onResume() {
Log.v(TAG, "onResume()"); Log.v(TAG, "onResume()");
super.onResume(); super.onResume();
mNextNativeState = NativeState.RESUMED;
mIsResumedCalled = true;
if (SDLActivity.mBrokenLibraries) { if (SDLActivity.mBrokenLibraries) {
return; return;
} }
SDLActivity.handleResume(); SDLActivity.handleNativeState();
} }
@@ -227,8 +241,12 @@ public class SDLActivity extends Activity {
SDLActivity.mHasFocus = hasFocus; SDLActivity.mHasFocus = hasFocus;
if (hasFocus) { if (hasFocus) {
SDLActivity.handleResume(); mNextNativeState = NativeState.RESUMED;
} else {
mNextNativeState = NativeState.PAUSED;
} }
SDLActivity.handleNativeState();
} }
@Override @Override
@@ -247,6 +265,9 @@ public class SDLActivity extends Activity {
protected void onDestroy() { protected void onDestroy() {
Log.v(TAG, "onDestroy()"); Log.v(TAG, "onDestroy()");
mNextNativeState = NativeState.PAUSED;
SDLActivity.handleNativeState();
if (SDLActivity.mBrokenLibraries) { if (SDLActivity.mBrokenLibraries) {
super.onDestroy(); super.onDestroy();
// Reset everything in case the user re opens the app // Reset everything in case the user re opens the app
@@ -295,37 +316,74 @@ public class SDLActivity extends Activity {
return super.dispatchKeyEvent(event); return super.dispatchKeyEvent(event);
} }
/** Called by onPause or surfaceDestroyed. Even if surfaceDestroyed /* Transition to next state */
* is the first to be called, mIsSurfaceReady should still be set public static void handleNativeState() {
* to 'true' during the call to onPause (in a usual scenario).
*/
public static void handlePause() {
if (!SDLActivity.mIsPaused && SDLActivity.mIsSurfaceReady) {
SDLActivity.mIsPaused = true;
SDLActivity.nativePause();
mSurface.handlePause();
}
}
/** Called by onResume or surfaceCreated. An actual resume should be done only when the surface is ready. if (mNextNativeState == mCurrentNativeState) {
* Note: Some Android variants may send multiple surfaceChanged events, so we don't need to resume // Already in same state, discard.
* every time we get one of those events, only if it comes after surfaceDestroyed return;
*/
public static void handleResume() {
if (SDLActivity.mIsPaused && SDLActivity.mIsSurfaceReady && SDLActivity.mHasFocus) {
SDLActivity.mIsPaused = false;
SDLActivity.nativeResume();
mSurface.handleResume();
} }
// Try a transition to init state
if (mNextNativeState == NativeState.INIT) {
mCurrentNativeState = mNextNativeState;
return;
}
// Try a transition to paused state
if (mNextNativeState == NativeState.PAUSED) {
nativePause();
mSurface.handlePause();
mCurrentNativeState = mNextNativeState;
return;
}
// Try a transition to resumed state
if (mNextNativeState == NativeState.RESUMED) {
if (mIsSurfaceReady && mHasFocus && mIsResumedCalled) {
if (mSDLThread == null) {
// This is the entry point to the C app.
// Start up the C app thread and enable sensor input for the first time
final Thread sdlThread = new Thread(new SDLMain(), "SDLThread");
mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
sdlThread.start();
// Set up a listener thread to catch when the native thread ends
mSDLThread = new Thread(new Runnable(){
@Override
public void run(){
try {
sdlThread.join();
}
catch(Exception e){}
finally{
// Native thread has finished
if (! mExitCalledFromJava) {
handleNativeExit();
}
}
}
}, "SDLThreadListener");
mSDLThread.start();
}
nativeResume();
mSurface.handleResume();
mCurrentNativeState = mNextNativeState;
}
return;
}
} }
/* The native thread has finished */ /* The native thread has finished */
public static void handleNativeExit() { public static void handleNativeExit() {
SDLActivity.mSDLThread = null; SDLActivity.mSDLThread = null;
mSingleton.finish(); mSingleton.finish();
// This fixes bad state errors when calling exit from C++, then resuming later
System.exit(0);
} }
@@ -1103,8 +1161,11 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
@Override @Override
public void surfaceDestroyed(SurfaceHolder holder) { public void surfaceDestroyed(SurfaceHolder holder) {
Log.v("SDL", "surfaceDestroyed()"); Log.v("SDL", "surfaceDestroyed()");
// Call this *before* setting mIsSurfaceReady to 'false'
SDLActivity.handlePause(); // Transition to pause, if needed
SDLActivity.mNextNativeState = SDLActivity.NativeState.PAUSED;
SDLActivity.handleNativeState();
SDLActivity.mIsSurfaceReady = false; SDLActivity.mIsSurfaceReady = false;
SDLActivity.onNativeSurfaceDestroyed(); SDLActivity.onNativeSurfaceDestroyed();
} }
@@ -1173,12 +1234,12 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
{ {
// Accept any // Accept any
} }
else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT)
{ {
if (mWidth > mHeight) { if (mWidth > mHeight) {
skip = true; skip = true;
} }
} else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { } else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE || requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
if (mWidth < mHeight) { if (mWidth < mHeight) {
skip = true; skip = true;
} }
@@ -1197,45 +1258,17 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
if (skip) { if (skip) {
Log.v("SDL", "Skip .. Surface is not ready."); Log.v("SDL", "Skip .. Surface is not ready.");
SDLActivity.mIsSurfaceReady = false;
return; return;
} }
/* Surface is ready */
// Set mIsSurfaceReady to 'true' *before* making a call to handleResume
SDLActivity.mIsSurfaceReady = true; SDLActivity.mIsSurfaceReady = true;
/* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
SDLActivity.onNativeSurfaceChanged(); SDLActivity.onNativeSurfaceChanged();
SDLActivity.handleNativeState();
if (SDLActivity.mSDLThread == null) {
// This is the entry point to the C app.
// Start up the C app thread and enable sensor input for the first time
final Thread sdlThread = new Thread(new SDLMain(), "SDLThread");
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
sdlThread.start();
// Set up a listener thread to catch when the native thread ends
SDLActivity.mSDLThread = new Thread(new Runnable(){
@Override
public void run(){
try {
sdlThread.join();
}
catch(Exception e){}
finally{
// Native thread has finished
if (! SDLActivity.mExitCalledFromJava) {
SDLActivity.handleNativeExit();
}
}
}
}, "SDLThreadListener");
SDLActivity.mSDLThread.start();
}
if (SDLActivity.mHasFocus && !SDLActivity.mIsPaused) {
SDLActivity.handleResume();
}
} }
// Key events // Key events