Skip to main content
gamedev.net
🔒 Locked

Trouble loading bitmaps

Started by ComputrJam Sep 4, 2000 at 6:38 PM 2 replies 600+ views
Original Post
ComputrJam
ComputrJam
I really don''t know what''s wrong with my code, I''m getting very frustrated with directx. OpenGL is so easy to use compared to directx its almost funny. But for some reason I want to continue torturing myself by trying to learn it. My "LoadBitmap" function is below, please help: ///////////////////////////////////////////////////////////////// // // Func: LoadBitmap // Desc: loads bitmap from file // ///////////////////////////////////////////////////////////////// IDirectDrawSurface7 *LoadBitmap() { HBITMAP hbm = NULL; BITMAP bm; HDC hdcImage = NULL; HDC hdc = NULL; DDSURFACEDESC2 ddsd; IDirectDrawSurface7 *pdds; // // Try to load the bitmap as a file // hbm = (HBITMAP) LoadImage(NULL, "test.bmp", IMAGE_BITMAP, 0, 0,LR_LOADFROMFILE); if(hbm == NULL) { MessageBox(NULL,"Loading Image Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); } // // Get size of the bitmap // GetObject(hbm, sizeof(bm), &bm); // // 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 = bm.bmWidth; ddsd.dwHeight = bm.bmHeight; if (DirectDraw7->CreateSurface(&ddsd, &pdds, NULL) != DD_OK) { // Failed return NULL; } // // Select bitmap into a memoryDC so it can be used // hdcImage = CreateCompatibleDC(NULL); if (pdds->GetDC(&hdc) == DD_OK) { BitBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, 0, 0, SRCCOPY); pdds->ReleaseDC(hdc); } DeleteObject(hbm); DeleteDC(hdcImage); return pdds; }
dj9781
dj9781
Heres my loadbitmap function:

    
LoadBitmap(LPDIRECTDRAW7 lpDD, RECT *dims)
{
HDC hdc;
HBITMAP bit;
LPDIRECTDRAWSURFACE7 surf=NULL;

// load the bitmap


bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,
LR_DEFAULTSIZE|LR_LOADFROMFILE);
if (!bit)
// failed to load, return failure to caller
return 0;

// get bitmap dimensions

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;
ddsd.dwWidth = surf_width;
ddsd.dwHeight = surf_height;

// attempt to create surface

ddrval=lpDD->CreateSurface(&ddsd,&surf,NULL);

// created ok?

if (ddrval!=DD_OK)
{
// no, release the bitmap and return failure to caller
DeleteObject(bit);
return 0;
}

else
{
// yes, get a DC for the surface
surf->GetDC(&hdc);

// generate a compatible DC

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);

// release the DCs

surf->ReleaseDC(hdc);
DeleteDC(bit_dc);

// save the dimensions if rectangle pointer provided

if (dims)
{
dims->left=0;
dims->top=0;
dims->right=surf_width;
dims->bottom=surf_height;
}
}

// clear bitmap

DeleteObject(bit);


//return succes

return 1;
}


and now surf has the bitmap loaded into it.
Ironblayde
Ironblayde
ComputrJam: Your function creates a device context for the bitmap, but never selects the bitmap into the device context, thus your call to BitBlt() is not copying anything. After the call to CreateCompatibleDC(), be sure to use SelectObject() to select the bitmap into the DC.

-Ironblayde
 Aeon Software
"Your superior intellect is no match for our puny weapons!"
ComputrJam
ComputrJam
You''re right, that''s what I was doing wrong. It works now...thanks for your input guys.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.