Jump to content
  • Advertisement
Sign in to follow this  
Shock

Just a few GDI questions :D

This topic is 4887 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 ! I was hoping someone could help me with the following. Im using bitmaps to hold my GUI resources. I call LoadImage() and SelectObject() it into a DC. 1. How can I load the image contents without keeping the bitmap loaded? Also I have some code that uses GDI resources like fonts, brushes, etc.. repeatedly. The thing is I only want to create these resources once. I know that I should get the HGDIOBJ from SelectObject() after selecting it into my DC, but how do I free it? Currently I am doing the following:
/* hPen = Created Pen */
HGDIOBJ hOldPen = SelectObject( hDC, hPen );
/* Do whatever needs to be done with the pen here */
SelectObject( hDC, hOldPen );
I do not delete the old pen after I use it, which is about every WM_PAINT call. Im worried im leaking resources/memory here. Please advise. Thanks Alot!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Shock
1. How can I load the image contents without keeping the bitmap loaded?
I don't quite understand the question. You need to keep a HBITMAP and a HDC around when you're dealing with images. The HBITMAP is the actual image, and the HDC is needed to manipulate the HBITMAP.

Quote:
Original post by Shock
Also I have some code that uses GDI resources like fonts, brushes, etc.. repeatedly. The thing is I only want to create these resources once.

I know that I should get the HGDIOBJ from SelectObject() after selecting it into my DC, but how do I free it?

Currently I am doing the following:


/* hPen = Created Pen */
HGDIOBJ hOldPen = SelectObject( hDC, hPen );
/* Do whatever needs to be done with the pen here */
SelectObject( hDC, hOldPen );


I do not delete the old pen after I use it, which is about every WM_PAINT call. Im worried im leaking resources/memory here.
That's correct. YOu don't need to delete the old pen, you just need to put it back into the DC and Windows will clean it up for you.
You do need to delete your pen, however - which you should do when your app exits (In WM_DESTROY probably).

Share this post


Link to post
Share on other sites
It's all right to keep all your GDI resources around for as long as you need them. Just be sure that when you're completely done you select the original object back into the DC. You can then delete everything you allocated after that.

Share this post


Link to post
Share on other sites
Hello

Sorry if I was unclear with my first question. I should have put some code as well.


/* hBitmap = Loaded 24Bit Bitmap using LoadImage() */
DeleteObject( SelectObject( hDC, hBitmap ));


Firstly, The above code is in one of the DevIL samples. I am getting buggy stuff happening when blitting. So i'm thinking this could be the cause?

Basically I wanted to know if I need the Bitmap or can (this may sound stupid) I 'blit' the bitmap on to the DC and keep the DC like that until I call DeleteDC()?

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Shock

/* hBitmap = Loaded 24Bit Bitmap using LoadImage() */
DeleteObject( SelectObject( hDC, hBitmap ));

Firstly, The above code is in one of the DevIL samples. I am getting buggy stuff happening when blitting. So i'm thinking this could be the cause?
That code is ok, so long as hBitmap is actually the return value from SelectObject(). SelectObject() returns the previous object that was selected into the DC. So if you put the old bitmap back into the DC, you get the bitmap you created with LoadImage as a return value, which is then passed to DeleteObject().
That code should really only be called when the application shuts down, not in the main loop - so it shouldn't affect blitting.

Quote:
Original post by Shock
Basically I wanted to know if I need the Bitmap or can (this may sound stupid) I 'blit' the bitmap on to the DC and keep the DC like that until I call DeleteDC()?
You can only blit from one DC to another DC. The following (psuedo)code should work fine:

// Create resources
HDC hDC = CreateCompatibleDC(...);
HBITMAP hBmp = LoadImage(...);

// Select the new bitmap into the DC and do blitting
hBmp = SelectObject(hDC,hBmp);
HDC hDCWindow = GetDC(hWnd);
BitBlt(hDC,hDCWindow);
ReleaseDC(hDCWindow);

// Get our real bitmap back
hBmp = SelectObject(hDC,hBmp);

// Cleanup
DeleteObject(hBmp);


I'm guessing that the DevIL source combines the last two lines into one. You don't *really* need to keep two HBITMAPs around, but I find it makes the code clearer, and since a HBITMAP is only 4 bytes, space isn't really a concern.

Share this post


Link to post
Share on other sites
From MSDN:

"The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type."

Selecting an HBITMAP returns the previously selected HBITMAP, no?


Matt

Share this post


Link to post
Share on other sites
Hi there !

I am having a few graphical problems. Im using bitmaps to store my GUI resources. For example checkbox.bmp, a single bitmap, stores two 16x16 images for checkbox checked and checkbox unchecked states.

When the user clicks (WM_LBUTTONDOWN)I draw the appropriate one. The actual dimensions of the .bmp is 16x32 (WxH).

Problem is, it's drawing both! I checked my coordinates in BitBlt() but as soon as the window resizes it draws the entire thing!

Atm, im using InvalidateRect() and specifying the coordinates of the checkbox itself.

The actual painting is done by calling a function from WM_PAINT.

Could anyone tell me what im doing wrong? The same this is happening to by menu.bmp where I keep menu headers and menu items. It overdraws on some of them.

I have checked my coordinates, and theyr'e perfect!

Please help! Thanks

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!