Skip to main content
gamedev.net
🔒 Locked

Loading Bitmaps

Started by vbisme 6 replies 200+ views
Original Post
vbisme
vbisme
Ok, ok. I know I''m an C++ idiot, just started. I''m doing DX7, but I couldn''t understand the concept of loading bitmaps(.bmp) into surfaces, I''ve read the one article here, but only got the code and little explaination. Could someone explain to me the concepts and how to do this?
nes8bit
nes8bit
Dude. It''s simple.

''Code starts here
Dim DDSD as DDSDESC7

with DDSD
.lFlags = DDSD_CAPS
.ddsCaps.lCaps = DDSCAPS_SYSTEMMEMORY
End With

Set DDSurf = DirectDraw.CreateSurfaceFromFile("c:\bitmapthingy.bmp",DDSD)

''Code ends here

That creates a Direct Draw surface from the "c:\bitmapthingy.bmp" in the system memory.

------------------------
Captured Reality
vbisme
vbisme
heheh, that''s the VB way, I''m asking about the C++ way.
Ironblayde
Ironblayde
It isn't too bad; here's what I use. This is a function to load a bitmap resource, where main_instance is the hinstance of my program. If you want to load from a file instead, all that needs changing is the LoadImage line. I'll comment as much as I can to try and make this clear. I haven't read the Sweet Snippets article that covers this, so if I'm just spitting out the same stuff, I apologize.

// the four parameters are an identifier for the resource, x
// and y coordinates for where the image should be placed on
// the surface, and a pointer to the surface itself. I wrote
// this using DirectX 6, but it should work fine for 7 as well
int Load_Bitmap(int ID, int x, int y, LPDIRECTDRAWSURFACE4 surf)
{
HBITMAP hbitmap = NULL;
BITMAP bmp;
HDC image_dc, surface_dc;

// first thing to do is to load the actual image. This will
// give you a handle to the bitmap you want to load
if (!(hbitmap = (HBITMAP)LoadImage(main_instance,
MAKEINTRESOURCE(ID), IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION)))
return(FALSE);

// now you need to create a device context for this image
if (!(image_dc = CreateCompatibleDC(NULL)))
return(FALSE);

// using the handle to your bitmap, select it into the DC
if (!(SelectObject(image_dc, hbitmap)))
return(FALSE);

// get a device context for the surface on which you want to
// place the image
if (FAILED(surf->GetDC(&surface_dc)))
return(FALSE);

// now use GetObject() to grab all the bitmap info, and from
// the resultant structure, read the bitmap's height and
// width
GetObject(hbitmap, sizeof(BITMAP), &bmp);
int width = bmp.bmWidth;
int height = bmp.bmHeight;

// now that you have source and destination device contexts,
// a call to BitBlt() will put the image onto the surface.
// If you want to scale the image, you can use StretchBlt()
// here instead
if (!(BitBlt(surface_dc, x, y, width, height, image_dc,
0, 0, SRCCOPY)))
return(FALSE);

// success! Kill the device contexts and you're finished
surf->ReleaseDC(surface_dc);
DeleteDC(image_dc);
return(TRUE);
}

-Ironblayde
 Aeon Software

Edited by - Ironblayde on July 1, 2000 3:09:17 AM
"Your superior intellect is no match for our puny weapons!"
vbisme
vbisme
The function I created here...why won''t it work?

