【第22期】观点:IT 行业加班,到底有没有价值?

cocos2d中间层创建(一)

原创 2016年08月30日 10:30:04

首先编辑jsb中间层,主要是在AppDelegate中进行注册单例,在js中进行调用

jsb_lcm_api.h:

//
//  jsb_lcm_api.h
//  MoonWarriors
//
//  Created by xiaohei on 2/21/17.
//
//

#ifndef __LCM__jsb_lcm_api__
#define __LCM__jsb_lcm_api__

#include "jsapi.h"
#include "jsfriendapi.h"
#include "js_bindings_lcm_api.h"

extern JSClass  *js_cocos2dx_LCMAPI_class;
extern JSObject *js_cocos2dx_LCMAPI_prototype;
///
/// @brief LCM API
///
void js_register_LCMAPI(JSContext* cx, JSObject* global );

void register_all_LCMAPI(JSContext* cx, JSObject* obj);

#endif /* defined(__LCM__jsb_lcm_api__) */


jsb_lcm_api.cpp:

//
//  jsb_lcm_api.cpp
//  MoonWarriors
//
//  Created by xiaohei on 2/21/17.
//
//

#include "jsb_lcm_api.h"

///
/// @brief  JS Internal Header Files
///
#include "jsapi.h"
#include "ScriptingCore.h"
#include "cocos2d_specifics.hpp"
#include "cocos2d.h"
#include "AppDelegate.h"

static bool empty_constructor(JSContext *cx, uint32_t argc, jsval *vp) {
	return false;
}

JSClass  *js_cocos2dx_LCMAPI_class;
JSObject *js_cocos2dx_LCMAPI_prototype;
extern JSObject *jsb_cocos2d_ActionInterval_prototype;

bool js_cocos2dx_extension_LCMAPI_getLTotalBalance(JSContext *cx, uint32_t argc, jsval *vp)
{
    jsval *argv = JS_ARGV(cx, vp);
    
    JSObject *obj = JS_THIS_OBJECT(cx, vp);
    js_proxy_t *proxy = jsb_get_js_proxy(obj);
    LCMJSB::LCMAPI* cobj = (LCMJSB::LCMAPI *)(proxy ? proxy->ptr : NULL);
    JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
    
    if (argc == 0)
    {
        std::string ret = cobj->getLTotalBalance();
        jsval jsret = std_string_to_jsval(cx, ret);
        JS_SET_RVAL(cx, vp, jsret);
        return true;
    }
    
    JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0);
    return false;
}

bool js_cocos2dx_extension_LCMAPI_lcmNotifyEveryday(JSContext *cx, uint32_t argc, jsval *vp)
{
    jsval *argv = JS_ARGV(cx, vp);
    
    JSObject *obj = JS_THIS_OBJECT(cx, vp);
    js_proxy_t *proxy = jsb_get_js_proxy(obj);
    LCMJSB::LCMAPI* cobj = (LCMJSB::LCMAPI *)(proxy ? proxy->ptr : NULL);
    JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
    
    if (argc == 6)
    {
        int hour = JSVAL_TO_INT(argv[0]);
        int min = JSVAL_TO_INT(argv[1]);
        JSString *str = JSVAL_TO_STRING(argv[2]);
        JSStringWrapper strWrap(str);
        std::string title = strWrap.get();
        JSString *str2 = JSVAL_TO_STRING(argv[3]);
        JSStringWrapper strWrap2(str2);
        std::string txt = strWrap2.get();
        JSString *str3 = JSVAL_TO_STRING(argv[4]);
        JSStringWrapper strWrap3(str3);
        std::string icon = strWrap3.get();
        int code = JSVAL_TO_INT(argv[5]);
        cobj->lcmNotifyEveryday(hour, min, title, txt, icon, code);
        JS_SET_RVAL(cx, vp, JSVAL_VOID);
        return true;
    }
    
    JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 6);
    return false;
}

