I don't want to clutter up the other thread, so I made a new one just for this topic. Can I please get an explanation of making a 'global' double buffer? I'm having trouble visualising the implementation.
From what I've been able to gather and as it's been pointed out, my code is creating / destroying the bitmap used as the buffer each time the DrawMain() function gets called. I've come up with the following; modified from my previous attempt. Going by my understanding, I put hdcBuffer = CreateCompatibleDC( hdc ); and destroy the buffer on exit.
If I still haven't gotten it right, I want to know! ?
HDC hdc;
HDC hdcMem;
HDC hdcBuffer;
HBITMAP hbmBuffer;
HBITMAP hbmOldBuffer;
PAINTSTRUCT ps;
RECT rcClient;
Globals at the top of the program.
void DrawMain( HDC hdc, RECT* rcClient )
{
hbmBuffer = CreateCompatibleBitmap( hdc, rcClient->right, rcClient->bottom );
hbmOldBuffer = (HBITMAP)SelectObject( hdcBuffer, hbmBuffer );
FillRect(hdcBuffer, rcClient, (HBRUSH)GetStockObject(WHITE_BRUSH));
// title
SelectObject( hdcMem, g_hbmTitle ); //not sure if HBITMAP hbmOld = (HBITMAP) is necessary as the program works without it
TransparentBlt( hdcBuffer, 50, 24, 297, 39, hdcMem, 0, 0, 297, 39, RGB( 255, 0, 255 ) );
// background
SelectObject( hdcMem, g_hbmBackground );
TransparentBlt( hdcBuffer, 112, 90, 186, 91, hdcMem, 0, 0, 186, 91, RGB( 255, 0, 255 ) );
// sprite
HBITMAP hbmOld3 = (HBITMAP)SelectObject( hdcMem, g_hbmSprite );
TransparentBlt( hdcBuffer, Sprite_Slow.x+(Sprite_Slow.amplitude*sin(Sprite_Slow.angle)+0.5), Sprite_Slow.y,15, 15 , hdcMem, 15*(frames%28), 0, 15, 15, RGB( 255, 0, 255 )) ;
// blit double buffer to window
BitBlt( hdc, 0, 0, rcClient->right, rcClient->bottom, hdcBuffer, 0, 0, SRCCOPY );
SelectObject( hdcMem, hbmOld3 );
DeleteDC( hdcMem );
}
The updated DrawMain() function.
case WM_PAINT:
{
hdc = BeginPaint( hwnd, &ps );
GetClientRect( hwnd, &rcClient );
hdcBuffer = CreateCompatibleDC( hdc );
hdcMem = CreateCompatibleDC( hdc );
dw_timestart = timeGetTime();
DrawMain( hdc, &rcClient );
AnimateFrames ();
UpdateSpritePosition();
dw_timestop = timeGetTime();
EndPaint( hwnd, &ps );
f_cycle_t = dw_timestop - dw_timestart;
}
return 0;
The updated WM_PAINT message.
case WM_DESTROY:
SelectObject( hdcBuffer, hbmOldBuffer );
DeleteDC( hdcBuffer );
DeleteObject( hbmBuffer );
DeleteObject( g_hbmSprite );
DeleteObject( g_hbmTitle );
DeleteObject( g_hbmBackground );
PostQuitMessage( 0 );
return 0;
How I'm destroying the bitmap. The program freezes after a few moments.