From 71e4ec6aca2387eae52d60c9f131d64f37ab3a98 Mon Sep 17 00:00:00 2001 From: natural-law Date: Mon, 25 Apr 2011 15:05:16 +0800 Subject: [PATCH 1/3] issue #468, Fix the compile error when the target platform is not win32. --- cocos2dx/support/CCProfiling.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2dx/support/CCProfiling.h b/cocos2dx/support/CCProfiling.h index 66b7c619ea..8c1203fb9a 100644 --- a/cocos2dx/support/CCProfiling.h +++ b/cocos2dx/support/CCProfiling.h @@ -29,7 +29,7 @@ THE SOFTWARE. #if CC_ENABLE_PROFILERS -#include +#include #include "CCObject.h" #include "platform/platform.h" From 99733c602ded26dc1bd41bfc9863f584b95ebf4a Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 25 Apr 2011 15:13:24 +0800 Subject: [PATCH 2/3] [android] fixed #462: call applicationDidEnterBackground and applicationWillEnterBackground on android --- .../cocos2dx/lib/Cocos2dxGLSurfaceView.java | 22 +++++++++++++++++++ .../org/cocos2dx/lib/Cocos2dxRenderer.java | 12 +++++++++- cocos2dx/platform/android/Cocos2dJni.cpp | 17 +++++++++++++- .../cocos2dx/lib/Cocos2dxGLSurfaceView.java | 22 +++++++++++++++++++ .../org/cocos2dx/lib/Cocos2dxRenderer.java | 12 +++++++++- 5 files changed, 82 insertions(+), 3 deletions(-) diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index f7b64beb08..4477fbb0fd 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -28,6 +28,28 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { setFocusableInTouchMode(true); setRenderer(mRenderer); } + + public void onPause(){ + queueEvent(new Runnable() { + @Override + public void run() { + mRenderer.handleOnPause(); + } + }); + + super.onPause(); + } + + public void onResume(){ + super.onResume(); + + queueEvent(new Runnable() { + @Override + public void run() { + mRenderer.handleOnResume(); + } + }); + } public boolean onTouchEvent(final MotionEvent event) { // these data are used in ACTION_MOVE and ACTION_CANCEL diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxRenderer.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxRenderer.java index 4a80d59ac6..06f389d65b 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxRenderer.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxRenderer.java @@ -64,6 +64,14 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer { nativeKeyDown(keyCode); } + public void handleOnPause(){ + nativeOnPause(); + } + + public void handleOnResume(){ + nativeOnResume(); + } + public static void setAnimationInterval(double interval){ animationInterval = (long)(interval * NANOSECONDSPERSECOND); } @@ -72,7 +80,9 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer { private static native void nativeTouchesEnd(int id, float x, float y); private static native void nativeTouchesMove(int[] id, float[] x, float[] y); private static native void nativeTouchesCancel(int[] id, float[] x, float[] y); -private static native boolean nativeKeyDown(int keyCode); + private static native boolean nativeKeyDown(int keyCode); private static native void nativeRender(); private static native void nativeInit(int w, int h); + private static native void nativeOnPause(); + private static native void nativeOnResume(); } diff --git a/cocos2dx/platform/android/Cocos2dJni.cpp b/cocos2dx/platform/android/Cocos2dJni.cpp index 3beb0f21b5..5ef1081df0 100644 --- a/cocos2dx/platform/android/Cocos2dJni.cpp +++ b/cocos2dx/platform/android/Cocos2dJni.cpp @@ -29,7 +29,8 @@ THE SOFTWARE. #include "CCTouchDispatcher.h" #include "CCFileUtils.h" #include "CCGeometry.h" -#include "platform/android/CCAccelerometer_android.h" +#include "CCAccelerometer.h" +#include "CCApplication.h" #include #if 0 @@ -172,6 +173,20 @@ extern "C" cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesCancelled(&set, NULL); } + + // handle onPause and onResume + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause() + { + CCApplication::sharedApplication().applicationDidEnterBackground(); + } + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume() + { + // Shared OpenGL View instance doesn't exist yet when Activity.onResume is first called + if (CCDirector::sharedDirector()->getOpenGLView()) + CCApplication::sharedApplication().applicationWillEnterForeground(); + } #define KEYCODE_BACK 0x04 #define KEYCODE_MENU 0x52 diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index f7b64beb08..4477fbb0fd 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -28,6 +28,28 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { setFocusableInTouchMode(true); setRenderer(mRenderer); } + + public void onPause(){ + queueEvent(new Runnable() { + @Override + public void run() { + mRenderer.handleOnPause(); + } + }); + + super.onPause(); + } + + public void onResume(){ + super.onResume(); + + queueEvent(new Runnable() { + @Override + public void run() { + mRenderer.handleOnResume(); + } + }); + } public boolean onTouchEvent(final MotionEvent event) { // these data are used in ACTION_MOVE and ACTION_CANCEL diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxRenderer.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxRenderer.java index 4a80d59ac6..06f389d65b 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxRenderer.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxRenderer.java @@ -64,6 +64,14 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer { nativeKeyDown(keyCode); } + public void handleOnPause(){ + nativeOnPause(); + } + + public void handleOnResume(){ + nativeOnResume(); + } + public static void setAnimationInterval(double interval){ animationInterval = (long)(interval * NANOSECONDSPERSECOND); } @@ -72,7 +80,9 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer { private static native void nativeTouchesEnd(int id, float x, float y); private static native void nativeTouchesMove(int[] id, float[] x, float[] y); private static native void nativeTouchesCancel(int[] id, float[] x, float[] y); -private static native boolean nativeKeyDown(int keyCode); + private static native boolean nativeKeyDown(int keyCode); private static native void nativeRender(); private static native void nativeInit(int w, int h); + private static native void nativeOnPause(); + private static native void nativeOnResume(); } From f11acc5e31ed396999c1b34233e64e4c73c8351c Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 25 Apr 2011 15:53:08 +0800 Subject: [PATCH 3/3] [android] pause/resume gl render thread when the activity is paused or remused --- tests/test.android/src/org/cocos2dx/tests/TestsDemo.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java b/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java index a3702a182c..e783fabeab 100644 --- a/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java +++ b/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java @@ -30,13 +30,17 @@ public class TestsDemo extends Cocos2dxActivity{ } @Override - protected void onPause() { + protected void onPause() { super.onPause(); + + mGLView.onPause(); } @Override protected void onResume() { super.onResume(); + + mGLView.onResume(); } protected void onDestroy()