Win32 Double Buffering

Started by
9 comments, last by Servant of the Lord 17 years, 7 months ago
I'm almost finished with the 2D level editor I'm working on, whihc I've built in win32, but I've hit a snag. The screen flickers so much when I continueally update the screen, I can barely see what's going on. I looked up some double buffering tuts and checked some old threads posted here on gamedev, but I can't seem to get it working correctly. I essentionally am drawing like this: //Not all the drawing code, but the rest is reduntant

HBITMAP hbmOld;
HBITMAP Bitmap;

hdc = BeginPaint(hwnd, &ps);

HDC hdcMem = CreateCompatibleDC(hdc);

hbmOld = (HBITMAP)SelectObject(hdcMem, Bitmap);

BitBlt(hdc, X, Y, 32, 32, hdcMem, 0, 0, SRCCOPY);

SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);

EndPaint(hwnd, &ps);
How can I double buffer it? My attempts leave me with a blank screen. [smile] I think I understand the concept, but I don't get exactly how it's done. Is my buffer supposed to be a HBITMAP or a HDC? How do I 'flip' the buffer to the front? I would much apreciate a code snippet or tutorial so I can see it in action, or a detailed enough explantion for one as thick as me to understand. =) Many thanks,
Advertisement
You never set Bitmap to be anything. You need to add the line

Bitmap = CreateCompatibleBitmap(hdc, some width, some height)

Then you need to draw to this bitmap and then BitBlt() as you are doing now.
This is how I always did it.

// settingsconst int width = 400;const int height = 300;// varsHDC bufDC;HBITMAP bufBMP;// set upbufDC = CreateCompatibleDC(hDC);bufBMP = CreateCompatibleBitmap(hDC, width, height);SelectObject(bufDC, bufBMP);// draw to the bufferMoveToEx(bufDC, 100, 100, NULL);LineTo(bufDC, 200, 200);// present the bufferBitBlt(hDC, 0, 0, width, height, bufDC, 0, 0, SRCCOPY);


[Edited by - tendifo on August 26, 2006 8:27:15 PM]
I greatly apreciate the help. I still can't seem to understand it, although I think it's close. I again got a blank white screen, with nothing drawing, and sometimes I just can't get it compiled.

This is what I currently have, and I can't pass 'BufferBitmap' as a DC handle, so I guess I still don't understand.
    const int height = 471;    const int width = 648;        HBRUSH Brush;    HBITMAP hbmOld;    HBITMAP Bitmap;    HBITMAP BufferBitmap;                 hdc = BeginPaint(hwnd, &ps);        HDC bufferDC = CreateCompatibleDC(hdc);    BufferBitmap = CreateCompatibleBitmap(hdc, width, height);    SelectObject(bufferDC, BufferBitmap);        Bitmap = GetTile();    hbmOld = (HBITMAP)SelectObject(BufferBitmap, Bitmap);    BitBlt(bufferDC, X, Y, 32, 32, (HDC)BufferBitmap, 0, 0, SRCCOPY);        BitBlt(hdc, 0, 0, width, height, bufferDC, 0, 0, SRCCOPY);        SelectObject(bufferDC, hbmOld);    DeleteDC(bufferDC);    DeleteObject(BufferBitmap);         DeleteObject(Brush);    EndPaint(hwnd, &ps);


