Cocos2d-x 2.2.6 in C + + via JNI and Java intermodulation

Source: Internet
Author: User

1 /****************************************************************************2 Copyright (c) 2010-2012 cocos2d-x.org3 4 http://www.cocos2d-x.org5 6 Permission is hereby granted, free of charge, to all person obtaining a copy7 of this software and associated documentation files (the "Software"), to deal8 in the software without restriction, including without limitation the rights9 To use , copy, modify, merge, publish, distribute, sublicense, and/or sellTen copies of the software, and to permit persons to whom the software is One furnished to does so, subject to the following conditions: A  - The above copyright notice and this permission notice shall is included in - All copies or substantial portions of the software. the  - The software is provided ' as is ', without WARRANTY of any KIND, EXPRESS OR - implied, including but isn't LIMITED to the warranties of merchantability, - FITNESS for A particular PURPOSE and noninfringement. In NO EVENT shall the + AUTHORS or COPYRIGHT holders be liable for any CLAIM, damages or other - liability, WHETHER in a ACTION of contract, TORT OR OTHERWISE, arising from, + Out of OR-CONNECTION with the software or the use or other dealings in A The software. at ****************************************************************************/ -  PackageOrg.cocos2dx.hellocpp; -  - Importorg.cocos2dx.lib.Cocos2dxActivity; -  -  in; - ImportAndroid.content.DialogInterface; to ImportAndroid.os.Bundle; + ImportAndroid.os.Handler; - ImportAndroid.os.Message; the  *  Public classHellocppextendscocos2dxactivity{ $     Static Panax Notoginseng     { -System.loadlibrary ("Hellocpp"); the     } +     //Java calls C + + A      Public Static native voidJavatoc (); the      +      -      $     Private StaticAlertdialog Mdialog =NULL; $      -      -     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); -Mdialog =NewAlertdialog.builder ( This). Create ();WuyiMdialog.setbutton ("OK",NewAlertdialog.onclicklistener () { the @Override -                  Public voidOnClick (Dialoginterface Dialog,intwhich) { WuHellocpp. This. Finish (); -                 } About             }); $              -Mdialog.setbutton2 ("Cancel",NewAlertdialog.onclicklistener () { - @Override -                  Public voidOnClick (Dialoginterface Dialog,intwhich) { A                 } +             }); theMdialog.setbutton3 ("Helloc",NewAlertdialog.onclicklistener () -             { $@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { the                      //Java calls C code theHellocpp. This. Javatoc (); the                 } the             }); -             in     } the      the  About    //C + + will pass in string[] and call C + + This method to call the following handler method the      Public Static voidSayhelloctojava (string[] str) { theMessage mes =NewMessage (); theMes.obj =str; + mhandler.sendmessage (MES); -     } the     Bayi     Private StaticHandler Mhandler =NewHandler (NewHandler.callback () the     { the @Override -          Public Booleanhandlemessage (Message msg) { -string[] str =(string[]) msg.obj; theMdialog.settitle (str[0]); theMdialog.setmessage (str[1]); the (); the         return true; -         } the     }); the}


#include"AppDelegate.h"#include"Platform/android/jni/jnihelper.h"#include<jni.h>#include<android/log.h>#include"cocos2d.h"#defineLog_tag "Main"#defineLOGD (...) __android_log_print (android_log_debug,log_tag,__va_args__)using namespacecocos2d;extern "C"{jint jni_onload (JAVAVM*VM,void*reserved)    {JNIHELPER::SETJAVAVM (VM); returnjni_version_1_4;}//Java native methods for invoking C + +/*naming rules: Java_java's Package Name _ Class name*/voidJava_org_cocos2dx_hellocpp_hellocpp_javatoc () {Cclog ("Hello Java, i ' M C");}voidJava_org_cocos2dx_lib_cocos2dxrenderer_nativeinit (jnienv*env, Jobject thiz, Jint W, Jint h) {    if(! Ccdirector::shareddirector ()Getopenglview ()) {Cceglview*view =Cceglview::sharedopenglview (); View-setframesize (W, h); Appdelegate*pappdelegate =Newappdelegate (); Ccapplication::sharedapplication ()-run (); }    Else{Ccglinvalidatestatecache (); Ccshadercache::sharedshadercache ()-reloaddefaultshaders ();        Ccdrawinit ();        Cctexturecache::reloadalltextures (); Ccnotificationcenter::sharednotificationcenter ()-postnotification (Event_come_to_foreground, NULL); Ccdirector::shareddirector ()-setgldefaultvalues (); }#if(Cc_target_platform = = cc_platform_android)//typedef struct JNIMETHODINFO_//    {//jnienv * ENV;//Jclass ClassID;//Jmethodid Methodid;//} jnimethodinfo; //C + + calls the Sayhelloctojava () method in the Java package Org/cocos2dx/hellocpp/hellocpp classJnimethodinfo Info; if(Jnihelper::getstaticmethodinfo (Info,"Org/cocos2dx/hellocpp/hellocpp","Sayhelloctojava","([ljava/lang/string;) V") {jclass str_cls= Info.env->findclass ("java/lang/string"); Jstring str1= Info.env->newstringutf ("I ' m a titile"); Jstring str2= Info.env->newstringutf ("This is the info"); Jobjectarray Arrs= Info.env->newobjectarray (2, Str_cls,0); Info.env->setobjectarrayelement (Arrs,0, STR1); Info.env->setobjectarrayelement (Arrs,1, STR2); Info.env-Callstaticvoidmethod (Info.classid, Info.methodid, Arrs); }   #endif}}

While the background will print



Cocos2d-x 2.2.6 in C + + via JNI and Java intermodulation

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