//---
void xmain(int argc, char *argv[])
{
	vc_initBuiltins();
	vc_initLibrary();

	InitGarlick();
	Handle::init();

	strcpy(mapname,"");

	LoadConfig();
	if (argc == 2)
	{
		if (strlen(argv[1]) > 254)
			err("Mapname argument too long!");
		strcpy(mapname, argv[1]);
	}

	InitVideo();

	mouse_Init();
	InitKeyboard();
	joy_Init();
	InitScriptEngine();

	gameWindow->setTitle(APPNAME);

	if (sound) snd_Init(soundengine);

	win_movie_init();
	ResetSprites();
	timer_Init(gamerate);

	LUA *lua;
	se = lua = new LUA();
	
	#ifdef ALLOW_SCRIPT_COMPILATION
	DisplayCompileImage();
	lua->compileSystem();
	CompileMaps("lua", lua);
	#endif
	
	se->ExecAutoexec();

	while (true && strlen(mapname))
		Engine_Start(mapname);
	err("");
}
int main()
{
	//lex();
	//ConvertShit("../asBindings/LegacyBindings.cpp");
	//return 0;

	InitScriptEngine();

	const char* filename = "../Scripts/TestObjects.h"; //"../Scripts/TestSyntax.h"; //"../Scripts/Test1.cs";
	bool r = LoadAndBuildScriptFile(filename);

	//////////////////////////////////////////////////////////////////////////
	//Todo: All the script messin' goes here.
	//////////////////////////////////////////////////////////////////////////

	if(r)
	{

		ScriptClassThunk thunk = {};
		foo(thunk);

		SimpleTimer timer;

		const int NUM_SCRIPTS = 1;
		Script script[NUM_SCRIPTS] = {};

		for(u32 i(0); i != NUM_SCRIPTS; ++i)
		{
			if(!script[i].Init(&thunk))
				Printf("-----------Init failed!------------");
		}

		int frames=0;
		int nd=0;
		timer.Start();

		CallGlobalCallbackFunction(0);
		CallGlobalCallbackFunction(1);

		while(nd < NUM_SCRIPTS)
		{
			for(u32 i(0); i != NUM_SCRIPTS; ++i)
			{
				if(script[i].OnUpdate() == asEXECUTION_EXCEPTION)
				{
					script[i].OnDestroy();
					nd++; //destroy object?
				}
			}

			frames++;
		}

		SMinuteSecondsInfo msi = timer.GetElapsedTime();
		Printf("Elapsed time: %i minutes, %.2f seconds.\n",
			msi.minutes,
			msi.seconds
			);

		Printf("frames: %i, %.5f.\n",
			frames,
			((msi.seconds + (float(msi.minutes * 60))) * 1000.0f) / float(frames)
			);

		for(u32 i(0); i != NUM_SCRIPTS; ++i)
		{
			script[i].Destroy();
		}


	} //r
	//////////////////////////////////////////////////////////////////////////
	ShutdownScriptEngine();

	return r ? 0 : 1;
}