int main( int argc, char** argv ) { printf( "\n//\n/// SGScript / CPPBC test\n//\n" ); SGS_CTX = sgs_CreateEngine(); printf( "\n> push Vec3(1,2,3)" ); pushVec3( C, 1, 2, 3 ); printf( "\n> print object: " ); sgs_PushItem( C, -1 ); sgs_GlobalCall( C, "print", 1, 0 ); printf( "\n> print property 'Vec3.length': " ); sgs_PushProperty( C, -1, "length" ); sgs_GlobalCall( C, "print", 1, 0 ); printf( "\n> print result of method 'Vec3.getLength()': " ); sgs_PushItem( C, -1 ); sgs_PushProperty( C, -1, "getLength" ); sgs_ThisCall( C, 0, 1 ); sgs_GlobalCall( C, "print", 1, 0 ); printf( "\n> print object after method 'Vec3.setLength(4.5)': " ); sgs_PushItem( C, -1 ); sgs_PushReal( C, 4.5 ); sgs_PushProperty( C, -2, "setLength" ); sgs_ThisCall( C, 1, 0 ); sgs_GlobalCall( C, "print", 1, 0 ); printf( "\n" ); sgs_DestroyEngine( C ); return 0; }
int ss_Free() { SGS_CTX = g_C; /* clean the application */ if( SGS_FAILED( sgs_GlobalCall( C, "cleanup", 0, 0 ) ) ) { sgs_Msg( C, SGS_ERROR, "Failed to clean the application." ); return -1; } sgs_membuf_destroy( &g_tmpbuf, C ); if( GEnabledProfiler ) { sgs_ProfDump( C, &P ); sgs_ProfClose( C, &P ); } if( GEnabledDebugging ) sgs_CloseIDbg( C, &D ); sgs_DestroyEngine( C ); ss_FreeGraphics( C ); return 0; }
int ss_Frame() { SGS_CTX = g_C; if( sgs_GlobalInt( C, "sys_exit" ) ) return 1; { int wait = sgs_GlobalBool( C, "sys_wait_events" ); SDL_Event event; while( wait ? SDL_WaitEvent( &event ) : SDL_PollEvent( &event ) ) { int ssz = sgs_StackSize( C ); if( event.type == SDL_KEYDOWN && event.key.state == SDL_PRESSED && event.key.keysym.sym == SDLK_F4 && ( event.key.keysym.mod & KMOD_ALT ) ) { sgs_ExecString( C, "global sys_exit = true;" ); break; } ss_CreateSDLEvent( C, &event ); if( SGS_FAILED( sgs_GlobalCall( C, "on_event", 1, 0 ) ) ) { sgs_Msg( C, SGS_ERROR, "error in event creation" ); sgs_Pop( C, sgs_StackSize( C ) - ssz ); // provide default handler if( event.type == SDL_QUIT ) { sgs_ExecString( C, "global sys_exit = true;" ); return 1; } } } if( sgs_GlobalInt( C, "sys_exit" ) ) return 1; /* advance the application exactly one frame */ if( SGS_FAILED( sgs_GlobalCall( C, "update", 0, 0 ) ) ) { sgs_Msg( C, SGS_ERROR, "Failed to update the application." ); return -1; } } return 0; }
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; }