最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Lua教程(十九):C調(diào)用Lua

 更新時間:2015年04月30日 10:10:52   投稿:junjie  
這篇文章主要介紹了Lua教程(十九):C調(diào)用Lua,本文講解了C調(diào)用Lua基礎(chǔ)知識、table操作、調(diào)用Lua函數(shù)等內(nèi)容,需要的朋友可以參考下

1. 基礎(chǔ):

    Lua的一項重要用途就是作為一種配置語言?,F(xiàn)在從一個簡單的示例開始吧。
 

復(fù)制代碼 代碼如下:

    --這里是用Lua代碼定義的窗口大小的配置信息
    width = 200
    height = 300
 

    下面是讀取配置信息的C/C++代碼:  

復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <string.h>
#include <lua.hpp>
#include <lauxlib.h>
#include <lualib.h>

void load(lua_State* L, const char* fname, int* w, int* h) {
    if (luaL_loadfile(L,fname) || lua_pcall(L,0,0,0)) {
        printf("Error Msg is %s.\n",lua_tostring(L,-1));
        return;
    }
    lua_getglobal(L,"width");
    lua_getglobal(L,"height");
    if (!lua_isnumber(L,-2)) {
        printf("'width' should be a number\n" );
        return;
    }
    if (!lua_isnumber(L,-1)) {
        printf("'height' should be a number\n" );
        return;
    }
    *w = lua_tointeger(L,-2);
    *h = lua_tointeger(L,-1);
}


int main()
{
    lua_State* L = luaL_newstate();
    int w,h;
    load(L,"D:/test.lua",&w,&h);
    printf("width = %d, height = %d\n",w,h);
    lua_close(L);
    return 0;
}

下面是針對新函數(shù)的解釋:

    lua_getglobal是宏,其原型為:#define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, (s))。

    每次調(diào)用這個宏的時候,都會將Lua代碼中與之相應(yīng)的全局變量值壓入棧中,第一次調(diào)用時將全局變量"width"的值壓入棧中,之后再次調(diào)用時再將"height"的值也壓入棧中。

    2. table操作:

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

復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <string.h>
#include <lua.hpp>
#include <lauxlib.h>
#include <lualib.h>

void load(lua_State* L) {

    if (luaL_loadstring(L,"background = { r = 0.30, g = 0.10, b = 0 }")
        || lua_pcall(L,0,0,0)) {
        printf("Error Msg is %s.\n",lua_tostring(L,-1));
        return;
    }
    lua_getglobal(L,"background");
    if (!lua_istable(L,-1)) {
        printf("'background' is not a table.\n" );
        return;
    }
    lua_getfield(L,-1,"r");
    if (!lua_isnumber(L,-1)) {
        printf("Invalid component in background color.\n");
        return;
    }
    int r = (int)(lua_tonumber(L,-1) * 255);
    lua_pop(L,1);
    lua_getfield(L,-1,"g");
    if (!lua_isnumber(L,-1)) {
        printf("Invalid component in background color.\n");
        return;
    }
    int g = (int)(lua_tonumber(L,-1) * 255);
    lua_pop(L,1);

    lua_pushnumber(L,0.4);
    lua_setfield(L,-2,"b");

    lua_getfield(L,-1,"b");
    if (!lua_isnumber(L,-1)) {
        printf("Invalid component in background color.\n");
        return;
    }
    int b = (int)(lua_tonumber(L,-1) * 255);
    printf("r = %d, g = %d, b = %d\n",r,g,b);
    lua_pop(L,1);
    lua_pop(L,1);
    return;
}

int main()
{
    lua_State* L = luaL_newstate();
    load(L);
    lua_close(L);
    return 0;
}

void lua_getfield(lua_State *L, int idx, const char *k); 第二個參數(shù)是table變量在棧中的索引值,最后一個參數(shù)是table的鍵值,該函數(shù)執(zhí)行成功后會將字段值壓入棧中。

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

復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <string.h>
#include <lua.hpp>
#include <lauxlib.h>
#include <lualib.h>

