Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading Bitmaps in DDraw

Started by zeroshot Dec 23, 2001 at 9:13 AM 1 replies 250+ views
Original Post
zeroshot
zeroshot
Can ne 1 plz give me some code to load a 16bit bmp onto a ddraw surface, i got some code out of a book i got but it does 8bit bmps plz or some decent web sites for total newbies
YoshiN
YoshiN
I recently had a lot of trouble with this myself, so I'll give you the code that I wrote:

    
///////////////////////////////////////////////////////////////

//

// Loads a bitmap from a file (fname) onto the surface(image)

//

///////////////////////////////////////////////////////////////

int Load_Bitmap(LPDIRECTDRAWSURFACE7 image,
char *fname)
{
HDC hdc, hdcimage; // device context of the surface and image

BITMAP bitmap; // bitmap object

HBITMAP hbitmap; // handle to the bitmap


// load the bitmap and get the object

hbitmap = (HBITMAP)LoadImage(NULL, fname, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE);
if(hbitmap == NULL)
return(0);

// get a device context that is compatible with the drawing surface

hdcimage = CreateCompatibleDC(NULL);
if(hdcimage == NULL)
return(0);

// select the handle to the bitmap as the object for the device context of the image

if(SelectObject(hdcimage, hbitmap) == NULL)
return(0);

// get the size of the object

if(GetObject(hbitmap, sizeof(bitmap), &bitmap) == NULL)
return(0);

// get the device context for the ddraw surface

if(image->GetDC(&hdc)!=DD_OK)
return(0);

// blit the image from the image's DC to the surface's DC

if(BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcimage, 0, 0, SRCCOPY) == NULL)
return(0);

// release the device context and destroy the image device context

if(image->ReleaseDC(hdc)!=DD_OK)
return(0);
if(DeleteDC(hdcimage) == NULL)
return(0);

return(1);
} // end Load_Image()



So just create a surface that is the same size of the image, and then use the following line:


// load penguinx.bmp onto the offscreen surface

if(!Load_Bitmap(penguin, "penguins.bmp"))
return(0);


penguin is an offscreenplain surface that is 300x200, and penguins.bmp is a 300x200x24 bitmap. This function will change the color depth of a bitmap so that it is compatible with the surface, so you can load a bitmap of any color depth onto a surface of any color depth.

-Yoshi
XGXCX ''99

Edited by - YoshiN on December 23, 2001 12:15:28 PM
-YoshiXGXCX ''99

Topic Locked

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

Sign in to reply to this topic.