Sign in to follow this  

GDI doublebuffer

This topic is 3877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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

This topic is 3877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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