我不知道是否已经有了一些东西,但是将 Lua 嵌入到 C 应用程序中并使用自定义函数对其进行扩展非常简单(如果您了解 C),对于图形我建议使用SDL和SDL_gfx。
编辑
抱歉,Linux 不知道您在使用什么,SDL_gfx 也不支持线宽...
apt-get install libsdl-gfx1.2-dev liblua5.1-0-dev
生成文件:
CC = gcc
CFLAGS = -Wall -O2
CFLAGS += $(shell pkg-config SDL_gfx --cflags)
LIBS += $(shell pkg-config SDL_gfx --libs)
CFLAGS += $(shell pkg-config lua5.1 --cflags)
LIBS += $(shell pkg-config lua5.1 --libs)
all: test
test.o: test.c
$(CC) $(CFLAGS) -c -o $@ $<
test: test.o
$(CC) -o $@ $< $(LIBS)
.PHONY: clean
clean:
-rm -f test *.o
测试.c:
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#define lua_toUINT(L,i) ((unsigned int)lua_tointeger(L,(i)))
static SDL_Surface *out;
/* draw_circle(x, y, radius, color) */
static int draw_circle(lua_State *L) {
unsigned int x, y, r, color;
x = lua_toUINT(L, 1);
y = lua_toUINT(L, 2);
r = lua_toUINT(L, 3);
color = (lua_toUINT(L, 4) << 8) | 0xff;
circleColor(out, x, y, r, color);
return 0;
}
/* draw_line(x1, y1, x2, y2, width, color) */
static int draw_line(lua_State *L) {
unsigned int x1, y1, x2, y2, color;
x1 = lua_toUINT(L, 1);
y1 = lua_toUINT(L, 2);
x2 = lua_toUINT(L, 3);
y2 = lua_toUINT(L, 4);
/* width ignored SDL_gfx have not such thing */
color = (lua_toUINT(L, 6) << 8) | 0xff;
lineColor(out, x1, y1, x2, y2, color);
return 0;
}
int main (int argc, char *argv[]) {
SDL_Event event;
int over = 0;
lua_State *L;
L = lua_open();
luaL_openlibs(L);
lua_register(L, "draw_circle", draw_circle);
lua_register(L, "draw_line", draw_line);
SDL_Init(SDL_INIT_VIDEO);
out = SDL_SetVideoMode(640, 480, 0, 0);
(void)luaL_dofile(L,"script.lua");
SDL_UpdateRect(out, 0, 0, 0, 0);
while (!over) {
if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
over = 1;
}
}
SDL_Quit();
lua_close(L);
return 0;
}
在这里...