av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

Lua游戲開(kāi)發(fā)中關(guān)于C接口學(xué)習(xí)教程

Lua游戲開(kāi)發(fā)中關(guān)于C接口是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)LUA中關(guān)于C接口的使用方法,具體內(nèi)容的實(shí)現(xiàn)來(lái)看本文詳解。

專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)常寧免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了數(shù)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

lua快一年了,因?yàn)橐娌糠直容^少改動(dòng),所以一直沒(méi)用過(guò)它的C接口,都是在寫腳本。年前看書時(shí)寫了一個(gè)小的demo做學(xué)習(xí)用,好像當(dāng)時(shí)遇到些困難,但是沒(méi)有記錄下來(lái),幾乎都忘了。這里貼點(diǎn)源碼出來(lái)做備忘吧:)lua的語(yǔ)法還是比較簡(jiǎn)單,其官網(wǎng)(www.lua.org)上有電子文檔(www.lua.org/pil/),看一看就會(huì)了。不過(guò)學(xué)會(huì)一門語(yǔ)言的語(yǔ)法跟用好一門語(yǔ)言還是兩回事,好在它的源碼也不多,多看看源碼理解就深了。

首先說(shuō)我比較討厭lua的幾個(gè)地方:

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

2、沒(méi)有continue,經(jīng)常出現(xiàn)循環(huán)里面嵌套N層if。

3、最最無(wú)聊的就是變量默認(rèn)是global的,要顯示聲明local才是本地變量。

大概就這幾個(gè)公認(rèn)的問(wèn)題了,下面貼代碼:)

程序?qū)崿F(xiàn)了一個(gè)lua解釋器,其實(shí)就是讀入lua語(yǔ)句然后解釋執(zhí)行,用了readline是為了輸入方便。另外啟動(dòng)的時(shí)候load了一個(gè)叫init.lua的腳本文件,提供了幾個(gè)api供腳本使用,全部代碼如下:(csdn怎么不提供附件功能呢)

 
 
  1. main.hpp  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  8. #include  
  9. #include  
  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)建一個(gè)lua運(yùn)行環(huán)境,可以傳入一個(gè)內(nèi)存管理參數(shù)  
  25.         luaL_openlibs(L);//打開(kāi)常用lib  
  26.         if ( ! init_script() )//load腳本  
  27.                 return -1;   
  28.         register_api(L);//注冊(cè)api  
  29.         create_table(L);//創(chuàng)建一個(gè)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í)行輸入的語(yǔ)句  
  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);//傳入?yún)?shù)  
  71.         lua_getglobal(L, "__init__");//獲取腳本中__init__變量  
  72.         if ( lua_isfunction(L, -1) )//判斷__init__是否一個(gè)函數(shù)  
  73.         ...{  
  74.                 if ( lua_pcall(L, 0, 1, NULL) != 0 )//調(diào)用__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  
  91. #include "main.hpp"  
  92. int lua_getcwd(lua_State* L)//獲取當(dāng)前工作目錄  
  93. ...{  
  94.         char path[MAXPATHLEN];  
  95.         bzero(path, MAXPATHLEN);  
  96.         if (lua_gettop(L) != 0 ) //不需要參數(shù)  
  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;//返回返回值個(gè)數(shù)  
  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);//把所有元素放到一個(gè)table中,以數(shù)組返回  
  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)//注冊(cè)api  
  132. ...{  
  133.         lua_register(L, "getcwd", lua_getcwd);//腳本中可以使用getcwd調(diào)用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);//注冊(cè)一個(gè)名為tlib的模塊,tlib.getcwd()  
  142. }  
  143. void create_table(lua_State* L)//創(chuàng)建一個(gè)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下編譯通過(guò)。

小結(jié):Lua游戲開(kāi)發(fā)中關(guān)于C接口學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!


文章題目:Lua游戲開(kāi)發(fā)中關(guān)于C接口學(xué)習(xí)教程
當(dāng)前URL:http://uogjgqi.cn/article/dpdojio.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流