char* runsgs( const char* script ) { if( !outbuf ) { outbuf = malloc( MAX_OUTPUT_SIZE ); } SGS_CTX = sgs_CreateEngine(); outbuf_at = outbuf; sgs_SetOutputFunc( C, output_to_buffer, NULL ); sgs_SetErrOutputFunc( C, output_to_buffer, NULL ); sgs_LoadLib_Fmt( C ); /* no need for I/O - can't use it from the browser */ sgs_LoadLib_Math( C ); sgs_LoadLib_OS( C ); sgs_LoadLib_RE( C ); sgs_LoadLib_String( C ); xgm_module_entry_point( C ); sgs_ExecString( C, script ); sgs_DestroyEngine( C ); *outbuf_at = 0; return outbuf; }
int ss_Initialize( int argc, char* argv[], int debug ) { int ret; SGS_CTX; GEnabledDebugging = debug; #if SS_STARTUP_PROFILING printf( "ss_Initialize called: %f\n", sgs_GetTime() ); #endif C = g_C = sgs_CreateEngine(); #if SS_STARTUP_PROFILING printf( "SGS engine created: %f\n", sgs_GetTime() ); #endif if( GEnabledDebugging ) sgs_InitIDbg( C, &D ); else sgs_SetMsgFunc( C, ss_MsgFunc, NULL ); #if SS_STARTUP_PROFILING printf( "debug printing initialized: %f\n", sgs_GetTime() ); #endif /* preinit first-use libs */ sgs_LoadLib_Fmt( C ); sgs_LoadLib_IO( C ); sgs_LoadLib_Math( C ); sgs_LoadLib_OS( C ); sgs_LoadLib_RE( C ); sgs_LoadLib_String( C ); #if SS_STARTUP_PROFILING printf( "SGS libraries loaded: %f\n", sgs_GetTime() ); #endif ss_InitDebug( C ); ss_InitExtMath( C ); ss_InitImage( C ); #if SS_STARTUP_PROFILING printf( "SS libraries loaded: %f\n", sgs_GetTime() ); #endif /* preinit tmp buffer */ g_tmpbuf = sgs_membuf_create(); sgs_membuf_reserve( &g_tmpbuf, C, 1024 ); /* load command line arguments */ { int i; for( i = 0; i < argc; ++i ) { sgs_PushString( C, argv[ i ] ); } sgs_CreateArray( C, NULL, argc ); sgs_SetGlobalByName( C, "sys_args", sgs_StackItem( C, -1 ) ); sgs_Pop( C, 1 ); } /* push some system info */ sgs_SetGlobalByName( C, "sys_scripting_engine", sgs_MakePtr( C ) ); #if SS_STARTUP_PROFILING printf( "system info pushed: %f\n", sgs_GetTime() ); #endif /* run the preconfig script */ sgs_ExecString( C, scr_preconfig ); #if SS_STARTUP_PROFILING printf( "preconfig done: %f\n", sgs_GetTime() ); #endif /* run the config file */ ret = sgs_Include( C, "core/config" ); if( !ret ) { sgs_Msg( C, SGS_ERROR, "Could not run core/config (configuration script)." ); return -2; } /* run the primary extensions */ ret = sgs_Include( C, "core/ext" ); if( !ret ) { sgs_Msg( C, SGS_ERROR, "Could not run core/ext." ); return -2; } #if SS_STARTUP_PROFILING printf( "configured engine scripts: %f\n", sgs_GetTime() ); #endif /* run the main file */ ret = sgs_Include( C, "main" ); if( !ret ) { sgs_Msg( C, SGS_ERROR, "Could not execute 'main'." ); return -3; } #if SS_STARTUP_PROFILING printf( "ran main script: %f\n", sgs_GetTime() ); #endif /* configure the framework (optional) */ sgs_GlobalCall( C, "configure", 0, 0 ); #if SS_STARTUP_PROFILING printf( "called 'configure': %f\n", sgs_GetTime() ); #endif /* check if already required to exit */ if( sgs_GlobalBool( C, "sys_exit" ) ) return 0; if( GEnabledProfiler ) sgs_ProfInit( C, &P, GEnabledProfiler ); #if SS_STARTUP_PROFILING printf( "pre-SDL init: %f\n", sgs_GetTime() ); #endif /* initialize SDL */ if( SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS | SDL_INIT_NOPARACHUTE ) < 0 ) { sgs_Msg( C, SGS_ERROR, "Couldn't initialize SDL: %s", SDL_GetError() ); return -5; } SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); #if SS_STARTUP_PROFILING printf( "post-SDL init: %f\n", sgs_GetTime() ); #endif /* initialize script-space SDL API */ ss_InitSDL( C ); /* initialize script-space rendering API */ ss_InitGraphics( C ); #if SS_STARTUP_PROFILING printf( "initialized SDL/Graphics subsystems: %f\n", sgs_GetTime() ); #endif /* initialize the application */ if( SGS_FAILED( sgs_GlobalCall( C, "initialize", 0, 0 ) ) ) { sgs_Msg( C, SGS_ERROR, "Failed to initialize the application." ); return -8; } #if SS_STARTUP_PROFILING printf( "called 'initialize': %f\n", sgs_GetTime() ); #endif return 1; }