【Cocos2d-x】Lua 资源热更新

前面2篇分别简单介绍

手动bind C++ 类和lua;http://blog.csdn.net/chenee543216/article/details/12074771

使用tolua++简化工作:http://blog.csdn.net/chenee543216/article/details/12172563

这篇简单聊聊 cocos2dx。

一、基本概念

所谓的lua绑定,其实目的就是导出C++的接口给lua,使得在lua中可以操作C++;

而lua和C/C++的唯一官方接口是stack。so,所谓的binding,就是C++,lua双方通过stack交换信息,然后对stack中信息进行解释,并执行相关动作的过程。

1 、

比如lua想生成一个dog 类,则在lua端: local d = Dog:creat(); 这个只是书面形式,其实lua解释器会自动填充stack。

比如,假设这个时候stack内容变成

"creat"

"Dog"

......... -- stack bottom

就是说,lua把2个标志入栈,一个是函数名,一个是类名;

在C/C++端,负责绑定的辅助代码(tolua++生成代码+libtolua++,或者我们第一篇文章中的LuaAnimal类)会根据 “Dog"生成一个类,并且根据某个表(见第一篇文章)去调用这个类中于”create“函数对应的函数。

OK,到这里,lua已经调用C、C++完成,并且生成相应的东西。

下面C/C++会把lua需要的返回值再压入stack。

lua端从stack中取出这个值,保留后面使用。

到这里一个完整过程over。

2、

lua端,再次调用 d:sound();

看官大概已经知道了。lua解释器会把相关信息压入stack,我们可以用第一篇中提供的函数stackDump()来展示stack内容。假设如下:

”sound"

"d"

在C++端,取出类实例地址 d,然后根据sound找对应类函数。比如sound00();然后调用d.sound00();

并且将相关返回值入栈,返回给lua。

3、用完以后gc,大家可以给Dog写个 Dog::~Dog()来看gc信息。

具体过程,给~Dog()下个断点,看C/C++ call stack即可。(我自己偷懒了~)

二、cocos2dx

1、

其实lua和cocos2dx木有啥关系,和cocos2dx sdk发生关系的是tolua++ 生成的C++代码而已。

lua是通过这个中间代码去操作cocos2dx的sdk的。

你实现一个功能,可能要写10几个class,还要调用native code,比如OC、或者java层代码。

但是一旦封装好以后,你使用这个功能只要一个简单调用而已。比如在C++端。我们调用 Money *m = Money::create();

我们最多只要用到几个public的函数,或者变量。并不关心private的函数和变量,和具体实现。

这个和tolua++的pkg文件编写规则一致。

再啰嗦一点:

比如上面Money类,我们只要 create,count 。。。 几个函数。那么tolua++生成的中间C++代码也只引用到这几个函数。

class MMM{

public:

int v00001;

int v00002;

....

int V99999;

void f00001();

void f00002()

.....

void f99999();

};

如果,我们在lua中只要调用v0001;和f00001;那么我们的tolua++的pkg只要包括

class MMM{

int v00001;

void f00001;

};

即可,大可不必管其它的东西。

2、cocos2dx自己的lua封装,可以到cocos2dx源码目录下面的tools/tolua++去看。

是官方提炼好的pkg,这些pkg最终都被include到Cocos2d.pkg,然后生成一个LuaCocos2d.cpp (见Makefile文件中描述)

这个LuaCocos2d.cpp就是整个cocos2dx中需要被lua使用的接口函数。

二、动手

我们自己写个练手

其实前面都已经写过了:见第二篇文章。唯一不同的是这里新的代码使用到cocos2dx sdk;

如下:

NativeHelper.h

#ifndef __NativeHelper_H__
#define __NativeHelper_H__

#include "cocos2d.h"
USING_NS_CC;
class NativeHelper : public cocos2d::CCLayer
{
public:
    virtual bool init();
    void openURL(std::string url,bool forceCheck=false);
    
