The-Powder-Toy/src/luaconsole.c

86 lines
1.8 KiB
C
Raw Normal View History

2011-05-30 10:22:39 -05:00
#ifdef LUACONSOLE
#include <luaconsole.h>
lua_State *l;
void luacon_open(){
const static struct luaL_reg tptluaapi [] = {
{"test", &luatpt_test},
2011-05-30 10:45:39 -05:00
{"drawtext", &luatpt_drawtext},
2011-05-30 10:22:39 -05:00
{NULL,NULL}
};
l = lua_open();
luaL_openlibs(l);
luaL_openlib(l, "tpt", tptluaapi, 0);
}
int luacon_step(){
//Nothing here yet
return 0;
}
int luacon_keypress(char key){
//Nothing here yet
return 0;
}
int luacon_eval(char *command){
return luaL_dostring (l, command);
}
char *luacon_geterror(){
char *error = lua_tostring(l, -1);
if(error==NULL || !error[0]){
error = "failed to execute";
}
return error;
}
2011-05-30 10:22:39 -05:00
void luacon_close(){
lua_close(l);
}
int process_command_lua(pixel *vid_buf, char *console, char *console_error)
{
int commandret;
char console2[15];
char console3[15];
char console4[15];
char console5[15];
//sprintf(console_error, "%s", console);
if (console && strcmp(console, "")!=0 && strncmp(console, " ", 1)!=0)
{
sscanf(console,"%14s %14s %14s %14s", console2, console3, console4, console5);
if (strcmp(console2, "quit")==0)
{
return -1;
} else {
commandret = luacon_eval(console);
if (commandret)
strcpy(console_error, luacon_geterror());
2011-05-30 10:22:39 -05:00
}
}
return 1;
}
//Being TPT interface methods:
int luatpt_test(lua_State* l)
{
int testint = 0;
testint = luaL_optint(l, 1, 0);
printf("Test successful, got %d\n", testint);
return 1;
}
2011-05-30 10:45:39 -05:00
int luatpt_drawtext(lua_State* l)
{
char *string;
int textx, texty, textred, textgreen, textblue, textalpha;
textx = luaL_optint(l, 1, 0);
texty = luaL_optint(l, 2, 0);
2011-05-30 12:25:02 -05:00
string = luaL_optstring(l, 3, "");
textred = luaL_optint(l, 4, 255);
textgreen = luaL_optint(l, 5, 255);
textblue = luaL_optint(l, 6, 255);
textalpha = luaL_optint(l, 7, 255);
2011-05-30 10:45:39 -05:00
if(vid_buf!=NULL){
drawtext(vid_buf, textx, texty, string, textred, textgreen, textblue, textalpha);
return 0;
}
return -1;
}
2011-05-30 10:22:39 -05:00
#endif