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.



Pre-Knowledge:



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 Proj.android 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 Appactivity.java this file, which is the only source file under this directory. Open it:


   1:  package org.cocos2dx.cpp;
   2:   
   3:  import org.cocos2dx.lib.Cocos2dxActivity;
   4:   
   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 Proj.android 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 Cocos2dxactivity.java, first focus on onCreate ():


1: @Override
    2: protected void onCreate(final Bundle savedInstanceState) {
    3: super.onCreate(savedInstanceState);
    4:
    5: onLoadNativeLibraries(); //Load the dynamic library compiled by c++, here is libcocos2dcpp.so
    6:
    7: sContext = this;
    8: this.mHandler = new Cocos2dxHandler(this); //Cocos2dxHandler inherits from Handler and handles some requests that display similar dialogs.
    9:          
   10: Cocos2dxHelper.init(this); //Cocos2dxHelper tool class, handle some Activity lifecycle events and initiate some requests
   11:
   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() {
   2:          
   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);
  10:
  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);
  18:
  19: // ...add to FrameLayout
  20: mFrameLayout.addView(edittext);
  twenty one:   
  22: //Create a Cocos2dxGLSurfaceView
  23: // Cocos2dxGLSurfaceView
  24: this.mGLSurfaceView = this.onCreateView();
  25:
  26: //Add Cocos2dxGLSurfaceView to the layout
  27: // ...add to FrameLayout
  28: mFrameLayout.addView(this.mGLSurfaceView);
  29:
  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);
  34:
  35: //Set the Render of Cocos2dxGLSurfaceView
  36: this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
  37: this.mGLSurfaceView.setCocos2dxEditText(edittext);
  38:
  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: 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.