# Possible to eliminate flickering in GDI?

## Recommended Posts

Hi, I've created several games using Windows GDI, and can't help notice that my sprite s flicker most of the time. Are there any ways in which I could eliminate flickering in GDI? Thanks in advance, - dadads -

##### Share on other sites
__filip    218
You can use a backbuffer. A backbuffer is an offscreen buffer, with the same dimensions as the window you are drawing to. Then, instead of drawing directly to the window, you draw everything in the backbuffer. When you have drawn everything and want to display it on screen, you simple copy the backbuffer to the window with BitBlt.

To create the backbuffer you simple do:
RECT rect;GetClientRect( hwnd,&rect );HDC windowdc = GetDC( hwnd );HDC backbufferdc = CreateCompatibleDC( windowdc );HBITMAP backbuffer = CreateCompatibleBitmap( windowdc, rect.right, rect.bottom );HBITMAP oldbitmap = SelectObject( backbufferdc, backbuffer );ReleaseDC( windowdc );

Now you can use backbufferdc instead of the window's device context.

NOTE: According to my interpretation of the MSDN, you need to do SelectObject(backbufferdc,oldbitmap); when you are finished with the backbuffer and wants to clean up. That's why we save oldbitmap above.