I went through several tutorials covering double buffering and flickerless GDI animations. I just cant get them to work. Every time i render, there is alot of flicker because I have to erase the entire screen and redraw everything on to it. switching to OpenGL or directX is not an option (due to time constriants etc).
The way i traied it was to catch WM_ERASEBKGND and do my own erasing when i render. this still causes flicker regardless. Anyone have any explanation why this is happening to me??
Here is some of the code I use:
Code:
POINT MousePos;
PAINTSTRUCT Paint;
HDC CurrentDeviceContext;
HDC DeviceContextMem;
RECT EraseRect;
HBRUSH BackGrnd;
// Invalidate the Window
InvalidateRect(WindowHwnd, NULL, true);
// Start the Drawing Process
CurrentDeviceContext = BeginPaint(WindowHwnd, &Paint);
DeviceContextMem = CreateCompatibleDC(CurrentDeviceContext);
// Erase Background
memset(&EraseRect, 0, sizeof(RECT));
EraseRect.bottom = WindowHeight;
EraseRect.right = WindowWidth;
BackGrnd = CreateSolidBrush(RGB(255, 0, 255));
FillRect(CurrentDeviceContext, &EraseRect, BackGrnd);
GetCursorPos(&MousePos);
// Specify which primitive type is to be drawn
for (int i = 0; i < NumPanel; i++)
{
if (PanelList[i])
{
PanelList[i]->Render(WindowHwnd, CurrentDeviceContext, DeviceContextMem);
}
}
DeleteDC(DeviceContextMem);
// Finish the drawing process
EndPaint(WindowHwnd, &Paint);
Here is my "Panel" rendering code:
Code:
void Panel::Render(HWND Hwnd, HDC DevContext, HDC Context)
{
HBITMAP OldBitmap;
BITMAP Bitmap;
BITMAPINFOHEADER BitmapInfo;
HBITMAP ImgMap;
if (Loaded && Renderable && ImgHeader.BitsPerPixel == 32)
{
// Clear our Bitmap
memset(&Bitmap, 0, sizeof(BITMAP));
// Create a struct ot hold the info about the image
BitmapInfo.biSize = sizeof(BitmapInfo);
BitmapInfo.biHeight = ImgHeader.Height;
BitmapInfo.biWidth = ImgHeader.Width;
BitmapInfo.biPlanes = 1;
BitmapInfo.biBitCount = ImgHeader.BitsPerPixel;
BitmapInfo.biCompression = BI_RGB;
BitmapInfo.biSizeImage = 0;
BitmapInfo.biXPelsPerMeter = BitmapInfo.biYPelsPerMeter = 10000;
BitmapInfo.biClrUsed = BitmapInfo.biClrImportant = 0;
// Create a DIB from our TGA data
ImgMap = CreateDIBitmap(DevContext, &BitmapInfo, CBM_INIT,
(KeyPressed ? Img.ClickData : Img.ImageData), (BITMAPINFO *) &BitmapInfo, DIB_RGB_COLORS);
// Set the position and Draw data
OldBitmap = (HBITMAP) SelectObject(Context, ImgMap);
BitBlt(DevContext, RasterX, RasterY, ImgHeader.Width, ImgHeader.Height, Context, 0, 0, MERGECOPY);
// Swap buffers
SelectObject(Context, OldBitmap);
// Delete our GDI Data
DeleteObject(ImgMap);
}
}