Reputation: 584
I'm searching for a application or a service where I can test Lua code with graphical output. Something like http://ideone.com or http://codepad.org, but not only text based output.
A lot of games use lua to script, in most of them are functions like draw_circle() and draw_line(). I need a tool that can output code with such functions on a 2D graph. Something that can give me the result of code like this:
--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)
Is there something like that? (Something in C or other languages would be nice too)
Upvotes: 1
Views: 522
Reputation: 3454
So, I've spent some time trying to getting the example working on Windows but all the solutions were too complex or/and required too much to install.
Instead, have found a very nice and powerful library called CD (Canvas Draw) with already precompiled binaries for Windows
(I used the dynamic
/Win32_dll8
version)
The library is so powerful that can output to a lot of system and with a lot of formats (svg, pdf etc.), but I'd like to output to a common window application, so another library is needed, IUP, again with precompiled binaries for Windows
(again dynamic
/Win32_dll8
)
I had to extract both the libraries and Lua5.1 to the same directory (but I guess there's some way on Lua to add search path for .dll
)
Also an helper script is needed in the same directory iupcdaux.lua
edit
And there's the script to get the same output for your example:
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)
canvas:LineStyle(cd.CONTINUOUS)
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)
Here, this app.lua
script look for a script script.lua
with the same syntax of your example.
The minimal setup:
├── 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
Upvotes: 1
Reputation: 3454
I don't know if there's something already but embedding Lua in a C application and extending it with custom functions is really simple (if you know C), for the graphic I would suggest SDL and SDL_gfx.
edit
Sorry it is for Linux don't know what are you using, also SDL_gfx doesn't support width for lines...
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;
}
And here it is...
Upvotes: 2