//-----------------------------------------------------------------------------
//	Handle delayed VXConsole transactions
//
//-----------------------------------------------------------------------------
static unsigned _DebugThreadFunc( void *pParam )
{
	while ( 1 )
	{
		Sleep( 10 );

		if ( !g_xbx_DebugStringQueue.Count() && !g_xbx_numProfileCounters && !g_xbx_freeMemory )
		{
			continue;
		}

		if ( g_xbx_numProfileCounters )
		{
			// build and send asynchronously
			char dbgCommand[XBX_MAX_RCMDLENGTH];
			_snprintf( dbgCommand, sizeof( dbgCommand ), "SetProfileData() %s 0x%8.8x", g_xbx_profileName, g_xbx_profileCounters );
			XBX_SendRemoteCommand( dbgCommand, true );

			// mark as sent
			g_xbx_numProfileCounters = 0;
		}

		if ( g_xbx_freeMemory )
		{
			// build and send asynchronously
			char dbgCommand[XBX_MAX_RCMDLENGTH];
			_snprintf( dbgCommand, sizeof( dbgCommand ), "FreeMemory() 0x%8.8x", g_xbx_freeMemory );
			XBX_SendRemoteCommand( dbgCommand, true );

			// mark as sent
			g_xbx_freeMemory = 0;
		}

		bool bRemoteValid = g_xbx_bUseVXConsoleOutput && XBX_IsConsoleConnected();
		while ( 1 )
		{
			DebugString_t debugString;
			if ( !g_xbx_DebugStringQueue.PopItem( &debugString ) )
			{
				break;
			}

			OutputStringToDevice( debugString.color, debugString.pString, bRemoteValid );
			free( debugString.pString );
		}
	}

	return 0;
}
//-----------------------------------------------------------------------------
//	Waits for debug queue to drain.
//-----------------------------------------------------------------------------
void CXboxConsole::FlushDebugOutput()
{
	while ( g_xbx_DebugStringQueue.Count() != 0 )
	{
		Sleep( 1 );
	}
}
void CParticleSystemQuery::UpdateProjectedTexture( const int nParticleID, IMaterial *pMaterial, Vector &vOrigin, float flRadius, float flRotation, float r, float g, float b, float a, void *&pUserVar )
{
#if defined( CLIENT_DLL )
	TProjectedTextureInfo *pInfo = reinterpret_cast< TProjectedTextureInfo * >( pUserVar );
	if ( pInfo == NULL )
	{
		pUserVar = pInfo = new TProjectedTextureInfo;
		memset( pInfo, 0, sizeof( *pInfo ) );
		m_ProjectedInfoAdds.PushItem( pInfo );
	}

	pInfo->m_nParticleID = nParticleID;
	pInfo->m_pMaterial = pMaterial;
	pInfo->m_vOrigin = vOrigin;
	pInfo->m_flSize = flRadius;
	pInfo->m_flRotation = flRotation;
	pInfo->m_r = r;
	pInfo->m_g = g;
	pInfo->m_b = b;
	pInfo->m_a = a;
	pInfo->m_bUsedThisFrame = true;

//	ClientEntityList().AddNonNetworkableEntity( this );
#endif // #if defined( CLIENT_DLL )
}
//-----------------------------------------------------------------------------
//	Output string to listening console. Queues output for slave thread.
//	Needs to be lightweight.
//-----------------------------------------------------------------------------
void CXboxConsole::DebugString( unsigned int color, const char* pFormat, ... )
{
	if ( XBX_NoXBDM() )
		return;

	va_list	args;
	char	szStringBuffer[XBX_MAX_MESSAGE];
	int		length;

	// resolve string
	va_start( args, pFormat );
	length = _vsnprintf( szStringBuffer, sizeof( szStringBuffer ), pFormat, args );
	if ( length == -1 )
	{
		szStringBuffer[sizeof( szStringBuffer ) - 1] = '\0';
	}
	va_end( args );

	if ( !g_xbx_bDoSyncOutput )
	{
		// queue string for delayed output
		DebugString_t debugString;
		debugString.color = color;
		debugString.pString = strdup( szStringBuffer );
		g_xbx_DebugStringQueue.PushItem( debugString );
	}
	else
	{
		bool bRemoteValid = g_xbx_bUseVXConsoleOutput && XBX_IsConsoleConnected();
		OutputStringToDevice( color, szStringBuffer, bRemoteValid );
	}
}
void CParticleSystemQuery::PostSimulate( ) 
{
#if defined( CLIENT_DLL )
	TProjectedTextureInfo *pInfo;

	while( m_ProjectedInfoAdds.PopItem( &pInfo ) == true )
	{
		m_ActiveProjectedInfos.AddToTail( pInfo );
	}

	for( int i = 0; i < m_ActiveProjectedInfos.Count(); i++ )
	{
		if ( m_ActiveProjectedInfos[ i ]->m_bUsedThisFrame == false )
		{
			delete m_ActiveProjectedInfos[ i ]->m_pEntity;
			m_ActiveProjectedInfos.Remove( i );
			i--;
			continue;
		}
		if ( m_ActiveProjectedInfos[ i ]->m_pEntity == NULL )
		{
			m_ActiveProjectedInfos[ i ]->m_pEntity = C_EnvProjectedTexture::Create();
		}

		m_ActiveProjectedInfos[ i ]->m_pEntity->SetAbsOrigin( m_ActiveProjectedInfos[ i ]->m_vOrigin );
		m_ActiveProjectedInfos[ i ]->m_pEntity->SetMaterial( m_ActiveProjectedInfos[ i ]->m_pMaterial );
		m_ActiveProjectedInfos[ i ]->m_pEntity->SetLightColor( m_ActiveProjectedInfos[ i ]->m_r * 255, m_ActiveProjectedInfos[ i ]->m_g * 255, m_ActiveProjectedInfos[ i ]->m_b * 255, m_ActiveProjectedInfos[ i ]->m_a * 255 );
		m_ActiveProjectedInfos[ i ]->m_pEntity->SetSize( m_ActiveProjectedInfos[ i ]->m_flSize );
		m_ActiveProjectedInfos[ i ]->m_pEntity->SetRotation( m_ActiveProjectedInfos[ i ]->m_flRotation );
	}
#endif // #if defined( CLIENT_DLL )
}