//-----------------------------------------------------------------------------
// The application main loop
//-----------------------------------------------------------------------------
int CIHVTestApp::Main()
{
	SpewOutputFunc( IHVTestSpewFunc );
		    
	if ( !SetupStudioRender() )
	{
		return 0;
	}

	if ( !InitMaterialSystem( m_hWnd ) )
	{
		return 0;
	}

#if !defined( _X360 ) // X360TBD:
extern void Sys_InitFloatTime( void ); //garymcthack
	Sys_InitFloatTime();
#endif

	LoadModels();

#if USE_VTUNE
	VTResume();
#endif
#ifdef USE_VPROF
	g_VProfCurrentProfile.Start();
#endif

	bool m_bExitMainLoop = false;
	while (!m_bExitMainLoop && !g_BenchFinished)
	{
		AppPumpMessages();
		RenderFrame();
	}

#ifdef USE_VPROF
	g_VProfCurrentProfile.Stop();
#endif
	g_IHVTestFP = fopen( "ihvtest_vprof.txt", "w" );
#ifdef USE_VPROF
	SpewOutputFunc( IHVTestVProfSpewFunc );
	g_VProfCurrentProfile.OutputReport( VPRT_SUMMARY );
	g_VProfCurrentProfile.OutputReport( VPRT_HIERARCHY_TIME_PER_FRAME_AND_COUNT_ONLY );
	fclose( g_IHVTestFP );
	SpewOutputFunc( IHVTestSpewFunc );
#endif
#if USE_VTUNE
	VTPause();
#endif

    return 0;
}
//-----------------------------------------------------------------------------
// main application
//-----------------------------------------------------------------------------
void CVMTEditApp::Main()
{
	if (!SetVideoMode())
		return;

	// load scheme
	if (!vgui::scheme()->LoadSchemeFromFile("resource/TrackerScheme.res", "VMTEDIT" ))
	{
		Assert( 0 );
	}

	// start vgui
	g_pVGui->Start();

	g_pMaterialSystem->SetForceBindWhiteLightmaps( true );

	// add our main window
	vgui::Panel *mainPanel = CreateVMTEditPanel();

	// run app frame loop
	while (g_pVGui->IsRunning())
	{
		AppPumpMessages();
	
		g_pMaterialSystem->ClearBuffers( true, true );

		g_pVGui->RunFrame();

		g_pVGuiSurface->PaintTraverse( NULL );

		g_pMaterialSystem->SwapBuffers();

		// Some debugging helpers
		VGui_DrawHierarchy();
		VGui_DrawPopups();
	}
}
//-----------------------------------------------------------------------------
// main application
//-----------------------------------------------------------------------------
int CSceneViewerApp::Main()
{
	g_pMaterialSystem->ModInit();
	if (!SetVideoMode())
		return 0;

	g_pDataCache->SetSize( 64 * 1024 * 1024 );

	InitDefaultEnvCubemap();

	g_pMaterialSystemConfig = &g_pMaterialSystem->GetCurrentConfigForVideoCard();

	// configuration settings
	vgui::system()->SetUserConfigFile("sceneviewer.vdf", "EXECUTABLE_PATH");

	// load scheme
	if (!vgui::scheme()->LoadSchemeFromFile("resource/BoxRocket.res", "SceneViewer" ))
	{
		Assert( 0 );
	}

	g_pVGuiLocalize->AddFile( "resource/boxrocket_%language%.txt" );

	// start vgui
	g_pVGui->Start();

	//vgui::input()->SetAppModalSurface( mainPanel->GetVPanel() );

	// load the base localization file
	g_pVGuiLocalize->AddFile( "Resource/valve_%language%.txt" );
	g_pFullFileSystem->AddSearchPath("platform", "PLATFORM");
	g_pVGuiLocalize->AddFile( "Resource/vgui_%language%.txt");
	g_pVGuiLocalize->AddFile( "Resource/dmecontrols_%language%.txt");

	// add our main window
	vgui::Panel *mainPanel = CreateSceneViewerPanel();

	// run app frame loop
	CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
	vgui::VPANEL root = vgui::surface()->GetEmbeddedPanel();
	vgui::surface()->Invalidate( root );

	// See if there was a DMX file passed on the command line

	for ( int i( CommandLine()->ParmCount() - 1 ); i > 0; --i )
	{
		const char *const arg( CommandLine()->GetParm( i ) );

		if ( arg && *arg != '\0' && _access( arg, 04 ) == 0 )
		{
			if ( !CommandLine()->FindParm( "-nozoom" ) )
			{
				KeyValues *oz( new KeyValues( "PinAndZoomIt" ) );
				mainPanel->PostMessage( mainPanel, oz );
			}

			KeyValues *ofs( new KeyValues( "LoadFile" ) );
			ofs->SetString( "fullpath", arg );
			mainPanel->PostMessage( mainPanel, ofs );

			if ( CommandLine()->FindParm( "-showasset" ) )
			{
				KeyValues *msg( new KeyValues( "ShowAssetBuilder" ) );
				mainPanel->PostMessage( mainPanel, msg );
			}

			if ( CommandLine()->FindParm( "-showcomboeditor" ) )
			{
				KeyValues *msg( new KeyValues( "ShowComboBuilder" ) );
				mainPanel->PostMessage( mainPanel, msg );
			}

			break;
		}
	}

	int nLastTime = Plat_MSTime();
	while (g_pVGui->IsRunning())
	{
		// Give other applications a chance to run
		Sleep( 1 );
		int nTime = Plat_MSTime();
		if ( ( nTime - nLastTime ) < 16 )
			continue;
		nLastTime = nTime;

		AppPumpMessages();
	
		vgui::GetAnimationController()->UpdateAnimations( Sys_FloatTime() );

		g_pMaterialSystem->BeginFrame( 0 );
		pRenderContext->ClearColor4ub( 76, 88, 68, 255 ); 
		pRenderContext->ClearBuffers( true, true );

		g_pVGui->RunFrame();

		g_pVGuiSurface->PaintTraverseEx( root, true );

		g_pMaterialSystem->EndFrame();
		g_pMaterialSystem->SwapBuffers();
	}

	delete mainPanel;

	ShutdownDefaultEnvCubemap();
 	g_pMaterialSystem->ModShutdown();

	// HACK -  this is a bit of a hack, since in theory, there could be multiple of these panels,
	// or there could be elements allocated outside of these panels, but since the filenames are hardcoded,
	// I don't feel too bad about unloading *all* files to make sure they're caught
	int nFiles = g_pDataModel->NumFileIds();
	for ( int i = 0; i < nFiles; ++i )
	{
		DmFileId_t fileid = g_pDataModel->GetFileId( i );
		g_pDataModel->UnloadFile( fileid );
	}

	return 1;
}
//-----------------------------------------------------------------------------
// main application
//-----------------------------------------------------------------------------
int CAVITestApp::Main()
{
	if (!SetVideoMode())
		return 0;

	// load scheme
	if (!vgui::scheme()->LoadSchemeFromFile("resource/BoxRocket.res", "ElementViewer" ))
	{
		Assert( 0 );
	}

	// load the boxrocket localization file
	g_pVGuiLocalize->AddFile( "resource/boxrocket_%language%.txt" );

	// load the base localization file
	g_pVGuiLocalize->AddFile( "Resource/valve_%language%.txt" );
	g_pFullFileSystem->AddSearchPath("platform", "PLATFORM");
	g_pVGuiLocalize->AddFile( "Resource/vgui_%language%.txt");

	// start vgui
	g_pVGui->Start();

	// add our main window
//	vgui::Panel *mainPanel = CreateElementViewerPanel();

	CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
	AVIParams_t params;
	Q_strcpy( params.m_pFileName, "c:\\temp\\out.avi" );
	Q_strcpy( params.m_pPathID, "MOD" );
	pRenderContext->GetWindowSize( params.m_nWidth, params.m_nHeight );
	params.m_nFrameRate = 24;
	params.m_nFrameScale = 1;
	params.m_nNumChannels = 0;

	AVIHandle_t h = g_pAVI->StartAVI( params );
	AVIMaterial_t hAVIMaterial = g_pAVI->CreateAVIMaterial( "avitest", "c:\\temp\\test.avi", "MOD" );

	// run app frame loop
	vgui::VPANEL root = g_pVGuiSurface->GetEmbeddedPanel();
	g_pVGuiSurface->Invalidate( root );

	int imagesize = params.m_nWidth * params.m_nHeight;
	BGR888_t *hp = new BGR888_t[ imagesize ];

	m_flStartTime = Sys_FloatTime();
	m_pMaterial = g_pMaterialSystem->FindMaterial( "debug/debugempty", "app" );
	while ( g_pVGui->IsRunning() )
	{
		AppPumpMessages();
	
		pRenderContext->ClearColor4ub( 76, 88, 68, 255 ); 
		pRenderContext->ClearBuffers( true, true );

		DrawStuff( hAVIMaterial );

		g_pVGui->RunFrame();

		g_pVGuiSurface->PaintTraverse( root );

		// Get Bits from material system
		Rect_t rect;
		rect.x = rect.y = 0;
		rect.width = params.m_nWidth;
		rect.height = params.m_nHeight;

		pRenderContext->ReadPixelsAndStretch( &rect, &rect, (unsigned char*)hp, 
			IMAGE_FORMAT_BGR888, rect.width * sizeof( BGR888_t ) );
		g_pAVI->AppendMovieFrame( h, hp );

		g_pMaterialSystem->SwapBuffers();
	}

	delete[] hp;
	g_pAVI->FinishAVI( h );
	g_pAVI->DestroyAVIMaterial( hAVIMaterial );

//	delete mainPanel;

	return 1;
}