1

我正在寻找可以使用图形输出测试 Lua 代码的应用程序或服务。像http://ideone.comhttp://codepad.org之类的东西,但不仅仅是基于文本的输出。

很多游戏使用lua编写脚本,其中大部分是draw_circle()和draw_line()等函数。我需要一个可以在二维图形上输出具有此类功能的代码的工具。可以给我这样的代码结果的东西:

--should draw two circles and connect their centers with a 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)

有没有类似的东西?(C 或其他语言的东西也会很好)

4

2 回答 2

2

我不知道是否已经有了一些东西,但是将 Lua 嵌入到 C 应用程序中并使用自定义函数对其进行扩展非常简单(如果您了解 C),对于图形我建议使用SDLSDL_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;
}

在这里...

结果窗口

于 2013-10-23T10:29:54.870 回答
1

因此,我花了一些时间尝试让示例在 Windows 上运行,但所有解决方案都太复杂或/并且需要安装太多。

相反,找到了一个非常好用且功能强大的库,称为CD (Canvas Draw),其中包含已为 Windows 预编译的二进制文件

(我用的是dynamic/Win32_dll8版本)

该库非常强大,可以输出到很多系统和多种格式(svg、pdf 等),但我想输出到一个常见的窗口应用程序,所以需要另一个库IUP,再次使用适用于 Windows 的预编译二进制文件

(再次dynamic/ Win32_dll8

我不得不将库和Lua5.1都提取到同一个目录中(但我猜 Lua 有某种方法可以添加搜索路径.dll

在同一目录iupcdaux.lua中还需要一个帮助脚本

编辑

并且有脚本可以为您的示例获得相同的输出:

应用程序.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)
  canvas:LineStyle(cd.CONTINUOUS) 
  dofile("script.lua")
end

dialog:show()
iup.MainLoop()

脚本.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-25T15:04:35.570 回答