Loading BMP to a DirectDrawSurface
Does this code do it right???
Im getting the surface but theres some wierd stuff in it, definitley no bitmap of mine.
This isnt all my code so i dont quite understand parts of it, for example, the GetObject, well anyway...
ddMain is IDirectDraw7*.
Code:
bool CreateSurfaceFromBMP( IDirectDrawSurface7** ppSurface,
TCHAR* strBMP,
DWORD dwDesiredWidth,
DWORD dwDesiredHeight )
{
HBITMAP hBMP = NULL;
BITMAP bmp;
DDSURFACEDESC2 ddsd;
*ppSurface = NULL;
hBMP = (HBITMAP) LoadImage( NULL, strBMP,
IMAGE_BITMAP, dwDesiredWidth, dwDesiredHeight,
LR_LOADFROMFILE | LR_CREATEDIBSECTION );
// Get size of the bitmap
GetObject( hBMP, sizeof(bmp), &bmp );
// Create a DirectDrawSurface for this bitmap
ZeroMemory( &ddsd, sizeof(ddsd) );
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = bmp.bmWidth;
ddsd.dwHeight = bmp.bmHeight;
ddMain->CreateSurface(&ddsd,ppSurface,NULL);
// Draw the bitmap on this surface
HDC hdcSprite = CreateCompatibleDC( NULL );
SelectObject(hdcSprite,hBMP);
//Now that the BMP is in a DC, get the DC for the surface
//and just blit it using Windows GDI
HDC hdcSurface;
(*ppSurface)->GetDC(&hdcSurface);
BitBlt(hdcSprite,0,0,bmp.bmWidth,bmp.bmWidth,hdcSurface,0,0,SRCCOPY);
(*ppSurface)->ReleaseDC(hdcSurface);
DeleteObject( hBMP );
return true;
}