// The "trick" of this class is that it works just upon construction and destruction. // You don't have to worry if the user resizes the window size, // because the BackBuffer is being allocated every time you declare the class. // When the class is not needed anymore in a specific block it will copy itself to the window's DC. class _BackBuffer { public: _BackBuffer(HWND hWnd, COLORREF bkg); ~_BackBuffer(); HDC BackBufferDC;
protected: HWND wnd; RECT wr; HDC hDC; HBITMAP BackBuffer, oBMP; HBRUSH hBKGBrush; };
_BackBuffer::_BackBuffer(HWND hWnd, COLORREF bkg) { wnd = hWnd;
hDC = GetDC(wnd); GetClientRect(wnd, &wr); hBKGBrush = CreateSolidBrush(bkg); BackBuffer = CreateCompatibleBitmap(hDC, wr.right, wr.bottom); BackBufferDC = CreateCompatibleDC(NULL);
oBMP = (HBITMAP)SelectObject(BackBufferDC, BackBuffer); FillRect(BackBufferDC, &wr, hBKGBrush); }
_BackBuffer::~_BackBuffer() { BitBlt(hDC, 0, 0, wr.right, wr.bottom, BackBufferDC, 0, 0, SRCCOPY); SelectObject(BackBufferDC, oBMP);
DeleteObject(hBKGBrush); DeleteObject(BackBuffer); DeleteObject(BackBufferDC);
ReleaseDC(wnd, hDC); }
while(gameloop) { _BackBuffer bbdc(hWnd, RGB(0, 255, 0)); // Handle to game window, Background color TextOut(bbdc.BackBufferDC, 0, 0, "So easy!", 8); } |