Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDI+ double buffering in C++

Tags:

c++

winapi

gdi+

I haven't written anything with GDI for a while now (and never with GDI+), and I'm just working on a fun project, but for the life of me, I can't figure out how to double buffer GDI+

void DrawStuff(HWND hWnd) {
    HDC          hdc;
    HDC          hdcBuffer;
    PAINTSTRUCT  ps;
    hdc = BeginPaint(hWnd, &ps);
    hdcBuffer = CreateCompatibleDC(hdc);
    Graphics graphics(hdc);
    graphics.Clear(Color::Black);

    // drawing stuff, i.e. bunnies:

    Image bunny(L"bunny.gif");
    graphics.DrawImage(&bunny, 0, 0, bunny.GetWidth(), bunny.GetHeight());  

    BitBlt(hdc, 0,0, WIDTH , HEIGHT, hdcBuffer, 0,0, SRCCOPY);
    EndPaint(hWnd, &ps);
}

The above works (everything renders perfectly), but it flickers. If I change Graphics graphics(hdc); to Graphics graphics(hdcBuffer);, I see nothing (although I should be bitblt'ing the buffer->hWnd hdc at the bottom).

My message pipeline is set up properly (WM_PAINT calls DrawStuff), and I'm forcing a WM_PAINT message every program loop by calling RedrawWindow(window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);

I'm probably going about the wrong way to do this, any ideas? The MSDN documentation is cryptic at best.

like image 367
David Titarenco Avatar asked Mar 18 '10 22:03

David Titarenco


People also ask

What is double buffering in C?

The double buffer is a memory area which is not directly mapped to the screen. When we want to make some change to the scene, we first modify this area, and then copy the whole area to the window. This way, a screen refresh may not take place while we are modifying the area.

What is single and double buffering?

There are two buffers in the system. One buffer is used by the driver or controller to store data while waiting for it to be taken by higher level of the hierarchy. Other buffer is used to store data from the lower level module. Double buffering is also known as buffer swapping.

What is double buffer mode?

The Double Buffer mode allows to set two different Memory addresses from/to which the DMA controller will access alternatively (after completing transfer to/from target memory 0, it will start transfer to/from target memory 1). This allows to reduce software overhead for double buffering and reduce the CPU access time.

What is double buffer and triple buffer?

In computer graphics, triple buffering is similar to double buffering but can provide improved performance. In double buffering, the program must wait until the finished drawing is copied or swapped before starting the next drawing.


1 Answers

CreateCompatibleDC(hdc) creates a DC with a 1x1 pixel monochrome bitmap as its drawing surface. You need to also CreateCompatibleBitmap and select that bitmap into the hdcBuffer if you want a drawing surface larger than that.

Edit:

the flickering is being caused by WM_ERASEBKGND, when you do this

hdc = BeginPaint(hWnd, &ps);

Inside the call to BeginPaint, Windows sends your WndProc a WM_ERASEBKGND message if it thinks the background needs to be redrawn, if you don't handle that message, then DefWindowProc handles it by filling the paint rectangle with your class brush, so to avoid the flickering, you should handle it and return TRUE.

case WM_ERASEBKGND:
   return TRUE; // tell Windows that we handled it. (but don't actually draw anything)

Windows thinks your background should be erased because you tell it that it should, that's what RDW_ERASE means, so you should probably leave that out of your RedrawWindow call

like image 110
John Knoeller Avatar answered Oct 17 '22 20:10

John Knoeller