青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

lxyfirst

C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
  33 Posts :: 3 Stories :: 27 Comments :: 0 Trackbacks
lua作為小巧精悍的腳本語言,易于嵌入c/c++中 , 廣泛應(yīng)用于游戲AI ,實(shí)際上在任何經(jīng)常變化的邏輯上都可以使用lua實(shí)現(xiàn),配合c/c++實(shí)現(xiàn)的底層接口服務(wù),能夠大大降低系統(tǒng)的維護(hù)成本。下面對(duì)lua和c/c++的交互調(diào)用做一個(gè)實(shí)例分析:
lua提供了API用于在c/c++中構(gòu)造lua的運(yùn)行環(huán)境,相關(guān)接口如下:
//創(chuàng)建lua運(yùn)行上下文
lua_State* luaL_newstate(void) ;
//加載lua腳本文件
int luaL_loadfile(lua_State *L, const char *filename);

lua和c/c++的數(shù)據(jù)交互通過"棧"進(jìn)行 ,操作數(shù)據(jù)時(shí),首先將數(shù)據(jù)拷貝到"棧"上,然后獲取數(shù)據(jù),棧中的每個(gè)數(shù)據(jù)通過索引值進(jìn)行定位,索引值為正時(shí)表示相對(duì)于棧底的偏移索引,索引值為負(fù)時(shí)表示相對(duì)于棧頂?shù)钠扑饕饕狄?或-1為起始值,因此棧頂索引值永遠(yuǎn)為-1 ,棧底索引值永遠(yuǎn)為1 。 "棧"相當(dāng)于數(shù)據(jù)在lua和c/c++之間的中轉(zhuǎn)地。每種數(shù)據(jù)都有相應(yīng)的存取接口 。
數(shù)據(jù)入"棧"接口:
void  (lua_pushnil) (lua_State *L);
void  (lua_pushnumber) (lua_State *L, lua_Number n);
void  (lua_pushinteger) (lua_State *L, lua_Integer n);
void  (lua_pushlstring) (lua_State *L, const char *s, size_t l);
void  (lua_pushstring) (lua_State *L, const char *s);
void  (lua_pushboolean) (lua_State *L, int b);
void  (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);

數(shù)據(jù)獲取接口:
lua_Number      (lua_tonumber) (lua_State *L, int idx);
lua_Integer     (lua_tointeger) (lua_State *L, int idx);
int             (lua_toboolean) (lua_State *L, int idx);
const char     *(lua_tolstring) (lua_State *L, int idx, size_t *len);
lua_CFunction   (lua_tocfunction) (lua_State *L, int idx);


"棧"操作接口:
int   (lua_gettop) (lua_State *L);
void  (lua_settop) (lua_State *L, int idx);
void  (lua_pushvalue) (lua_State *L, int idx);
void  (lua_remove) (lua_State *L, int idx);
void  (lua_insert) (lua_State *L, int idx);
void  (lua_replace) (lua_State *L, int idx);
int   (lua_checkstack) (lua_State *L, int sz);

lua中定義的變量和函數(shù)存放在一個(gè)全局table中,索引值為L(zhǎng)UA_GLOBALSINDEX ,table相關(guān)操作接口:
void  (lua_gettable) (lua_State *L, int idx);
void  (lua_getfield) (lua_State *L, int idx, const char *k);
void  (lua_settable) (lua_State *L, int idx);
void  (lua_setfield) (lua_State *L, int idx, const char *k);

當(dāng)"棧"中包含執(zhí)行腳本需要的所有要素(函數(shù)名和參數(shù))后,調(diào)用lua_pcall執(zhí)行腳本:
int   (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);

下面進(jìn)行實(shí)例說明:
func.lua
--變量定義
width
=1 ;
height
=2 ;
--lua函數(shù)定義,實(shí)現(xiàn)加法
function sum(a,b)
    
return a+b ;
end
--lua函數(shù)定義,實(shí)現(xiàn)字符串相加
function mystrcat(a,b)
    
return a..b ;
end
--lua函數(shù)定義,通過調(diào)用c代碼中的csum函數(shù)實(shí)現(xiàn)加法
function mysum(a,b)
    
return csum(a,b) ;
end

test_lua.c
#include <stdio.h>
#include 
<stdlib.h>
#include 
<string.h>
#include <errno.h>
//lua頭文件
#include <lua.h>
#include 
<lualib.h>
#include 
<lauxlib.h>


#define err_exit(num,fmt,args)  \
    
do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);exit(num);} while(0)
#define err_return(num,fmt,args)  \
    
do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);} while(0)

