Cocos2d-x complete solution from Win32 to Android porting

Source: Internet
Author: User

Introduction: We used the Cocos2d-x engine to create a flying shooting game, which was creatively integrated into the gesture recognition function. But we encountered a lot of problems in the transplant process, but also found that the information on the Internet is small and incomplete. So at the end of the project, we wrote this article to fully document our entire transplant process, to commemorate the successful completion of our project, and to express our gratitude to those who have helped us. Transplant process we get a lot of help on the Internet, but also to thank the Boxwood seniors in the last moment to help us overcome the difficulties!

0. Development platform

System: WIN8 Profession 64bit

ide:vs2012 RTM, eclipse

Cocos2d-x version: 2.1.2

1. Preparation for transplant

Porting under Windows requires the following environment support: Android SDK, NDK, Eclipse, Cygwin.

1.1 Download Cygwin installation program

On the Cygwin official website (http://cygwin.com/install.html) to the online installation program, download the corresponding version. Here I choose the Windows 64bit version.

1.2 Cygwin installation process

Open the downloaded Setup-x86_64.exe, you can click on the next step (space limit, too much details of the problem is not elaborated), until the following page:

Select the downloaded site, the correct selection of the site after the download will soon. We chose the ftp://mirrors.neusoft.edu.cn (at Beihang University) and it was very fast. Here to remind you that not all of the site download is complete! Rylynn (blog name) selected several sites are not complete download, if you encounter this problem, you can try different sites.

After you select the site, you will see the above interface, which is to select the installation package. To enable Cywin to compile the program, we need to select some of the necessary components. Without altering the default selection, we need to make sure that the following package has been selected in the Devel branch: binutils, gcc-c++,gcc-core,gdb,make,mingw-gcc-core,mingw-gcc-c++. After successful installation, run Cygwin, you can enter gcc+-V, g++-version, Make-v, gdb-v and so on in the Cygwin window and enter, if you see the window output the version information of each component, then the installation is successful. If you enter a command, the window output command not found, then you need to reopen the installer and install the corresponding components, do not need to reinstall all.

1.3 Installing Eclipse, Android SDK, Android NDK

Here is a link to the website, you can download and install yourself. The NDK can be downloaded and decompressed. Eclipse Installation and environment configuration, please refer to the corresponding article, do not repeat here.

Android sdk:http://developer.android.com/sdk/index.html

Android ndk:http://developer.android.com/tools/sdk/ndk/index.html

eclipse:http://www.eclipse.org/downloads/

1.4 Editing Create-android-project Scripts

The Create-android-project script can be found under the root directory of the Cocos2d-x engine. Under Windows, the corresponding is "Create-android-project.bat", the script used to generate the game Android project.

Use any UNIX-style line-wrapping editor (we chose Ultra Editor) to open the script file, find the following statement, and modify it.

Set _cygbin = E:\cygwin\bin

Set _androidtools =e:\android\andoird-sdk\tools

Set _ndkroot = e:\android\android-ndk-r8d

Where _cygbin is set to the "Bin" folder path under the Cygwin installation directory, _androidtools is set to the "Tools" folder path under the Andoird SDK installation path, and _ndkroot is set to the NDK's decompression path.

When you have finished setting up, run the "create-android-project" script in Cygwin (you can drag the file directly into the Cygwin window to enter). If prompted as follows, the modification succeeds:

Input package path. For example:org.cocos2dx.example:

2. Build the Project

Next we execute the Create-android-project script. Run "Create-android-project.bat" with Cygwin and see the following prompt:

Input package path. For example:org.cocos2dx.example:

At this point, enter the package name that we want to create (the package naming should follow the Andoird naming convention). We entered: "Org.cocofish.skyline" (our team named Cocofish, the project is called Skyline. You can choose as long as you remember. After carriage return, the window output:

Input Project Name:

We've entered "Skyline".

When you enter, the window lists all of the Android SDK versions installed on your computer and you can choose your own as needed. We have entered "1", which is the version of Android 4.1.2. In Lmeng (blog name) of the computer, the entire process window output of the content is posted below:

Please enter the your package path. For Example:org.cocos2dx.example:org.cocofish.skylinePlease Enter your project Name:skyline "now Cocos2d-x suppurts Android 2.1-update1, 2.2, 2.3 & 3.0 "" Other versions has not tested. "     Available Android targets:----------id:1 or "android-16" name:android 4.1.2 Type:platform API level:16     Revision:4 SKINS:HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, wxga800-7in     ABIS:ARMEABI-V7A, MIPS, x86----------id:2 or "Google inc.:google apis:16" Name:google APIs type:add-on     Vendor:google INC. Revision:3 description:android + Google APIs Based on Android 4.1.2 (API level) Libraries: * Com.goo Gle.android.media.effects (Effects.jar) Collection of video effects * Com.android.future.usb.accessory (USB. JAR) API for USB accessories * COM.GOOGLE.ANDROID.MAPS (Maps.jar) APIs for Google Maps SKINS:WV GA854, WQVGA400, WSVGA, wxga800-7in, WXGA720, HVGA, WQVGA432, WVGA800 (default), QVGA, WXGA800 abis:armeabi-v7a----------id:3 or "Android -17 "Name:android 4.2.2 type:platform API level:17 revision:2 SKINS:HVGA, QVGA, WQVGA400, WQVGA43 2, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, wxga800-7in abis:armeabi-v7a, MIPS, x86----------Id:4 or "G     Oogle inc.:google apis:17 "Name:google APIs type:add-on Vendor:google Inc. Revision:3 description:android + Google APIs Based on Android 4.2.2 (API level) Libraries: * Com.goo Gle.android.media.effects (Effects.jar) Collection of video effects * Com.android.future.usb.accessory (USB. JAR) API for USB accessories * COM.GOOGLE.ANDROID.MAPS (Maps.jar) APIs for Google Maps SKINS:WV GA854, WQVGA400, WSVGA, wxga800-7in, WXGA720, HVGA, WQVGA432, WVGA800 (default), QVGA, WXGA800 Abis:armeabi-v7apleas E input Target Id:1

When finished, the script creates a directory of "Skyline" in the root directory and generates an Android project file in this directory. The newly created project file has a HelloWorld project, and you can then jump straight to continue from "execute build_native.sh", and complete the migration of such a default project to detect the correctness of the entire configuration and operation process. This will continue the porting of your project.

Open the newly created project directory (we are Here "Skyline"), we can see "Classes" and "Resources" directory, the script will create the default HelloWorld code and resource files in both directories, we delete them all. Next, we copy our own project source code and resources into these two directories. In general, we copy the source code (. cpp and. h) to the "Classes" directory and copy the resource files (pictures, music, etc.) to the "Resources" directory. When the operation is complete, proceed to the next step.

Important! after the new project, Proj.android\jni\hellocpp, there is a main.cpp, which is the entrance to the whole project. A direct click will pop up "access Denied". If you want to see what's inside, modify its properties-security-advanced, set the permission to full control in the permission entry (double-click to modify). Main.cpp content, generally do not need to make changes.

3. Compiling

After doing this, we compile the project and build the. so file (the UNIX dynamic-link library, similar to the Windows. dll).

3.1 Modifying "Android.mk"

The "android.mk" file is located in the project's "Proj.android\jni" directory, which records information about the source files that the project contains. Use the editor (such as Ultra Editor) to open the file and modify it by referencing the file below. The contents of the/*...*/are the comments I added.

Local_path: = $ (call my-dir)/* Do not change */include $ (clear_vars)/* Do not change */local_module: = sky_shared//default is Game_sh Arred, if changes, please remember */local_module_filename: = Libsky/* Default is Libgame, if changes, please remember. */local_src_files: = hellocpp/main.cpp \. /.. /classes/appdelegate.cpp. /.. /classes/other source files. cpp/* Add all the source files on your project, the end of the ' \ ' is the connector, if the next line is still content, please do not miss. and "\" should be directly after the carriage */local_c_in Cludes: = $ (Local_path)/. /.. /classes $ (local_path)/. /.. /.. /cocos2dx/platform/third_party/win32 $ (local_path)/. /.. /.. /extensions/* Add the directory where the files are located in your project. The last two lines are added separately by the author because of the project, the previous one is the thread, the latter is the directory where the header file is introduced for the control class */local_whole_static_libraries: = C Ocos2dx_static cocosdenshion_static cocos_extension_static/* No changes below * * include $ (build_shared_library) $ (call               Import-module,cocosdenshion/android) $ (call IMPORT-MODULE,COCOS2DX) $ (call import-module,extensions)             

Important! Wrong point! I have encountered many problems in this step, so I also ask readers to pay attention to it.

(1) Make sure to remember the name you have modified after Local_module and Local_module_filename. For example, I changed them to skyline_shared and Libskyline. This is because the skyline will be used after the eclipse step is introduced. We need to have the "< your project name >.java" (such as "Skyline.java") in the project folder created by executing "create-android-project" in this file System.loadlibrary (" Game "" Game "change to their own modified name, in the author here is" Skyline ", otherwise is a black screen. It is estimated that the generated libskyline.so cannot be loaded (mentioned later). The author because of this problem tangled for a long time! Finally found the original just a string of things!! If you do not change the previous local_module and Local_module_filename, because the default is "game", then there is no problem.

(2) If you introduce threading and control classes in your project, refer to the author's local_c_includes in this code box. For threads, note that the file you introduced into the thread "#include ' pthread/pthread.h '" is changed to "#include ' pthread.h '" to make the compilation pass. (I do not know the reason, perhaps not so troublesome, but in the author's computer success.) Also want to understand the people tell ~)

3.2 Performing "build_native.sh"

The next step is to start the compilation. Go to the Android project path and use Cygwin to execute the "build_native.sh" script (Tip: You can drag the file directly into the Cygwin window), and if it goes well, we can find it in the Cygwin window output "Install:libsky.so = = libs/armeabi/libsky.so ", and find the compiled" libskyline.so "in the Libs/armeabi directory (note that this is the local_module_filename set up in the previous step).

Important! Wrong point! If you are prompted "please define Ndk_root", locate the Cygwin\home\xxx (your user name) directory. bash_profile file, open it with a text editor, and add the following two lines at the end:

Ndk_root=/cygdrive/d/coco/android-ndk-r9

Export Ndk_root

The error will no longer appear. D/COCO/ANDROID-NDK-R9 is your NDK decompression directory, the author of the original directory is D:\COCO\ANDROID-NDK-R9, make similar changes can be.

However, things are often not so smooth!

Important! Wrong point! It is very likely that you will see the Cygwin output a lot of clutter and stop halfway. This is the compile error. But do not worry, a little tune. Although the error, but the error point is still very accurate. So please look down from the top of the line. Found "error" In this line, this is the error point. Look closely at the contents of this line, it is very clear to tell you which file which line is wrong. At this time to find the wrong position, according to the information of the error, find some information, plus their own judgment and try to deal with the error. And then compile it again. If there is an error, continue processing. Don't look at a bunch of letters feeling irritable (looking like a garbled feeling), this time you need is slowly. And this error may come from the engine code itself!!

For example, I encountered such a few errors:

(1) An undeclared "itoa" is used. So I went to the classes directory under the Props_number_shower.cpp 27th line, did call Itoa this method. Although this is not a problem to run in vs2012, it is not supported here. So I looked for an alternative to the ITOA function source code, added to the source file, the compilation passed. User Han Xi (blog name) hint, this is a problem that contains files. Thank you for that!

(2) Format problem. The same idea, find Cocosdenshion/android/simpleaudioengine.cpp line 77th. LOGD (Devicemodel) () was found. On the internet to find out that it seems that Linux is more stringent format requirements, so I try to change this line to Logd ("%s", Devicemodel); In the process of compiling found a lot of similar errors, if you see the error message such as, to find the specified location modification.

(3) file stream. I have not done enough testing on whether C + + file stream can be compiled in Cygwin. But the author will fstream into C file operation when the error is not, if you encounter problems here, you can try to change. But for porting to Android, you should also pick up ccfileutils. Examples are as follows:

String Fullpath=ccfileutils::sharedfileutils ()->getwritablepath () + "rank.txt";   /* "Rank.txt" is your own use of the resource files, the other do not change. This line is used to get the file location */unsigned long file_size;                                                       /* Declare the variable, used in the next row, to receive the file content size */char *filedata= (char*) ccfileutils::sharedfileutils ()->getfiledata (Fullpath.c_str ( ), "R", &file_size); /* Get file pointer */if (filedata==null)                                                                                ... /* For the use of file pointers, as in C + + * *

If other errors are encountered, the reader can solve the problem by itself in accordance with the above problem solving method.

4. Import Eclipse

4.1 New Project

Open Eclipse, open "File"-"New"-"project"-"Android"-"Android project from Existing Code" project, click Browser in the interface that appears, then find the game project " Proj.android "and confirm that this is the new Android project from the original code.

4.2 Copying the necessary files

4.2.1 Copy the files under the cocos2d-2.1rc0-x-2.1.2 (root directory) \cocos2dx\platform\android\java\src to the sky (root directory of the game project) \PROJ.ANDROID\SRC, and manually import in eclipse

4.2.2 If Sky (game project Root) \proj.android\assets does not have a resource file, copy the Sky (game project Root) \resources all of your resource files to assets and import them manually in eclipse

4.3 Making the necessary changes

4.3.1 in the sky (game root) \src\< project name > in the Sky.java file, the line marked, please remember to LoadLibrary ("xxx"), the string changed to the previous modified Local_module_ The value of filename (remove Lib), as I changed to Sky. Watch out! This has been emphasized above, if the string here and Local_module_filename (remove Lib) mismatch, the result is very tragic ! Can't see the interface! I spent a lot of time here!

4.3.2 Other simple errors, such as the following: Drawable/icon cannot be found, a icon.png file can be added to the res in the Eclipse project. You can also rename the icon to another image file that exists in the project (typically a PNG file)

5. Commissioning

Virtual machine is very card, and does not seem to support OpenGL, so it is recommended to use the Real machine debugging (the use of the method is not detailed here, you can check the relevant information on the Internet). Debugging process is unavoidable problems, here will list the author in the debugging process encountered problems, to explore the solution of the idea.

5.1 File permissions issues. During debugging we see an error in the Logcat message window that means that a file has insufficient permissions. If this is the time for everyone to copy such a long list of things that look like directories to search, is not found. Because they are not directory names after all. This time, you can choose to search keywords. For example, in this picture, we think "shader" is the key word, sure enough in the Project Android Project found the "shaders" folder, the permissions of all the files are modified to "Full Control", found that debugging no longer error, you can think of the success of the modification!

5.2 File read/write issues

In a sense, the author does not want to call it a file read and write problem. Explanations will be made later.

If the reader remembers, read and write questions have been mentioned above. If you simply use C to read and write the file, the project will not find a resource file. So change it to the above-mentioned ccfileutils, which will solve this kind of problem.

String Fullpath=ccfileutils::sharedfileutils ()->getwritablepath () + "rank.txt";   /* "Rank.txt" is your own use of the resource files, the other do not change. This line is used to get the file location */unsigned long file_size;                                                       /* Declare the variable, used in the next row, to receive the file content size */char *filedata= (char*) ccfileutils::sharedfileutils ()->getfiledata (Fullpath.c_str ( ), "R", &file_size); /* Get file pointer */if (filedata==null) ...    

5.3 How to debug

The reason why readers are reluctant to call the "5.2 file read and write problem" a "file read and write problem" is that the way the reader uses it in solving such problems is actually telling us how to debug. After completing 5.1 that step, I found that the real machine still did not show any content. black screen! this time we are worried, has already shown helloword on the phone, why this project can not?! Seniors tip We are not the main menu loading is not successful, and know that we add an output to each of the key parts, through the console output to see which step out of the problem! For example, we added Cclog ("%s", "Init () Xxx;--from MainMenu") to each load operation in "Init ()" Of the main Menu "Mainmenu.cpp" ("XXX"), which was changed to a specific value to determine the location of the error. It is also through this approach that we have identified all the "file read and write problems". Therefore, if you encounter problems, please do not be discouraged, a little bit to judge the location of the error, and then modify, try.

6. Resolution adaptation

The last problem is resolution adaptation. After the online reading of the relevant information, finally solved the problem. is actually a line of code things.

In the AppDelegate.cpp bool Appdelegate::applicationdidfinishlaunching () method, add

Cceglview::sharedopenglview ()->setdesignresolutionsize (576,324,kresolutionnoborder) To resolve resolution adaptation issues.

Among them, 576,324 is the author in Win32 design game when the width and height, we have to set the width of the high can be. Kresolutionnoborder refers to one of the strategies used to stretch the screen, guaranteeing no boundaries after stretching, but the edge portion of the design-time interface may be obscured at the end. The author has found the following definition through VS2012 's "go To Definition" operation. The following is the full adaptation strategy.

Enum resolutionpolicy{    //The entire application is visible in the specified area without trying to preserve the Origi nal aspect ratio.    Distortion can occur, and the application may appear stretched or compressed.    Kresolutionexactfit,    //The entire application fills the specified area, without distortion but possibly with some CRO Pping,    //while maintaining the original aspect ratio of the application.    Kresolutionnoborder,    //The entire application is visible in the specified area without distortion while maintaining t He original    //aspect ratio of the application. Borders can appear on both sides of the application.    Kresolutionshowall,    Kresolutionunknown,};

Reference materials: The information is more fragmented, more online articles. Because the comparison Fanluan, did not record down. Also refer to the porting section of the Cocos2d-x Advanced Development tutorial.

Summarize:

The author and teammates in the entire transplant process is suffering, almost hopeless. But always believe that since others can transplant success, we can certainly! Finally we spent more than two days to finally let our game run successfully on the phone! The excitement, the thrill of that ledge is still unforgettable! So please cocos2d-x comrades do not be discouraged, the problem can be solved, 1.1 points, do not worry, give yourself confidence. First show the HelloWorld, and then the project slowly transferred out. Hold on, always succeed!

Here are our game <skyline beta1.0>, download it-support US!!!

Http://pan.baidu.com/s/13O4dT

Hopefully this article will help the friends who are troubled by the cocosd-x transplant Android process. If the article content and error or some part of the better solution, please leave a message. We very much hope to exchange experience with you! Thank you!

Reprint please specify the original address: http://www.cnblogs.com/Z-XML/p/3349518.html

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.