//-----------------------------------------------------------------------------
// Purpose: This is called every frame, and can also be called explicitly to flush
//  text to the screen.
//-----------------------------------------------------------------------------
void SCR_UpdateScreen (void)
{
	// Always force the Gamma Table to be rebuilt. Otherwise,
	// we'll load textures with an all white gamma lookup table.
	V_CheckGamma();

	if (scr_skipupdate)
		return;

	if ( scr_disabled_for_loading )
	{
		if ( realtime - scr_disabled_time <= 60 )
			return;

		scr_disabled_for_loading = false;
		Con_Printf( "Connecting may have failed.\n" );
	}

	// No screen refresh on dedicated servers
	if ( cls.state == ca_dedicated )
		return;				

	if (!scr_initialized || !con_initialized)
		return;				// not initialized yet

	// Let demo system overwrite view origin/angles during playback
	demo->GetInterpolatedViewpoint();

	// Simulation meant to occur before any views are rendered
	ClientDLL_FrameStageNotify( FRAME_RENDER_START );
	
	{

		Shader_BeginRendering ();
		
		//
		// determine size of refresh window
		//
		if ( vid.recalc_refdef )
		{
			SCR_CalcRefdef ();
		}
		
		// Draw world, etc.
		V_RenderView();

		CL_TakeSnapshotAndSwap();

	}

	
	ClientDLL_FrameStageNotify( FRAME_RENDER_END );
}
void CLocalNetworkBackdoor::StartEntityStateUpdate()
{
	m_EntsAlive.ClearAll();
	m_nEntsCreated = 0;
	m_nEntsChanged = 0;

	// signal client that we start updating entities
	ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_START );
}
//-----------------------------------------------------------------------------
// Purpose: 
// Output : int
//-----------------------------------------------------------------------------
void CEngine::Frame( void )
{
	// yield the CPU for a little while when paused, minimized, or not the focus
	// FIXME:  Move this to main windows message pump?
	if ( IsPC() && !game->IsActiveApp() && !sv.IsDedicated() )
	{
		g_pInputSystem->SleepUntilInput( NOT_FOCUS_SLEEP );
	}

	// Get current time
	m_flCurrentTime	= Sys_FloatTime();

	// Determine dt since we last checked
	float dt = m_flCurrentTime - m_flPreviousTime;

	// Remember old time
	m_flPreviousTime = m_flCurrentTime;

	// Accumulate current time delta into the true "frametime"
	m_flFrameTime += dt;

	// If the time is < 0, that means we've restarted. 
	// Set the new time high enough so the engine will run a frame
	if ( m_flFrameTime < 0.0f )
		return;

	// If the frametime is still too short, don't pass through
	if ( !FilterTime( m_flFrameTime ) )
	{
		m_flFilteredTime += dt;
		return;
	}

	if ( ShouldSerializeAsync() )
	{
		static ConVar *pSyncReportConVar = g_pCVar->FindVar( "fs_report_sync_opens" );
		bool bReportingSyncOpens = ( pSyncReportConVar && pSyncReportConVar->GetInt() );
		int reportLevel = 0;
		if ( bReportingSyncOpens )
		{
			reportLevel = pSyncReportConVar->GetInt();
			pSyncReportConVar->SetValue( 0 );
		}
		g_pFileSystem->AsyncFinishAll();
		if ( bReportingSyncOpens )
		{
			pSyncReportConVar->SetValue( reportLevel );
		}
	}

#ifdef VPROF_ENABLED
	PreUpdateProfile( m_flFilteredTime );
#endif
	
	// Reset swallowed time...
	m_flFilteredTime = 0.0f;

#ifndef SWDS
	if ( !sv.IsDedicated() )
	{
		ClientDLL_FrameStageNotify( FRAME_START );
	}
#endif

#ifdef VPROF_ENABLED
	PostUpdateProfile();
#endif

	{ // profile scope

	VPROF_BUDGET( "CEngine::Frame", VPROF_BUDGETGROUP_OTHER_UNACCOUNTED );


	switch( m_nDLLState )
	{
	case DLL_PAUSED:			// paused, in hammer
	case DLL_INACTIVE:			// no dll
		break;

	case DLL_ACTIVE:			// engine is focused
	case DLL_CLOSE:				// closing down dll
	case DLL_RESTART:			// engine is shutting down but will restart right away
		// Run the engine frame
		HostState_Frame( m_flFrameTime );
		break;
	}

	// Has the state changed?
	if ( m_nNextDLLState != m_nDLLState )
	{
		m_nDLLState = m_nNextDLLState;

		// Do special things if we change to particular states
		switch( m_nDLLState )
		{
		case DLL_CLOSE:
			SetQuitting( QUIT_TODESKTOP );
			break;
		case DLL_RESTART:
			SetQuitting( QUIT_RESTART );
			break;
		}
	}

	} // profile scope

	// Reset for next frame
	m_flFrameTime = 0.0f;

#if defined( VPROF_ENABLED ) && defined( _X360 )
	UpdateVXConsoleProfile();
#endif
}
void CLocalNetworkBackdoor::EndEntityStateUpdate()
{
	ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_POSTDATAUPDATE_START );

	// Handle entities created.
	int i;
	for ( i=0; i < m_nEntsCreated; i++ )
	{
		MDLCACHE_CRITICAL_SECTION_( g_pMDLCache );

		int iEdict = m_EntsCreatedIndices[i];
		CCachedEntState *pCached = &m_CachedEntState[iEdict];
		IClientNetworkable *pNet = pCached->m_pNetworkable;

		pNet->PostDataUpdate( DATA_UPDATE_CREATED );
		pNet->NotifyShouldTransmit( SHOULDTRANSMIT_START );
		pCached->m_bDormant = false;
	}

	// Handle entities changed.
	for ( i=0; i < m_nEntsChanged; i++ )
	{
		MDLCACHE_CRITICAL_SECTION_( g_pMDLCache );

		int iEdict = m_EntsChangedIndices[i];
		m_CachedEntState[iEdict].m_pNetworkable->PostDataUpdate( DATA_UPDATE_DATATABLE_CHANGED );
	}

	ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_POSTDATAUPDATE_END );

	// Handle entities removed (= SV_WriteDeletions() in normal mode)
	int nDWords = m_PrevEntsAlive.GetNumDWords();

	// Handle entities removed.
	for ( i=0; i < nDWords; i++ )
	{
		unsigned long prevEntsAlive = m_PrevEntsAlive.GetDWord( i );
		unsigned long entsAlive = m_EntsAlive.GetDWord( i );
		unsigned long toDelete = (prevEntsAlive ^ entsAlive) & prevEntsAlive;

		if ( toDelete )
		{
			for ( int iBit=0; iBit < 32; iBit++ )
			{
				if ( toDelete & (1 << iBit) )
				{
					int iEdict = (i<<5) + iBit;
					if ( iEdict < MAX_EDICTS )
					{
						if ( m_CachedEntState[iEdict].m_pNetworkable )
						{
							m_CachedEntState[iEdict].m_pNetworkable->Release();
							m_CachedEntState[iEdict].m_pNetworkable = NULL;
						}
						else
						{
							AssertOnce( !"EndEntityStateUpdate:  Would have crashed with NULL m_pNetworkable\n" );
						}
					}
					else
					{
						AssertOnce( !"EndEntityStateUpdate:  Would have crashed with entity out of range\n" );
					}
				}
			}
		}
	}

	// Remember the previous state of which entities were around.
	m_PrevEntsAlive = m_EntsAlive;

	// end of all entity update activity
	ClientDLL_FrameStageNotify( FRAME_NET_UPDATE_END );

	/*
	#ifdef _DEBUG
	for ( i=0; i <= highest_index; i++ )
	{
	if ( !( m_EntsAlive[i>>5] & (1 << (i & 31)) ) )
	Assert( !m_CachedEntState[i].m_pNetworkable );

	if ( ( m_EntsAlive[i>>5] & (1 << (i & 31)) ) &&
	( m_EntsCreated[i>>5] & (1 << (i & 31)) ) )
	{
	Assert( FindInList( m_EntsCreatedIndices, m_nEntsCreated, i ) );
	}

	if ( (m_EntsAlive[i>>5] & (1 << (i & 31))) && 
	!(m_EntsCreated[i>>5] & (1 << (i & 31))) &&
	(m_EntsChanged[i>>5] & (1 << (i & 31)))
	)
	{
	Assert( FindInList( m_EntsChangedIndices, m_nEntsChanged, i ) );
	}
	}
	#endif
	*/
}