cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding

cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding,第1张

概述原文地址:http://www.cnblogs.com/kenkofox/p/3910462.html 参考地址:http://www.tairan.com/archives/4902 1 JS调用C++ 3.0中写这个绑定比较简单,跟ANE调用java如出一辙,一个JSContext,一个jsval,使用cocos2d提供的c++和js变量转换的函数做好转换即可。 cocos2d-js原来就定义

原文地址:http://www.cnblogs.com/kenkofox/p/3910462.HTML

参考地址:http://www.tairan.com/archives/4902

1 Js调用C++

3.0中写这个绑定比较简单,跟ANE调用java如出一辙,一个jscontext,一个Jsval,使用cocos2d提供的c++和Js变量转换的函数做好转换即可。

cocos2d-Js原来就定义好了代码风格:

    sc->addRegisterCallback(MinXmlhttpRequest::_Js_register);     sc->addRegisterCallback(register_Jsb_websocket);     sc->addRegisterCallback(register_Jsb_socketio);         #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)     sc->addRegisterCallback(JavaScriptJavaBrIDge::_Js_register);     #endif         sc->addRegisterCallback(register_Jsb_kenko_all);         sc->start();

我们也顺着这个风格,添加一个函数:register_Jsb_kenko_all,这是一个全局函数。

Jsb_kenko_auto.h#ifndef Jsb_Jsb_kenko_auto_h #define Jsb_Jsb_kenko_auto_h#include "cocos2d.h"std::string os_info(); bool Jsb_os_info(jscontext *cx,uint32_t argc,Js::Value *vp); bool Jsb_callback(jscontext *cx,255); line-height:1.5!important">voID register_Jsb_kenko_all(jscontext* cx,JsObject* obj);#endif
Jsb_kenko_auto.cpp#include Jsb_kenko_auto.h" #include cocos2d_specifics.hppstring os_info() {     cclOG(it's c++ os_info here");     return os_info"; }  vp) {     Jsval ret = std_string_to_Jsval(cx,os_info());     Js_SET_RVAL(cx,vp,ret);       return true; }   voID register_Jsb_kenko_all(jscontext *cx,JsObject *obj) {     Js_defineFunction(cx,obj,osInfo",Jsb_os_info,0,128); line-height:1.5!important">0);  //生成名为osInfo的Js全局函数
}

把h和cpp文件都放到AppDelegate.cpp同一个地方。上述的c++代码会在spIDermonkey运行环境中生成相应的Js接口,所以,我们不需要自己额外写对应的Js接口。

然后就可以写Js代码试试了。从运行结果可以看到,Js调用成功,并获取到返回值。

cc.game.onStart = function(){     cc.vIEw.setDesignResolutionSize(800,128); line-height:1.5!important">450,cc.ResolutionPolicy.SHOW_ALL);     cc.vIEw.resizeWithbrowserSize(true);     cc.director.runScene(new MainScene());         cc.log(Js get from c++: " + osInfo()); }; cc.game.run();

2 C++回调

关键在于使用Scriptingcore提供的方法,调用Js。首先来看看Scriptingcore的源代码,都有些什么方法可以用。

executeFunctionWithOwner可以实现类似cc.sprite之类的c++对象和Js对象的调用,没有深究。这里演示的是如何做全局调用。

evalString对任何一个前端开发来说都不会太陌生,毕竟这里不是浏览器,排除各种乱七八糟的安全问题,我们直接用这个函数。

/**      @brIEf Execute a scripted global function.      @brIEf The function should not take any parameters and should return an integer.      @param functionname String object holding the name of the function,in the global script environment,that is to be executed.      @return The integer value returned from the script function.      */     virtual int executeGlobalFunction(const char* functionname) { 0; }    int sendEvent(cocos2d::ScriptEvent* message) overrIDe;         bool parseConfig(ConfigType type,const std::string& str) overrIDe;    bool handleAssert(char *msg) { false; }    voID setCalledFromScript(bool callFromScript) { _callFromScript = callFromScript; };     bool isCalledFromScript() { return _callFromScript; };         bool executeFunctionWithObjectData(voID* nativeObj,255); line-height:1.5!important">char *name,JsObject *obj);     bool executeFunctionWithOwner(Jsval owner,uint32_t argc =  NulL);    voID executeJsFunctionWithThisObj(Jsval thisObj,Jsval callback,0); line-height:1.5!important">*      * will eval the specifIEd string      * @param string The string with the JavaScript code to be evaluated      * @param outVal The Jsval that will hold the return value of the evaluation.      * Can be NulL.      bool evalString(char *string,Jsval *outVal,255); line-height:1.5!important">char *filename = NulL,jscontext* cx = NulL,JsObject* global = NulL);

修改Jsb_kenko_auto.cpp:

#include "; }vp) {     cclOG(it's c++ testCallback here");     jscontext* jc = Scriptingcore::getInstance()->getGlobalContext();     // 注释部分适合有对象化的调用      参考:http://www.tairan.com/archives/4902     Jsval v[2];     v[0] = int32_to_Jsval(jc,32);     v[1] = int32_to_Jsval(jc,12);          通过 Scriptingcore 封装好的方法实现回调,可以帮助我们节省很多细节上的研究     Js_proxy_t * p = Jsb_get_native_proxy();     return Scriptingcore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JsVAL(p->obj),"cpp_callback",2,v);        2是参数个数,v是参数列表         找到一个更适合全局函数的方法     Jsval ret;     return Scriptingcore::getInstance()->evalString(cpp_callback(2,3)ret); }   0);      Js_defineFunction(cx,0); line-height:1.5!important">test_cpp_callback0);  }

相应在Js侧添加一个全局函数,给c++调用。

" + osInfo()); test_cpp_callback(); }; cc.game.run();function cpp_callback(a,b) { cc.log(cpp return two integer: " + a + " " + b); }

看输出结果:

  

3 各种变量转换函数

都在js_manual_conversions.h这里了,真是应有尽有。下边只列出一部分。

bool Jsval_to_ushort( jscontext *cx,Jsval vp,unsigned short *ret ); bool Jsval_to_int32( jscontext *cx,int32_t *ret ); bool Jsval_to_uint32( jscontext *cx,uint32_t *ret ); bool Jsval_to_uint16( jscontext *cx,uint16_t *ret ); bool Jsval_to_long( jscontext *cx,255); line-height:1.5!important">long *out); bool Jsval_to_ulong( jscontext *cx,255); line-height:1.5!important">bool Jsval_to_long_long(jscontext *cx,Jsval v,255); line-height:1.5!important">long long* ret); bool Jsval_to_std_string(jscontext *cx,std::string* ret);Jsval int32_to_Jsval( jscontext *cx,int32_t l); Jsval uint32_to_Jsval( jscontext *cx,uint32_t number ); Jsval ushort_to_Jsval( jscontext *cx,255); line-height:1.5!important">short number ); Jsval long_to_Jsval( jscontext *cx,255); line-height:1.5!important">long number ); Jsval ulong_to_Jsval(jscontext* cx,255); line-height:1.5!important">long v); Jsval long_long_to_Jsval(jscontext* cx,255); line-height:1.5!important">long v); Jsval std_string_to_Jsval(jscontext* cx,255); line-height:1.5!important">string& v);
总结

以上是内存溢出为你收集整理的cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding全部内容,希望文章能够帮你解决cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046985.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存