Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Double Buffering

Status
Not open for further replies.

Jaredino

Technical User
Jan 31, 2006
17
GB
Hi I have the following code to draw 3 bitmaps on the screen (of a formview):

Code:
	CBitmap bitmap[4];	
	CDC dcMemory[4];
    BITMAP bmap[4];		
		
    int bmpWidth = 0, bmpHeight = 0;		

    //Load Bitmap1
	bitmap[1].LoadBitmap(IDB_GUITAR1);
	dcMemory[1].CreateCompatibleDC(pDC);
	dcMemory[1].SelectObject(&bitmap[1]);

	//Load Bitmap2
	bitmap[2].LoadBitmap(IDB_GUITAR2);
	dcMemory[2].CreateCompatibleDC(pDC);
	dcMemory[2].SelectObject(&bitmap[2]);

	//Load Bitmap3
	bitmap[3].LoadBitmap(IDB_BASS);
	dcMemory[3].CreateCompatibleDC(pDC);
	dcMemory[3].SelectObject(&bitmap[3]);

	//store bitmapnumber in new int
	tempBitmapNumber = pDoc->getBitmapNumber();

	strTest7.Format("tempBitmapNumber = %d", tempBitmapNumber);
	pDC->TextOut(0, 140, strTest7);

	for (int i = 1; i < 4; i++)
	{
		pDoc->setBitmapNumber(i);
		
		//Load Bitmap
		bitmap[i].GetBitmap(&bmap[i]);
		bmpWidth = bmap[i].bmWidth;
		bmpHeight = bmap[i].bmHeight;

		//Get size of bitmap
		bitmap[i].GetBitmap(&bmap[i]);
		bmpWidth = bmap[i].bmWidth;
		bmpHeight = bmap[i].bmHeight;
		
		//set bitmap size
		pDoc->setOriginalBitmapWidth(bmpWidth);
		pDoc->setOriginalBitmapHeight(bmpHeight);
	

		pDC->StretchBlt(pDoc->getBitmapLocationX(), pDoc->getBitmapLocationY(), pDoc->getBitmapWidth(), pDoc-  >getBitmapHeight(), &dcMemory[i], 0, 0, pDoc- >getOriginalBitmapWidth(), pDoc->getOriginalBitmapHeight(), SRCCOPY);	
		
	}

The user can click on each bitmap and drag it around the screen but this causes extreme flickering. I've read about double buffering to reduce flicker but I'm not sure how to implement it in this example.
Please can you help?
Thanks
 
Create a memory DC that is the same size as the screen. Draw to the memory DC and then BitBlt the memory DC to the screen.
 
Thanks for the reply. Sorry I am new to this - how do I create a memory DC the same size as the screen?
When you say 'draw to the memory DC' do I use the StretchBlt() function?
 
Creating a memory DC
Code:
CDC memDC;
memDC.CreateCompatibleDC (pDC);
Drawing to the memory DC: instead of using pDC, use memDC

Screen size: use SystemMetrics. There are also calls to get a dialog size but I can't remember what they are.

And finally
Code:
pDC->BitBlt (x, y, width, height, &memDC, 0, 0, SRCCOPY)
Ideally this should only be in the clip region but with the speed of machines nowadays, it doesn't really matter.
 
Hi
Sorry I still can't get this working - I'm obviously not understanding something

Code:
	CBitmap bitmap;	//declare array of CBitmap objects
	CDC dcMemory;	//declare array of Memorys
	BITMAP bmap;		//declare array of BITMAP objects (for getsize of bitmap)
	CDC memDC;
	
	//Load Bitmap
	bitmap.LoadBitmap(IDB_GUITAR1);
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(&bitmap);

	memDC.BitBlt(pDoc->getBitmapLocationX(), pDoc->getBitmapLocationY(),
		80, 200, &memDC, 0, 0, SRCCOPY);

	dcMemory.CreateCompatibleDC(pDC);
	dcMemory.SelectObject(&bitmap);
	
	pDC->BitBlt(pDoc->getBitmapLocationX(), pDoc->getBitmapLocationY(),
		80, 200, &memDC, 0, 0, SRCCOPY);

What am I doing wrong?
Thanks for your help!
 
Don't forget to also overload the WM_ERASEBKGND message handler. You add a Windows message handler to your window's class, which will probably be called "OnEraseBkgnd", and inside you "return false" instead of calling the default handler. A lot of the flicker comes from when the window is repainted gray before you draw to it.

Good luck,

Vincent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top