Engine/sdk/src/d3dutil.cpp
HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE8 pd3dDevice, HCURSOR hCursor )
{
    HRESULT hr = E_FAIL;
    ICONINFO iconinfo;
    BOOL bBWCursor;
    LPDIRECT3DSURFACE8 pCursorBitmap = NULL;
    HDC hdcColor = NULL;
    HDC hdcMask = NULL;
    HDC hdcScreen = NULL;
    BITMAP bm;
    DWORD dwWidth;
    DWORD dwHeightSrc;
    DWORD dwHeightDest;
    COLORREF crColor;
    COLORREF crMask;
    UINT x;
    UINT y;
    BITMAPINFO bmi;
    COLORREF* pcrArrayColor = NULL;
    COLORREF* pcrArrayMask = NULL;
    DWORD* pBitmap;
    HGDIOBJ hgdiobjOld;

    ZeroMemory( &iconinfo, sizeof(iconinfo) );
    if( !GetIconInfo( hCursor, &iconinfo ) )
        goto End;

    if (0 == GetObject((HGDIOBJ)iconinfo.hbmMask, sizeof(BITMAP), (LPVOID)&bm))
        goto End;
    dwWidth = bm.bmWidth;
    dwHeightSrc = bm.bmHeight;

    if( iconinfo.hbmColor == NULL )
    {
        bBWCursor = TRUE;
        dwHeightDest = dwHeightSrc / 2;
    }
    else 
    {
        bBWCursor = FALSE;
        dwHeightDest = dwHeightSrc;
    }

    // Create a surface for the fullscreen cursor
    if( FAILED( hr = pd3dDevice->CreateImageSurface( dwWidth, dwHeightDest, 
        D3DFMT_A8R8G8B8, &pCursorBitmap ) ) )
    {
        goto End;
    }

    pcrArrayMask = new DWORD[dwWidth * dwHeightSrc];

    ZeroMemory(&bmi, sizeof(bmi));
    bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
    bmi.bmiHeader.biWidth = dwWidth;
    bmi.bmiHeader.biHeight = dwHeightSrc;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;

    hdcScreen = GetDC( NULL );
    hdcMask = CreateCompatibleDC( hdcScreen );
    if( hdcMask == NULL )
    {
        hr = E_FAIL;
        goto End;
    }
    hgdiobjOld = SelectObject(hdcMask, iconinfo.hbmMask);
    GetDIBits(hdcMask, iconinfo.hbmMask, 0, dwHeightSrc, 
        pcrArrayMask, &bmi, DIB_RGB_COLORS);
    SelectObject(hdcMask, hgdiobjOld);

    if (!bBWCursor)
    {
        pcrArrayColor = new DWORD[dwWidth * dwHeightDest];
        hdcColor = CreateCompatibleDC( GetDC( NULL ) );
        if( hdcColor == NULL )
        {
            hr = E_FAIL;
            goto End;
        }
        SelectObject(hdcColor, iconinfo.hbmColor);
        GetDIBits(hdcColor, iconinfo.hbmColor, 0, dwHeightDest, 
            pcrArrayColor, &bmi, DIB_RGB_COLORS);
    }

    // Transfer cursor image into the surface
    D3DLOCKED_RECT lr;
    pCursorBitmap->LockRect( &lr, NULL, 0 );
    pBitmap = (DWORD*)lr.pBits;
    for( y = 0; y < dwHeightDest; y++ )
    {
        for( x = 0; x < dwWidth; x++ )
        {
            if (bBWCursor)
            {
                crColor = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
                crMask = pcrArrayMask[dwWidth*(dwHeightSrc-1-y) + x];
            }
            else
            {
                crColor = pcrArrayColor[dwWidth*(dwHeightDest-1-y) + x];
                crMask = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
            }
            if (crMask == 0)
                pBitmap[dwWidth*y + x] = 0xff000000 | crColor;
            else
                pBitmap[dwWidth*y + x] = 0x00000000;
        }
    }
    pCursorBitmap->UnlockRect();

    // Set the device cursor
    if( FAILED( hr = pd3dDevice->SetCursorProperties( iconinfo.xHotspot, 
        iconinfo.yHotspot, pCursorBitmap ) ) )
    {
        goto End;
    }

    hr = S_OK;

End:
    if( iconinfo.hbmMask != NULL )
        DeleteObject( iconinfo.hbmMask );
    if( iconinfo.hbmColor != NULL )
        DeleteObject( iconinfo.hbmColor );
    if( hdcScreen != NULL )
        ReleaseDC( NULL, hdcScreen );
    if( hdcColor != NULL )
        DeleteDC( hdcColor );
    if( hdcMask != NULL )
        DeleteDC( hdcMask );
    SAFE_DELETE_ARRAY( pcrArrayColor );
    SAFE_DELETE_ARRAY( pcrArrayMask );
    SAFE_RELEASE( pCursorBitmap );
    return hr;
}
//-----------------------------------------------------------------------------
// Name: D3DUtil_SetDeviceCursor
// Desc: Gives the D3D device a cursor with image and hotspot from hCursor.
//-----------------------------------------------------------------------------
D3DUtil_SetDeviceCursor
Desc: Gives the D3D device a cursor with image and hotspot from hCursor.
D3DUtil_SetDeviceCursor Desc : Gives the D3D device a cursor with image and hotspot from hCursor .
HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE8 pd3dDevice, HCURSOR hCursor )
{
    HRESULT hr = E_FAIL;
    ICONINFO iconinfo;
    BOOL bBWCursor;
    LPDIRECT3DSURFACE8 pCursorBitmap = NULL;
    HDC hdcColor = NULL;
    HDC hdcMask = NULL;
    HDC hdcScreen = NULL;
    BITMAP bm;
    DWORD dwWidth;
    DWORD dwHeightSrc;
    DWORD dwHeightDest;
    COLORREF crColor;
    COLORREF crMask;
    UINT x;
    UINT y;
    BITMAPINFO bmi;
    COLORREF* pcrArrayColor = NULL;
    COLORREF* pcrArrayMask = NULL;
    DWORD* pBitmap;
    HGDIOBJ hgdiobjOld;
    ZeroMemory( &iconinfo, sizeof(iconinfo) );
    if( !GetIconInfo( hCursor, &iconinfo ) )
        goto End;
    if (0 == GetObject((HGDIOBJ)iconinfo.hbmMask, sizeof(BITMAP), (LPVOID)&bm))
        goto End;
    dwWidth = bm.bmWidth;
    dwHeightSrc = bm.bmHeight;
    if( iconinfo.hbmColor == NULL )
    {
        bBWCursor = TRUE;
        dwHeightDest = dwHeightSrc / 2;
    }
    else 
    {
        bBWCursor = FALSE;
        dwHeightDest = dwHeightSrc;
    }
    if( FAILED( hr = pd3dDevice->CreateImageSurface( dwWidth, dwHeightDest, 
        D3DFMT_A8R8G8B8, &pCursorBitmap ) ) )
    {
        goto End;
    }
    pcrArrayMask = new DWORD[dwWidth * dwHeightSrc];
    ZeroMemory(&bmi, sizeof(bmi));
    bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
    bmi.bmiHeader.biWidth = dwWidth;
    bmi.bmiHeader.biHeight = dwHeightSrc;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    hdcScreen = GetDC( NULL );
    hdcMask = CreateCompatibleDC( hdcScreen );
    if( hdcMask == NULL )
    {
        hr = E_FAIL;
        goto End;
    }
    hgdiobjOld = SelectObject(hdcMask, iconinfo.hbmMask);
    GetDIBits(hdcMask, iconinfo.hbmMask, 0, dwHeightSrc, 
        pcrArrayMask, &bmi, DIB_RGB_COLORS);
    SelectObject(hdcMask, hgdiobjOld);
    if (!bBWCursor)
    {
        pcrArrayColor = new DWORD[dwWidth * dwHeightDest];
        hdcColor = CreateCompatibleDC( GetDC( NULL ) );
        if( hdcColor == NULL )
        {
            hr = E_FAIL;
            goto End;
        }
        SelectObject(hdcColor, iconinfo.hbmColor);
        GetDIBits(hdcColor, iconinfo.hbmColor, 0, dwHeightDest, 
            pcrArrayColor, &bmi, DIB_RGB_COLORS);
    }
    D3DLOCKED_RECT lr;
    pCursorBitmap->LockRect( &lr, NULL, 0 );
    pBitmap = (DWORD*)lr.pBits;
    for( y = 0; y < dwHeightDest; y++ )
    {
        for( x = 0; x < dwWidth; x++ )
        {
            if (bBWCursor)
            {
                crColor = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
                crMask = pcrArrayMask[dwWidth*(dwHeightSrc-1-y) + x];
            }
            else
            {
                crColor = pcrArrayColor[dwWidth*(dwHeightDest-1-y) + x];
                crMask = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
            }
            if (crMask == 0)
                pBitmap[dwWidth*y + x] = 0xff000000 | crColor;
            else
                pBitmap[dwWidth*y + x] = 0x00000000;
        }
    }
    pCursorBitmap->UnlockRect();
    if( FAILED( hr = pd3dDevice->SetCursorProperties( iconinfo.xHotspot, 
        iconinfo.yHotspot, pCursorBitmap ) ) )
    {
        goto End;
    }
    hr = S_OK;
End:
    if( iconinfo.hbmMask != NULL )
        DeleteObject( iconinfo.hbmMask );
    if( iconinfo.hbmColor != NULL )
        DeleteObject( iconinfo.hbmColor );
    if( hdcScreen != NULL )
        ReleaseDC( NULL, hdcScreen );
    if( hdcColor != NULL )
        DeleteDC( hdcColor );
    if( hdcMask != NULL )
        DeleteDC( hdcMask );
    SAFE_DELETE_ARRAY( pcrArrayColor );
    SAFE_DELETE_ARRAY( pcrArrayMask );
    SAFE_RELEASE( pCursorBitmap );
    return hr;
}
HRESULT D3DUtil_SetDeviceCursor ( LPDIRECT3DDEVICE8 pd3dDevice , HCURSOR hCursor ) { HRESULT hr = E_FAIL ; ICONINFO iconinfo ; BOOL bBWCursor ; LPDIRECT3DSURFACE8 pCursorBitmap = NULL ; HDC hdcColor = NULL ; HDC hdcMask = NULL ; HDC hdcScreen = NULL ; BITMAP bm ; DWORD dwWidth ; DWORD dwHeightSrc ; DWORD dwHeightDest ; COLORREF crColor ; COLORREF crMask ; UINT x ; UINT y ; BITMAPINFO bmi ; COLORREF * pcrArrayColor = NULL ; COLORREF * pcrArrayMask = NULL ; DWORD * pBitmap ; HGDIOBJ hgdiobjOld ; ZeroMemory ( & iconinfo , sizeof ( iconinfo ) ) ; if ( ! GetIconInfo ( hCursor , & iconinfo ) ) goto End ; if ( 0 == GetObject ( ( HGDIOBJ ) iconinfo . hbmMask , sizeof ( BITMAP ) , ( LPVOID ) & bm ) ) goto End ; dwWidth = bm . bmWidth ; dwHeightSrc = bm . bmHeight ; if ( iconinfo . hbmColor == NULL ) { bBWCursor = TRUE ; dwHeightDest = dwHeightSrc / 2 ; } else { bBWCursor = FALSE ; dwHeightDest = dwHeightSrc ; } if ( FAILED ( hr = pd3dDevice -> CreateImageSurface ( dwWidth , dwHeightDest , D3DFMT_A8R8G8B8 , & pCursorBitmap ) ) ) { goto End ; } pcrArrayMask = new DWORD [ dwWidth * dwHeightSrc ] ; ZeroMemory ( & bmi , sizeof ( bmi ) ) ; bmi . bmiHeader . biSize = sizeof ( bmi . bmiHeader ) ; bmi . bmiHeader . biWidth = dwWidth ; bmi . bmiHeader . biHeight = dwHeightSrc ; bmi . bmiHeader . biPlanes = 1 ; bmi . bmiHeader . biBitCount = 32 ; bmi . bmiHeader . biCompression = BI_RGB ; hdcScreen = GetDC ( NULL ) ; hdcMask = CreateCompatibleDC ( hdcScreen ) ; if ( hdcMask == NULL ) { hr = E_FAIL ; goto End ; } hgdiobjOld = SelectObject ( hdcMask , iconinfo . hbmMask ) ; GetDIBits ( hdcMask , iconinfo . hbmMask , 0 , dwHeightSrc , pcrArrayMask , & bmi , DIB_RGB_COLORS ) ; SelectObject ( hdcMask , hgdiobjOld ) ; if ( ! bBWCursor ) { pcrArrayColor = new DWORD [ dwWidth * dwHeightDest ] ; hdcColor = CreateCompatibleDC ( GetDC ( NULL ) ) ; if ( hdcColor == NULL ) { hr = E_FAIL ; goto End ; } SelectObject ( hdcColor , iconinfo . hbmColor ) ; GetDIBits ( hdcColor , iconinfo . hbmColor , 0 , dwHeightDest , pcrArrayColor , & bmi , DIB_RGB_COLORS ) ; } D3DLOCKED_RECT lr ; pCursorBitmap -> LockRect ( & lr , NULL , 0 ) ; pBitmap = ( DWORD * ) lr . pBits ; for ( y = 0 ; y < dwHeightDest ; y ++ ) { for ( x = 0 ; x < dwWidth ; x ++ ) { if ( bBWCursor ) { crColor = pcrArrayMask [ dwWidth * ( dwHeightDest - 1 - y ) + x ] ; crMask = pcrArrayMask [ dwWidth * ( dwHeightSrc - 1 - y ) + x ] ; } else { crColor = pcrArrayColor [ dwWidth * ( dwHeightDest - 1 - y ) + x ] ; crMask = pcrArrayMask [ dwWidth * ( dwHeightDest - 1 - y ) + x ] ; } if ( crMask == 0 ) pBitmap [ dwWidth * y + x ] = 0xff000000 | crColor ; else pBitmap [ dwWidth * y + x ] = 0x00000000 ; } } pCursorBitmap -> UnlockRect ( ) ; if ( FAILED ( hr = pd3dDevice -> SetCursorProperties ( iconinfo . xHotspot , iconinfo . yHotspot , pCursorBitmap ) ) ) { goto End ; } hr = S_OK ; End : if ( iconinfo . hbmMask != NULL ) DeleteObject ( iconinfo . hbmMask ) ; if ( iconinfo . hbmColor != NULL ) DeleteObject ( iconinfo . hbmColor ) ; if ( hdcScreen != NULL ) ReleaseDC ( NULL , hdcScreen ) ; if ( hdcColor != NULL ) DeleteDC ( hdcColor ) ; if ( hdcMask != NULL ) DeleteDC ( hdcMask ) ; SAFE_DELETE_ARRAY ( pcrArrayColor ) ; SAFE_DELETE_ARRAY ( pcrArrayMask ) ; SAFE_RELEASE ( pCursorBitmap ) ; return hr ; }
Name: D3DUtil_SetDeviceCursor
Desc: Gives the D3D device a cursor with image and hotspot from hCursor.
Name : D3DUtil_SetDeviceCursor Desc : Gives the D3D device a cursor with image and hotspot from hCursor .
// Create a surface for the fullscreen cursor // Transfer cursor image into the surface // Set the device cursor