GDI doublebuffer

Started by
7 comments, last by _Sigma 16 years, 11 months ago
I am using GDI to output my FPS (no DX atm). I am trying to double buffer, but I end up with black rectangles flickering up and down my window.

long OnPaint(TWindow &wnd,HWND hwnd, long wparam, long lparam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	hdc = BeginPaint(kernel->GetHWND(), &ps); 

	HDC memDC = CreateCompatibleDC(hdc);
	HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, kernel->GetWidth(), kernel->GetHeight());
	HBITMAP hOldBmp = (HBITMAP)SelectObject(memDC, hMemBmp);

	// Now do all your drawing in memDC instead of in hDC!
	std::string str = boost::lexical_cast<std::string>(kernel->fps.GetCurrent());
	LPCSTR ptr2 = str.c_str();

	TextOut(memDC, 0, 0,ptr2, str.length());
	EndPaint(kernel->GetHWND(), &ps);

	// As a last step copy the memdc to the hdc
	BitBlt(hdc, 0, 0, kernel->GetWidth(), kernel->GetHeight(), memDC, 0, 0, SRCCOPY);

	// Always select the old bitmap back into the device context
	SelectObject(memDC, hOldBmp);
	DeleteObject(hMemBmp);
	DeleteDC(memDC);


	
	return 0;
}


In my main game loop, I am calling InvalidateRect(m_wnd->GetHWND(),NULL,TRUE); to force a redraw. Anyideas?
Advertisement
You want EndPaint() to be called after you're done using hdc (ie. sometime after you call BitBlt()).
Yeah, I tried that. No dice.
Since you're painting over the entire background anyway, make the final parameter to InvalidateRect() FALSE. This will tell Windows to not bother erasing the background.
Everything but the background under the text stays black...
Where are you painting the rest of the frame?
Other than the call in the main loop to InvalidateRect(), all the painting is being done in on paint.
Right now, you're not painting anything other than the output to TextOut(). Since the pixels of a memory DC are unset by default this is why everything but the background under the text stays black. When you paint other things you wont get a black output [smile].
Ah. well that makes things simple then! Many thanks!

This topic is closed to new replies.

Advertisement