//lua中調(diào)用的c函數(shù)定義,實(shí)現(xiàn)加法
int csum(lua_State* l)
{
    
int a = lua_tointeger(l,1) ;
    
int b = lua_tointeger(l,2) ;
    lua_pushinteger(l,a
+b) ;
    
return 1 ;
}

int main(int argc,char** argv)
{
    lua_State 
* l = luaL_newstate() ;        //創(chuàng)建lua運(yùn)行環(huán)境
    
if ( l == NULL ) err_return(-1,"luaL_newstat() failed"); 
    
int ret = 0 ;
    ret 
= luaL_loadfile(l,"func.lua") ;      //加載lua腳本文件
    
if ( ret != 0 ) err_return(-1,"luaL_loadfile failed") ;
    ret 
= lua_pcall(l,0,0,0) ;
    
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;

    lua_getglobal(l,
"width");              //獲取lua中定義的變量
    lua_getglobal(l,
"height");
    printf(
"height:%ld width:%ld\n",lua_tointeger(l,-1),lua_tointeger(l,-2)) ;
    lua_pop(l,
1) ;                        //恢復(fù)lua的棧

    
int a = 11 ;
    
int b = 12 ;
    lua_getglobal(l,
"sum");               //調(diào)用lua中的函數(shù)sum
    lua_pushinteger(l,a) ;
    lua_pushinteger(l,b) ;
    ret 
= lua_pcall(l,2,1,0) ;
    
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf(
"sum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
    lua_pop(l,
1) ;

    
const char str1[] = "hello" ;
    
const char str2[] = "world" ;
    lua_getglobal(l,
"mystrcat");          //調(diào)用lua中的函數(shù)mystrcat
    lua_pushstring(l,str1) ;
    lua_pushstring(l,str2) ;
    ret 
= lua_pcall(l,2,1,0) ;
    
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf(
"mystrcat:%s%s = %s\n",str1,str2,lua_tostring(l,-1)) ;
    lua_pop(l,
1) ;

    lua_pushcfunction(l,csum) ;         //注冊(cè)在lua中使用的c函數(shù)
    lua_setglobal(l,
"csum") ;           //綁定到lua中的名字csum

    lua_getglobal(l,
"mysum");           //調(diào)用lua中的mysum函數(shù),該函數(shù)調(diào)用本程序中定義的csum函數(shù)實(shí)現(xiàn)加法
    lua_pushinteger(l,a) ;
    lua_pushinteger(l,b) ;
    ret 
= lua_pcall(l,2,1,0) ;
    
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf(
"mysum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
    lua_pop(l,
1) ;

    lua_close(l) ;                     //釋放lua運(yùn)行環(huán)境
    
return 0 ;
}



posted on 2008-10-29 14:37 star 閱讀(28626) 評(píng)論(12)  編輯 收藏 引用

Feedback

# re: lua和c/c++互相調(diào)用實(shí)例分析 2008-11-01 11:00 金山詞霸2008
有個(gè)疑問,C++已經(jīng)足夠強(qiáng)大,為什么還需要lua腳本語言?  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析 2008-11-03 18:45 diwayou
靈活性!  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析 2008-11-03 20:30 amuro1987218
除去圖像這一層極端效率敏感的部分外,其他部分其實(shí)用效率適當(dāng)下降換取靈活是值得的。  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析[未登錄] 2008-12-19 17:00 hunter
這些都是簡(jiǎn)單數(shù)據(jù)類型的操作,能不能提供一個(gè)操作struct(里面還有struct)的例子啊。  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析 2009-01-06 20:34 lua
寫的非常好,我折騰兩三天都沒有搞定.看了之后我全明白了.  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析 2011-11-09 14:00 無賴二號(hào)
非常好,非常感謝。  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析[未登錄] 2012-04-24 14:56 jerome
為什么我不能編譯

]# gcc testlua.C -llua -lm -ldl -I/usr/local/include/ -L/usr/local/lib/ -o testlua
/tmp/ccIkmvNG.o: In function `csum(lua_State*)':
testlua.C:(.text+0x16): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x27): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x3c): undefined reference to `lua_pushinteger(lua_State*, long)'
/tmp/ccIkmvNG.o: In function `main':
testlua.C:(.text+0x59): undefined reference to `luaL_newstate()'
testlua.C:(.text+0x8d): undefined reference to `luaL_loadfile(lua_State*, char const*)'
testlua.C:(.text+0xc4): undefined reference to `lua_pcall(lua_State*, int, int, int)'
testlua.C:(.text+0xf6): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x109): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x117): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x128): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x14b): undefined reference to `lua_settop(lua_State*, int)'
testlua.C:(.text+0x16c): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x17b): undefined reference to `lua_pushinteger(lua_State*, long)'
testlua.C:(.text+0x18a): undefined reference to `lua_pushinteger(lua_State*, long)'
testlua.C:(.text+0x1a2): undefined reference to `lua_pcall(lua_State*, int, int, int)'
testlua.C:(.text+0x1be): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
testlua.C:(.text+0x1ea): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x210): undefined reference to `lua_settop(lua_State*, int)'
testlua.C:(.text+0x24b): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x258): undefined reference to `lua_pushstring(lua_State*, char const*)'
testlua.C:(.text+0x265): undefined reference to `lua_pushstring(lua_State*, char const*)'
testlua.C:(.text+0x27d): undefined reference to `lua_pcall(lua_State*, int, int, int)'
testlua.C:(.text+0x299): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
testlua.C:(.text+0x2ca): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
testlua.C:(.text+0x2f2): undefined reference to `lua_settop(lua_State*, int)'
testlua.C:(.text+0x305): undefined reference to `lua_pushcclosure(lua_State*, int (*)(lua_State*), int)'
testlua.C:(.text+0x318): undefined reference to `lua_setfield(lua_State*, int, char const*)'
testlua.C:(.text+0x32b): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x33a): undefined reference to `lua_pushinteger(lua_State*, long)'
testlua.C:(.text+0x349): undefined reference to `lua_pushinteger(lua_State*, long)'
testlua.C:(.text+0x361): undefined reference to `lua_pcall(lua_State*, int, int, int)'
testlua.C:(.text+0x37d): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
testlua.C:(.text+0x3a6): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x3cc): undefined reference to `lua_settop(lua_State*, int)'
testlua.C:(.text+0x3d5): undefined reference to `lua_close(lua_State*)'
/tmp/ccIkmvNG.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析[未登錄] 2012-04-26 17:43 tommy
請(qǐng)看清楚了作者寫的文件名是test_lua.c
如果你保存為cpp文件了,關(guān)于lua相關(guān)頭文件應(yīng)該使用:
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
否則編譯會(huì)提示你的那種問題  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析 2012-05-03 10:20 star
@jerome
鏈接時(shí)找不到函數(shù)定義,這個(gè)應(yīng)該是lua庫的路徑問題。  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析 2014-02-02 14:46 ixil
@hunter
point=alien.buffer
賦值
x=point:get(1,"long")
y=point:get(5,"long")  回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析 2014-03-13 18:50 fy
lua_pop(l,1) ; //恢復(fù)lua的棧

