• <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>

            每天早晨叫醒你的不是鬧鐘,而是夢(mèng)想

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              62 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

             1. 基礎(chǔ):
                Lua的一項(xiàng)重要用途就是作為一種配置語言。現(xiàn)在從一個(gè)簡(jiǎn)單的示例開始吧。
                --這里是用Lua代碼定義的窗口大小的配置信息
                width = 200
                height = 300
                下面是讀取配置信息的C/C++代碼:   

            復(fù)制代碼
            1 #include <stdio.h>  
            2
            #include <string.h>
            3
            #include <lua.hpp>
            4
            #include <lauxlib.h>
            5
            #include <lualib.h>
            6

            7
            void load(lua_State* L, const char* fname, int* w, int* h) {
            8
            if (luaL_loadfile(L,fname) || lua_pcall(L,0,0,0)) {
            9
            printf("Error Msg is %s.\n",lua_tostring(L,-1));
            10 return;
            11 }
            12 lua_getglobal(L,"width");
            13 lua_getglobal(L,"height");
            14 if (!lua_isnumber(L,-2)) {
            15 printf("'width' should be a number\n" );
            16 return;
            17 }
            18 if (!lua_isnumber(L,-1)) {
            19 printf("'height' should be a number\n" );
            20 return;
            21 }
            22 *w = lua_tointeger(L,-2);
            23 *h = lua_tointeger(L,-1);
            24 }
            25
            26
            27 int main()
            28 {
            29 lua_State* L = luaL_newstate();
            30 int w,h;
            31 load(L,"D:/test.lua",&w,&h);
            32 printf("width = %d, height = %d\n",w,h);
            33 lua_close(L);
            34 return 0;
            35 }
            復(fù)制代碼

                下面是針對(duì)新函數(shù)的解釋:
                lua_getglobal是宏,其原型為:#define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, (s))
                每次調(diào)用這個(gè)宏的時(shí)候,都會(huì)將Lua代碼中與之相應(yīng)的全局變量值壓入棧中,第一次調(diào)用時(shí)將全局變量"width"的值壓入棧中,之后再次調(diào)用時(shí)再將"height"的值也壓入棧中。

                2. table操作:
                我們可以在C語言的代碼中操作Lua中的table數(shù)據(jù),這是一個(gè)非常非常方便且實(shí)用的功能。這樣不僅可以使Lua代碼的結(jié)構(gòu)更加清晰,也可以在C語言代碼中定義等同的結(jié)構(gòu)體與之對(duì)應(yīng),從而大大提高代碼的可讀性。見如下代碼:

            復(fù)制代碼
            1 #include <stdio.h>  
            2
            #include <string.h>
            3
            #include <lua.hpp>
            4
            #include <lauxlib.h>
            5
            #include <lualib.h>
            6

            7
            void load(lua_State* L) {
            8

            9 if (luaL_loadstring(L,"background = { r = 0.30, g = 0.10, b = 0 }")
            10 || lua_pcall(L,0,0,0)) {
            11 printf("Error Msg is %s.\n",lua_tostring(L,-1));
            12 return;
            13 }
            14 lua_getglobal(L,"background");
            15 if (!lua_istable(L,-1)) {
            16 printf("'background' is not a table.\n" );
            17 return;
            18 }
            19 lua_getfield(L,-1,"r");
            20 if (!lua_isnumber(L,-1)) {
            21 printf("Invalid component in background color.\n");
            22 return;
            23 }
            24 int r = (int)(lua_tonumber(L,-1) * 255);
            25 lua_pop(L,1);
            26 lua_getfield(L,-1,"g");
            27 if (!lua_isnumber(L,-1)) {
            28 printf("Invalid component in background color.\n");
            29 return;
            30 }
            31 int g = (int)(lua_tonumber(L,-1) * 255);
            32 lua_pop(L,1);
            33
            34 lua_pushnumber(L,0.4);
            35 lua_setfield(L,-2,"b");
            36
            37 lua_getfield(L,-1,"b");
            38 if (!lua_isnumber(L,-1)) {
            39 printf("Invalid component in background color.\n");
            40 return;
            41 }
            42 int b = (int)(lua_tonumber(L,-1) * 255);
            43 printf("r = %d, g = %d, b = %d\n",r,g,b);
            44 lua_pop(L,1);
            45 lua_pop(L,1);
            46 return;
            47 }
            48
            49 int main()
            50 {
            51 lua_State* L = luaL_newstate();
            52 load(L);
            53 lua_close(L);
            54 return 0;
            55 }
            復(fù)制代碼

                void lua_getfield(lua_State *L, int idx, const char *k); 第二個(gè)參數(shù)是table變量在棧中的索引值,最后一個(gè)參數(shù)是table的鍵值,該函數(shù)執(zhí)行成功后會(huì)將字段值壓入棧中。
                void lua_setfield(lua_State *L, int idx, const char *k); 第二個(gè)參數(shù)是table變量在棧中的索引值,最后一個(gè)參數(shù)是table的鍵名稱,而字段值是通過上一條命令lua_pushnumber(L,0.4)壓入到棧中的,該函數(shù)在執(zhí)行成功后會(huì)將剛剛壓入的字段值彈出棧。
                
                下面的代碼示例是在C語言代碼中構(gòu)造table對(duì)象,同時(shí)初始化table的字段值,最后再將table對(duì)象賦值給Lua中的一個(gè)全局變量。

            復(fù)制代碼
            1 #include <stdio.h>  
            2
            #include <string.h>
            3
            #include <lua.hpp>
            4
            #include <lauxlib.h>
            5
            #include <lualib.h>
            6

            7
            void load(lua_State* L)
            8
            {
            9
            lua_newtable(L);
            10 lua_pushnumber(L,0.3);
            11 lua_setfield(L,-2,"r");
            12
            13 lua_pushnumber(L,0.1);
            14 lua_setfield(L,-2,"g");
            15
            16 lua_pushnumber(L,0.4);
            17 lua_setfield(L,-2,"b");
            18 lua_setglobal(L,"background");
            19
            20 lua_getglobal(L,"background");
            21 if (!lua_istable(L,-1)) {
            22 printf("'background' is not a table.\n" );
            23 return;
            24 }
            25 lua_getfield(L,-1,"r");
            26 if (!lua_isnumber(L,-1)) {
            27 printf("Invalid component in background color.\n");
            28 return;
            29 }
            30 int r = (int)(lua_tonumber(L,-1) * 255);
            31 lua_pop(L,1);
            32 lua_getfield(L,-1,"g");
            33 if (!lua_isnumber(L,-1)) {
            34 printf("Invalid component in background color.\n");
            35 return;
            36 }
            37 int g = (int)(lua_tonumber(L,-1) * 255);
            38 lua_pop(L,1);
            39
            40 lua_getfield(L,-1,"b");
            41 if (!lua_isnumber(L,-1)) {
            42 printf("Invalid component in background color.\n");
            43 return;
            44 }
            45 int b = (int)(lua_tonumber(L,-1) * 255);
            46 printf("r = %d, g = %d, b = %d\n",r,g,b);
            47 lua_pop(L,1);
            48 lua_pop(L,1);
            49 return;
            50 }
            51
            52 int main()
            53 {
            54 lua_State* L = luaL_newstate();
            55 load(L);
            56 lua_close(L);
            57 return 0;
            58 }
            復(fù)制代碼

                上面的代碼將輸出和之前代碼相同的結(jié)果。
                lua_newtable是宏,其原型為:#define lua_newtable(L) lua_createtable(L, 0, 0)。調(diào)用該宏后,Lua會(huì)生成一個(gè)新的table對(duì)象并將其壓入棧中。
                lua_setglobal是宏,其原型為:#define lua_setglobal(L,s) lua_setfield(L,LUA_GLOBALSINDEX,(s))。調(diào)用該宏后,Lua會(huì)將當(dāng)前棧頂?shù)闹蒂x值給第二個(gè)參數(shù)指定的全局變量名。該宏在執(zhí)行成功后,會(huì)將剛剛賦值的值從棧頂彈出。

                3. 調(diào)用Lua函數(shù):
                調(diào)用函數(shù)的API也很簡(jiǎn)單。首先將待調(diào)用函數(shù)壓入棧,再壓入函數(shù)的參數(shù),然后使用lua_pcall進(jìn)行實(shí)際的調(diào)用,最后將調(diào)用結(jié)果從棧中彈出。見如下代碼:

            復(fù)制代碼
            1 #include <stdio.h>  
            2
            #include <string.h>
            3
            #include <lua.hpp>
            4
            #include <lauxlib.h>
            5
            #include <lualib.h>
            6

            7
            const char* lua_function_code = "function add(x,y) return x + y end";
            8

            9
            void call_function(lua_State* L)
            10 {
            11 //luaL_dostring 等同于luaL_loadstring() || lua_pcall()
            12 //注意:在能夠調(diào)用Lua函數(shù)之前必須執(zhí)行Lua腳本,否則在后面實(shí)際調(diào)用Lua函數(shù)時(shí)會(huì)報(bào)錯(cuò),
            13 //錯(cuò)誤信息為:"attempt to call a nil value."
            14 if (luaL_dostring(L,lua_function_code)) {
            15 printf("Failed to run lua code.\n");
            16 return;
            17 }
            18 double x = 1.0, y = 2.3;
            19 lua_getglobal(L,"add");
            20 lua_pushnumber(L,x);
            21 lua_pushnumber(L,y);
            22 //下面的第二個(gè)參數(shù)表示帶調(diào)用的lua函數(shù)存在兩個(gè)參數(shù)。
            23 //第三個(gè)參數(shù)表示即使帶調(diào)用的函數(shù)存在多個(gè)返回值,那么也只有一個(gè)在執(zhí)行后會(huì)被壓入棧中。
            24 //lua_pcall調(diào)用后,虛擬棧中的函數(shù)參數(shù)和函數(shù)名均被彈出。
            25 if (lua_pcall(L,2,1,0)) {
            26 printf("error is %s.\n",lua_tostring(L,-1));
            27 return;
            28 }
            29 //此時(shí)結(jié)果已經(jīng)被壓入棧中。
            30 if (!lua_isnumber(L,-1)) {
            31 printf("function 'add' must return a number.\n");
            32 return;
            33 }
            34 double ret = lua_tonumber(L,-1);
            35 lua_pop(L,-1); //彈出返回值。
            36 printf("The result of call function is %f.\n",ret);
            37 }
            38
            39 int main()
            40 {
            41 lua_State* L = luaL_newstate();
            42 call_function(L);
            43 lua_close(L);
            44 return 0;
            45 }
            復(fù)制代碼
            posted on 2014-02-17 17:44 沛沛 閱讀(357) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Script
            久久精品人成免费| 青青草原综合久久大伊人| www.久久热| 欧美精品一本久久男人的天堂| 97久久超碰国产精品2021| 精品国产乱码久久久久久郑州公司 | 国产91色综合久久免费| 日本精品久久久久中文字幕8| 久久精品国产99久久丝袜| 99久久夜色精品国产网站| 国产真实乱对白精彩久久| 久久天天躁夜夜躁狠狠躁2022 | 丁香色欲久久久久久综合网| 丁香狠狠色婷婷久久综合| 色天使久久综合网天天| 国产L精品国产亚洲区久久| 久久夜色精品国产网站| 亚洲精品国精品久久99热| 久久se精品一区精品二区| 老男人久久青草av高清| 久久精品二区| 久久99精品九九九久久婷婷| 久久精品国产99久久久| 国产精品久久久久久久人人看| 国产综合免费精品久久久| 99久久无码一区人妻a黑| 久久国产色av免费看| 亚洲国产日韩欧美综合久久| 品成人欧美大片久久国产欧美...| 人妻少妇久久中文字幕| 久久精品国产亚洲AV忘忧草18| 欧美久久一区二区三区| 久久精品国产色蜜蜜麻豆| 国产成人精品久久亚洲| 91久久精品视频| 超级碰久久免费公开视频| 久久精品国产黑森林| 日韩欧美亚洲国产精品字幕久久久 | Xx性欧美肥妇精品久久久久久| 久久久久久久女国产乱让韩| 成人精品一区二区久久久|