    CREATE_FUNC(NativeHelper);
private:
    
};

#endif

NativeHelper.cpp
#ifdef ANDROID
#include "NativeHelper.h"
#include <jni.h>
#include "platform/android/jni/JniHelper.h"

bool NativeHelper::init()
{
    
    return true;
}


void NativeHelper::openURL(std::string url,bool forceCheck)
{
    JniMethodInfo jmi;
  if(JniHelper::getStaticMethodInfo(jmi,"org/cocos2dx/lib/Cocos2dxActivity","openUrl","([Ljava/lang/String;)V")){
    jclass str_cls = jmi.env->FindClass("java/lang/String");
        
    jstring str1 = jmi.env->NewStringUTF(url.c_str());
    jstring str2 = jmi.env->NewStringUTF("Nothing ...");
    
    jobjectArray arrs = jmi.env->NewObjectArray(2,str_cls,0);
    jmi.env->SetObjectArrayElement(arrs,0,str1);
    jmi.env->SetObjectArrayElement(arrs,1,str2);
    jmi.env->CallStaticVoidMethod(jmi.classID,jmi.methodID,arrs);
        
  }
}

#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "NativeHelper.h"


bool NativeHelper::init()
{
    
    return true;
}


#define kTextFieldTagOfForumPassword 100


//====================================
//typedef void(^AlertBlock)(NSInteger);

@interface BlockUIAlertView : UIAlertView
{
    NSString *url;
}
//@property(nonatomic,copy)AlertBlock block;
@property(nonatomic,copy)NSString *url;

- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
  cancelButtonTitle:(NSString *)cancelButtonTitle
//        clickButton:(AlertBlock)_block
  otherButtonTitles:(NSString *)otherButtonTitles;

//-(void)setURL:(NSString*)url;

@end
@implementation BlockUIAlertView

//@synthesize block;
@synthesize url;

- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
  cancelButtonTitle:(NSString *)cancelButtonTitle
//        clickButton:(AlertBlock)_block
  otherButtonTitles:(NSString *)otherButtonTitles {
    
    self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil];
    
    //    if (self) {
    //        self.block = _block;
    //    }
    
    return self;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //    self.block(buttonIndex);
    
    if (buttonIndex == 1) {
        CCLOG("------------button canceled");
        return;
    }
    
    UITextField *pwdTextField = (UITextField *)[alertView viewWithTag:kTextFieldTagOfForumPassword];
    NSString *pwd = [pwdTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    CCLOG("--------------%s",[pwd UTF8String]);
    
    if ([pwd caseInsensitiveCompare:@"iamadult"] == NSOrderedSame) {
        if ([url length]>[@"http://www.1.com" length]) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
        }
    }
    
}
@end

//====================================
void NativeHelper::openURL(std::string url,bool forceCheck)
{
    if (!forceCheck) {
        NSString * nsurl = [NSString stringWithCString:url.c_str()
                                              encoding:[NSString defaultCStringEncoding]];
        
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:nsurl]];
        
        return;
    }
    
    BlockUIAlertView *alertView = [[BlockUIAlertView alloc]
                                   initWithTitle:@"请输入字符: iamadult"
                                   message:@"\n\n"
                                   cancelButtonTitle:@"确认"
                                   //                                   clickButton:^(NSInteger indexButton){
                                   //        if (indexButton == 0) {
                                   //            CCLOG("You press btn 0");
                                   //        }else if(indexButton == 1){
                                   //            CCLOG("You press btn 1");
                                   //        }
                                   //    }
                                   otherButtonTitles:@"取消"];
    
    
    [alertView setUrl:[NSString stringWithCString:url.c_str()
                                         encoding:[NSString defaultCStringEncoding]]];
    
    UITextField *textFieldPwd = [[UITextField alloc] initWithFrame:CGRectMake(27.0,60.0,230.0,25.0)];
    [textFieldPwd setBackgroundColor:[UIColor whiteColor]];
    [textFieldPwd setTag:kTextFieldTagOfForumPassword];
    [textFieldPwd setPlaceholder:@"请输入密码"];
    [alertView addSubview:textFieldPwd];
    [textFieldPwd release];
    
    [alertView show];
    [alertView release];
    
}
#endif