這一行應(yīng)該改為 lua_pop(l,2) ;   回復(fù)  更多評(píng)論
  

# re: lua和c/c++互相調(diào)用實(shí)例分析 2014-11-29 13:54 iHuahua
@金山詞霸2008
術(shù)業(yè)有專攻,C++那么強(qiáng)大,你倒是用他寫個(gè)網(wǎng)頁我看看  回復(fù)  更多評(píng)論
  


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲视频在线观看网站| 久久综合九色| 亚洲电影在线观看| 欧美在线欧美在线| 黄色国产精品| 亚洲黑丝一区二区| 国产精品白丝av嫩草影院| 99精品视频免费观看| 日韩视频一区二区三区在线播放免费观看 | 麻豆精品在线观看| 伊大人香蕉综合8在线视| 欧美国产激情| 欧美偷拍另类| 久久国产精彩视频| 免播放器亚洲| 午夜精品久久久久99热蜜桃导演| 午夜亚洲精品| 亚洲三级视频| 亚洲视频在线观看| 亚洲福利久久| 亚洲视频免费在线观看| 国产主播一区二区三区| 亚洲国产精品成人综合| 国产精品天美传媒入口| 欧美高清视频| 国产视频精品va久久久久久| 欧美国产综合一区二区| 国产精品视频不卡| 亚洲国产一区视频| 国内精品久久久久影院优| 亚洲三级影片| 尤物视频一区二区| 亚洲已满18点击进入久久| 亚洲激情av在线| 午夜精品剧场| 亚洲婷婷综合色高清在线| 在线日韩电影| 在线视频亚洲| 久久亚洲国产精品一区二区 | 国产精品大片免费观看| 欧美gay视频| 国产精品视频一| 亚洲精品视频中文字幕| 亚洲第一成人在线| 欧美一级二区| 性做久久久久久久免费看| 欧美日韩免费一区二区三区| 欧美黄色视屏| 国产亚洲欧美一区| 亚洲影院一区| 亚洲欧美另类在线| 欧美午夜精品久久久久久浪潮| 亚洲福利视频免费观看| 国外视频精品毛片| 香蕉免费一区二区三区在线观看| 亚洲一二三区视频在线观看| 欧美激情91| 亚洲激情在线观看视频免费| 91久久夜色精品国产网站| 久久婷婷综合激情| 你懂的国产精品| 亚洲福利专区| 女女同性精品视频| 久久综合网色—综合色88| 国产综合久久久久久鬼色| 欧美一级一区| 噜噜噜噜噜久久久久久91| 永久久久久久| 美女性感视频久久久| 欧美国产日韩一区二区在线观看 | 在线看片一区| 久久人91精品久久久久久不卡| 久久精品一级爱片| 好吊日精品视频| 久久综合99re88久久爱| 欧美激情在线观看| 一本色道久久综合亚洲精品婷婷| 欧美日本韩国一区| 国产精品99久久久久久www| 午夜精品久久久久久久久| 国产日韩欧美三级| 久久婷婷久久一区二区三区| 亚洲国产精品成人综合| 99在线精品视频| 国产精品久久久久一区| 性刺激综合网| 欧美国产日韩一区二区在线观看 | 亚洲线精品一区二区三区八戒| 国产精品乱子久久久久| 午夜久久美女| 亚洲高清色综合| 亚洲综合导航| 伊人狠狠色j香婷婷综合| 欧美大片第1页| 亚洲一区综合| 欧美激情1区2区3区| 亚洲一区欧美一区| 精品999在线播放| 欧美日韩精品中文字幕| 西西人体一区二区| 亚洲激情成人| 欧美亚洲三级| 亚洲精选视频免费看| 伊人色综合久久天天| 麻豆成人在线播放| 亚洲综合视频一区| 亚洲欧洲在线一区| 久久精品一区中文字幕| 亚洲精品视频在线看| 国产农村妇女精品一区二区| 欧美福利在线| 久久国产精品黑丝| 99av国产精品欲麻豆| 免费欧美在线| 欧美伊久线香蕉线新在线| 亚洲乱亚洲高清| 在线播放国产一区中文字幕剧情欧美| 欧美日本中文| 米奇777在线欧美播放| 欧美一区二区三区在线免费观看| 99精品视频免费全部在线| 免费成人黄色| 久久午夜精品| 欧美在线黄色| 亚洲欧美在线看| 在线亚洲欧美专区二区| 亚洲精品国产精品国产自| 国内不卡一区二区三区| 国产精品素人视频| 国产精品大全| 国产精品久久久久一区二区三区共| 欧美激情一区二区三区| 欧美成人tv| 久热爱精品视频线路一| 久久免费国产精品| 久久综合99re88久久爱| 久久久综合精品| 久久久噜噜噜久久人人看| 欧美在线啊v一区| 欧美在线观看视频一区二区三区| 亚洲欧洲99久久| 欧美一区二区三区四区视频| 亚洲国产色一区| 午夜一区在线| 久久精彩视频| 玖玖国产精品视频| 久热这里只精品99re8久| 老司机午夜精品| 欧美成人午夜激情| 欧美激情国产日韩| 欧美日韩一区二区在线视频 | 亚洲精品久久久蜜桃| 欧美国产日韩一区| 亚洲精品社区| 亚洲午夜激情网页| 亚洲免费在线看| 久久精品欧美| 蜜臀av在线播放一区二区三区| 麻豆精品传媒视频| 欧美国产精品日韩| 欧美亚州一区二区三区| 国产免费一区二区三区香蕉精| 国产综合第一页| 亚洲黄色天堂| 亚洲一区二区三区在线视频| 久久电影一区| 嫩草伊人久久精品少妇av杨幂| 亚洲激情偷拍| 亚洲一区欧美激情| 久久久精彩视频| 欧美成人综合一区| 国产精品久久久久久久久久免费看| 国产精品美女在线| 在线观看成人一级片| 9色porny自拍视频一区二区| 亚洲影视中文字幕| 久久久综合精品| 亚洲精品久久久久久久久久久久| 亚洲女人小视频在线观看| 蜜桃av一区| 国产日韩欧美高清| 99riav久久精品riav| 久久国内精品自在自线400部| 欧美黄色日本| 午夜精品99久久免费| 免费在线国产精品| 国产欧美日韩另类一区| 亚洲精品视频在线| 久热精品视频在线观看一区| 99视频精品| 免费久久99精品国产自| 国产丝袜一区二区三区| 一区二区三区免费网站| 久久婷婷国产综合精品青草| 在线亚洲自拍| 欧美日本韩国一区二区三区| 亚洲第一视频网站| 欧美在线播放| 99国产精品99久久久久久| 久久久最新网址|