Call Org.cocos2dx.cpp.AppActivity
Appactivity is a developer class (i.e. developer-defined class) located under Proj.android/src. It inherits from Org.cocos2dx.lib.Cocos2dxActivity, and it does not add any code after the project is built, purely a cocos2dxactivity and an activity.
Appactivity is called because it is configured in Androidmanifest.xml
<application android:label= "@string/app_name" android:icon= "@drawable/icon" > <activity android:name= "Org.cocos2dx.cpp.AppActivity" android:label= "@string/app_name" android:screenorientation= " Landscape " Android:theme= "@android: Style/theme.notitlebar.fullscreen" android:configchanges= "Orientation" > <!--tell nativeactivity the name---- <meta-data android:name= "Android.app.lib_name" android:value= "Cocos2dcpp"/> < intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application>
Load the libcocos2dcpp.so (or put the library under Proj.android/libs)
Libcocos2dcpp.so after compiling to Proj.android/libs/armeabi, from the above code can see the android:value= "Cocos2dcpp" line configuration content, it indicates the name of so.
The code to load so in Appactivity is as follows:
@Override protected void onCreate (Final Bundle savedinstancestate) { super.oncreate (savedinstancestate); try { applicationinfo ai = Getpackagemanager (). Getapplicationinfo (Getpackagename (), Packagemanager.get_meta_data); bundle Bundle = ai.metadata; try { string libname = bundle.getstring ("Android.app.lib_name"); system.loadlibrary (libname); } catch (Exception e) { & nbsp //error } } catch (Packagemanager.namenotfoundexception e) { //error } scontext = this; this.mhandler = new Cocos2dxhandler (this); this.init (); cocos2dxhelper.init (this); }
Call Cocos_android_app_init
The Cocos_android_app_init function in Proj.android/jni/hellocpp/main.cpp is called.
Let's look at This.init (); Follow code snippet:
public void init () {... this.mGLSurfaceView.setCocos2dxRenderer (new Cocos2dxrenderer ()); ... }
@Override public void onsurfacecreated (final GL10 pGL10, final EGLConfig peglconfig) {Cocos2dxrenderer.nativein It (This.mscreenwidth, this.mscreenheight); This.mlasttickinnanoseconds = System.nanotime (); }
Until now, the Java Code, Cocos2dxrenderer.nativeinit (This.mscreenwidth, This.mscreenheight) in the previous paragraph, is a JNI call, and Java calls C + + code.
Its code is in Cocos2d/cocos/2d/platform/android/javaactivity.cpp, as follows:
void Java_org_cocos2dx_lib_cocos2dxrenderer_nativeinit (jnienv* env, Jobject thiz, Jint W, Jint h) { & nbsp Auto Director = Cocos2d::D irector::getinstance (); Auto Glview = Director->getopenglview (); if (!glview) { Glview = Cocos2d::glview:: Create ("Android app"); Glview->setframesize (W, h); Director->setopenglview (glview); Cocos_ Android_app_init (env, thiz); cocos2d::application::getinstance () Run (); } else { Cocos2d::gl::invalidatestatecache (); Cocos2d::ShaderCache:: GetInstance ()->reloaddefaultshaders (); cocos2d::D rawpriMitives::init (); cocos2d::volatiletexturemgr::reloadalltextures (); cocos2d::eventcustom foregroundevent (event_come_to_foreground); Director->geteventdispatcher (->dispatchevent) (&foregroundevent); director->setgldefaultvalues (); }}
We see Cocos_android_app_init (env, Thiz), which is connected to the Cocos_android_app_init in Main.cpp.
Create Appdelegate
The appdelegate is created in Cocos_android_app_init, and the Cocos2d-x engine is the thing behind it.
void Cocos_android_app_init (jnienv* env, Jobject thiz) {logd ("cocos_android_app_init"); Appdelegate *pappdelegate = new Appdelegate ();}
A brief analysis on the starting call process of Cocos2d-x Android application