January 19th, 2011, 04:16 AM
[Need Help] SetDIBits with manually set bitmap
Hey guys, I've been working with my software rasterizer but stuck with painting hand made bitmap onto the screen. The idea is:
1. Create compatible DC and bitmap with current DC
2. Set up BITMAPINFO struct
3. Set the buffer for pixel
4. SetDIBits with the BITMAPINFO and buffer into the compatible DC
5. BitBlt compatible DC to current DC
Am I wrong with all these steps? Here is part of the code: (32 bits)
HDC hdcReal, hdcMem;
buffer = (BYTE *)malloc(SCREEN_WIDTH * SCREEN_HEIGHT * COLOR_BYTES)
hdcReal = GetDC(hwnd);
hdcMem = GetCompatibleDC(hdcReal);
hBitmap = GetCompatibleBitmap(hdcReal, SCREEN_WIDTH, SCREEN_HEIGHT);
memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
pHeader = &(bitmapInfo.bmiHeader);
pHeader->biSize = sizeof(BITMAPINFOHEADER);
pHeader->biWidth = SCREEN_WIDTH;
pHeader->biHeight = SCREEN_HEIGHT;
pHeader->biPlanes = 1;
pHeader->biBitCount = 32;
pHeader->biCompression = BI_RGB;
/* copy it from msdn, deal with alignment */
pHeader->biSizeImage = ((pHeader->biWidth * 32 + 31) & ~31) / 8 * pHeader->biHeight;
menset(buffer, 255, SCREEN_WIDTH * SCREEN_HEIGHT * COLOR_BYTES);
SetDIBits(hdcMem, hBitmap, 0, SCREEN_HEIGHT, buffer, &bitmapInfo, DIB_RGB_COLORS);
BitBlt(hdcReal, 0, 0, SCREEN_WIDHT, SCREEN_HEIGHT, hdcMem, 0, 0, SRCCOPY);
I suppose it would display a white screen, cause the buffer is fill with 255, but what I got is just a blank black screen, what's wrong with my code? Any reply would be appreciated!
January 22nd, 2011, 06:56 AM
Re: [Need Help] SetDIBits with manually set bitmap
Why do you show uncompilable code?
There are several typos in that code before it even compiles...
Anyway, you need to select hBitmap into hdcMem before you can use it. Something like:
Don't forget to store the result of SelectObject and restore it when you are finished.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!