Jump to content
  • Advertisement
Sign in to follow this  
cptrnet

HDC question

This topic is 4844 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

Is it ok to have an HDC as a member in a class. All the code I've seen it Declares it locally and the releases it locally. I was just wondering since I don't want to have it localally declared in every function that draws, if I don't have to. Another question, should I make it global so all I have to to is Rectangle(g_hdc, 0, 0, 50, 50); or should i make it a function in my class to get it Rectangle(GethDC(), 0, 0, 50, 50);

Share this post


Link to post
Share on other sites
Advertisement
Nothing wrong with that. I'm doing it in my game engine.

Edit:
Though if you're going to be using it a lot in a particular function I would store the handle so you're not spending time jumping in and out of functions.

i.e.

MyFunction()
{
HDC deviceContext = classA.GetHDC();
Rectangle(deviceContext, 0, 0, 50, 50);
}

because if you do that for hundreds of rectangles you're spending quite a bit of time just calling the same function over and over to get the exact same value (the handle is a DWORD ).

Share this post


Link to post
Share on other sites
There's nothing that stops you from holding an HDC as a member, but there are a couple of problems. First off is that HDCs are one of the more limited resources in terms of the Win32 architecture, especially the Win9x family of operating systems. Not freeing the HDCs may lead to resource exhaustion, so its good manners to release them as soon as your done. The second, more serious, problem is that HDCs can become invalidated, and if you use an invalidated HDC the results are undefined. So by holding on the HDC you risk having the device context being invalidated underneath your feet. Fortunately, the number of ways a device context can be invalidated are low, so this may not cause you any problems.

So in summary, most of the times it won't cause problems, but when it does, the problems are pretty big.

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!