Cocos2d-x 3.2 Start-up process Analysis-Win32 and Android platform (ii)

Source: Internet
Author: User

The previous article tried to analyze the start-up process of Cocos2d-x 3.2 under the Win32 platform. Today we continue our quest to see how the engine launches under the Android platform.


1. A little understanding of JNI (Java calls C + + dynamic Library) technology

2. Get an overview of the Android app development process. (Not too much)

First, the entrance

Friends with Android development experience should be aware that the common Android application, the portal is application or its subclass type, and then the need for interface or not to choose to implement Activity or Service class. Cocos2d-x Although developed mainly in C + +, at this point there is nothing special. The difference is that in cocos2d-x, these classes are really just portals, the engine rendering process, and all the internal logic, are implemented in C + +. The C + + code that we write ourselves, together with the engine code, is compiled into a dynamic library, which is then invoked by the Java layer through JNI technology to get into the core of the engine. So the concept is rather vague, and then we analyze it concretely.

Second, the project catalogue

Obviously, this time we should focus on the Android-related parts of the project:

Enter the directory, which is a configuration file for some eclipse projects and some directories, where:

The SRC directory contains the Java source files.

The JNI directory holds the C + + source file and the NDK's compilation script (makefile).

SRC Directory

JNI Directory

Open Hellocpp/main.cpp, there is only one function:

 1:  void cocos_android_app_init (JNIEnv* env, jobject thiz) 
   2:  {
   3:      LOGD("cocos_android_app_init");
   4:      AppDelegate *pAppDelegate = new AppDelegate();
   5:  }


is not a bit strange, obviously called main.cpp, but there is no main function, this also forget, the function actually just defined a Appdelegate type object, but did not use it. Don't worry, we accept this reality for a while and turn our gaze to another aspect.

Go to the SRC directory, only one Org folder, open all the way, and finally find this file, which is the only source file under this directory. Open it:

   1:  package org.cocos2dx.cpp;
   3:  import org.cocos2dx.lib.Cocos2dxActivity;
   5:  public class AppActivity extends Cocos2dxActivity {
   6:  }

There is nothing in this, just define a appacvivity class as the interface entrance of the game (can refer to androidmanifest.xml), it seems that the real mystery is in its inherited cocos2dxactivity class.

So where is this class? Let's leave the directory and enter the COCOS2D directory of the same class, which is where the core code of the engine is stored. Enter the Cocos/cocos/platform, which puts the implementation of the Platform code in the engine.

.. /cocos2d/cocos/platform/

Enter Android/java/src/org/cocos2dx/lib:

It's all here! Look at what's inside of, first focus on onCreate ():

1: @Override
    2: protected void onCreate(final Bundle savedInstanceState) {
    3: super.onCreate(savedInstanceState);
    5: onLoadNativeLibraries(); //Load the dynamic library compiled by c++, here is
    7: sContext = this;
    8: this.mHandler = new Cocos2dxHandler(this); //Cocos2dxHandler inherits from Handler and handles some requests that display similar dialogs.
   10: Cocos2dxHelper.init(this); //Cocos2dxHelper tool class, handle some Activity lifecycle events and initiate some requests
   12: this.init(); // initialize itself
   13: if (mVideoHelper == null) {
   14: mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); //As the name suggests
   15: }
   16: }

As you can see, onCreate () Initializes a series of helper classes, as described in the comments, and we look at the init () function:

      1: public void init() {
   3: // Set the layout of the Activity to FrameLayout
   4: // FrameLayout
   5: ViewGroup.LayoutParams framelayout_params =
   6: new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
   7: ViewGroup.LayoutParams.MATCH_PARENT);
   8: mFrameLayout = new FrameLayout(this);
   9: mFrameLayout.setLayoutParams(framelayout_params);
  11: //It's a bit strange here. I don't know the role of Cocos2dxEditText here. I look forward to the answer.
  12: // Cocos2dxEditText layout
  13: ViewGroup.LayoutParams edittext_layout_params =
  14: new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  15: ViewGroup.LayoutParams.WRAP_CONTENT);
  16: Cocos2dxEditText edittext = new Cocos2dxEditText(this);
  17: edittext.setLayoutParams(edittext_layout_params);
  19: // ...add to FrameLayout
  20: mFrameLayout.addView(edittext);
  twenty one:   
  22: //Create a Cocos2dxGLSurfaceView
  23: // Cocos2dxGLSurfaceView
  24: this.mGLSurfaceView = this.onCreateView();
  26: //Add Cocos2dxGLSurfaceView to the layout
  27: // ...add to FrameLayout
  28: mFrameLayout.addView(this.mGLSurfaceView);
  30: //If it is an Android emulator, set some parameters of OpenglES
  31: // Switch to supported OpenGL (ARGB888) mode on emulator
  32: if (isAndroidEmulator())
  33: this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);
  35: //Set the Render of Cocos2dxGLSurfaceView
  36: this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
  37: this.mGLSurfaceView.setCocos2dxEditText(edittext);
  39: // Set framelayout as the content view
  40: setContentView(mFrameLayout);
  41: }
We know that cocos2d-x is rendered through OpenGL, and it can be concluded that these two sentences in the code play a more important role:

Enter Cocos2dxrenderer inside:

It's self-evident! It is here that the Java layer is connected to the native code (c + + code), and we see that many local methods are declared in the Cocos2dxrenderer type, in which Nativeinit () enters the C + + level and begins the initialization of a series of engines. So where is the Nativeinit () method? Go to the Cocos2d\cocos\platform\android directory and open the Javaactivity.cpp:

Do you think it's almost like the entrance to the Win32? The back is left to everyone to see it! You can refer to my previous article on the launch process under the Win32 platform.

Behind the relatively hasty writing, but also just learned to write a blog soon, if there are shortcomings, but also hope you haihan.

Reprint please specify.

Cocos2d-x 3.2 Start-up process Analysis-Win32 and Android platform (ii)

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: 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.