Add a way to set the context when other activities are active so many SDL API functions still work.

This commit is contained in:
Sam Lantinga 2017-08-28 23:04:47 -07:00
parent 130138fa59
commit c684eb2cde
1 changed files with 16 additions and 4 deletions

View File

@ -56,6 +56,7 @@ public class SDLActivity extends Activity {
public static boolean mSeparateMouseAndTouch; public static boolean mSeparateMouseAndTouch;
// Main components // Main components
protected static Context mContext;
protected static SDLActivity mSingleton; protected static SDLActivity mSingleton;
protected static SDLSurface mSurface; protected static SDLSurface mSurface;
protected static View mTextEdit; protected static View mTextEdit;
@ -135,6 +136,7 @@ public class SDLActivity extends Activity {
public static void initialize() { public static void initialize() {
// The static nature of the singleton and Android quirkyness force us to initialize everything here // The static nature of the singleton and Android quirkyness force us to initialize everything here
// Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values // Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
mContext = null;
mSingleton = null; mSingleton = null;
mSurface = null; mSurface = null;
mTextEdit = null; mTextEdit = null;
@ -155,6 +157,9 @@ public class SDLActivity extends Activity {
} }
// Setup // Setup
public static void setContext(Context context) {
mContext = context;
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "Device: " + android.os.Build.DEVICE); Log.v(TAG, "Device: " + android.os.Build.DEVICE);
@ -165,7 +170,7 @@ public class SDLActivity extends Activity {
SDLActivity.initialize(); SDLActivity.initialize();
// So we can call stuff from static callbacks // So we can call stuff from static callbacks
mSingleton = this; mContext = mSingleton = this;
// Load shared libraries // Load shared libraries
String errorMsgBrokenLib = ""; String errorMsgBrokenLib = "";
@ -564,8 +569,9 @@ public class SDLActivity extends Activity {
*/ */
public static void setOrientation(int w, int h, boolean resizable, String hint) public static void setOrientation(int w, int h, boolean resizable, String hint)
{ {
if (mSingleton != null) {
mSingleton.setOrientationBis(w, h, resizable, hint); mSingleton.setOrientationBis(w, h, resizable, hint);
return; }
} }
/** /**
@ -638,6 +644,9 @@ public class SDLActivity extends Activity {
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
*/ */
public static boolean sendMessage(int command, int param) { public static boolean sendMessage(int command, int param) {
if (mSingleton == null) {
return false;
}
return mSingleton.sendCommand(command, Integer.valueOf(param)); return mSingleton.sendCommand(command, Integer.valueOf(param));
} }
@ -645,7 +654,7 @@ public class SDLActivity extends Activity {
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
*/ */
public static Context getContext() { public static Context getContext() {
return mSingleton; return mContext;
} }
static class ShowTextInputTask implements Runnable { static class ShowTextInputTask implements Runnable {
@ -716,6 +725,9 @@ public class SDLActivity extends Activity {
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
*/ */
public static Surface getNativeSurface() { public static Surface getNativeSurface() {
if (SDLActivity.mSurface == null) {
return null;
}
return SDLActivity.mSurface.getNativeSurface(); return SDLActivity.mSurface.getNativeSurface();
} }