Jump to content
  • Advertisement
Sign in to follow this  
Dangling Pointer

Trouble with Bitmaps in VC++

This topic is 5046 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I'm in the middle of making a level-editor for a simple 2D tile-based game. I have it working to the point where I can color my map with black and white to my heart's desire, but when I try to use any other color, it becomes black! I'll give an overview of what I'm doing first. I made a Tile Manager class, so that I can easily keep track of my tiles, and use the same class to get tiles for drawing in the game side of things as well. I have a struct representing each tile, holding information such as: an HDC to the tile, an LPSTR (which is either the filename, or the result of LOADINTRESOURCE(id), if it's a resource), and some numbers 'n stuff such as width/height/location on the bitmap/etc When I load a new tile in the tile manager, I load the bitmap, select it to a temporary DC, and bitblt the proper location to the HDC for the tile. From then on, when I need that tile to draw with, I get it from the tile manager using its ID number. For displaying the map that's being made, I blit the proper tiles to a back-buffer (according to a 2D array with information such as which tile is there), and then blit that buffer to the screen. I've tested played around with it using 2 tiles as resources - one that is pure black, and one that is white. Those two work fine. However, as alluded to above, I tried testing with some other colors, but they display as black. I'm wondering if this is some kind of palette problem? I've got a bit of experience using bitmaps/etc in vc++, but I've not come across this before. Does anyone have any ideas as to what it might be, off the bat? I can post code if you need to take a look at it. Thanks in advance for your help! (sorry for the long-ish post :P)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Dangling Pointer


Hello all, I'm in the middle of making a level-editor for a simple 2D tile-based game. I have it working to the point where I can color my map with black and white to my heart's desire, but when I try to use any other color, it becomes black!

I'll give an overview of what I'm doing first.

I made a Tile Manager class, so that I can easily keep track of my tiles, and use the same class to get tiles for drawing in the game side of things as well.

I have a struct representing each tile, holding information such as:
an HDC to the tile, an LPSTR (which is either the filename, or the result of LOADINTRESOURCE(id), if it's a resource), and some numbers 'n stuff such as width/height/location on the bitmap/etc

When I load a new tile in the tile manager, I load the bitmap, select it to a temporary DC, and bitblt the proper location to the HDC for the tile. From then on, when I need that tile to draw with, I get it from the tile manager using its ID number.

For displaying the map that's being made, I blit the proper tiles to a back-buffer (according to a 2D array with information such as which tile is there), and then blit that buffer to the screen.

I've tested played around with it using 2 tiles as resources - one that is pure black, and one that is white. Those two work fine. However, as alluded to above, I tried testing with some other colors, but they display as black.

I'm wondering if this is some kind of palette problem? I've got a bit of experience using bitmaps/etc in vc++, but I've not come across this before.

Does anyone have any ideas as to what it might be, off the bat? I can post code if you need to take a look at it.

Thanks in advance for your help! (sorry for the long-ish post :P)
If you upload the whole thing including image files and the vc++ workspace. I'll have a look. Errors like these are usually a pain.

Share this post


Link to post
Share on other sites
Hi, thanks for the reply.

I rar-ed it all up into one so I could show it to you.. but how do I upload it to this forum? I don't have have any web space online I can link to.. though I guess I could just make a new gmail account and leave it there, hehe.

Share this post


Link to post
Share on other sites
How do you create the temporary dc? Usually dc without any context to clone from are created mono, 1bit, with a black and white palette set. You might want to use createcompatibledc and insert the desktop dc.

Share this post


Link to post
Share on other sites

Here's where I load the bitmaps.


BOOL TileManager::loadTile(int id)
{
HBITMAP oldhbm;
HINSTANCE hinstance;

HDC hSrcDC;
HBITMAP oldBmp, newBmp;

// find the tile with this ID
tm_iter i;
i = theTiles.find(id);
if (i == theTiles.end())
return FALSE;

// get a pointer to the tile it points to
tm_tile* p_t = &((*i).second);

hinstance = AfxGetInstanceHandle();

p_t->hdc = CreateCompatibleDC(mh_dc);
p_t->hbm = CreateCompatibleBitmap(p_t->hdc, p_t->width, p_t->height);
SelectObject(p_t->hdc, p_t->hbm);
RealizePalette(p_t->hdc);

hSrcDC = CreateCompatibleDC(mh_dc);

newBmp = (HBITMAP)LoadImage(hinstance, p_t->bmpName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
if(newBmp == NULL)
return FALSE;

//TODO: Support of loading from a file, rather than a resource

// select the bitmap to the temp DC
oldhbm = (HBITMAP)SelectObject(hSrcDC, newBmp);
RealizePalette(hSrcDC);

// blit the specified bits to the target DC
if (BitBlt(p_t->hdc, 0, 0, p_t->width, p_t->height,
hSrcDC, p_t->x0, p_t->y0, SRCCOPY) == FALSE)
return FALSE;

//release the bitmap and the temp DC
SelectObject(hSrcDC, oldhbm);
DeleteObject(newBmp);
DeleteDC(hSrcDC);

return TRUE;
}




The mh_dc is an HDC which was passed into the TileManager at construction time, as follows:

CClientDC dc(this);
HDC hdc = (HDC)dc;
int n;

m_pTileMan = new TileManager(hdc);


This is called from within the main dialog of my dialog-based application.

Although, if for some reason the original DC was only 1bit I guess it would trickle into everything else.. I'm gonna check that out after posting this, and let you know if I find something.

Share this post


Link to post
Share on other sites
*bump*
Sorry, still haven't figured this out. I've found a way around it so that I can continue with what I'm working on despite the problem. If anyone's got any suggestions that'd be great.

Share this post


Link to post
Share on other sites
Just use the DC of the desktop to create your compatible dc.

Use GetDC( NULL ) to get the dc of the desktop, create a compatible dc and release the dc of the desktop as soon as possible. Use your "copy" of the dc to create the other dcs in your app.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!