Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Code Snippet - Back Buffer
Author:Arkon
Category:Win32API
File Size:~ 1.30 KB
Uploaded at:27-Nov-02 02:24:16 pm
Description:
  If you sick of the laggy Windows' drawing... then this is the time to use a back buffer for flicker free animation.
  
// 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);
}
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[105418]
2D Rotated Rectangles Collision Detection[78723]
Keyboard Hook[68414]
UDP[52506]
HTTP Proxy[35823]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10562]
PS2 Mouse Driver[6063]
Wave Format Player[4945]
Reading FAT12[4828]
CodeGuru[4521]


All rights reserved to RageStorm © 2009