Cocos2d-Lua(Quick-Cocos2d-x)集成第三方SDK(二)

上一篇文章中,我们介绍了,怎么集成友盟的Cocos2d-x版本SDK,接下来我们来说下怎么将友盟的C++接口导出到Lua中使用。

引擎版本: Quick-Cocos2d-x 3.3 开发系统: Windows 7 64bit

编写.tolua文件

我们打开libMobClickCpp\include文件夹可以看到里面有两个文件,分别是:

1
2
MobClickCpp.h
MobClickJniHelper.h

打开这两个文件看他们的内容可以发现,MobClickJniHelper.h的内容中只有一个

1
static void setJavaVM(JavaVM *javaVM);

而在MobClickCpp.h中包含了我们基本上能使用的所有的umeng的接口,所以我们需要导出的接口其实就是MobClickCpp中的接口。

新建一个MobClickLua.tolua文件,将下面的内容复制进去,我们再来详细分析下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
$#include "MobClickCpp.h"
 
$ using namespace umeng;
 
class MobClickCpp {
public :
     static void setLogEnabled( bool value);
 
     static void setProxy( const char * host, int port);
 
     static void setSessionIdleLimit( int seconds);
 
     static void startWithAppkey( const char * appKey, const char * channelId = NULL);
 
     static void end();
 
     static void mainloop( float dt);
 
     static void event( const char * eventId, const char * label = NULL);
 
     static void beginLogPageView( const char *pageName);
     static void endLogPageView( const char *pageName);
 
     static void setUserLevel( const char *level);
 
     enum Sex{
         Unkonwn = 0,
         Male = 1,
         Female = 2,
     };
 
     static void setUserInfo( const char * userId, Sex sex, int age, const char * platform);
 
     static void startLevel( const char * level);
 
     static void finishLevel( const char * level);
 
     static void failLevel( const char * level);
 
     static void pay( double cash, int source, double coin);
 
     static void pay( double cash, int source, const char * item, int amount, double price);
 
     static void buy( const char *item, int amount, double price);
 
     static void use( const char * item, int amount, double price);
 
     static void bonus( double coin, int source);
 
     static void bonus( const char * item, int amount, double price, int source);
};

我们可以看到上面的内容基本上都是MobClickCpp.h文件中的接口。我们现在来详细说下这几个部分的意思。

第一部分的代码是:

1
$#include "MobClickCpp.h"

这里的代码是引用代码,需要包含的头文件可以填到这里。

第二部分是这一句:

1
$ using namespace umeng;

我们打开MobClickCpp.h的内容可以看到里面定义了一个命名空间为

1
2
3
namespace umeng{
 
}

为了能在转出来的.cpp文件中使用umeng的接口,我们需要把命名空间引用过去。所以这里加了这一句话方便在.cpp文件中使用umeng命名空间。

第三部分就是我们具体的类的定义了,这里最好是直接拷贝.h文件中的类的声明。当然,你想自己定义一个类来作为中间层,也可以声明为自定义的层。这个时候记得要修改第一部分和第二部分的代码。

将.tolua文件转为luabinding c++代码

在Quick-Cocos2d-x 3.3中自带了tolua++工具,位置在[QUICK_V3_ROOT]/quick/bin下面的win32或者是mac下,我们最好将它们都添加到系统的PATH环境变量中,方便下次使用。

在命令行下直接输入:

1
tolua++

可以看到tolua++的帮助信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
usage: tolua++ [options] input_file
 
Command line options are:
   -v       : print version information.
   -o  file : set output file; default is stdout.
   -H  file : create include file.
   -n  name : set package name; default is input file root name.
   -p       : parse only.
   -P       : parse and print structure information ( for debug).
   -S       : disable support for c++ strings.
   -1       : substract 1 to operator[] index ( for compatibility with tolua5).
   -L  file : run lua file (with dofile()) before doing anything.
   -D       : disable automatic exporting of destructors for classes that have
              constructors ( for compatibility with tolua5)
   -W       : disable warnings for unsupported features ( for compatibility
              with tolua5)
   -C       : disable cleanup of included lua code ( for easier debugging)
   -E  value[=value] : add extra values to the luastate
   -t       : export a list of types asociates with the C++ typeid name
   -q       : don't print warnings to the console
   -h       : print this message.
Should the input file be omitted, stdin is assumed;
in that case , the package name must be explicitly set.
 
aaaaaa

了解上面这些信息后,我们执行命令:

1
tolua++  -o lua_binding_MobClickCpp.cpp -H lua_binding_MobClickCpp.h MobClickCpp.tolua

执行完成后将会生成两个文件:

1
2
lua_binding_MobClickCpp.cpp
lua_binding_MobClickCpp.h

生成的两个文件就是我们将.tolua的描述信息转为lua_binding的接口之后的.cpp和.h文件,这两个文件既可以放到引擎的目录下也可以放到工程目录下,这里为了描述方面我就把它们放到工程目录下了。

添加C++代码到工程中

拷贝上面的两个文件到工程下的umeng\frameworks\runtime-src\Classes目录,然后打开proj.android_no_anysdk\jni下的Android.mk文件,给LOCAL_SRC_FILES加上 ../../Classes/lua_binding_MobClickCpp.cpp \,加上之后的LOCAL_SRC_FILES内容如下:

1
2
3
4
5
LOCAL_SRC_FILES := hellolua/main.cpp \
../../Classes/VisibleRect.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/ConfigParser.cpp \
../../Classes/lua_binding_MobClickCpp.cpp \

添加Java代码(仅限Android)

打开proj.android工程中的AppActivity.java文件,在函数onCreate中添加下面的语句:

1
MobClickCppHelper.init( this );

添加C++代码

打开AppDelegate.cpp文件,添加头文件

1
2
#include "MobClickCpp.h"
#include "lua_binding_MobClickCpp.h"

然后再在函数 bool AppDelegate::applicationDidFinishLaunching()中添加以下代码:

1
tolua_MobClickCpp_open(L);

注意上面这一句添加的位置必须要在获取到lua_State之后。

接着分别在AppDelegate::applicationDidEnterBackgroundAppDelegate::applicationWillEnterForeground中分别添加

1
umeng::MobClickCpp::applicationDidEnterBackground();

1
umeng::MobClickCpp::applicationWillEnterForeground();

改完C++代码后,build_native编译C++代码。稍等片刻,我们等待C++编译完。

在Lua代码中使用umeng

我们打开Quick-Cocos2d-x工程中的MyApp.lua代码文件,修改function MyApp:run()的内容为:

1
2
3
4
5
6
7
function MyApp:run()
     cc.FileUtils:getInstance():addSearchPath( "res/" )
     self:enterScene( "MainScene" )
 
     MobClickCpp:startWithAppkey( "5539bb6767e58e62eb000288" , "GooglePlay" )
 
end

这里的 5539bb6767e58e62eb000288 是我们在umeng官网上创建APP的时候所给的AppKey。GooglePlay为我们所使用的渠道。

打开Eclipse,在手机上运行工程,我们可以在后台看到umeng的数据

umeng_data

打开应用的详细页面,点击渠道分析,里面有一个渠道列表的子选项,点击之后我们可以看到我们上传的渠道用户数量:

channel

好了,友盟的C++版本在Lua的使用就介绍到这里,其他umeng的接口大家可以看下MobClickCpp.h文件中查看接口说明。

下一篇我们看在Lua里直接使用Java版本的umeng接口。

猜你喜欢

转载自blog.csdn.net/houjia159/article/details/46891161