🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Non-kludgy bitmap-drawing

Published May 18, 2000
Advertisement
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 :)
Previous Entry Shared: The Object
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement