/src/bindings/bind_main.cpp
https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 67 lines · 46 code · 14 blank · 7 comment · 4 complexity · 36fb9c6c190acd9d81174aca7a9230d4 MD5 · raw file
- #include "common.h"
- #include "bind_all.h"
-
- #ifndef MAX_PATH
- #define MAX_PATH 260
- #endif
-
- namespace luab
- {
-
- static const char* g_script_dir = "../scripts";
-
- // loads and executes Lua script
- // NOTE: assumes the cwd is where the exe is.
- static int f_include( struct lua_State* L )
- {
- const char* filename;
- size_t sz;
- char path[ MAX_PATH ];
- filename = luaL_checklstring( L, 1, &sz );
- sprintf( path, "%s/%s", g_script_dir, filename );
- if( luaL_loadfile( L, path ) || (lua_call( L, 0, LUA_MULTRET ),0) )
- {
- lua_error( L );
- }
- return 0;
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- static void bind_all( lua_State* L )
- {
- lua_register( L, "include", &f_include );
-
- // Call bindings here...
- bind_app( L );
- bind_renderer( L );
- bind_input( L );
- }
-
-
- //////////////////////////////////////////////////////////////////////////
-
- // works by simply calling the above f_include() for "main.lia" through lua state.
- // this way, an unhandled error prints the message and stack trace to stderr
- int start_scripts()
- {
- int ret = 1;
- lua_State* L = luaL_newstate();
- luaL_openlibs(L);
- bind_all(L);
-
- lua_getglobal( L, "debug" ); lua_getfield( L, -1, "traceback" ); // traceback function @ -3
- lua_pushcfunction( L, &f_include ); // loader function @ -2
- lua_pushstring( L, "main.lua" ); // module name @ -1
- if( 0 != lua_pcall( L, 1, LUA_MULTRET, -3 ) )
- {
- fprintf( stderr, "%s\n", lua_tostring( L, -1 ) ); // error message and stack trace
- ret = 0;
- }
- lua_close( L );
- return 0;
- }
-
-
- }