1. java entry HelloCpp. java HelloCpp class is very simple, because it inherits the parent class Cocos2dxActivity to take all internal operations and establish the relationship with various JNI interfaces, the displayed work is also completed through the OnCreate function of Cocos2dxActivity. For details, refer to its implementation. System. loadLibrary ("hellocpp"); this statement is loaded into the JNI library of the project, and the JNI_OnLoad function interface in JNI is executed. [Java] import org. cocos2dx. lib. cocos2dxActivity; import android. OS. bundle; public class HelloCpp extends Cocos2dxActivity {protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} static {System. loadLibrary ("hellocpp") ;}} in addition to the features described above, if your published application is charged, you need to add a verification process in this type, for more information, see the Android development documentation. 2. main. cpp main. the cpp path is jni/hellocpp/main. cpp: JNI_OnLoad will be called when loading data to the database. The important thing it does is to save the javaVM value, with this value, you can go to the valid JNIEnv environment variable anywhere in the JNI space. On the contrary, it is dangerous to try to save JNIEnv in future calls. [Cpp] jint JNI_OnLoad (JavaVM * vm, void * reserved) {JniHelper: setJavaVM (vm); return JNI_VERSION_1_4;} another task that must be done is to implement virtual function worker, obviously the Cocos2dxActivity mentioned above will call this function. [Cpp] void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit (JNIEnv * env, jobject thiz, jint w, jint h) {if (! CCDirector: sharedDirector ()-> getOpenGLView () {CCEGLView * view = CCEGLView: Export dopenglview (); view-> setFrameSize (w, h ); appDelegate * pAppDelegate = new AppDelegate (); CCApplication: sharedApplication ()-> run () ;}} the function first checks whether OpenGL View can be obtained normally, for mobile phones with lower Android API versions, this function may fail. In this case, a black screen is displayed on the simulator. You can modify it to display a prompt on the upper layer, so that users do not think the program is faulty. The following is the most important thing: Create your own Application representative (Application Delegate) instance. In this instance, you need to implement initialization, message response functions, and other interfaces. CCApplication: sharedApplication ()-> run (); will help you run the program. The CCApplication class is very simple and somewhat strange. The CCApplicationProtocol type inherited by CCApplication is a pure Virtual Interface Definition. This parent class is designed for cross-platform applications. CCApplication has a static function sharedApplication (), which always returns the this pointer for the last time this type of object was created. This means that cocos2dx wants to give the pointer of this object to the developer after creating this object, and then the developer calls the run member function to run the program. [Cpp] int CCApplication: run () {// Initialize instance and cocos2d. if (! ApplicationDidFinishLaunching () {return 0;} return-1;} you have reason to believe that the pointer you obtained actually points to a CCApplication subclass to complete the actual message processing cycle. Now, we will study the AppDelegate type. 3. The AppDelegate type is defined in jni/Classes/AppDelegate. h and implemented in jni/Classes/AppDelegate. cpp. Let's take a look at the following definition and find that the CCApplication in the previous section actually becomes the parent class of AppDelegate. You need to implement three interface functions, such as applicationDidFinishLaunching. The call relationship in the run function in the previous section is solved here. (I want to see if cocos2dx can be more elegant as the MFC interface and completely hide the interface call process .) [Cpp] class AppDelegate: private cocos2d: CCApplication {public: AppDelegate (); virtual ~ AppDelegate ();/** @ brief implements CCDirector and CCScene initialization code @ return true. The app continues. @ return false initialization fails and the app ends. */virtual bool applicationDidFinishLaunching ();/** @ brief: when the application enters the background, execute this function @ param the pointer of the application ?? Why can't I see the parameter */virtual void applicationDidEnterBackground (); /** @ brief when the application is about to enter the background to execute this function @ param the pointer of the application */virtual void applicationWillEnterForeground () ;}; the IMPLEMENTATION of the AppDelegate interface function is like a template, when you build your own applications, you can use them. [Cpp] bool AppDelegate: Pull () {// initialize director CCDirector * pDirector = CCDirector: sharedDirector (); CCEGLView * pEGLView = CCEGLView: Export dopenglview (); pDirector-> setOpenGLView (pEGLView); // Set the design resolution pEGLView-> setDesignResolutionSize (designResolutionSize. width, designResolutionSize. height, kResolutionNoBorder); CCSize frameSize = pEGLView-> getFra MeSize (); vector <string> searchPath; // In this demo, we select resource according to the frame's height. // If the resource size is different from design resolution size, you need to set contentScaleFactor. // We use the ratio of resource's height to the height of design resolution, // this can make sure that the resource's height cocould fit for the height of design resolution. // if the frame's h Eight is larger than the height of medium resource size, select large resource. if (frameSize. height> mediumResource. size. height) {searchPath. push_back (largeResource. directory); pDirector-> setContentScaleFactor (MIN (largeResource. size. height/designResolutionSize. height, largeResource. size. width/designResolutionSize. width);} // if the frame's height is larger than the height of small resource si Ze, select medium resource. else if (frameSize. height> smallResource. size. height) {searchPath. push_back (mediumResource. directory); pDirector-> setContentScaleFactor (MIN (mediumResource. size. height/designResolutionSize. height, mediumResource. size. width/designResolutionSize. width);} // if the frame's height is smaller than the height of medium resource size, select small resource. else {searchPat H. push_back (smallResource. directory); pDirector-> setContentScaleFactor (MIN (smallResource. size. height/designResolutionSize. height, smallResource. size. width/designResolutionSize. width);} // set searching path CCFileUtils: sharedFileUtils ()-> setSearchPaths (searchPath); // turn on display FPS pDirector-> setDisplayStats (true ); // set FPS. the default value is 1.0/60 if you don't call this pDirector-> SetAnimationInterval (1.0/60); // create a scene. it's an autorelease object CCScene * pScene = HelloWorld: scene (); // run pDirector-> runWithScene (pScene); return true ;} // This function will be called when the app is inactive. when comes a phone call, it's be invoked too void AppDelegate: applicationDidEnterBackground () {CCDirector: sharedDirector ()-> stopAnimation (); // if you use SimpleAudioEn Gine, it must be pause // SimpleAudioEngine: sharedEngine ()-> pauseBackgroundMusic () ;}// this function will be called when the app is active again void AppDelegate :: applicationWillEnterForeground () {CCDirector: sharedDirector ()-> startAnimation (); // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine: sharedEngine () -> resumeBackgroundMusic ();} among them, CCScene * pScene = HelloW is important. Orld: scene (); this statement, because it involves another custom type HelloWorld. 4. The HelloWorld type is defined in jni/Classes/HelloWorldScene. h and implemented in jni/Classes/HelloWorldScene. cpp. [Cpp] class HelloWorld: public cocos2d: CCLayer {public: // Here's a difference. method 'init 'in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init (); // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d: CCScene * scene (); // a selector callback void menuCloseCallback (CCObject * pSender); // implement the "static node () "Method manually CREATE_FUNC (HelloWorld) ;}; CREATE_FUNC (HelloWorld) after expansion, the Code is as follows. The basic meaning is to define the static function create () to create and initialize a HelloWorld class object. [Cpp] static HelloWorld * create () \ {\ HelloWorld * pRet = new HelloWorld (); \ if (pRet & pRet-> init ()) \{\ pRet-> autorelease (); \ return pRet ;\}\ else \{\ delete pRet; \ pRet = NULL; \ return NULL; \}\} in another static function sence, A CCScene object and HelloWorld object will be created, and the HelloWorld object will be combined into the CCScene object, which is also the cocos2dx internal operation object.