Basic DX question: bitmap resources

Started by
3 comments, last by Ironblayde 23 years, 10 months ago
I''m fairly new to Windows programming, and what I''m trying to do is to load a bitmap resource from my program and place it in an offscreen buffer. The resource is included properly, and all required surfaces are created without any errors. I get a handle to the bitmap something like this: HBITMAP bitmap = LoadBitmap(main_instance, MAKEINTRESOURCE(MAIN_TILESET)); But what do I do from there? I''ve been reading Tricks of the Windows Game Programming Gurus, which includes sample code for reading a bitmap from an external file and dumping it onto the primary surface. It''s not so much the fact that it works with external files and not resources that gets me confused, it''s more that the sample code doesn''t run right on my computer. ^_^ It errors out somewhere and just gives me a black screen, so that''s not helping too much. What I tried doing is casting the HBITMAP handle to an HFILE and then using some file I/O routines to read in the headers and the image data, but that didn''t work at all. Can anyone point me to some explanations on how to work with bitmap resources? I haven''t been able to find anything. Thanks a lot- -Joe [Ironblayde] Aeon Software
"Your superior intellect is no match for our puny weapons!"
Advertisement
First you have to get a memory DC for your bitmap:

HBITMAP hbm; // your bitmap
HDC hdcImage; // will be the memory DC for our Image

hdcImage=CreateCompatibleDC(NULL);
SelectObject(hdcImage, hbm);

You can also get a DC for the surface

HDC hdc;
pdds->GetDC(&hdc);

Now you can use a GDI function like BitBlt or StrechBlt to copy your bitmap into the surface.
Don''t forget to release the DC of the surface using

pdds->ReleaseDC(hdc)

You could also use the DDCopyBitmap function from the DDUtils.cpp and DDUtils.h files found in the DX-SDK (that''s at least where I got this information from).

Hope that''s what you need.
That doesn''t sound too bad at all. ^_^ I should be able to get it working that way; thanks a lot!

-Joe [Ironblayde]
Aeon Software
"Your superior intellect is no match for our puny weapons!"
Hmm. I''ve been doing this for most of the day now, and it''s still not working. Basically all the function does now is what you suggested. It sets up DCs for the image and the surface, makes a call to LoadBitmap to get the HBITMAP, then selects it into the image DC using SelectObject. Then I made a call to BitBlt, followed by releasing the DC and all that cleanup stuff.

But the bitmap isn''t getting plotted on the surface. Instead, the surface is getting blanked. When I run the program without calling the above function, I have it set for now to flip the back buffer onto my primary surface, just to see what''s back there. Invariably, it starts with some garbage on the surface that shows up immediately. But when I call the above function, it just ends up black. None of the functions returns an error code, so it''s doing *something*. I just don''t know what.

I''ve checked to make sure that the surfaces are set up right. No function in the program is returning an error code, and basic stuff like pixel plotting on the back buffer, or on offscreen buffers, works fine. But I can''t figure out the bitmap resources! What am I doing wrong? The only ideas I could come up with are:

1) SelectObject says in the help that it requires a bitmap created with CreateBitmap; it doesn''t say anything about using LoadBitmap. Does it expect a pointer right to the color data, without all the headers? What exactly is a ''handle'' to a bitmap anyway? It can''t be the same as a pointer, but it sure would be nice if I could find a pointer to the data and just read the info out!

2) It''s a 24-bit image and I''m using a 16-bit resolution. I assume this would be a problem. This function doesn''t need to be called often and doesn''t need to have blinding speed, so I was just going to go through a pixel at a time doing a color transform down to 16-bit, then plotting it out.

If anyone can help me out a little further on getting bitmap resources onto a surface, I would be very grateful! Thanks-

-Joe [Ironblayde]
Aeon Software
"Your superior intellect is no match for our puny weapons!"
One thing you could try. Make the DC of the image compatible to the DC of the surface, by using
CreateCompatibleDC(hdc);  


By the way, did you look into DDUtils.cpp. For example it may be found ind the DDex2 subdir of the DDraw samples in the SDK? They used LoadImage to load the Bitmap as DIB.


Edited by - VolkerG on May 29, 2000 2:57:47 PM

This topic is closed to new replies.

Advertisement