Handmade Hero»Forums»Code
94 posts
Graphics questions (GDI/DX/OpenGL)
Edited by C_Worm on Reason: Initial post

Im wondering how to generaly think about the graphics platform code.

1. Is stretchDIBits() or BitBlt() responsible for all the rendering that happends in your game?

2. are you supposed to write all the graphics into the backbuffer allocated by virtualAlloc() that is setup by the BITMAPINFO structure?

or do ever need to setup multiple BITMAPINFO structures for every object in your game?

3. Will all assets in your game just be loaded and written in to the same backbuffer and rendered by BitBlt() or strechDIBits()?

4. Does the rendering-setup and the rendering-functions differ much between GDI and DirectX/OpenGL.
Or should you be thinking about it in the same way?
like so:

- setting up some BITMAP(header/description)
- allocate some memory for it
- Draw all the graphics into it

Mārtiņš Možeiko
2238 posts / 1 project
Graphics questions (GDI/DX/OpenGL)
Casey used GDI to display pixels for HandmadeHero only in beginning. After a while he switched to use OpenGL to display pixels to screen - it's waaaaay more performant than raw GDI. But a bit later he switched fully to OpenGL, no GDI calls at all for rendering sprites images.

So if you care about performance don't worry about GDI much. It's ok to understand what it does on how it works. But for real games it is better not to use it.

Back to your questions:

1) Blitting (BitBlt/StretchDIBits) in HandmadeHero is only done to transfer final image (full 1920x1080 pixels) to window. All other rendering happens to this 1920x1080 memory buffer. This is done by reading/writing memory yourself, not using any WindowsAPI GDI fucntions

2) It depends what you want to do. If you want to draw using GDI primitives you'll want to set up multiple bitmaps. But in simplest case you want only one bitmap which you blit to window.

3) Assets will be rendered by manually written software-rendering function - by reading and writing bytes to memory. Not using GDI functions. And eventually all that will be replaced with OpenGL rendering - textures & trinagles.

4) In very high-level - they don't differ. You first setup resources / allocate memory, then you draw it. But on more technical level - its very different. GDI is more about manipulating raw memory - reading from one bitmap, and writing to another. With OpenGL (or D3D) it is more about allocating textures and drawing triangles by applying shaders (custom program code) to calculate pixel values. Which is more flexible because it allows very trivially to implement custom calculations. And it is waaaaaay faster, because GPUs render many pixels all at the same time. GDI executes on CPU so it is more about - read one pixel, write one pixel (sometimes few more pixels at same time with SIMD).
94 posts
Graphics questions (GDI/DX/OpenGL)
Okay thank you for that answer! :)