The process of migrating from cocos2d-x2 to cocos2d-x3 sharing _android

Source: Internet
Author: User
Tags event listener webp

The core code migration is relatively smooth, and the approximate process is as follows:

First, create the project

1) CD cocos2d-x-3.0rc0;
2 Execute setup.py, set the engine dependent environment variable, script will write Cocos_console_root and ant_root into ~/.bash_profile, execute source ~/.bash_profile make environment variable take effect;
3) to establish a projects catalogue under COCOS2D-X-3.0RC0;
4 Use Cocos2d-console tool to establish new project: Cocos new Gamedemo-p com.tonybai.game.gamedemo-l cpp-d./projects
5 CD./projects/gamedemo, we can see the project directory structure as follows:

Copy Code code as follows:
Bin/classes/cmakelists.txt Cocos2d/proj.android/
proj.ios_mac/proj.linux/proj.win32/resources/

6) Execute Cocos compile-p android-j 4–ap 19-m release, this demo apk will be generated, is roughly a cpp-empty-test;

Second, code porting

The main tasks of code porting include:
1) renamed
Most of the class names with the CC prefix are removed by the prefix;
The single case method of each main class was changed to getinstance Sharedxxxx;

2 menu, button event handling
Changed from Menu_selector (Gamescene::menustartcallback) to Cc_callback_1 (Gamescene::menustartcallback, this);

3 Touch-screen event handling

In Cocos2d-x 2.2.2, we directly use layer settouchenabled (true) and override three touch-screen event handlers.
In the new engine, we need to establish an event listener and register Listener in the global Eventdispatcher, such as:

Copy Code code as follows:

Auto listener = eventlistenertouchonebyone::create ();
Listener->setswallowtouches (TRUE);
Listener->ontouchbegan = Cc_callback_2 (Gamelayer::ontouchbegan, this);
listener->ontouchmoved = Cc_callback_2 (gamelayer::ontouchmoved, this);
listener->ontouchended = Cc_callback_2 (gamelayer::ontouchended, this);
Director::getinstance ()->geteventdispatcher ()->addeventlistenerwithscenegraphpriority (listener, this);

You can then implement the three event-handling methods here.

Core function migration, Gamedemo in the Genymotion 4.4 Android simulator and the real machine can operate normally, in the simulator can maintain about 40 frame rate, in the real machine frame rate has been around 60. After playing for a while, the feeling of the engine rendering performance has improved, and this elevation can be intuitively felt on the real machine.

But it didn't last, and I tried to run Gamedemo on Genymotion 2.3.7 Android, and the result was: black screen. The cpp-empty-test of the Cocos2d-x 3.0RC0 is compiled and placed on the simulator to run and get the same black screen result, which is obviously a problem of RC0. The result of a cursory search on the Cocos2d-x forum is that the upgrade to the latest version can solve the problem of black screen. So to the official download of the current version of the latest release of Cocos2d-x 3.0rc2. Here also spit: Cocos2d-x engine package size is too large, it does not seem to provide any patch files, resulting in each release to download a hundreds of M package. Official Git repository is too big, try to clone a few times failed, and finally can only download the source of the zip package.

Cocos2d-x 3.0rc2 Download decompression, first compiled a bit of cpp-empty-test, and then deployed to the Android 2.3.7 run, this time "black screen" really disappeared, it seems that the RC2 fixed the problem. The next step is to transplant my gamedemo to the RC2.

I use the extracted "cocos2d-x 3.0rc2" to replace the cocos2d under Gamedemo, and then run Cocos compile compile, install to the emulator line run, the program fails to start, and a row of error logs from the monitor Logcat:

"Anativeactivity_oncreate not Found"

How could it be? Anativeactivity_oncreate is provided by NDK's native_app_glue Static library, how could it not be found?

So open gamedemo/cocos2d/cocos/2d/platform/android/android.mk to see what it is:

Copy Code code as follows:

Local_whole_static_libraries: = cocos_png_static cocos_jpeg_static cocos_tiff_static cocos_webp_static

Include $ (build_static_library)

$ (call import-module,jpeg/prebuilt/android)
$ (call import-module,png/prebuilt/android)
$ (call import-module,tiff/prebuilt/android)
$ (call import-module,webp/prebuilt/android)

