Handmade Hero » Forums » Code » Bitmap Functions
Bits Please
8 posts
#16514 Bitmap Functions
4 months, 1 week ago Edited by Bits Please on Oct. 9, 2018, 9:59 p.m. Reason: Initial post

What's shakin' everybody?
I'm having a really hard time figuring out what's the difference between some of the bitmap functions and because of that I don't know which one to use to create a back buffer for my game.

First of all, I would like to know what's the difference between a regular bitmap, device-dependent bitmap (DDB), and device-independent bitmap (DIB).

Secondly, at the very beginning of the handmade hero series, Casey mentions that he had never used StretchDIBits and always preferred BitBlt because of performance. Is there really a performance difference between these two functions? And if not, then what's the difference between them at all?

Lastly, it seems that BitBlt doesn't even take a pointer to the rgb array, so how would I store the data and draw it onto the screen if I were to use BitBlt?

Thank you for everything!
Mārtiņš Možeiko
1877 posts / 1 project
#16517 Bitmap Functions
4 months, 1 week ago

DDB store pixel value in some "optimized" device-specific format. DIB allows you to specify what format to use - RGBA32, BGRA32, RGB565, etc.. Because device can operate with pixels values with different performance theoretically DDB gives better performance.

That said, you should not really care about this. This was relevant only for Windows 3.x, maybe some of Win9x. After that it is pretty much irrelevant. You should really do your own rendering, pixel-filling, etc with your bitmap and just upload big image to GDI. It gives you more flexibility, better options for optimization (SSE, AVX instructions?), and easier way to do cross-platform code. And I would suggest to look at OpenGL or D3D to upload final image buffer to window (Casey chose OpenGL). It will give you significantly better performance.

BitBlt only copies pixels. StretchDIBits allows to resize the rectangle you are copying. This means it has more complex code, thus potentially slower.

If you want to use DIB-like structure just for setting whole DC, you can use SetDIBits. But if I remember correctly it won't allow to specify offset where to put it - it will always be (0,0).

Alternative is to create DIB bitmap (CreateDIBSection) which can give you pointer to its memory. Then create DC context for this bitmap (CreateCompatibleDC + SelectObject). Then you'll be able to use BitBlt to copy pixels from your bitmap to destination DC.
Bits Please
8 posts
#16518 Bitmap Functions
4 months, 1 week ago

Holy moly!
With that kind of knowledge you should really consider doing a series of your own, I'll be the first one to watch!
Thanks a lot man!!!