Improve Android pause/resume behavior.

This commit is contained in:
Gabriel Jacobo 2013-11-29 10:06:08 -03:00
parent e2be1694c1
commit 7f809e64d8
2 changed files with 10 additions and 10 deletions

View file

@ -300,16 +300,17 @@ void Java_org_libsdl_app_SDLActivity_nativeQuit(
void Java_org_libsdl_app_SDLActivity_nativePause( void Java_org_libsdl_app_SDLActivity_nativePause(
JNIEnv* env, jclass cls) JNIEnv* env, jclass cls)
{ {
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
if (Android_Window) { if (Android_Window) {
/* Signal the pause semaphore so the event loop knows to pause and (optionally) block itself */
if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0); SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0); SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
/* *After* sending the relevant events, signal the pause semaphore
* so the event loop knows to pause and (optionally) block itself */
if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
} }
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
} }
/* Resume */ /* Resume */
@ -317,8 +318,6 @@ void Java_org_libsdl_app_SDLActivity_nativeResume(
JNIEnv* env, jclass cls) JNIEnv* env, jclass cls)
{ {
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()"); __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
if (Android_Window) { if (Android_Window) {
/* Signal the resume semaphore so the event loop knows to resume and restore the GL Context /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
@ -326,6 +325,9 @@ void Java_org_libsdl_app_SDLActivity_nativeResume(
* and this function will be called from the Java thread instead. * and this function will be called from the Java thread instead.
*/ */
if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem); if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0); SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0); SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
} }

View file

@ -66,8 +66,6 @@ Android_PumpEvents(_THIS)
/* /*
* Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
* When the pause semaphore is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted. * When the pause semaphore is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted.
* When the resume semaphore is signaled, SDL_GL_CreateContext is called which in turn calls Java code
* SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
*/ */
#if SDL_ANDROID_BLOCK_ON_PAUSE #if SDL_ANDROID_BLOCK_ON_PAUSE