这3个文件实现一个NativeHelper类,这个类只干一件事:调用系统web浏览器打开一个URL。通过宏定义来控制android和ios的独立实现。(ios端和android端的区别是:ios端打开URL之前会弹出一个对话框,让用户输入验证字符。 just for parent protect )

我们调用方式简单如下:

NativeHelper *nhelper = NativeHelper::create();
        const std::string strPrivacy = "http://http://blog.csdn.net/chenee543216/article/details/12172563";
        nhelper->openURL(strPrivacy,true);

现在,我们把调用需要的2个函数用tolua++导出给lua,there are create() and openURL()

toluaNativeHelper.pkg

1
2 $#include "cocos2d.h"
  3 $#include "NativeHelper.h"                                                                                           
  4 class NativeHelper : public cocos2d::CCLayer
  5 {
  6     void openURL(std::string url,bool forceCheck=false);
  7     static NativeHelper* create();
  8 };
  9

生成的命令为: tolua++ -o toluaNativeHelper.cpp toluaNativeHelper.pkg 生成如下cpp文件。
/*
** Lua binding: toluaNavieHelper
** Generated automatically by tolua++-1.0.92 on Mon Sep 30 16:21:12 2013.
*/

#ifndef __cplusplus
#include "stdlib.h"
#endif
#include "string.h"

#include "tolua++.h"

/* Exported function */
TOLUA_API int  tolua_toluaNavieHelper_open (lua_State* tolua_S);

#include "cocos2d.h"
#include "NativeHelper.h"

/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
 tolua_usertype(tolua_S,"NativeHelper");
 tolua_usertype(tolua_S,"cocos2d::CCLayer");
}

/* method: openURL of class  NativeHelper */
#ifndef TOLUA_DISABLE_tolua_toluaNavieHelper_NativeHelper_openURL00
static int tolua_toluaNavieHelper_NativeHelper_openURL00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isusertype(tolua_S,"NativeHelper",&tolua_err) ||
     !tolua_iscppstring(tolua_S,2,&tolua_err) ||
     !tolua_isboolean(tolua_S,3,&tolua_err) ||
     !tolua_isnoobj(tolua_S,4,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  NativeHelper* self = (NativeHelper*)  tolua_tousertype(tolua_S,0);
  std::string url = ((std::string)  tolua_tocppstring(tolua_S,0));
  bool forceCheck = ((bool)  tolua_toboolean(tolua_S,false));
#ifndef TOLUA_RELEASE
  if (!self) tolua_error(tolua_S,"invalid 'self' in function 'openURL'",NULL);
#endif
  {
   self->openURL(url,forceCheck);
  }
 }
 return 0;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'openURL'.",&tolua_err);
 return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* method: create of class  NativeHelper */
#ifndef TOLUA_DISABLE_tolua_toluaNavieHelper_NativeHelper_create00
static int tolua_toluaNavieHelper_NativeHelper_create00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isusertable(tolua_S,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  {
   NativeHelper* tolua_ret = (NativeHelper*)  NativeHelper::create();
    tolua_pushusertype(tolua_S,(void*)tolua_ret,"NativeHelper");
  }
 }
 return 1;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
 return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* Open function */
TOLUA_API int tolua_toluaNavieHelper_open (lua_State* tolua_S)
{
 tolua_open(tolua_S);
 tolua_reg_types(tolua_S);
 tolua_module(tolua_S,NULL,0);
 tolua_beginmodule(tolua_S,NULL);
  tolua_cclass(tolua_S,"cocos2d::CCLayer",NULL);
  tolua_beginmodule(tolua_S,"NativeHelper");
   tolua_function(tolua_S,"openURL",tolua_toluaNavieHelper_NativeHelper_openURL00);
   tolua_function(tolua_S,"create",tolua_toluaNavieHelper_NativeHelper_create00);
  tolua_endmodule(tolua_S);
 tolua_endmodule(tolua_S);
 return 1;
}


