Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址: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所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:
猜你在找的Cocos2d-x相关文章