Skip to content

Commit beffdf0

Browse files
Lua API
1 parent def32d2 commit beffdf0

File tree

6 files changed

+86
-32
lines changed

6 files changed

+86
-32
lines changed

example/hello.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
print("Hello World")
1+
function hello(e, instance)
2+
print("Hello World")
3+
end
4+
5+
function destruction(e, instance)
6+
while (true)
7+
do
8+
print("go to link")
9+
end
10+
end
11+
12+
function create_room(e, instance)
13+
action(e, instance, '{ "type": "music", "file": "background.ogg" }')
14+
action(e, instance, '{ "type": "gravity", "h": 0, "v": 50 }')
15+
action(e, instance, '{ "type": "spawn", "object": "rocket.json", "x": 50, "y": 240 }')
16+
action(e, instance, '{ "type": "spawn", "object": "obstacle_t.json", "x": 400, "y": 50 }')
17+
action(e, instance, '{ "type": "spawn", "object": "obstacle.json", "x": 400, "y": 400 }')
18+
action(e, instance, '{ "type": "timer", "num": 0, "time": 10 }')
19+
end

example/rocket.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
"type": "uri",
6666
"link": "https://github.com/EmperorPenguin18"
6767
},
68+
{
69+
"type": "lua_async",
70+
"function": "destruction"
71+
},
6872
{
6973
"type": "destroy"
7074
}

example/room.json

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,8 @@
1010
],
1111
"creation": [
1212
{
13-
"type": "music",
14-
"file": "background.ogg"
15-
},
16-
{
17-
"type": "gravity",
18-
"h": 0,
19-
"v": 50
20-
},
21-
{
22-
"type": "spawn",
23-
"object": "rocket.json",
24-
"x": 50,
25-
"y": 240
26-
},
27-
{
28-
"type": "spawn",
29-
"object": "obstacle_t.json",
30-
"x": 400,
31-
"y": 50
32-
},
33-
{
34-
"type": "spawn",
35-
"object": "obstacle.json",
36-
"x": 400,
37-
"y": 400
38-
},
39-
{
40-
"type": "timer",
41-
"num": 0,
42-
"time": 10
13+
"type": "lua",
14+
"function": "create_room"
4315
}
4416
],
4517
"timer_0": [

src/actions.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,30 @@ static void action_reload(Engine* e, Instance* instance, char* args) {
211211
action_close(e, NULL, NULL);
212212
}
213213

214+
/* Run a Lua function. See action documentation. */
215+
static void action_lua(Engine* e, Instance* instance, char* args) {
216+
char* function = args;
217+
lua_getglobal(e->L, function);
218+
lua_pushlightuserdata(e->L, e);
219+
lua_pushlightuserdata(e->L, instance);
220+
lua_pcall(e->L, 2, 0, 0);
221+
}
222+
223+
static int thread_function(void* L) {
224+
lua_pcall(L, 2, 0, 0);
225+
return 0;
226+
}
227+
228+
/* Run a Lua function in another thread. See action documentation. */
229+
static void action_lua_async(Engine* e, Instance* instance, char* args) {
230+
char* function = args;
231+
/*lua_getglobal(e->L, function);
232+
lua_pushlightuserdata(e->L, e);
233+
lua_pushlightuserdata(e->L, instance);
234+
SDL_CreateThread(thread_function, function, e->L);*/
235+
SDL_Log("Not implemented");
236+
}
237+
214238
/* Fill in memory with specified structure.
215239
* Variadic arguments must be in pairs of two.
216240
* STRING is string, INTEGER is number, and REAL is bool.
@@ -274,6 +298,7 @@ static char* args_generator(zpl_json_object* json, size_t n, ...) {
274298
/* Based on the type provided, setup args so calling an action is faster. */
275299
static int action_handler(Action* action, zpl_json_object* json, Asset* assets, const size_t assets_num) {
276300
char* type = args_generator(json, 1, "type", ZPL_ADT_TYPE_STRING);
301+
if (type == NULL) return -1;
277302

278303
if (strcmp(type, "spawn") == 0) {
279304
action->args =
@@ -388,6 +413,18 @@ static int action_handler(Action* action, zpl_json_object* json, Asset* assets,
388413
action->args = NULL;
389414
action->run = &action_reload;
390415

416+
} else if (strcmp(type, "lua") == 0) {
417+
action->args =
418+
args_generator(json, 1, "function", ZPL_ADT_TYPE_STRING);
419+
if (action->args == NULL) return ERROR("Args generator failed");
420+
action->run = &action_lua;
421+
422+
} else if (strcmp(type, "lua_async") == 0) {
423+
action->args =
424+
args_generator(json, 1, "function", ZPL_ADT_TYPE_STRING);
425+
if (action->args == NULL) return ERROR("Args generator failed");
426+
action->run = &action_lua_async;
427+
391428
} else return ERROR("Invalid action type found");
392429
return 0;
393430
}
@@ -403,3 +440,20 @@ int action_init(Action* action, zpl_json_object* json, Asset* assets, const size
403440
void action_cleanup(Action* action) {
404441
free(action->args);
405442
}
443+
444+
int action_api(lua_State *L) {
445+
Engine* e = lua_touserdata(L, 1);
446+
Instance* instance = lua_touserdata(L, 2);
447+
const char* string = luaL_checkstring(L, 3);
448+
char* str = malloc(strlen(string)+1);
449+
strcpy(str, string);
450+
Action action;
451+
zpl_json_object json;
452+
zpl_json_parse(&json, str, zpl_heap());
453+
if (action_init(&action, &json, e->assets, e->assets_num) < 0) return 0; //should improve error handling later
454+
free(str);
455+
zpl_json_free(&json);
456+
action.run(e, instance, action.args);
457+
action_cleanup(&action);
458+
return 0;
459+
}

src/common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int ERROR(const char* format, ...) {
5252
va_list arg;
5353
va_start(arg, format);
5454
//fprintf(stderr, "[%s: %d] ", __FILE__, __LINE__);
55-
SDL_LogError(SDL_LOG_CATEGORY_ERROR, format, arg);
55+
SDL_LogMessageV(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_ERROR, format, arg);
5656
va_end(arg);
5757
return -1;
5858
}

src/engine.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ static unsigned char collisionCallback(cpArbiter *arb, cpSpace *space, void *dat
4545
return 0;
4646
}
4747

48+
int action_api(lua_State* L);
49+
4850
/* Setup libraries */
4951
static int setup_env(Engine* e) {
5052
time_t t;
@@ -85,6 +87,8 @@ static int setup_env(Engine* e) {
8587
col_hand->userData = e; //Set the collision handler's user data to the context
8688
e->L = luaL_newstate();
8789
luaL_openlibs(e->L);
90+
lua_pushcfunction(e->L, action_api);
91+
lua_setglobal(e->L, "action");
8892
#ifndef NDEBUG
8993
e->fps = malloc(sizeof(float));
9094
e->vars = realloc(e->vars, sizeof(var));
@@ -258,6 +262,8 @@ static int spawn_level(Engine* e) {
258262
e->first_layer = layer;
259263
}
260264
e->inert_ins_num++;
265+
} else if (strcmp(getextension(e->assets[i].name), "lua") == 0) {
266+
luaL_dostring(e->L, e->assets[i].data);
261267
}
262268
}
263269
if (e->first) {

0 commit comments

Comments
 (0)