Jump to content
  • Advertisement
Sign in to follow this  
Nanven

Win32 GDI/Double Buffering Question

This topic is 4820 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

Well I was wondering what everyone thought. Basically I set up a back buffer at the beginning before I go into my main loop and my WM_PAINT structure just copies over the back buffer when it needs to be redrawn:
case WM_PAINT:
		{
			hDC = BeginPaint(hwnd, &ps);  // validate

			BitBlt(hDC, 0, 0, WINWIDTH, WINHEIGHT, hdcBackBuffer, 0, 0, SRCCOPY);

			EndPaint(hwnd, &ps);  // release the DC

			return 0;
		}



I'm doing this so I am not constantly deleting/recreating my back buffer. However, I then thought i'm going to need to clear the back buffer before drawing the next frame because the old frame will still be there. So I can accomplish this one of two ways which is deleting/initializing it again before WM_PAINT gets called(this seems very bad) or just draw a black rectangle from 0, 0 to the winwidth/winheight. Neither of those seem like an ideal solution and I was wondering if there was a better way to clear the back buffer before rendering the next frame into it.

Share this post


Link to post
Share on other sites
Advertisement
A good way to clear in GDI is to use BitBlt in a special way:

BitBlt(backBufferDC,0,0,width,height,NULL,0,0,BLACKNESS);

The BLACKNESS raster operation sets all of the pixels to 0 (black), this is as fast as it gets in GDI.

You can also use WHITENESS if you want white instead,

If you need to clear to a different color, look into FillRect, which allows you to pass a color brush instead.

You are definetly right about not wanting to remake the backbuffer, that would be very slow =)

Share this post


Link to post
Share on other sites
The method mentioned by EDI is most likely the fastest way to clear it (and it is what I've used in the past), but realize that it is possible for WM_PAINT to be called at any time, so that could cause a black frame to be drawn.

I suggest you move all rendering code out of WM_PAINT and instead make it part of your message pump or a seperate thread. This has the drawback that your frame won't be redrawn if something covers your window, but usually you'll be processing frames quickly enough that the next frame will redraw the window before anybody cares about the delay.

If you really want to make sure the window is always drawn (and just waiting until the next frame isn't good enough), you'll need two back buffers. You draw to backbuffer #1 and at the end of each frame copy it to backbuffer #2 then draw backbuffer #2 to the window. In your WM_PAINT, draw backbuffer #2 to the window. This way, the backbuffer that WM_PAINT is using to draw will always be the last image displayed and will never be an in-between image. The reason you need this is because basically backbuffer #2 is becoming your 'active' image ('the screen') and backbuffer #1 is acting as a 'real' back buffer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
why not just do what every other graphics package does and use a front buffer plus a back buffer? the front buffer is always used for display and the back buffer is always used for updating.

then you don't have to copy one back buffer to another. you just simply blank out the back buffer and do the drawing there. any paint requests received during the back buffer update cycle will be satisfied via the front buffer. when done, "flip" the buffers by swapping the hDCs of the front buffer and the back buffer then call your drawing routine directly, post a paint message, etc.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
exactly as he said^^

do you backbuffer drawing to the backbufferdc in your animation loop, or winmain, whereever, and when you handle wm_paint bitblt the front buffer to the window dc
because you dont update front buffer until you finish drawing the back buffer, you dont see flickers or anything else because front buffer is always complete

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!