lua --- 表操作

c api 参考手册:http://www.leeon.me/a/lua-c-api-manual

  1 // LuaTest.cpp : 定义控制台应用程序的入口点。
  2 //
  3 #include "stdafx.h"
  4 #pragma comment (lib,"Lua.lib")
  5 #include "lua.hpp"
  6 #include<iostream>
  7 
  8 #define MAX_COLOR 255
  9 
 10 //设置窗口的size
 11 void setWinSize(char *fileName, int *width, int *height)
 12 {
 13     lua_State *lua_state = luaL_newstate();
 14     luaL_openlibs(lua_state);
 15 
 16     if (luaL_loadfile(lua_state, fileName) || lua_pcall(lua_state, 0, 0, 0))
 17     {
 18         luaL_error(lua_state, "cannot run configuration fiile: %s", lua_tostring(lua_state, -1));
 19     }
 20 
 21     //变量压入栈
 22     lua_getglobal(lua_state, "width");
 23     lua_getglobal(lua_state, "height");
 24 
 25     if (!lua_isnumber(lua_state, -2))
 26     {
 27         luaL_error(lua_state, " 'width' should be a number\n ");
 28     }
 29 
 30     if (!lua_isnumber(lua_state, -1))
 31     {
 32         luaL_error(lua_state, " 'height' should be a number\n ");
 33     }
 34 
 35     *width = (int)lua_tonumber(lua_state, -2);
 36     *height = (int)lua_tonumber(lua_state, -1);
 37 
 38     lua_close(lua_state);
 39 }
 40 
 41 //调函数之前需要假设栈顶元素为一个有效的颜色 table
 42 int getField(lua_State *lua_state, char *key)
 43 {
 44     int result = 0;
 45 
 46     //压入元素
 47     lua_pushstring(lua_state, key);
 48 
 49     //将原来栈顶的元素弹出,以栈顶的值作为key来访问 - 2位置上的table 并将其值放入栈顶
 50     lua_gettable(lua_state, -2);// 第二个参数为 table 在栈中的位置参数
 51 
 52     if (!lua_isnumber(lua_state, -1))
 53     {
 54         luaL_error(lua_state, "invalid component in background color");
 55     }
 56 
 57     result = (int)lua_tonumber(lua_state, -1) * MAX_COLOR;
 58 
 59     lua_pop(lua_state, 1);
 60 
 61     return result;
 62 }
 63 
 64 void setWinBackgroundColor(char *fileName, int *r, int *g, int *b)
 65 {
 66     lua_State *lua_state = luaL_newstate();
 67     luaL_openlibs(lua_state);
 68 
 69     if (luaL_loadfile(lua_state, fileName) || lua_pcall(lua_state, 0, 0, 0))
 70     {
 71         luaL_error(lua_state, "cannot run configuration fiile: %s", lua_tostring(lua_state, -1));
 72     }
 73     
 74     //将背景颜色压入栈
 75     lua_getglobal(lua_state, "background");
 76     if (!lua_istable(lua_state, -1))
 77     {
 78         luaL_error(lua_state, "'background' is not a valid color table");
 79     }
 80 
 81     *r = getField(lua_state, "r");
 82     *g = getField(lua_state, "g");
 83     *b = getField(lua_state, "b");
 84 
 85     lua_close(lua_state);
 86 }
 87 
 88 void testSetWndBackground()
 89 {
 90     int _width = 0, _height = 0;
 91     setWinSize("Config.lua", &_width, &_height);
 92     printf("width = %d\n", _width);
 93     printf("height = %d\n", _height);
 94 
 95     int r = 0, g = 0, b = 0;
 96     setWinBackgroundColor("Config.lua", &r, &g, &b);
 97     printf("backgroundColor = (%d, %d, %d)\n", r, g, b);
 98 }
 99 
100 //在应用中定义颜色
101 struct  ColorTable
102 {
103     char *name;
104     unsigned char red, green, blue;
105 }colortable[] =
106 {
107     { "WHITE", MAX_COLOR, MAX_COLOR, MAX_COLOR },
108     { "RED", MAX_COLOR, 0, 0 },
109     { "GREEN", 0, MAX_COLOR, 0 },
110     { "BLUE", 0, 0, MAX_COLOR },
111     { "BLACK", 0, 0, 0 },
112     { NULL, 0, 0, 0 }
113 };
114 
115 void setfield(lua_State *L, char *key, int value)
116 {
117     lua_pushstring(L, key);                            //key
118     lua_pushnumber(L, (double)(value / MAX_COLOR));    //value
119     //以栈顶元素作为 value,以栈顶的下一个元素作为 key,调用完成后弹出栈顶的两个元素
120     lua_settable(L, -3);
121 }
122 
123 void setcolor(lua_State *L, struct ColorTable *colTab)
124 {
125     lua_newtable(L);   //创建一个新的 table,然后将其入栈
126     setfield(L, "r", colTab->red);
127     setfield(L, "g", colTab->green);
128     setfield(L, "b", colTab->blue);
129     lua_setglobal(L, colTab->name);  //将 table 出栈并将其赋给一个全局变量名
130 }
131 
132 void registerAllColor(lua_State *L, struct ColorTable *colTab)
133 {
134     int i = 0;
135     while (colTab[i].name != NULL)
136     {
137         setcolor(L, &colTab[i++]);
138     }
139 }
140 
141 void testRegisterAllColor()
142 {
143     lua_State *l = luaL_newstate();
144     luaL_openlibs(l);
145     registerAllColor(l, colortable);
146     lua_close(l);
147 }
148 
149 int _tmain(int argc, _TCHAR* argv[])
150 {
151     testSetWndBackground();
152     testRegisterAllColor();
153     system("pause");
154     return 0;
155 }

 

posted @ 2019-04-14 18:52  小·糊涂仙  阅读(445)  评论(0编辑  收藏  举报