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)
|
if(game->paused)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
lua_onframe();
|
||||||
|
|
||||||
game->ttime += GetFrameTime();
|
game->ttime += GetFrameTime();
|
||||||
|
|
||||||
while(game->ttime > TPS_TIME){
|
while(game->ttime > TPS_TIME){
|
||||||
|
lua_ontick();
|
||||||
update_machines(game);
|
update_machines(game);
|
||||||
update_workers(game);
|
update_workers(game);
|
||||||
game->ttime -= TPS_TIME;
|
game->ttime -= TPS_TIME;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
|
@ -9,14 +10,6 @@
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
void lua_ontick(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void lua_onframe(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int lua_add_machine(lua_State *L){
|
static int lua_add_machine(lua_State *L){
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -34,27 +27,60 @@ struct LuaFn {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LuaFn exported_funcs[] = {
|
struct LuaFn exported_funcs[] = {
|
||||||
{"mtprint", lua_mtprint}
|
{"mtprint", lua_mtprint},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct load_dat {
|
int lstate_n;
|
||||||
char *str;
|
lua_State **lstates;
|
||||||
int pos;
|
|
||||||
int len;
|
|
||||||
};
|
|
||||||
|
|
||||||
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(){
|
void script_init(){
|
||||||
gl_L = luaL_newstate();
|
|
||||||
|
|
||||||
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
FilePathList scripts = LoadDirectoryFilesEx("script",".lua",true);
|
||||||
lua_register(gl_L, exported_funcs[i].name, exported_funcs[i].fn);
|
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");
|
printf("Loading script \"%s\"...\n", scripts.paths[i]);
|
||||||
lua_pcall(gl_L, 0, 0, 0);
|
int ret = luaL_dofile(lstates[i], scripts.paths[i]);
|
||||||
|
if(ret)
|
||||||
|
puts(lua_tostring(lstates[i], -1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void script_clean(){
|
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
|
#pragma once
|
||||||
|
|
||||||
|
void lua_ontick();
|
||||||
|
|
||||||
|
void lua_onframe();
|
||||||
|
|
||||||
void script_init();
|
void script_init();
|
||||||
|
|
||||||
void script_clean();
|
void script_clean();
|
||||||
|
|
Loading…
Add table
Reference in a new issue