   [win32 c++] Erase from a Bitmap in Dbl Buffering
  HelloI am making my own simple 2d Graphic engine in c++ win32.The way I draw an object onto the screen is through double buffering:- I copy the main window HDC into a BufferHDC when the application loads   Every 100 Clock steps (computer cycles):    Step 1 - I erase an objects previous position from the BufferHDC - HOW?    Step 2 - I draw an objects new position onto the bufferHDC - FillRegion(BufferHDC, ...);    Step 3 - I then copy the bufferHDC to the main HDCMy problem: I dont know how to do Step 1? How do I delete/erase an area of a HDC/Bitmap? I know how to erase a HRGN from the main window using InvalidateRgn() but how do I erase a HRGN from my BufferHDC.I know I can just use InvalidateRgn() thus erase the region from the main window then draw onto the BufferHDC etc. But the delay between erasing, drawing then copying to the main HDC is long enough to create a flicker effect which completely defeats the purpose of using double buffering in the 1st place.I also know I could clear the BufferHDC (of ALL objects) every clock rotation, then just draw every object again(regardless of if they need to be redrawn or not) but this is inefficient.Any suggestions would be extremely helpful :)My current Draw function which doesn't erase a region from the HDC yetbool DrawEngine :: ExecuteDraw( HDC WindowHDC ){   // Post: Draw all 'Dirty' Objects onto window using Double Buffering (avoids   //  flicker affect). // PROBLEM: Because I erase an objects old position from the main window(instead of buffer HDC)   //& then redraw its new postion in this function there is a short delay between   //erasing then redrawing (causing flickering) which defaults the whole purpose of   //double buffering.   //   //Is there a way to erase the object's old position from the buffer HDC bitmap?   //Is there a better way to do this that I am not considering?  // Copy window to buffer canvas   // In reality this maybe only needs to be done once(copying Window HDC to buffer HDC) but when   // & where should I do this; In WM_CREATE, in WM_PAINT (it will be done every time the msg is called    // wont it?) or other?    RECT ClientRect;   GetClientRect( hwnd, &ClientRect );   BufferCanvas = CreateCompatibleDC( WindowHDC );   HBITMAP hBlt   = CreateCompatibleBitmap( WindowHDC, ClientRect.right,    ClientRect.bottom );   HGDIOBJ hMCBmp = SelectObject( BufferCanvas, hBlt ); BitBlt(BufferCanvas, ClientRect.left, ClientRect.top, ClientRect.right, ClientRect.bottom, WindowHDC, 0, 0, SRCCOPY );  // Draw objects onto Buffer Canvas   while ( !qDrawList.empty() )   {   qDrawList.front()-> DrawSelf( BufferCanvas );   qDrawList.pop();   }  // Copy BufferCanvas HDC to WindowCanvas HDC   BitBlt( WindowHDC, ClientRect.left, ClientRect.top, ClientRect.right,  ClientRect.bottom, BufferCanvas, 0, 0, SRCCOPY );SelectObject( BufferCanvas, hMCBmp );   DeleteObject( hBlt );   DeleteDC( BufferCanvas );}
  As explained in your previous post on this subject, you can't erase or delete a rectangle or region; you can only draw over what's already there.You can use the FillRgn function to draw over a region with a specific brush.   ;  Thanks for your replyIts pretty rare that I find there isn't a function to do something in Win32 lol.Oh well. But how do other 2d drawing engines work? Do they just redraw everything at every clock step after clearing the BufferHDC?With you FillRegion suggestion are you saying I should paint it/fill it with the background colour? Or is there a brush that makes something transparent? The former will work for me now, but I want to make my background tiled, so I am not sure if this will still work? Maybe it will.Does anyone have any suggestions on what I could do to solve my problem even if it is something where I have to completely change my whole algoritm etc?   ;Quote:Its pretty rare that I find there isn't a function to do something in Win32What function are you looking for? BTW, InvalidateRgn doesn't "erase" anything, if that's what you're seeking. It only adds a region to the invalid portion of the window - meaning you need to redraw that portion of the window the next time you process WM_PAINT.Quote:With you FillRegion suggestion are you saying I should paint it/fill it with the background colour?If you have a tiled background (i.e., your background is NOT a solid color), then you probably need to draw over everything in the desired region - the background, the foreground, animations, etc.Your questions imply you're thinking of the HDC as a piece of paper with something drawn on it that you can "erase," thus revealing the original paper color or texture (tile). HDCs are not like that. It's as if you've drawn on the paper in indelible ink. You can't erase it; you can only draw over it. If you want the background to be as it was originally, you have to redraw that background (draw the tiles again, etc.)EDIT: If redrawing the entire HDC is time-efficient for you (you might want to try that to see), you can certainly do that. Otherwise, draw only the background and foreground in that region by testing or calculating what should be drawn in that region.   