Hi Surith,
Cocos2d-x android port is using native activity instead of Java activity.
Thanks to this improvement, the performance of render loop has been
increased a lot. But there are two problems those cause compatibility issues.
Most of third party SDKs for android are written in Java, the question is
how to integrate these SDKs to cocos2d-x games after this change.
What I found was that the instance of native activity was passed to"org/cocos2dx/lib/Cocos2dxHelper/init" method, the third party sdk could get
the context through Cocos2dxHelper.getActivity.
Some third party SDKs like analysis alway need hack the onPause(onStop)
and onResume(onStart), so I think java also should get responses fromnativeactivity.cpp when these events happen.
Pseudo codes may be:
public class Cocos2dxHelper {
...
// jni method called from native
public static void onPause() {
// The codes for the third party sdk need to be inserted here.
MobclickAgent.onPause(mContext);
}
// jni method called from native
public static void onResume() {
// The codes for the third party sdk need to be inserted here.
// And the context argument is needed.
MobclickAgent.onResume(mContext);
}
...
}Maybe you know that our Plugin-X has integrated lots of third party SDKs.
After this change, all the plugins in Plugin-X were broken.
So we need to fix this issue before the next release.
However, I don’t know whether what I mentioned above are the best ways to
resolve the issues.
Could you help to resolve the issues or give us some suggestions? Thanks.
Best Regards
James
Hi Surith,
Cocos2d-x android port is using native activity instead of Java activity.
Thanks to this improvement, the performance of render loop has been
increased a lot. But there are two problems those cause compatibility issues.
1) How to pass an activity instance ( context argument ) to Java ?
Most of third party SDKs for android are written in Java, the question is
how to integrate these SDKs to cocos2d-x games after this change.
What I found was that the instance of native activity was passed to"org/cocos2dx/lib/Cocos2dxHelper/init"method, the third party sdk could get
the context throughCocos2dxHelper.getActivity.
2) How to get response of system events in Java
Some third party SDKs like
analysisalway need hack theonPause(onStop)
andonResume(onStart), so I think java also should get responses from
nativeactivity.cppwhen these events happen.
Pseudo codes may be:public class Cocos2dxHelper { ... // jni method called from native public static void onPause() { // The codes for the third party sdk need to be inserted here. MobclickAgent.onPause(mContext); } // jni method called from native public static void onResume() { // The codes for the third party sdk need to be inserted here. // And the context argument is needed. MobclickAgent.onResume(mContext); } ... }Maybe you know that our
Plugin-Xhas integrated lots of third party SDKs.
After this change, all the plugins inPlugin-Xwere broken.
So we need to fix this issue before the next release.However, I don’t know whether what I mentioned above are the best ways to
resolve the issues.
Could you help to resolve the issues or give us some suggestions? Thanks.
--Best Regards
James
You received this message because you are subscribed to the Google Groups "cocos2d JS development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocos2d-js-dev...@googlegroups.com.
To post to this group, send email to cocos2d-...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Great! I think that the rendering performance has improved slightly. However the response time to user events (like touch) has improved a LOT. This allows Cocos games to be much more responsive. You can easily see this with SimpleGame - a user can shoot way more bullets in the same amount of time as compared to the earlier, slower Java based port.
I tested SimpleGame project with v2.1.4 and develop branch on the github.
But I could not figure out how much responsive after using native activity since the result seems be the same.
The android device I used was MX2 android4.1, 2.0G CPU, 2G RAM.
Use the callbacks defined in /platforms/android–9/arch_[arm|x86|mips]/usr/include/native_activity.h
void (onPause)(ANativeActivity activity);
void (onResume)(ANativeActivity activity);
etc…
I prefer (2) Why?
Third party SDKs are often buggy and cause game crashes. It is good to be able to disable calling into these SDKs from game code so that the developer can isolate issues. It is much easier to isolate issues without Java subclassing and XML modifications.
Yep, I prefer 2) too. Plugin-x also needs to provide c++ interfaces too.
Game code is more readable when plugins are not in the main application lifecycle. i.e. entire game lifecycle is in C/C++ and Java/JNI calls are only made for non-game processing.
That seems to be great. But there are some questions:
UI Thread or GL Thread like before?In Plugin-x, all JNI invocations of Java API should be run on UI thread.
In that case, we got a method like runOnUIThread to post events to UI thread and
call the third party API in UI thread.
I don’t know how to do that since there aren’t any Java codes now.
JNI_OnLoad works now ?Before, we have in SomeGame/proj.android/jni/main.cpp
jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
JniHelper::setJavaVM(vm);
PluginJniHelper::setJavaVM(vm); // Pass VM argument to Plugin which is for JNI interactions.
return JNI_VERSION_1_4;
}Now, we get this:
void cocos_android_app_init (void) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
}In order to set vm to plugin, we need to insert codes as follows:
void cocos_android_app_init (void) {
LOGD("cocos_android_app_init");
PluginJniHelper::setJavaVM(JniHelper::getJavaVM()); // Set vm for plugin here.
AppDelegate *pAppDelegate = new AppDelegate();
}Since cocos_android_app_init is done in game code, it’s the entry developer could see.
All initalization of third party SDKs need to be done here.
One thing we have to know is Plugin-x doesn’t depend on cocos2d-x, so any plugin
codes should not be placed in engine. Therefore, in plugin codes in Java should not
invoke Cocos2dxHelper.getActivity directly.
I think the arguments of cocos_android_app_init
should not be void. It should contain an activity at least.
Thing may go to in this way:
void cocos_android_app_init (sometype nativeApp) {
LOGD("cocos_android_app_init");
PluginJniHelper::setJavaVM(nativeApp->activity->vm);// set vm for plugin here.
initPluginMananger(nativeApp->activity); // Set the context for plugin
AppDelegate *pAppDelegate = new AppDelegate();
}
When I am done with my current work of fixing bugs in the Android rendering my plan is to update one of the plugin samples. It would be great if you get a chance to try it out before I do!
@ZhangBin, who is the maintainer of plugin-x, will try to fix the broken issue.
We will let you know our progress.
Could you help us figure out the two problems I mentioned above?
Regards
James
Great! I think that the rendering performance has improved slightly. However the response time to user events (like touch) has improved a LOT. This allows Cocos games to be much more responsive. You can easily see this with SimpleGame - a user can shoot way more bullets in the same amount of time as compared to the earlier, slower Java based port.I tested SimpleGame project with v2.1.4 and develop branch on the github.
But I could not figure out how much responsive after using native activity since the result seems be the same.
The android device I used was MX2 android4.1, 2.0G CPU, 2G RAM.
Regards
James
On Tuesday, August 20, 2013 7:39:51 AM UTC-7, jianhua.chen wrote:Great! I think that the rendering performance has improved slightly. However the response time to user events (like touch) has improved a LOT. This allows Cocos games to be much more responsive. You can easily see this with SimpleGame - a user can shoot way more bullets in the same amount of time as compared to the earlier, slower Java based port.I tested SimpleGame project with v2.1.4 and develop branch on the github.
But I could not figure out how much responsive after using native activity since the result seems be the same.
The android device I used was MX2 android4.1, 2.0G CPU, 2G RAM.I am using an LG Nexus 4/Android 4.3. Could you please let me know what benchmark you are using? Thanks.
2. The VM and the pointer to the current Android Application Context is available from struct android_app* app.For example, it is referenced here https://github.com/cocos2d/cocos2d-x/blob/develop/cocos2dx/platform/android/nativeactivity.cpp#L419This can be easily referenced to set the VM and to obtain an Application Context as the plugin requires.The relevant headers are in the android-ndk<android-ndk>/platforms/android-9/arch-arm/usr/include/android/native_activity.h<android-ndk>/sources/android/native_app_glue/android_native_app_glue.h
Hi, Surith.It's really a good idea. I will modify the code of sample project in plugin and send a pull request.Then we can discuss the problems on the pull request.I will finish this work in 1 or 2 days. Thanks for your suggestion!
Best Regards!----------------------------------------------------------Bill ZhangE-mail: zhan...@cocos2d-x.org