Jump to content
  • Advertisement
  • entries
    1212
  • comments
    1738
  • views
    1144368

Non-kludgy bitmap-drawing

Sign in to follow this  
johnhattan

56 views

Phew. I finally came up with an elegant way to draw to bitmaps.

Drawing to bitmaps has always been a bit of a pain, especially if you've got a custom bitmap format. The Windows GDI functions are pretty limited as to what they'll draw to (i.e. HDC's), but they're so nice and robust that you don't wanna have to roll your own.

My original idea was to have a bitmap constructor that takes an HDC, then copies the bits from the HDC into the bitmap. It worked, but it was awfully limited. Problem is, it's difficult to do bitmap-to-bitmap stuff, and mix it with GDI stuff. Drawing to a bitmap, then drawing else on top of what you drew later, for example, is pretty-much impossible.

My other idea was to put HDC support into the bitmap itself. This is what I did with my original bitmap objects for my old games. Something like this. . .

Bitmap B(100,100);

HDC hDC = B.CreateOutputDevice(); // copies the bits to the HDC

DrawAlignedText(hDC, Rectangle(0,0,100,100), String("Howdy"));

B.ReleaseOutputDevice(); // copies the bits from the HDC back to the bitmap


This works quite nicely, but it adds a bunch of member-data baggage to the bitmaps that's rarely used. My goal with the latest games is to make the often-used stuff as lightweight as possible. I finally came up with the idea to make a separate class that provides an HDC for the bitmap temporarily. It looks like the one above, but it's a separate class, so it's only got baggage as long as it's in use. . .

Bitmap B(100,100);

BitmapDC *pBmpDC = new BitmapDC(B); // copies the bits to the HDC

DrawAlignedText(pBmpDC->DC(), Rectangle(0,0,100,100), String("Howdy"));

delete pBmpDC; // copies the bits from the HDC back to the bitmap


I'm happy to report that it works wonderfully, which is why I'm reporting on it here :)
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 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!