#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
 TOLUA_API int luaopen_toluaNavieHelper (lua_State* tolua_S) {
 return tolua_toluaNavieHelper_open(tolua_S);
};
#endif

自己添加到cocos2dx工程的class目录即可。

然后在

AppDelegate.cpp 中加入

文件开始部位:

extern int tolua_toluaNavieHelper_open ( lua_State * tolua_S);


调用lua环境初始化之后

CCLuaEngine * pEngine = CCLuaEngine :: defaultEngine ();

CCScriptEngineManager :: sharedManager ()-> setScriptEngine (pEngine);

tolua_toluaNavieHelper_open (pEngine-> getLuaStack ()-> getLuaState ());


ok;

现在你只要在lua文件中直接调用NativeHelper相关即可。如下

local nativehelper = NativeHelper:create()

nativehelper:openURL("http://www.sina.com",true)

写那那么多,也不知道说清楚了没有,有问题留言吧。

有错误,也欢迎留言指点。

参考:

http://blog.csdn.net/chenee543216/article/details/12172563

http://blog.csdn.net/chenee543216/article/details/12074771

http://www.himigame.com/lua-game/1259.html

http://blog.csdn.net/musicvs/article/details/8166655

tolua++官网各种文档

lua官网各种文档

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


    本文实践自 RayWenderlich、Ali Hafizji 的文章《How To Create Dynamic Textures with CCRenderTexture in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建实时纹理、如何用Gimp创建无缝拼接纹
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@163.com微信公众号:HopToad 欢迎转载,转载标注出处:http://blog.csdn.netotbaron/article/details/424343991.  软件准备 下载地址:http://cn.cocos2d-x.org/download 2.  简介2.1         引用C
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从Cocos2D-x官网上下载,进入网页http://www.cocos2d-x.org/download,点击Cocos2d-x以下的Download  v3.0,保存到自定义的文件夹2:从python官网上下载。进入网页https://www.python.org/downloads/,我当前下载的是3.4.0(当前最新
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发引擎,易学易用,支持多种智能移动平台。官网地址:http://cocos2d-x.org/当前版本:2.0    有很多的学习资料,在这里我只做为自己的笔记记录下来,错误之处还请指出。在VisualStudio2008平台的编译:1.下载当前稳
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《最强大脑》娱乐节目。将2048改造成一款挑战玩家对数字记忆的小游戏。邮箱:appdevzw@163.com微信公众号:HopToadAPK下载地址:http://download.csdn.net/detailotbaron/8446223源码下载地址:http://download.csdn.net/
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试以QtCreatorIDE来进行CMake构建。Cocos2d-x3.X地址:https://github.com/cocos2d/cocos2d-x1.打开QtCreator,菜单栏→"打开文件或项目...",打开cocos2d-x目录下的CMakeLists.txt文件;2.弹出CMake向导,如下图所示:设置
 下载地址:链接:https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ提取码:p1pb下载完成后,解压进入build目录使用vs2013打开工程设置平台工具集,打开设置界面设置: 点击开始编译等待编译结束编译成功在build文件下会出现一个新文件夹Debug.win32,里面就是编译
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net前言上次用象棋演示了cocos2dx的基本用法,但是对cocos2dx并没有作深入的讨论,这次以超级马里奥的源代码为线索,我们一起来学习超级马里奥的实
1. 圆形音量button事实上作者的本意应该是叫做“电位计button”。可是我觉得它和我们的圆形音量button非常像,所以就这么叫它吧~先看效果:好了,不多解释,本篇到此为止。(旁白: 噗。就这样结束了?)啊才怪~我们来看看代码:[cpp] viewplaincopyprint?CCContro
原文链接:http://www.cnblogs.com/physwf/archive/2013/04/26/3043912.html为了进一步深入学习贯彻Cocos2d,我们将自己写一个场景类,但我们不会走的太远,凡是都要循序渐进,哪怕只前进一点点,那也至少是前进了,总比贪多嚼不烂一头雾水的好。在上一节中我们建
2019独角兽企业重金招聘Python工程师标准>>>cocos2d2.0之后加入了一种九宫格的实现,主要作用是用来拉伸图片,这样的好处在于保留图片四个角不变形的同时,对图片中间部分进行拉伸,来满足一些控件的自适应(PS: 比如包括按钮,对话框,最直观的形象就是ios里的短信气泡了),这就要求图
原文链接:http://www.cnblogs.com/linji/p/3599478.html1.环境和工具准备Win7VS2010/2012,至于2008v2版本之后似乎就不支持了。 2.安装pythonv.2.0版本之前是用vs模板创建工程的,到vs2.2之后就改用python创建了。到python官网下载版本2.7.5的,然后
环境:ubuntu14.04adt-bundle-linux-x86_64android-ndk-r9d-linux-x86_64cocos2d-x-3.0正式版apache-ant1.9.3python2.7(ubuntu自带)加入环境变量exportANDROID_SDK_ROOT=/home/yangming/adt-bundle-linux/sdkexportPATH=${PATH}:/$ANDROID_SDK_ROOTools/export
1开发背景游戏程序设计涉及了学科中的各个方面,鉴于目的在于学习与进步,本游戏《FlappyBird》采用了两个不同的开发方式来开发本款游戏,一类直接采用win32底层API来实现,另一类采用当前火热的cocos2d-x游戏引擎来开发本游戏。2需求分析2.1数据分析本项目要开发的是一款游
原文链接:http://www.cnblogs.com/linji/p/3599912.html//纯色色块控件(锚点默认左下角)CCLayerColor*ccc=CCLayerColor::create(ccc4(255,0,0,128),200,100);//渐变色块控件CCLayerGradient*ccc=CCLayerGradient::create(ccc4(255,0,0,
原文链接:http://www.cnblogs.com/linji/p/3599488.html//载入一张图片CCSprite*leftDoor=CCSprite::create("loading/door.png");leftDoor->setAnchorPoint(ccp(1,0.5));//设置锚点为右边中心点leftDoor->setPosition(ccp(240,160));/
为了答谢广大学员对智捷课堂以及关老师的支持,现购买51CTO学院关老师的Cocos2d-x课程之一可以送智捷课堂编写图书一本(专题可以送3本)。一、Cocos2d-x课程列表:1、Cocos2d-x入门与提高视频教程__Part22、Cocos2d-x数据持久化与网络通信__Part33、Cocos2d-x架构设计与性能优化内存优
Spawn让多个action同时执行。Spawn有多种不同的create方法,最终都调用了createWithTwoActions(FiniteTimeAction*action1,FiniteTimeAction*action2)方法。createWithTwoActions调用initWithTwoActions方法:对两个action变量初始化:_one=action1;_two=action2;如果两个a
需要环境:php,luajit.昨天在cygwin上安装php和luajit环境,这真特么是一个坑。建议不要用虚拟环境安装打包环境,否则可能会出现各种莫名问题。折腾了一下午,最终将环境转向linux。其中,luajit的安装脚本已经在quick-cocos2d-x-develop/bin/中,直接luajit_install.sh即可。我的lin
v3.0相对v2.2来说,最引人注意的。应该是对触摸层级的优化。和lambda回调函数的引入(嗯嗯,不枉我改了那么多类名。话说,每次cocos2dx大更新。总要改掉一堆类名函数名)。这些特性应该有不少人研究了,所以今天说点跟图片有关的东西。v3.0在载入图片方面也有了非常大改变,仅仅只是