void js_register_LCMAPI(JSContext *cx, JSObject *global) {
    
    js_cocos2dx_LCMAPI_class              = (JSClass *)calloc(1, sizeof(JSClass));
    js_cocos2dx_LCMAPI_class->name        = "LCMAPI";
    js_cocos2dx_LCMAPI_class->addProperty = JS_PropertyStub;
    js_cocos2dx_LCMAPI_class->delProperty = JS_DeletePropertyStub;
    js_cocos2dx_LCMAPI_class->getProperty = JS_PropertyStub;
    js_cocos2dx_LCMAPI_class->setProperty = JS_StrictPropertyStub;
    js_cocos2dx_LCMAPI_class->enumerate   = JS_EnumerateStub;
    js_cocos2dx_LCMAPI_class->resolve     = JS_ResolveStub;
    js_cocos2dx_LCMAPI_class->convert     = JS_ConvertStub;
    js_cocos2dx_LCMAPI_class->finalize    = js_cocos2dx_LCMAPI_finalize;
    js_cocos2dx_LCMAPI_class->flags       = JSCLASS_HAS_RESERVED_SLOTS(4);
    
    static JSPropertySpec properties[] = {
		{0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER}
    };
    
    static JSFunctionSpec funcs[] = {
        JS_FN("continueProcessing",js_cocos2dx_extension_LCMAPI_continueProcessing, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("LUpdateBalance",js_cocos2dx_extension_LCMAPI_LUpdateBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("checkBalanceEnoughForPay",js_cocos2dx_extension_LCMAPI_checkBalanceEnoughForPay, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("setLExtraData",js_cocos2dx_extension_LCMAPI_setLExtraData, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLAccessToken",js_cocos2dx_extension_LCMAPI_getLAccessToken, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLUserStringInfo",js_cocos2dx_extension_LCMAPI_getLUserStringInfo, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLIdFromUser",js_cocos2dx_extension_LCMAPI_getLIdFromUser, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLTotalBalance",js_cocos2dx_extension_LCMAPI_getLTotalBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLPaidBalance",js_cocos2dx_extension_LCMAPI_getLPaidBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLFreeBalance",js_cocos2dx_extension_LCMAPI_getLFreeBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("getLIsShowBalance",js_cocos2dx_extension_LCMAPI_getLIsShowBalance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("setLMenuBarVisible",js_cocos2dx_extension_LCMAPI_setLMenuBarVisible, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("showLCustomerService",js_cocos2dx_extension_LCMAPI_showLCustomerService, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("lcmLogout",js_cocos2dx_extension_LCMAPI_lcmLogout, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("lcmLogin",js_cocos2dx_extension_LCMAPI_lcmLogin, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("lcmNotifyClean",js_cocos2dx_extension_LCMAPI_lcmNotifyClean, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FN("lcmNotifyEveryday",js_cocos2dx_extension_LCMAPI_lcmNotifyEveryday, 6, JSPROP_PERMANENT | JSPROP_ENUMERATE),
        JS_FS_END
    };
    
	static JSFunctionSpec st_funcs[] = {
		JS_FN("getInstance", js_cocos2dx_extension_LCMAPI_getInstance, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
		JS_FS_END
	};
    
    js_cocos2dx_LCMAPI_prototype = JS_InitClass(
                                                   cx, global,
                                                   NULL,
                                                   js_cocos2dx_LCMAPI_class,
                                                   empty_constructor, 0, // constructor
                                                   properties,
                                                   funcs,
                                                   NULL, // no static properties
                                                   st_funcs);
    
			TypeTest<LCMJSB::LCMAPI> t;
			js_type_class_t *p;
			std::string typeName = t.s_name();
			if (_js_global_type_map.find(typeName) == _js_global_type_map.end())
			{
				p = (js_type_class_t *)malloc(sizeof(js_type_class_t));
				p->jsclass = js_cocos2dx_LCMAPI_class;
				p->proto = js_cocos2dx_LCMAPI_prototype;
				p->parentProto = jsb_cocos2d_ActionInterval_prototype;
				_js_global_type_map.insert(std::make_pair(typeName, p));
			}

}

void register_all_LCMAPI(JSContext* cx, JSObject* obj)
{
	JS::RootedValue nsval(cx);
	JS::RootedObject ns(cx);
	
	JS_GetProperty(cx, obj, "LCMJSB", &nsval);
	if (nsval == JSVAL_VOID) {
		ns = JS_NewObject(cx, NULL, NULL, NULL);
		nsval = OBJECT_TO_JSVAL(ns);
		JS_SetProperty(cx, obj, "LCMJSB", nsval);
	} else {
		JS_ValueToObject(cx, nsval, &ns);
	}
	obj = ns;
	
	js_register_LCMAPI(cx, obj);
}

AppDelegate.cpp 中进行注册:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "../SDK/lcm9.3/LCMJS/jsb_lcm_api.h"
#include "../SDK/lcm9.3/LCMJS/js_bindings_lcm_api.h"
#endif

ScriptingCore* sc = ScriptingCore::getInstance();
    
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
    //lcm
    sc->addRegisterCallback(register_all_LCMAPI);
#endif


js中进行调用:

if (g_targetOS != TargetOS.WINDOWS && g_targetOS != TargetOS.MACOS && MobageTool.isAvailable()) {
    LCMJSB.LCMAPI.getInstance().lcmNotifyClean();
    LCMJSB.LCMAPI.getInstance().lcmNotifyEveryday(12,0,__getClientMsg(371),client_msg_hash[354].n,"icon",1);
    LCMJSB.LCMAPI.getInstance().lcmNotifyEveryday(18,0,__getClientMsg(371),client_msg_hash[356].n,"icon",2);
}




版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

IOS 内存优化和调试技巧(基于cocos2d游戏)

前言 : 万年没有来csdn了,原因嘛很简单:最近半年差不多都在忙公司的游戏了,不过庆幸的是游戏快上线了。到时候在发一个链接上来求关注!废话不多说,来看看这篇文章了,聊聊游戏后期内存优化和调试的问题 ...

cocos2d初始化

首先,winmain创建AppDelegate,AppDelegate继承于CCApplication,其构造函数获取应用程序句柄,以及设置动画。紧接着,创建单例CCApplication,接着cocos2d::CCApplication::sharedApplication().run();。 在里面,执行PVRFrameEnableControlWindow,这儿是设置与获取注册表里的值,具体没怎么细看,但把参数设为true,出现一个对话框,暂且不管。接着QueryPerformanceFrequency(&nFreq);QueryPerformanceCounte r (&n

cocos2d中间层调用Android(三)

ios通过.cpp文件进行调用AppActivity.java的方法 js_bindings_lcm_api.cpp: // // js_bindings_lcm_api.cpp // Moo...

2014年中国开源软件前100名

不知道从什么时候开始,很多一说起国产好像就非常愤慨,其实大可不必。做开源中国六年有余,这六年时间国内的开源蓬勃发展,从一开始的使用到贡献,到推出自己很多的开源软件,而且还有很多软件被国外的认可。中国是开源不可忽视的力量。 而我们这个榜单也是从这些国人开发、主要参与开发或者基于国外开源软件进行改进并形成独立版本的软件中,根据该软件的访问、收藏、下载等多个角度, 从而得出前 100 名最受欢迎的开源软件。我们所

cocos2d中间层调用IOS(二)

ios通过.mm文件进行调用AppController.mm的方法 js_bindings_lcm_api.h: // // js_bindings_lcm_api.h // MoonWa...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)