Example #1
0
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;
}
Example #2
0
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;
}
Example #3
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;
}
Example #4
0
int main( int argc, char* argv[] )
{
	int i;
	BYTE buf[ 4096 ], *path, *data;
	DWORD read, written, scriptsize = 0;
	HANDLE fh;
	
	path = buf;
	
	/* open EXE file */
	read = GetModuleFileName( NULL, (CHAR*) buf, sizeof( buf ) );
	if( read >= sizeof( buf ) )
	{
		path = malloc( read + 1 );
		GetModuleFileName( NULL, (CHAR*) buf, sizeof( buf ) );
	}
	fh = CreateFileFast( (CHAR*) buf, FILE_READ, OPEN_EXISTING );
	if( path != buf )
		free( path );
	if( fh == INVALID_HANDLE_VALUE )
		return E_FAIL;
	SetFilePointer( fh, -4, NULL, FILE_END );
	ReadFile( fh, &scriptsize, sizeof( scriptsize ), &read, NULL );
	
	/* read the following data */
	if( scriptsize == 0 )
	{
		if( argc == 3 )
		{
			HANDLE hsgs, hout = CreateFileFast( argv[ 1 ], FILE_WRITE, CREATE_ALWAYS );
			if( !hout )
			{
				MessageBox( 0, "Could not open executable file for writing", APPNAME, MB_ICONERROR );
				CloseHandle( fh );
				return E_FAIL;
			}
			hsgs = CreateFileFast( argv[ 2 ], FILE_READ, OPEN_EXISTING );
			if( !hsgs )
			{
				MessageBox( 0, "Could not open script file for reading", APPNAME, MB_ICONERROR );
				CloseHandle( hout );
				CloseHandle( fh );
				return E_FAIL;
			}
			SetFilePointer( fh, 0, NULL, FILE_BEGIN );
			while( ReadFile( fh, buf, sizeof( buf ), &read, NULL ) && read )
				WriteFile( hout, buf, read, &written, NULL );
			while( ReadFile( hsgs, buf, sizeof( buf ), &read, NULL ) && read )
				WriteFile( hout, buf, read, &written, NULL );
			scriptsize = GetFileSize( hsgs, NULL );
			WriteFile( hout, &scriptsize, 4, &written, NULL );
			CloseHandle( fh );
			CloseHandle( hsgs );
			CloseHandle( hout );
			MessageBox( 0, "File saved!", APPNAME, MB_ICONINFORMATION );
			return S_OK;
		}
		else
		{
			const char* info = "To create an executable from .sgs"
				", run sgsexe <output-file.exe> <script-file.sgs>."
				"\n\nglobal 'argv' will be the array of arguments";
			MessageBox( 0, info, APPNAME, MB_ICONINFORMATION );
			CloseHandle( fh );
			return E_ABORT;
		}
	}
	
	data = malloc( scriptsize );
	SetFilePointer( fh, -4-(LONG)scriptsize, NULL, FILE_END );
	ReadFile( fh, data, scriptsize, &read, NULL );
	CloseHandle( fh );
	
	{
		SGS_CTX = sgs_CreateEngine();
		
		for( i = 0; i < argc; ++i )
			sgs_PushString( C, argv[ i ] );
		
		sgs_CreateArray( C, NULL, argc );
		sgs_SetGlobalByName( C, "argv", sgs_StackItem( C, -1 ) );
		sgs_Pop( C, 1 );
		
		sgs_SetGlobalByName( C, "argc", sgs_MakeInt( argc ) );
		
		sgs_ExecBuffer( C, (char*) data, scriptsize );
		
		sgs_DestroyEngine( C );
	}
	
	free( data );
	
	return 0;
}