Click to See Complete Forum and Search --> : double Buffering


plasmon
November 19th, 2003, 03:37 AM
How can i animate a rotating line using double buffering

Marc G
November 19th, 2003, 02:00 PM
Which drawing API are you using: OpenGL? DirectX? GDI? GDI+? or something else?

Marc G
November 20th, 2003, 07:03 AM
Suppose you are using GDI to do all the drawing, then you should create a memory device context, do all your drawing in this memory device context and the last step is to blit this memorydc to the screen.

(in the code below, hDC is the handle to your DC on the screen):

HDC memDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, iWidth, iHeight);
HBITMAP hOldBmp = SelectObject(memDC, hMemBmp);

// Now do all your drawing in memDC instead of in hDC!


// As a last step copy the memdc to the hdc
BitBlt(hDC, 0, 0, iWidth, iHeight, memDC, 0, 0, SRCCOPY);

// Always select the old bitmap back into the device context
SelectObject(memDC, hOldBmp);
DeleteObject(hMemBmp);
DeleteDC(memDC);


I hope this makes any sense to you...

Shorin
January 9th, 2007, 04:22 PM
YOU ARE 1337!

Thank you.

The detail I was missing all along is "Selecting the old bitmap back into the memDC"

If you don't do that then you will not see anything at all on your DC.

HinDRAncE
January 11th, 2007, 04:01 AM
you could also swap buffers (but i dont know how to do that in c++ yet ) its supposed to be faster than blitting to the screen.

Marc G
January 11th, 2007, 12:49 PM
As far as I know you should use blitting in Win32 API. If you work with DirectX or OpenGL, then you can use buffer swapping, but not in Win32 API.

probin
February 3rd, 2007, 07:02 PM
When I tried this, I got an error message:
error C2440:'initializing':cannot convert from 'HGDIOBJ' to 'HBITMAP'

What went wrong?

Marc G
February 5th, 2007, 11:50 AM
Since you give no information whatsoever, I'm assuming it is on the following line:
HBITMAP hOldBmp = SelectObject(memDC, hMemBmp);
Change it to:
HBITMAP hOldBmp = (HBITMAP)SelectObject(memDC, hMemBmp);

probin
February 5th, 2007, 09:50 PM
Marc,

You did it! With no info other than your correct assumption, you fixed my problem. Now I can write nicer looking Media player visualizations (Visual C++).

Thank you very much,

Paul

CannibalSmith
September 17th, 2007, 01:34 AM
The lifetime of the hMemBmp may be the whole application, right? But what about the memDC? May I create it at the beginning and destroy when the application ends; or do I have to create and destroy it every time I draw to the hMemBmp?