Interface Lua complete
This commit is contained in:
parent
3b39ff4301
commit
fd4321605b
4 changed files with 63 additions and 22 deletions
|
@ -1 +1,9 @@
|
|||
print("hello")
|
||||
mtprint("hello")
|
||||
|
||||
function on_tick()
|
||||
mtprint("hello from tick")
|
||||
end
|
||||
|
||||
function on_frame()
|
||||
|
||||
end
|
||||
|
|
|
@ -325,9 +325,12 @@ void update(Game *game){
|
|||
if(game->paused)
|
||||
return;
|
||||
|
||||
lua_onframe();
|
||||
|
||||
game->ttime += GetFrameTime();
|
||||
|
||||
while(game->ttime > TPS_TIME){
|
||||
lua_ontick();
|
||||
update_machines(game);
|
||||
update_workers(game);
|
||||
game->ttime -= TPS_TIME;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
|
@ -9,14 +10,6 @@
|
|||
|
||||
#include "types.h"
|
||||
|
||||
void lua_ontick(){
|
||||
|
||||
}
|
||||
|
||||
void lua_onframe(){
|
||||
|
||||
}
|
||||
|
||||
static int lua_add_machine(lua_State *L){
|
||||
|
||||
return 0;
|
||||
|
@ -34,27 +27,60 @@ struct LuaFn {
|
|||
};
|
||||
|
||||
struct LuaFn exported_funcs[] = {
|
||||
{"mtprint", lua_mtprint}
|
||||
{"mtprint", lua_mtprint},
|
||||
|
||||
};
|
||||
|
||||
struct load_dat {
|
||||
char *str;
|
||||
int pos;
|
||||
int len;
|
||||
};
|
||||
int lstate_n;
|
||||
lua_State **lstates;
|
||||
|
||||
lua_State *gl_L;
|
||||
// TODO : Pass main as param
|
||||
void lua_ontick(){
|
||||
for(int i = 0; i < lstate_n; i++){
|
||||
lua_State *L = lstates[i];
|
||||
lua_getglobal(L, "on_tick");
|
||||
int ret = lua_pcall(L, 0, 0, 0);
|
||||
if(ret)
|
||||
puts(lua_tostring(L, -1));
|
||||
}
|
||||
}
|
||||
|
||||
void lua_onframe(){
|
||||
for(int i = 0; i < lstate_n; i++){
|
||||
lua_State *L = lstates[i];
|
||||
lua_getglobal(L, "on_frame");
|
||||
int ret = lua_pcall(L, 0, 0, 0);
|
||||
if(ret)
|
||||
puts(lua_tostring(L, -1));
|
||||
}
|
||||
}
|
||||
|
||||
void lua_mt_init(lua_State *L){
|
||||
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
||||
lua_register(L, exported_funcs[i].name, exported_funcs[i].fn);
|
||||
}
|
||||
|
||||
void script_init(){
|
||||
gl_L = luaL_newstate();
|
||||
|
||||
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
||||
lua_register(gl_L, exported_funcs[i].name, exported_funcs[i].fn);
|
||||
FilePathList scripts = LoadDirectoryFilesEx("script",".lua",true);
|
||||
lstate_n = scripts.count;
|
||||
lstates = malloc(sizeof(void*)*lstate_n);
|
||||
if(!lstates){
|
||||
return;
|
||||
}
|
||||
for(int i = 0; i < scripts.count; i++){
|
||||
lstates[i] = luaL_newstate();
|
||||
lua_mt_init(lstates[i]);
|
||||
|
||||
luaL_loadstring(gl_L, "mtprint(\"hello world\")\n");
|
||||
lua_pcall(gl_L, 0, 0, 0);
|
||||
printf("Loading script \"%s\"...\n", scripts.paths[i]);
|
||||
int ret = luaL_dofile(lstates[i], scripts.paths[i]);
|
||||
if(ret)
|
||||
puts(lua_tostring(lstates[i], -1));
|
||||
}
|
||||
}
|
||||
|
||||
void script_clean(){
|
||||
lua_close(gl_L);
|
||||
for(int i = 0; i < lstate_n; i++)
|
||||
lua_close(lstates[i]);
|
||||
free(lstates);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
void lua_ontick();
|
||||
|
||||
void lua_onframe();
|
||||
|
||||
void script_init();
|
||||
|
||||
void script_clean();
|
||||
|
|
Loading…
Add table
Reference in a new issue