CHARGEMENT D'IMAGE BMP
//////////////////////////////////////////////
// //
// Chargement d'un Bitmap et creation //
// d'une surface DDRAW sur cette surface //
// //
//////////////////////////////////////////////
LPDIRECTDRAWSURFACE7 BitmapSurface(LPCTSTR file_name)
{
HDC hdc;
HBITMAP bit;
LPDIRECTDRAWSURFACE7 surf;
// Chargement de la Bitmap
bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
if (!bit)
{
MessageBox(NULL,"Erreur chargement image Bitmap","Boulder",MB_OK);
return NULL;
}
// Obtient les dimensions du Bitmap
BITMAP bitmap;
GetObject( bit, sizeof(BITMAP), &bitmap );
int surf_width=bitmap.bmWidth;
int surf_height=bitmap.bmHeight;
// create surface
HRESULT ddrval;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY;
ddsd.dwWidth = surf_width;
ddsd.dwHeight = surf_height;
// Creation de la surface
ddrval=lpDD->CreateSurface(&ddsd,&surf,NULL);
if (ddrval!=DD_OK)
{
DeleteObject(bit);
return NULL;
} else {
// Obtenir le DC pour la surface
surf->GetDC(&hdc);
// Generer une surface compatible
HDC bit_dc=CreateCompatibleDC(hdc);
// blit the interface to the surface
SelectObject(bit_dc,bit);
BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY);
// Libere le DC
surf->ReleaseDC(hdc);
DeleteDC(bit_dc);
}
DeleteObject(bit);
return surf;
}
Voici le prototype de la fonction:
LPDIRECTDRAWSURFACE7 BitmapSurface(LPCTSTR file_name);
Retour:
Une surface LPDIRECTDRAWSURFACE7 contenant l'image BMP ou NULL en cas d'erreur.
Parametre:
Le nom du fichier BMP a charger.