Android.mk content in incredibly did not include Native_app_glue, and looked at the Cocos2d-x 3.0rc0 in the same position android.mk, the latter is a native_app_glue of the library dependent. Is RC2 this piece forgotten? So I tried to add Native_app_glue dependencies:

Copy Code code as follows:

Local_whole_static_libraries: = Android_native_app_glue cocos_png_static cocos_jpeg_static cocos_tiff_static cocos_ Webp_static

Include $ (build_static_library)

$ (call import-module,jpeg/prebuilt/android)
$ (call import-module,png/prebuilt/android)
$ (call import-module,tiff/prebuilt/android)
$ (call import-module,webp/prebuilt/android)
$ (call Import-module,android/native_app_glue)

Attempt to compile again, but this time even the compilation did not pass, the wrong build result is as follows:

Copy Code code as follows:

/home1/tonybai/android-dev/adt-bundle-linux-x86_64/android-ndk-r9c/sources/android/native_app_glue/android_ native_app_glue.c:232:error:undefined reference to ' Android_main '
Collect2:error:ld returned 1 exit status
Make: * * * [obj/local/armeabi/libgamedemo.so] Error 1

From the result, the linker failed to find the corresponding function body definition in Native_app_glue android_main. Android_main is an implementation of the COCOS2D-X 3.0 engine. So I went into the RC2 engine code again to find out why, but it surprised me: "Nativeactivity was removed by the engine!" There are no nativeactivity.h and nativeactivity.cpp under the Cocos2d/cocos/2d/platform/android directory:

Copy Code code as follows:

$ ls-f cocos2d/cocos/2d/platform/android
Android.mk CCApplication.h CCDevice.cpp CCFileUtilsAndroid.h CCGLView.cpp CCPlatformDefine.h jni/
CCApplication.cpp CCCommon.cpp CCFileUtilsAndroid.cpp CCGL.h CCGLView.h CCStdC.h Tivity.cpp

We saw a new file: Javaactivity.cpp, opening the file, we found a similar name to the Cocos2d-x 2.2.2 Version: Java_org_cocos2dx_lib_cocos2dxrenderer_ Nativeinit. RC2 the engine entry code for the Android platform back to the 2.x version of the design? So hurried into the/cocos2d/cocos/2d/platform/android/java/src/org/cocos2dx/lib directory to see exactly.

Sure enough, everything seems so familiar: Cocos2dxactivity.java, Cocos2dxglsurfaceview.java, Cocos2dxrenderer.java ... From this we can conclude that the engine design of the Android platform in RC2 is back to the 2.x version:
-Your gameactivity to integrate cocos2dxactivity;
–mglsurfaceview.setcocos2dxrenderer (New Cocos2dxrenderer ()), Glthread (rendering thread) was born
– Dead Loop Call Cocos2dxrenderer.ondrawframe
– The engine logic is executed in the cocos2dxrenderer.ondrawframe.

Back to the 2.2.2 version of the engine that is designed, does it feel like RC0, even if the renderer is newly written? The results of the true machine test show that there is no intuitive sense of ascension. Is it the difference between native thread (Pthread_create creation) and Java thread? Unknown, follow up slowly experience it.

Another thing to mention: Javaactivity.cpp the previous 2.2.2 version in the project JNI Java_org_cocos2dx_lib_cocos2dxrenderer_nativeinit moved to the engine, is basically the same code, It's better to put it in the engine. RC2 's design regression also has some benefits, one is that the previous understanding of the engine is also applicable, two is suitable for integration in the 2.2.2 version of the Third-party tools should also be suitable for the 3.0RC2 version, so the cost of porting is estimated to be smaller. So we focus on the new 3.0 engine, focusing on the renderer, the event distribution mechanism, and the physical engine changes.

In fact, Nativeactivity was removed in RC1, and the larger changes were unexpected. Such a big change, such a short time release, gives a little bit of concern about the quality of the current 3.0 engine, at least the Android version of the engine. I don't know what the code will be like in the 3.0 official edition, wait and see.

Btw,rc2 version cpp-empty-test on the Android 2.3.7 simulator frame number below 10 frames, my demo is only 5 frames, and in the 4.4 version of the simulator, you can reach 40 frames, but fortunately.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.