me.fier.engine/modules/engine/addon/lua.c

71 lines
2.2 KiB
C
Raw Normal View History

2023-05-14 10:21:03 +00:00
#include <lua5.3/lua.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
lua_State * engine_lua_state;
2023-05-14 16:29:55 +00:00
// TIME
int engine_luaf_time_get(lua_State *L) {
lua_pushinteger(L,engine_time_get());
return 1;
}
int engine_luaf_time_sleep(lua_State *L) {
2023-05-14 10:21:03 +00:00
int time = luaL_checkinteger(L,1);
2023-05-14 15:48:31 +00:00
engine_time_sleep(time);
2023-05-14 10:21:03 +00:00
return 0;
}
2023-05-14 16:29:55 +00:00
// TEXTURES
int engine_luaf_texture_create(lua_State *L) {
int width = luaL_checkinteger(L,1);
int height = luaL_checkinteger(L,2);
struct ENGINE_TEXTURE * texture = engine_texture_create(width,height);
2023-05-14 10:21:03 +00:00
lua_pushlightuserdata(L,texture);
return 1;
}
2023-05-14 16:29:55 +00:00
int engine_luaf_texture_destroy(lua_State *L) {
struct ENGINE_TEXTURE * texture = (struct ENGINE_TEXTURE *)lua_touserdata(L,1);
engine_texture_destroy(texture);
return 0;
}
int engine_luaf_texture_render_2d(lua_State *L) {
2023-05-14 10:21:03 +00:00
struct ENGINE_TEXTURE * texture = (struct ENGINE_TEXTURE *)lua_touserdata(L,1);
int x = luaL_checkinteger(L,2);
int y = luaL_checkinteger(L,3);
2023-05-14 15:48:31 +00:00
engine_texture_render_2d(texture,x,y);
2023-05-14 10:21:03 +00:00
return 0;
}
2023-05-14 16:29:55 +00:00
int engine_luaf_texture_from_file(lua_State *L) {
struct ENGINE_TEXTURE * texture = (struct ENGINE_TEXTURE *)lua_touserdata(L,1);
char * fpath = (char *)luaL_checkstring(L,2);
engine_texture_from_file(texture,fpath);
return 0;
}
2023-05-14 10:21:03 +00:00
void engine_luaInit() {
engine_lua_state = luaL_newstate();
luaL_openlibs(engine_lua_state);
2023-05-14 16:29:55 +00:00
// // FUNCTIONS
// TIME
lua_pushcfunction(engine_lua_state,engine_luaf_time_get);
lua_setglobal (engine_lua_state,"engine_time_get");
lua_pushcfunction(engine_lua_state,engine_luaf_time_sleep);
lua_setglobal (engine_lua_state,"engine_time_sleep");
// TEXTURES
lua_pushcfunction(engine_lua_state,engine_luaf_texture_create);
lua_setglobal (engine_lua_state,"engine_texture_create");
lua_pushcfunction(engine_lua_state,engine_luaf_texture_destroy);
lua_setglobal (engine_lua_state,"engine_texture_destroy");
lua_pushcfunction(engine_lua_state,engine_luaf_texture_render_2d);
lua_setglobal (engine_lua_state,"engine_texture_render_2d");
lua_pushcfunction(engine_lua_state,engine_luaf_texture_from_file);
lua_setglobal (engine_lua_state,"engine_texture_from_file");
2023-05-14 10:21:03 +00:00
2023-05-14 16:29:55 +00:00
// // USER SCRIPT
2023-05-14 10:21:03 +00:00
luaL_loadfile(engine_lua_state,"assets/scripts/main.lua");
lua_call(engine_lua_state,0,0);
}