This is the entire paint code, but I cut out the parts I thought irrelevant, like the way I cycle through tiles to paint to the screen, and the code that gets the location of the screen reletive to the map. The entire paint function is below, if it helps any.
void PaintScreen(){    if(MapX < 0){MapX = 0;}    if(MapX > 85){MapX = 85;}    if(MapY < 0){MapY = 0;}    if(MapY > 88){MapY = 88;}    GetScreen(MapX,MapY);        const int height = 471;    const int width = 648;        HBRUSH Brush;    HBITMAP hbmOld;    HBITMAP Bitmap;    HBITMAP BufferBitmap;                 hdc = BeginPaint(hwnd, &ps);        HDC bufferDC = CreateCompatibleDC(hdc);    BufferBitmap = CreateCompatibleBitmap(hdc, width, height);    SelectObject(bufferDC, BufferBitmap);                 //Fill background    Brush = CreateSolidBrush(RGB(0, 105, 130));    SelectObject(bufferDC, Brush);    Rectangle(bufferDC, 126, 0, 624, 400);    current = 0; //Current tile being drawn to screen    int X = 128;    int Y = 2;    int ColCount = 0;    for(int i = 0; i < 180; i++)    {        Bitmap = GetTile();        hbmOld = (HBITMAP)SelectObject(BufferBitmap, Bitmap);        BitBlt(bufferDC, X, Y, 32, 32, (HDC)BufferBitmap, 0, 0, SRCCOPY);        ColCount++;        current++;        X += 33;        if(ColCount >= 15)        {            ColCount = 0;            X = 128;            Y += 33;        }    }            BitBlt(hdc, 0, 0, width, height, bufferDC, 0, 0, SRCCOPY);        SelectObject(bufferDC, hbmOld);    DeleteDC(bufferDC);    DeleteObject(BufferBitmap);         DeleteObject(Brush);    EndPaint(hwnd, &ps);}


Any idea what I'm doing wrong? I tried making the buffer a HDC instead, and that's when I get the white screen.
Here are some notes on double buffering that I took from the Windows 2000 Graphics API Black Book

Quote:
/* Windows 2000 Graphics API Black Book

Double-Buffer Multiple or Expensive Rendering Operations

To implement a double-buffered solution, you perform the following steps:

1. Use the CreateCompatibleDC function to create a memory device context
that is compatible with the target display device.

2. Create an appropriately sized device-dependent bitmap or DIB section bitmap
that will serve as the "memory" of the memory device context.
You can use the CreateCompatibleBitmap function to create a device-dependent bitmap
or the CreateDIBSection function to create a DIB section bitmap.

3. Use the SelectObject function to select the bitmap into the memory device context.

4. Direct any rendering to the memory device context. You can simply modify your existing
code by replacing all references to the original target device context with references
to the memory device context.

5. When the rendering is complete, use the BitBlt function to transfer the bits of the bitmap
to the original target device context.
*/


The section from the book that I took that from used to be available online but the link to it is bad now.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
your code, in particular "hbmOld = (HBITMAP)SelectObject(BufferBitmap, Bitmap);" doesn't make sense. SelectObject object takes as the 1st parameter an HDC, not a bitmap.

in your case, what you need is two compatible DCs. one to select the buffer, and one to select the tile. then you want to copy the contents of tile bitmap (which is selected into one of the contexts), into the contents of the buffer bitmap (which is selected into the other context).

something like this:

HDC bufferDC = CreateCompatibleDC(paintDC);
HDC tileDC = CreateCompatibleDC(paintDC);
SelectObject(bufferDC, BufferBitmap);
SelectObject(tileDC, TileBitmap);
BitBlt(bufferDC, x, y, w, h, tileDC, 0, 0, SCRCOPY); // copy contetns of tile bitmap into buffer bitmap
// etc. cleanup code, DeleteObject

as for the concept, think of it like this:
your buffer bitmap is kinda like a back buffer... it's what you render your tiles to. the paint device context is kinda like the front buffer... when you're done rendering to the back buffer, you send it to the screen (window context) using another BitBlt call (though techincally i'm not sure this can truly be called double-buffering because there is no swapping involved... i prefer to call it just buffering).
Looking at the code for void PaintScreen() I see a call to

hdc = BeginPaint(hwnd, &ps);

That is only supposed to be called from inside the WM_PAINT handler. There's not much point to drawing on a double buffer from there, just draw right on the hdc.

The point of the double buffer is to have it available to draw on everywhere else in the program. Create it once, either after CreateWindow or from inside WM_CREATE. You'll likely need to make it a global variable. Don't forget to clean it up afterwards. A helpful tool to ensure that you aren't leaking gdi objects is the free GDIObj tool.

Have your rendering code draw on the buffer. When it completes, blit the buffer to the actual hdc. If you need the drawing to be updated frequently, put the call to the rendering function inside the window message loop as the code to execute when there are no messages. If you do that, you won't need a WM_PAINT handler. If you don't need the drawing to be updated frequently, put the blit into the WM_PAINT handler.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Thanks LessBread. Let me see if I get this, now:

HDC hdc;HBITMAP Bitmap;PAINTSTRUCT ps;...HDC memHdc;HDC bufHdc;HBITMAP buffer;hdc = BeginPaint(hwnd, &ps);bufHdc = CreateCompatibleDC(hdc);hdcMem = CreateCompatibleDC(bufHdc);buffer = CreateCompatibleBitmap(hdc);//make buffer be my imagesbuffer = Bitmap;SelectObject(hdcMem, buffer);//Do my blitting to the backbufferBltBit(bufHdc, 0, 0, 32, 32, hdcMem, 0, 0, SRCCOPY);//And finally paint to the screenBltBit(hdc, 0, 0, width, hieght, bufHdc, 0, 0, SRCCOPY);/*Clean up*/
HDC hdc;HDC memHdc;HBITMAP bitmap;// instead of BeginPaint use GetDC or GetWindowDChdc = GetDC(hwnd); hdcMem = CreateCompatibleDC(hdc); // always create the bitmap for the memdc from the window dcbitmap = CreateCompatibleBitmap(hdc,width,height);SelectObject(hdcMem, bitmap);// only execute the code up to this point one time// that is, you only need to create the back buffer once// you can reuse it over and over again after that// draw on hdcMem// for example  ...Rectangle(hdcMem, 126, 0, 624, 400);// when finished drawing blit the hdcMem to the hdcBltBit(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);// note, height is not spelled i before e// Clean up - only need to do this one time as wellDeleteDC(hdcMem);DeleteObject(bitmap);ReleaseDC(hwnd, hdc);


Here is another important note

Quote:
When the entire client area of your window is to be covered by a drawing,
you should instruct the default window procedure not to fill the background with
the class background-brush object. You can do this by assigning NULL
(or the NULL_BRUSH stock object) to the WNDCLASS::hbrBackground data member
before the window’s class is registered:

An equivalent technique is to always return non-zero in response to the WM_ERASEBKGND message:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Thanks for the help thus far.

It works somewhat now, but apparently something is happening that is stopping all my child windows from drawing. (Which is all the control panels, and the dialog boxes. The menu bar is still there)

NM. Many thanks for all the help guys!

[Edited by - Servant of the Lord on August 27, 2006 2:50:29 PM]

This topic is closed to new replies.

Advertisement