# Double buffering - Win32 C API



## burnthepc (Aug 12, 2007)

I've spent this afternoon adding double buffering to a program I'm writing. Finally have it working... only thing is it seems to make no difference!

Here's the key bits of the code:

HDC hdc, HDC buffer and HBITMAP bitmap are globals, and a few others.

```
// Main message pump with rendering

hdc = GetDC(hwnd);
buffer = CreateCompatibleDC(hdc); 
bitmap = CreateCompatibleBitmap(hdc,CLIENT_X_SZ,CLIENT_Y_SZ);


while(msg.message != WM_QUIT)
{
	
	if(PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	else  {
		SelectObject(buffer, bitmap);
		world.render(buffer);
		BitBlt(hdc, 0, 0, CLIENT_X_SZ,CLIENT_Y_SZ, buffer, 0, 0, SRCCOPY);
	}

// From WinProc

case WM_PAINT:
			hdc = BeginPaint(hwnd,&ps);
                                                EndPaint(hwnd, &ps);
			return 0;

case WM_SIZE:
			CLIENT_X_SZ = LOWORD(lParam);
			CLIENT_Y_SZ =HIWORD(lParam);
			hdc = GetDC(hwnd);
			if(bitmap)
				DeleteObject(bitmap);
			buffer = CreateCompatibleDC(hdc); 
	bitmap = CreateCompatibleBitmap(hdc,CLIENT_X_SZ,CLIENT_Y_SZ);
			SelectObject(buffer, bitmap);
			return 0;
```
It renders as well as it did before, and I'm only drawing on the buffer and then blitting it to the hdc, no where else. It still has some nasty flicker.

Could it have something to do with the WM_TIMER messages?

```
case WM_TIMER:
			// TO DO move this to use a better timer call
			world.update(16);
```


----------



## Lilian33 (Dec 13, 2008)

For Win32 api, you can ask on Professional Win32 api newsgroup :
news://comp.os.ms-windows.programmer.win32
where this has often been explained..


----------

