Lua游戏开发中关于C接口学习教程

移动开发 iOS 游戏开发
Lua游戏开发中关于C接口是本文要介绍的内容,主要是来学习LUA中关于C接口的使用方法,具体内容的实现来看本文详解。

Lua游戏开发中关于C接口是本文要介绍的内容,主要是来学习LUA中关于C接口的使用方法,具体内容的实现来看本文详解。

lua快一年了,因为引擎部分比较少改动,所以一直没用过它的C接口,都是在写脚本。年前看书时写了一个小的demo做学习用,好像当时遇到些困难,但是没有记录下来,几乎都忘了。这里贴点源码出来做备忘吧:)lua的语法还是比较简单,其官网(www.lua.org)上有电子文档(www.lua.org/pil/),看一看就会了。不过学会一门语言的语法跟用好一门语言还是两回事,好在它的源码也不多,多看看源码理解就深了。

首先说我比较讨厌lua的几个地方:

1、把数组和table混在一起,数组可以很方便取得size,而table就只能自己遍历去数。

2、没有continue,经常出现循环里面嵌套N层if。

3、最最无聊的就是变量默认是global的,要显示声明local才是本地变量。

大概就这几个公认的问题了,下面贴代码:)

程序实现了一个lua解释器,其实就是读入lua语句然后解释执行,用了readline是为了输入方便。另外启动的时候load了一个叫init.lua的脚本文件,提供了几个api供脚本使用,全部代码如下:(csdn怎么不提供附件功能呢)

  1. main.hpp  
  2. #include <unistd.h> 
  3. #include <sys/param.h> 
  4. #include <errno.h> 
  5. #include <stdlib.h> 
  6. #include <iostream> 
  7. #include <readline/readline.h> 
  8. #include <readline/history.h> 
  9. #include <lua.hpp> 
  10. extern lua_State *L;   
  11. bool exelua(const char*);  
  12. bool init_script();  
  13. int lua_getcwd(lua_State*);  
  14. int lua_dir(lua_State*);  
  15. void register_api(lua_State*);  
  16. void create_table(lua_State*);  
  17. main.cpp  
  18. #include "main.hpp"  
  19.  
  20. lua_State *L;   
  21.  
  22. int main(int argc, char** argv)  
  23. ...{   
  24.         L = luaL_newstate();//创建一个lua运行环境,可以传入一个内存管理参数  
  25.         luaL_openlibs(L);//打开常用lib  
  26.         if ( ! init_script() )//load脚本  
  27.                 return -1;   
  28.         register_api(L);//注册api  
  29.         create_table(L);//创建一个table  
  30.         char* input = NULL;  
  31.         while(1)  
  32.         ...{     
  33.                 input = readline(">>");//提示输入  
  34.                 if (input)  
  35.                 ...{     
  36.                         if ( *input )  
  37.                         ...{     
  38.                                 if( exelua(input) )//执行输入的语句  
  39.                                         add_history(input);//增加到历史命令  
  40.                         }     
  41.                         free(input);  
  42.                         input = NULL;  
  43.                 }     
  44.                 else  
  45.                 ...{     
  46.                         break;  
  47.                 }     
  48.         }     
  49.         lua_close(L);  
  50.         return 0;  
  51. }  
  52. bool exelua(const char* line)  
  53. ...{  
  54.         int error = luaL_loadbuffer(L, line, strlen(line), "line") || lua_pcall(L, 0, 0, 0);//load并执行   
  55.         if ( error )  
  56.         ...{     
  57.                 std::cerr << lua_tostring(L, -1) << std::endl;  
  58.                 lua_pop(L, 1);   
  59.                 return false;  
  60.         }     
  61.         return true;  
  62. }  
  63. bool init_script()  
  64. ...{  
  65.         if ( luaL_dofile(L, "init.lua") != 0 )   
  66.         ...{  
  67.                 std::cerr << "load init.lua failed ";  
  68.                 return false;  
  69.         }  
  70.         lua_pushnumber(L, 1);//传入参数  
  71.         lua_getglobal(L, "__init__");//获取脚本中__init__变量  
  72.         if ( lua_isfunction(L, -1) )//判断__init__是否一个函数  
  73.         ...{  
  74.                 if ( lua_pcall(L, 0, 1, NULL) != 0 )//调用__init__  
  75.                 ...{  
  76.                         std::cerr << "call __init__ error ";  
  77.                         return false;  
  78.                 }  
  79.                 int ret = lua_tonumber(L, -1) || lua_toboolean(L, -1);//取得__init__的返回值  
  80.                 lua_pop(L, 1);  
  81.                 if ( !ret )  
  82.                 ...{  
  83.                         std::cerr << "__init__ failed ";  
  84.                         return false;  
  85.                 }  
  86.         }  
  87.         return true;  
  88. }  
  89. api.cpp  
  90. #include <dirent.h> 
  91. #include "main.hpp"  
  92. int lua_getcwd(lua_State* L)//获取当前工作目录  
  93. ...{  
  94.         char path[MAXPATHLEN];  
  95.         bzero(path, MAXPATHLEN);  
  96.         if (lua_gettop(L) != 0 ) //不需要参数  
  97.         ...{     
  98.                 luaL_argerror(L, 0, "no arg expected");  
  99.                 return 0;  
  100.         }     
  101.         if ( !getcwd(path, MAXPATHLEN) )  
  102.         ...{     
  103.                 luaL_error(L, "getcwd error %d, %s", errno, strerror(errno));  
  104.                 return 0;  
  105.         }     
  106.         lua_pushlstring(L, path, strlen(path));//将返回值压栈  
  107.         return 1;//返回返回值个数  
  108. }  
  109. int lua_dir(lua_State* L)//取得目录下元素  
  110. ...{  
  111.         const char* path = luaL_checkstring(L, 1);   
  112.         DIR* dir = opendir(path);  
  113.         if ( !dir )  
  114.         ...{     
  115.                 lua_pushnil(L);  
  116.                 lua_pushstring(L, strerror(errno));  
  117.                 return 2;  
  118.         }     
  119.         int i = 1;  
  120.         struct dirent *ent;  
  121.         lua_newtable(L);//把所有元素放到一个table中,以数组返回  
  122.         while( ent = readdir(dir) )  
  123.         ...{     
  124.                 lua_pushnumber(L, i++);  
  125.                 lua_pushstring(L, ent->d_name);  
  126.                 lua_settable(L, -3);  
  127.         }     
  128.         closedir(dir);  
  129.         return 1;  
  130. }  
  131. void register_api(lua_State* L)//注册api  
  132. ...{  
  133.         lua_register(L, "getcwd", lua_getcwd);//脚本中可以使用getcwd调用lua_getcwd  
  134.         lua_register(L, "dir", lua_dir);  
  135.         const luaL_Reg mylib[] =   
  136.         ...{     
  137.                 ...{"getcwd", lua_getcwd},  
  138.                 ...{"dir", lua_dir},  
  139.                 ...{NULL, NULL},  
  140.         };  
  141.         luaL_register(L, "tlib", mylib);//注册一个名为tlib的模块,tlib.getcwd()  
  142. }  
  143. void create_table(lua_State* L)//创建一个table  
  144. ...{  
  145.         lua_newtable(L);  
  146.         lua_pushnumber(L, 123);  
  147.         lua_setfield(L, -2, "id");  
  148.         lua_pushcfunction(L, lua_getcwd);  
  149.         lua_setfield(L, -2, "fun");  
  150.         lua_setglobal(L, "tb");  
  151. }  
  152. init.lua  
  153. function __init__()  
  154.         print("__init__ ok")  
  155.         return 1;  
  156. end  
  157. Makefile CPPFLAGS=-Wall -g -O0 -I /usr/local/include/lua51/  
  158. LIB=-L/usr/local/lib/lua51/ -llua -lreadline  
  159. CC=g++  
  160. SRC=main.cpp api.cpp  
  161. OBJ=${SRC:%.cpp=%.o}  
  162. all: depend main  
  163. depend:  
  164.         @$(CC) -MM $(SRC)  > .depend  
  165. -include .depend  
  166. main: $(OBJ)  
  167.         $(CC) $(OBJ) $(CPPFLAGS)  $(LIB) -o $@  
  168. clean:  
  169.         -rm -rf *.o main .depend 

       
以上代码在freebsd 6.2  gcc 3.4.6 lua 5.1.2下编译通过。

小结:Lua游戏开发中关于C接口学习教程的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: CSDN博客
相关推荐

2011-08-24 13:56:12

Lua游戏

2011-08-23 16:37:05

Lua数学库

2011-08-24 17:09:35

LUA闭包函数

2011-08-23 15:34:56

Lua模式 匹配

2011-08-23 17:06:03

2011-08-24 14:14:13

LUA环境 配置

2011-08-23 16:48:41

Lua 5.1API 函数

2011-08-23 13:54:10

LUA全局变量

2011-08-24 15:42:38

LUA源代码

2011-08-25 15:41:42

Lua源码

2011-08-24 15:34:44

MinGWLua环境配置

2011-08-23 16:59:16

C++LUA脚本LUA API

2011-08-23 15:57:21

Lua元表元方法

2011-08-24 11:08:09

Lua

2011-08-23 17:33:08

LuaMetatable

2011-08-25 17:01:50

LUA网游游戏

2011-08-24 15:22:09

2011-08-23 17:11:13

Lua事件C#

2011-08-25 16:20:33

Lua脚本变量

2011-08-23 16:22:45

Lua 4.0函数
点赞
收藏

51CTO技术栈公众号