////////////////////////////////////////////////////////////////
//
// CRenderItemManager::CreateRenderTarget
//
//
//
////////////////////////////////////////////////////////////////
CRenderTargetItem* CRenderItemManager::CreateRenderTarget ( uint uiSizeX, uint uiSizeY, bool bWithAlphaChannel, bool bForce )
{
    if ( !bForce && !CanCreateRenderItem ( CRenderTargetItem::GetClassId () ) )
        return NULL;

    CRenderTargetItem* pRenderTargetItem = new CRenderTargetItem ();
    pRenderTargetItem->PostConstruct ( this, uiSizeX, uiSizeY, bWithAlphaChannel );

    if ( !pRenderTargetItem->IsValid () )
    {
        SAFE_RELEASE ( pRenderTargetItem );
        return NULL;
    }

    UpdateMemoryUsage ();

    return pRenderTargetItem;
}
////////////////////////////////////////////////////////////////
//
// CRenderItemManager::CreateRenderTarget
//
//
//
////////////////////////////////////////////////////////////////
CRenderTargetItem* CRenderItemManager::CreateRenderTarget ( uint uiSizeX, uint uiSizeY, bool bWithAlphaChannel, bool bForce )
{
    if ( !bForce && !CanCreateRenderItem ( CRenderTargetItem::GetClassId () ) )
        return NULL;

    // Include in memory stats only if render target is not for MTA internal use
    bool bIncludeInMemoryStats = (bForce == false);

    CRenderTargetItem* pRenderTargetItem = new CRenderTargetItem ();
    pRenderTargetItem->PostConstruct ( this, uiSizeX, uiSizeY, bWithAlphaChannel, bIncludeInMemoryStats );

    if ( !pRenderTargetItem->IsValid () )
    {
        SAFE_RELEASE ( pRenderTargetItem );
        return NULL;
    }

    UpdateMemoryUsage ();

    return pRenderTargetItem;
}
////////////////////////////////////////////////////////////////
//
// CScreenGrabber::GetBackBufferPixels
//
//
//
////////////////////////////////////////////////////////////////
bool CScreenGrabber::GetBackBufferPixels ( uint uiSizeX, uint uiSizeY, CBuffer& buffer, SString& strOutError )
{
    HRESULT hr;

    // Try to get the back buffer
    IDirect3DSurface9* pD3DBackBufferSurface = NULL;
    hr = m_pDevice->GetBackBuffer ( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pD3DBackBufferSurface );
    if ( FAILED( hr ) )
    {
        strOutError = SString( "GetBackBuffer failed (0x%08x)", hr );
        return false;
    }

    // Adjust/create screenshot target size
    if ( !m_pScreenShotTemp || m_pScreenShotTemp->m_uiSizeX != uiSizeX || m_pScreenShotTemp->m_uiSizeY != uiSizeY )
    {
        // Delete old one if it exists
        SAFE_RELEASE( m_pScreenShotTemp );

        // Try to create new one if needed
        if ( uiSizeX > 0 )
            m_pScreenShotTemp = CGraphics::GetSingleton ().GetRenderItemManager ()->CreateRenderTarget ( uiSizeX, uiSizeY, false, true );
    }

    if ( !m_pScreenShotTemp )
    {
        strOutError = "No ScreenShotTemp";
        return false;
    }


    // Copy back buffer into our private render target
    D3DTEXTUREFILTERTYPE FilterType = D3DTEXF_LINEAR;
    hr = m_pDevice->StretchRect( pD3DBackBufferSurface, NULL, m_pScreenShotTemp->m_pD3DRenderTargetSurface, NULL, FilterType );
    if ( FAILED( hr ) )
    {
        strOutError = SString( "StretchRect failed (0x%08x)", hr );
        return false;
    }

    // Clean up
    SAFE_RELEASE( pD3DBackBufferSurface );

    if ( !m_pScreenShotTemp->ReadPixels ( buffer, strOutError ) )
    {
        dassert( !strOutError.empty() );
        return false;
    }

    return true;
}