Cocos2dx-2.X front and back switch analysis, based on android platform

Source: Internet
Author: User

Cocos2dx-2.X front and back switch analysis, based on android platform

From the online android lifecycle diagram:

 

Cocos2dx-2.X before the background switch analysis, based on android platform: 1, from the background into the foreground project activity generally inherited from Cocos2dxActivity, have seen the activity life cycle know onCreate, onResume and other methods, these functions are the most important functions in the activity life cycle. You can view the relevant information when to call them. // The @ Overrideprotected void onResume () {super. onResume (); Log. d (TAG, "onResume ++"); Cocos2dxHelper. onResume (); this. mGLSurfaceView. onResume (); --- >>} this. mGLSurfaceView. onResume (); method --- >>@ Overridepublic void onResume () {super. onResume (); this. setRenderMode (RENDERMODE_CONTINUOUSLY); // use the queueEvent method: switch from the UI thread to the OpenGL rendering thread this. queueEvent (new Runnable () {@ Overridepubl Ic void run () {Cocos2dxGLSurfaceView. this. mCocos2dxRenderer. handleOnResume () ;}}) ;}------ >>> public void handleOnResume () {// private static native void nativeOnResume (); // call a native method, implement Cocos2dxRenderer on the C ++ side. nativeOnResume ();} // The implementation of C ++ in the Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp file: JNIEXPORT void JNICALL encode () {// mark (1) this is related to the one I want to talk about below. *** if (CCDir Ector: sharedDirector ()-> getOpenGLView () {CCApplication: sharedApplication ()-> applicationWillEnterForeground ();}} // this function will be called when the app is active again // enter the foreground, where we can do some processing void AppDelegate: applicationWillEnterForeground () {CCDirector: shareddire () -> startAnimation (); SimpleAudioEngine: sharedEngine ()-> resumeBackgroundMusic ();} 2. // The process of Calling @ Overrideprotected void onPa when you enter the background from the foreground Use () {super. onPause (); Log. d (TAG, "onPause ++"); Cocos2dxHelper. onPause (); this. mGLSurfaceView. onPause ();} // this. mGLSurfaceView. onPause (); --- >>@ Overridepublic void onPause () {this. queueEvent (new Runnable () {@ Overridepublic void run () {Cocos2dxGLSurfaceView. this. mCocos2dxRenderer. handleOnPause () ;}}); this. setRenderMode (RENDERMODE_WHEN_DIRTY); // super. onPause () ;}---- >>> mCocos2dxRend Erer. handleOnPause: public void handleOnPause () {Cocos2dxRenderer. nativeOnPause () ;}----- >>> native method, call the function of C ++: private static native void nativeOnPause (); // The implementation of C ++ In the examples file: JNIEXPORT void JNICALL publish () {// enter the background CCApplication: sharedApplication ()-> applicationDidEnterBackground (); CCNotificationCenter: sharedNotifi CationCenter ()-> postNotification (EVENT_COME_TO_BACKGROUND, NULL);} // This function will be called when the app is inactive. when comes a phone call, it's be invoked too // enter the background, we can do some processing here. Void AppDelegate: applicationDidEnterBackground () {CCDirector: sharedDirector ()-> stopAnimation (); SimpleAudioEngine: sharedEngine ()-> pauseBackgroundMusic ();} 3. I have some doubts (solved). From the activity lifecycle, we can see that the onResume method will also be called when I first enter the game. If so, we can't think that the time to call the applicationWillEnterForeground method is to enter the foreground from the background. If this is the case, we need to pay attention to this problem when processing the game to enter the foreground from the background. In fact, the applicationWillEnterForeground method in the game for the first time won't be called. We don't have to worry about the problem I mentioned above. As for why, the following analysis: 3.1. Add log logs to the onResume method in the activity and the onSurfaceCreated method in the Cocos2dxRenderer class to view the execution sequence of the following two places: @ Override // onSurfaceCreated is called when the surface is created. Here, the nativeInit method is called for initialization. // For detailed information about the cocos2dx startup process. Public void onSurfaceCreated (final GL10 pGL10, final EGLConfig pEGLConfig) {Log. d ("", "onSurfaceCreated ++"); Cocos2dxRenderer. nativeInit (this. mScreenWidth, this. mScreenHeight); this. mLastTickInNanoSeconds = System. nanoTime () ;}--- >>> void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit (JNIEnv * env, jobject thiz, jint w, jint h) {if (! CCDirector: sharedDirector ()-> getOpenGLView () {// create CCEGLView only here. The second mark (2) ***** CCEGLView * view = CCEGLView :: export dopenglview (); view-> setFrameSize (w, h); AppDelegate * pAppDelegate = new AppDelegate (); CCApplication: sharedApplication ()-> run ();} else {// actually I have a question here? This is when the branch will be executed. I tried it many times and did not see when to execute it. // You need to learn later. Repository (); CCShaderCache: sharedShaderCache ()-> reloaddefashadshaders (); ccDrawInit (); CCTextureCache: Repository (); ccicationicationcenter: sharedicationicationcenter ()-> postNotification (callback, NULL); CCDirector: sharedDirector ()-> setgldefavaluvalues ();} execution sequence of the onResume method in the activity and the onSurfaceCreated method in the Cocos2dxRenderer class: you can see the output information below to clearly understand that the onResume method is executed first while the onSurfaceCreated Method. 16:03:32 05-21. 520: D/Cocos2dxActivity (7953): onResume ++ 05-21 16:03:32. 740: D/(7953): onSurfaceCreated ++: Do we still remember the two marks (1) and (2) We have done? Mark (1): You should understand it here. Here we add a CCDirector ctor: sharedDirector ()-> getOpenGLView () to determine whether CCEGLView exists in applicationWillEnterForeground function, follow the instructions above to create an instance of the CCEGLView class in onSurfaceCreated --> response --> ---> CCEGLView * view = CCEGLView: Export dopenglview () in the nativeInit method, according to the execution sequence described above, the onResume method is executed before the onSurfaceCreated method, which means that it is executed before the nativeInit method. It also means that when you first enter the game, because CCDirector: sharedDirec The tor ()-> getOpenGLView () method returns NULL. The applicationWillEnterForeground method is not executed because it has not been instantiated. When the game switches from the background to the foreground, The CCDirector: shareddire()-> getOpenGLView () method returns the constructed instance variables, so you can enter the applicationWillEnterForeground function. JNIEXPORT void JNICALL listener () {// mark (1) Here it is related to what I want to say below *** if (CCDirector: shareddire () -> getOpenGLView () {CCApplication: sharedApplication ()-> applicationWillEnterForeground ();}}


 

Related Article

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.