int CreateBitmappedSurface(IDirectDrawSurface7 *lpSurface, char FileName, int sWidth, int sHeight)
{

DDSURFACEDESC2 ddsdOffScn;

HDC hdc = NULL;
HDC surfacehdc;
HBITMAP hbitmap = NULL;

ZeroMemory(&ddsdOffScn, sizeof(ddsdOffScn));
ddsdOffScn.dwSize = sizeof(ddsdOffScn);
ddsdOffScn.dwFlags = DDSD_CAPS / DDSD_WIDTH / DDSD_HEIGHT;
ddsdOffScn.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN / DDSCAPS_SYSTEMMEMORY;
ddsdOffScn.dwWidth = sWidth;
ddsdOffScn.dwHeight = sHeight;

lpDD->CreateSurface(&ddsdOffScn, &lpSurface, NULL);

hbitmap = HBITMAP(LoadImage(NULL, &FileName, IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION / LR_LOADFROMFILE / LR_DEFAULTSIZE));
if (!hbitmap)
{
return(0);
}//end if

hdc = CreateCompatibleDC(NULL);
if (!SelectObject(hdc, hbitmap))
{
return(0);
}//end if

lpSurface->GetDC(&surfacehdc);
if (!BitBlt(surfacehdc, 0, 0, sWidth, sHeight, hdc, 0, 0, SRCCOPY))
{
return(0);
}//end if
lpSurface->ReleaseDC(surfacehdc);
DeleteDC(hdc);
DeleteObject(hbitmap);

//success
return(1);
}
cow_in_the_well
cow_in_the_well
vbisme: what is the error?

try removing LR_CREATEDIBSECTION from the loadimage function...here is my bitmap loading function for ur reading pleasure :

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

// Loads a bitmap using the gdi

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

int ddLoadBitmap (char *filename, int priority)
{
int id;
HBITMAP bitmap;
BITMAP bmpinfo;
HDC dc;

// look for a free slot

for (int i = 1; i<MAX_BITMAPS; i++)
if (Bitmap<i>.lpdds == NULL)
{
id = i;
break;
}

if (!id)
{
AddError ("Bitmap load: No more slots");
return 0;
}

// Load the bitmap using loadimage

bitmap = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE / LR_DEFAULTSIZE);
if (!bitmap)
{
AddError ("Bitmap load: Could not load %s from disk", filename);
return 0;
}

// Copy bitmap into a memdc

HDC memdc = CreateCompatibleDC (NULL);
SelectObject (memdc, bitmap);

// Get bitmap info

GetObject (bitmap, sizeof (bmpinfo), &bmpinfo);

// Store dimensions

Bitmap[id].width = bmpinfo.bmWidth;
Bitmap[id].height = bmpinfo.bmHeight;

// Create offscreen surface

if (priority == PRIORITY_HIGH)
Bitmap[id].lpdds = CreateSurface (lpdd, Bitmap[id].width, Bitmap[id].height, DDSCAPS_VIDEOMEMORY);
if (priority == PRIORITY_LOW)
Bitmap[id].lpdds = CreateSurface (lpdd, Bitmap[id].width, Bitmap[id].height, DDSCAPS_SYSTEMMEMORY);
if (!Bitmap[id].lpdds)
{
DeleteObject (bitmap);
AddError ("Bitmap load: Cannot create offscreen surface");
return 0;
}

// Get the surfaces dc

Bitmap[id].lpdds->GetDC (&dc);
if (!dc)
{
DeleteObject (bitmap);
AddError ("Bitmap load: Error using lpdds->GetDC");
}

// BitBlt the bitmap to the surface

if (!BitBlt ( dc,
0,
0,
bmpinfo.bmWidth,
bmpinfo.bmHeight,
memdc,
0,
0,
SRCCOPY))
{
DeleteObject (bitmap);
AddError ("Bitmap load: Cannot blit bitmap data to surface");
return 0;
}

// Release DC

Bitmap[id].lpdds->ReleaseDC (dc);

// Release bitmap object

DeleteObject (bitmap);

return id;
}


there ya go

note: Bitmap[id].lpdds is the LPDIRECTDRAWSURFACE7...


-----------------------------
-cow_in_the_well

http://cowswell.gda.ods.org/
omegasyphon
omegasyphon
now is your bitmap loading functions for 8 bit files or 24bit files? and if there for 24 bit files how do u load a sprite from the file onto a surface"? thats the kind of article that they need. i dont want another dam article that is about loading files and putting sprites onto a surface and its all done with 8 bits i want to know how to do it in 16 bits and 24 bits and 32 bits not just one

Topic Locked

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

Sign in to reply to this topic.