PageRenderTime 46ms CodeModel.GetById 28ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 1
 2#include "common.h"
 3#include "bind_all.h"
 4
 5#ifndef MAX_PATH
 6#define MAX_PATH 260
 7#endif
 8
 9namespace luab
10{
11
12static const char* g_script_dir = "../scripts";
13
14// loads and executes Lua script
15// NOTE: assumes the cwd is where the exe is.
16static int f_include( struct lua_State* L )
17{
18    const char* filename;
19    size_t sz;
20    char path[ MAX_PATH ];
21    filename = luaL_checklstring( L, 1, &sz );
22    sprintf( path, "%s/%s", g_script_dir, filename );
23    if( luaL_loadfile( L, path ) || (lua_call( L, 0, LUA_MULTRET ),0) )
24    {
25        lua_error( L );
26    }
27    return 0;
28}
29
30//////////////////////////////////////////////////////////////////////////
31
32static void bind_all( lua_State* L )
33{
34    lua_register( L, "include", &f_include );
35
36	// Call bindings here...
37	bind_app( L );
38	bind_renderer( L );
39	bind_input( L );
40}
41
42
43//////////////////////////////////////////////////////////////////////////
44
45// works by simply calling the above f_include() for "main.lia" through lua state.
46// this way, an unhandled error prints the message and stack trace to stderr
47int start_scripts()
48{
49    int ret = 1;
50    lua_State* L = luaL_newstate();
51    luaL_openlibs(L);
52    bind_all(L);
53
54    lua_getglobal( L, "debug" ); lua_getfield( L, -1, "traceback" ); // traceback function @ -3
55    lua_pushcfunction( L, &f_include );								 // loader function @ -2
56    lua_pushstring( L, "main.lua" );                                 // module name  @ -1
57    if( 0 != lua_pcall( L, 1, LUA_MULTRET, -3 ) )
58    {
59        fprintf( stderr, "%s\n", lua_tostring( L, -1 ) );            // error message and stack trace
60        ret = 0;
61    }
62    lua_close( L );
63    return 0;
64}
65
66
67}