自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Lua游戲開發(fā)中關于C接口學習教程

移動開發(fā) iOS 游戲開發(fā)
Lua游戲開發(fā)中關于C接口是本文要介紹的內容,主要是來學習LUA中關于C接口的使用方法,具體內容的實現來看本文詳解。

Lua游戲開發(fā)中關于C接口是本文要介紹的內容,主要是來學習LUA中關于C接口的使用方法,具體內容的實現來看本文詳解。

lua快一年了,因為引擎部分比較少改動,所以一直沒用過它的C接口,都是在寫腳本。年前看書時寫了一個小的demo做學習用,好像當時遇到些困難,但是沒有記錄下來,幾乎都忘了。這里貼點源碼出來做備忘吧:)lua的語法還是比較簡單,其官網(www.lua.org)上有電子文檔(www.lua.org/pil/),看一看就會了。不過學會一門語言的語法跟用好一門語言還是兩回事,好在它的源碼也不多,多看看源碼理解就深了。

首先說我比較討厭lua的幾個地方:

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

2、沒有continue,經常出現循環(huán)里面嵌套N層if。

3、最最無聊的就是變量默認是global的,要顯示聲明local才是本地變量。

大概就這幾個公認的問題了,下面貼代碼:)

程序實現了一個lua解釋器,其實就是讀入lua語句然后解釋執(zhí)行,用了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();//創(chuàng)建一個lua運行環(huán)境,可以傳入一個內存管理參數  
  25.         luaL_openlibs(L);//打開常用lib  
  26.         if ( ! init_script() )//load腳本  
  27.                 return -1;   
  28.         register_api(L);//注冊api  
  29.         create_table(L);//創(chuàng)建一個table  
  30.         char* input = NULL;  
  31.         while(1)  
  32.         ...{     
  33.                 input = readline(">>");//提示輸入  
  34.                 if (input)  
  35.                 ...{     
  36.                         if ( *input )  
  37.                         ...{     
  38.                                 if( exelua(input) )//執(zhí)行輸入的語句  
  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并執(zhí)行   
  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)//創(chuàng)建一個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游戲開發(fā)中關于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-24 14:14:13

LUA環(huán)境 配置

2011-08-23 17:06:03

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-24 15:34:44

MinGWLua環(huán)境配置

2011-08-25 15:41:42

Lua源碼

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-24 15:22:09

2011-08-25 17:01:50

LUA網游游戲

2011-08-25 16:20:33

Lua腳本變量

2011-08-23 17:33:08

LuaMetatable

2011-08-23 17:11:13

Lua事件C#

2011-08-09 16:08:58

IOS游戲Cocos2d
點贊
收藏

51CTO技術棧公眾號