Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

GDI doublebuffer

This topic is 4092 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!