Sign in to follow this  
_Sigma

GDI doublebuffer

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Other than the call in the main loop to InvalidateRect(), all the painting is being done in on paint.

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this