void load(lua_State* L)
{
    lua_newtable(L);
    lua_pushnumber(L,0.3);
    lua_setfield(L,-2,"r");

    lua_pushnumber(L,0.1);
    lua_setfield(L,-2,"g");

    lua_pushnumber(L,0.4);
    lua_setfield(L,-2,"b");
    lua_setglobal(L,"background");

    lua_getglobal(L,"background");
    if (!lua_istable(L,-1)) {
        printf("'background' is not a table.\n" );
        return;
    }
    lua_getfield(L,-1,"r");
    if (!lua_isnumber(L,-1)) {
        printf("Invalid component in background color.\n");
        return;
    }
    int r = (int)(lua_tonumber(L,-1) * 255);
    lua_pop(L,1);
    lua_getfield(L,-1,"g");
    if (!lua_isnumber(L,-1)) {
        printf("Invalid component in background color.\n");
        return;
    }
    int g = (int)(lua_tonumber(L,-1) * 255);
    lua_pop(L,1);

    lua_getfield(L,-1,"b");
    if (!lua_isnumber(L,-1)) {
        printf("Invalid component in background color.\n");
        return;
    }
    int b = (int)(lua_tonumber(L,-1) * 255);
    printf("r = %d, g = %d, b = %d\n",r,g,b);
    lua_pop(L,1);
    lua_pop(L,1);
    return;
}

int main()
{
    lua_State* L = luaL_newstate();
    load(L);
    lua_close(L);
    return 0;
}

 上面的代碼將輸出和之前代碼相同的結(jié)果。

    lua_newtable是宏,其原型為:#define lua_newtable(L) lua_createtable(L, 0, 0)。調(diào)用該宏后,Lua會生成一個新的table對象并將其壓入棧中。

    lua_setglobal是宏,其原型為:#define lua_setglobal(L,s) lua_setfield(L,LUA_GLOBALSINDEX,(s))。調(diào)用該宏后,Lua會將當前棧頂?shù)闹蒂x值給第二個參數(shù)指定的全局變量名。該宏在執(zhí)行成功后,會將剛剛賦值的值從棧頂彈出。

    3. 調(diào)用Lua函數(shù):

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

復(fù)制代碼 代碼如下:

 #include <stdio.h>
#include <string.h>
#include <lua.hpp>
#include <lauxlib.h>
#include <lualib.h>

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

void call_function(lua_State* L)
{
    //luaL_dostring 等同于luaL_loadstring() || lua_pcall()
    //注意:在能夠調(diào)用Lua函數(shù)之前必須執(zhí)行Lua腳本,否則在后面實際調(diào)用Lua函數(shù)時會報錯,
    //錯誤信息為:"attempt to call a nil value."
    if (luaL_dostring(L,lua_function_code)) {
        printf("Failed to run lua code.\n");
        return;
    }
    double x = 1.0, y = 2.3;
    lua_getglobal(L,"add");
    lua_pushnumber(L,x);
    lua_pushnumber(L,y);
    //下面的第二個參數(shù)表示帶調(diào)用的lua函數(shù)存在兩個參數(shù)。
    //第三個參數(shù)表示即使帶調(diào)用的函數(shù)存在多個返回值,那么也只有一個在執(zhí)行后會被壓入棧中。
    //lua_pcall調(diào)用后,虛擬棧中的函數(shù)參數(shù)和函數(shù)名均被彈出。
    if (lua_pcall(L,2,1,0)) {
        printf("error is %s.\n",lua_tostring(L,-1));
        return;
    }
    //此時結(jié)果已經(jīng)被壓入棧中。
    if (!lua_isnumber(L,-1)) {
        printf("function 'add' must return a number.\n");
        return;
    }
    double ret = lua_tonumber(L,-1);
    lua_pop(L,-1); //彈出返回值。
    printf("The result of call function is %f.\n",ret);
}

int main()
{
    lua_State* L = luaL_newstate();
    call_function(L);
    lua_close(L);
    return 0;
}
 

