cocos2dx关于在c/c++中调用lua函数

这个帖子是介绍如何使用原生lua的API完成在c/c++中调用lua函数.

并非是cocos2dx封装的接口.不过,cocos2dx引擎的底层也是用的这些接口写的.

我们在CCLuaStack.cpp中可以看到代码如下:

#include "CCLuaStack.h"

#include "tolua_fix.h"


extern "C" {

#include "lua.h"

#include "tolua++.h"

#include "lualib.h"

#include "lauxlib.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_QT ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)

#include "lua_extensions.h"

#endif

}

我们在写代码的时候也可以直接调用下文的lua_open luaL_openlibs luaL_dofile lua_getglobal lua_pushinteger lua_pushinteger lua_call lua_pop lua_close等api.

这个帖子很简洁,明了,很适合第一次做C++调用lua的朋友学习.

后续我会写一篇通过cocos2dx二次封装后的接口实现c++中调用lua函数的文章!搞cocos2dx的童鞋欢迎关注!


闲话少说,奉上由<Alex Zhou>撰写的优质文章 <<在c/c++中调用lua函数>>

_______________________________

以下是转载原文内容:

原文链接:

http://codingnow.cn/language/1530.html 在c/c++中调用lua函数


上篇文章完成了在lua中调用c/c++函数,现在来实现在c/c++中调用lua函数。

首先完成lua代码,创建sum.lua:

1
2
3
function add(x,y)
return x + y;
end

为了在c中调用lua中的add函数,首先需要把函数压入堆栈,然后把函数的参数压入堆栈,然后执行函数,最后从栈中获取函数返回值。先看看下面的代码:

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
#include <iostream>
using namespace std;
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
static lua_State *L = NULL;
int ladd( x, y) {
sum;
lua_getglobal(L, "add" );
lua_pushinteger(L,x);
lua_call(L,2,1);
sum = ( )lua_tointeger(L,-1);
lua_pop(L,1);
return sum;
}
main() {
L = lua_open();
luaL_openlibs(L);
luaL_dofile(L,monospace!important; font-size:1em!important; padding:0px!important; color:blue!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important">"sum.lua" );
sum = ladd(10,20);
cout << "sum=" << sum << endl;
lua_close(L);
0;
}

在ladd函数中执行了lua中的add函数,首先看lua_getglobal函数:

1
void lua_getglobal (lua_State *L, const char *name);

把全局变量 name 里的值压入堆栈。这个是用一个宏定义出来的:

#define lua_getglobal(L,s) lua_getfield(L,LUA_GLOBALSINDEX,s)

这里lua_getglobal(L,“add”)把add函数压入堆栈,接着把x和y参数压入堆栈,然后调用lua_call执行add函数,关于lua_call函数:

lua_call (lua_State *L,monospace!important; font-size:1em!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important">nargs,monospace!important; font-size:1em!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important">nresults);

它的功能是调用一个函数,需要遵循以下协议:

首先,要调用的函数应该被压入堆栈;

接着把需要传递给这个函数的参数按正序压栈;

这是指第一个参数首先压栈。

最后调用一下lua_call; nargs 是你压入堆栈的参数个数。

当函数调用完毕后,所有的参数以及函数本身都会出栈。

而函数的返回值这时则被压入堆栈。

返回值的个数将被调整为 nresults 个, 除非 nresults 被设置成 LUA_MULTRET。

在这种情况下,所有的返回值都被压入堆栈中。 Lua 会保证返回值都放入栈空间中。

函数返回值将按正序压栈(第一个返回值首先压栈), 因此在调用结束后,最后一个返回值将被放在栈顶。
这里lua_call(L,1)是指函数有两个参数和一个返回值。
lua_tointeger(L,-1):表示从栈顶取得返回值。
lua_pop(L,1):表示从堆栈中弹出一个元素,因为此时add函数已经执行完毕,参数和函数本身已经出栈,堆栈中只有返回值。
main函数中的代码跟上篇博客差不多,就不过多解释了。

ctrl+f5,最终运行结果如下:

相关话题:

http://codingnow.cn/language/1524.html 在lua中调用c/c++函数

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...