有带有图形输出的Lua测试器吗?

我正在寻找一个能够测试带有图形输出的 Lua 代码的应用程序或者服务。

http://ideone.com 或者 http://codepad.org 这样的只有基础文本输出的就不太行。

很多游戏都使用 Lua 脚本,大多数的脚本都有类似 draw_circle()draw_line() 这样的函数。我需要一款工具,能够输出这类函数在二维图上的结果。

可以给我输出以下代码结果的工具:

--会画出两个圆,且用一条直线链接它们的中心
--draw_circle(x, y, radius, color)
draw_circle(300, 300, 100, 0xff0000)
draw_circle(600, 300, 100, 0x00ff00)
--draw_line(x1, y1, x2, y2, width, color)
draw_line(300, 300, 600, 300, 1, 0x0000ff)

是否有这样的工具呢? (如果有用其他语言开发的也可以)

点赞
用户2498790
用户2498790

我不知道是否已经有了,但在C应用程序中嵌入Lua并使用自定义函数进行扩展真的很简单(如果你知道C)。对于图形,我建议使用SDLSDL_gfx

编辑

很抱歉这是针对Linux的,不知道您在使用什么。此外,SDL_gfx不支持线条宽度...

apt-get install libsdl-gfx1.2-dev liblua5.1-0-dev

Makefile:

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

test.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;
}

而这就是...

result window

2013-10-23 10:29:54
用户2498790
用户2498790

所以,我花了一些时间尝试在Windows上让示例程序运行,但所有的解决方案都太复杂或/和需要安装太多东西。

相反,我找到了一个非常不错和强大的库叫做CD(Canvas Draw),它已经有适用于Windows的预编译二进制文件

(我使用了dynamic/ Win32_dll8版本)

这个库非常强大,可以输出到很多系统和很多格式(svg,pdf等),但是我想要输出到一个通用的窗口应用程序,因此需要另一个库,[IUP] (http://www.tecgraf.puc-rio.br/iup/),再次用于[适用于Windows的预编译二进制文件](http://sourceforge.net/projects/iup/files/3.8/Windows%20Libraries/)

(同样是dynamic/ Win32_dll8

我不得不将两个库和Lua5.1提取到同一个目录中(但我想在Lua中添加搜索路径来查找.dll

此外,还需要在同一目录中使用帮助程序iupcdaux.lua

编辑

这里是获得您的示例相同输出的脚本:

app.lua:

require "iupcdaux"

dialog = iupcdaux.new_dialog(640, 480)
canvas = dialog[1]

function hex2rgb(hex)
    hex = string.format("%06x", hex)
    return cd.EncodeColor(tonumber("0x"..hex:sub(1,2)),
           tonumber("0x"..hex:sub(3,4)),
           tonumber("0x"..hex:sub(5,6)))
end

function draw_circle(x, y, r, rgb)
  canvas:LineWidth(1)
  canvas:Foreground(hex2rgb(rgb))
  canvas:Arc(x, y, r*2, r*2, 0, 360)
end

function draw_line(x1, y1, x2, y2, w, rgb)
  canvas:LineWidth(w)
  canvas:Foreground(hex2rgb(rgb))
  canvas:Line(x1, y1, x2, y2)
end

function canvas:Draw(canvas)
  dofile("script.lua")
end

dialog:show()
iup.MainLoop()

script.lua:

draw_circle(300, 300, 100, 0xff0000)
draw_circle(600, 300, 100, 0xff00)
draw_line(300, 300, 600, 300, 1, 0xff)

在这里,此app.lua脚本查找具有与您的示例相同语法的脚本script.lua

最小设置:

├── cd.dll
├── cdlua51.dll
├── app.lua
├── freetype6.dll
├── iupcdaux.lua
├── iupcd.dll
├── iup.dll
├── iuplua51.dll
├── iupluacd51.dll
├── lua5.1.dll
├── lua5.1.exe
├── Microsoft.VC80.CRT
│   ├── Microsoft.VC80.CRT.manifest
│   ├── msvcm80.dll
│   ├── msvcp80.dll
│   └── msvcr80.dll
├── script.lua
└── zlib1.dll
2013-10-25 15:04:35