相關(guān)文章

  • Lua中函數(shù)與面向?qū)ο缶幊痰幕A(chǔ)知識整理

    Lua中函數(shù)與面向?qū)ο缶幊痰幕A(chǔ)知識整理

    函數(shù)在面對對象的編程中又被叫做方法,會受到作用域的制約,Lua中具有類等面向?qū)ο蟮奶匦?接下來我們就來看一下Lua中函數(shù)與面向?qū)ο缶幊痰幕A(chǔ)知識整理
    2016-06-06
  • Lua中遍歷文件操作代碼實例

    Lua中遍歷文件操作代碼實例

    這篇文章主要介紹了Lua中遍歷文件操作代碼實例,本文直接給出示例代碼,需要的朋友可以參考下
    2015-05-05
  • Lua中的協(xié)同程序探究

    Lua中的協(xié)同程序探究

    這篇文章主要介紹了Lua中的協(xié)同程序探究,本文講解了什么是協(xié)同程序、創(chuàng)建協(xié)同程序、更像樣的協(xié)同程序、讓協(xié)同程序掛起、resume操作的返回值,需要的朋友可以參考下
    2014-09-09
  • Lua教程(三):值與類型介紹

    Lua教程(三):值與類型介紹

    這篇文章主要介紹了Lua教程(三):值與類型介紹,本文起講解了Lua的八種基本類型、userdata、thread、table等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • Lua loadstring函數(shù)用法實例

    Lua loadstring函數(shù)用法實例

    這篇文章主要介紹了Lua loadstring函數(shù)用法實例,loadstring最典型的用處是去執(zhí)行外部代碼,也就是位于程序之外的代碼,需要的朋友可以參考下
    2015-04-04
  • Lua中的一些常用函數(shù)庫實例講解

    Lua中的一些常用函數(shù)庫實例講解

    這篇文章主要介紹了Lua中的一些常用函數(shù)庫實例講解,本文講解了數(shù)學庫、table庫、字符串庫、I/O庫等常用函數(shù)庫,需要的朋友可以參考下
    2014-09-09
  • Lua中實現(xiàn)sleep函數(shù)功能的4種方法

    Lua中實現(xiàn)sleep函數(shù)功能的4種方法

    這篇文章主要介紹了Lua中實現(xiàn)sleep函數(shù)功能的4種方法,本文講解了在一個死循環(huán)中設(shè)置一個跳出條件方法、調(diào)用系統(tǒng)的sleep函數(shù)法、Windows下ping命令法、socket庫中select函數(shù)法4種方法,需要的朋友可以參考下
    2015-04-04
  • Lua判斷一個目錄或文件是否存在的方法

    Lua判斷一個目錄或文件是否存在的方法

    這篇文章主要介紹了Lua判斷一個目錄或文件是否存在的方法,Lua中可以使用io.open判斷文件或目錄是否存在,本文總結(jié)了判斷方法,并給出了一個自定義函數(shù),需要的朋友可以參考下
    2015-04-04
  • 舉例詳解Lua中的協(xié)同程序編程

    舉例詳解Lua中的協(xié)同程序編程

    這篇文章主要介紹了Lua中的協(xié)同程序編程,是Lua入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • Lua中rawset和rawget的作用淺析

    Lua中rawset和rawget的作用淺析

    這篇文章主要介紹了Lua中rawset和rawget的作用淺析,本文分別用兩段代碼演示了rawset和rawget的作用,需要的朋友可以參考下
    2015-04-04

最新評論

库尔勒市| 逊克县| 无为县| 宁陕县| 张家港市| 赤水市| 汾阳市| 昭苏县| 松溪县| 仪陇县| 乌苏市| 家居| 桐庐县| 藁城市| 巴林左旗| 浪卡子县| 定兴县| 赣州市| 临武县| 恩施市| 沽源县| 北京市| 江孜县| 夏河县| 紫云| 张家界市| 平潭县| 锡林郭勒盟| 长岛县| 临西县| 东丰县| 合肥市| 江川县| 修水县| 五大连池市| 涿鹿县| 赞皇县| 拜泉县| 牙克石市| 丹寨县| 赫章县|