[RESOLVED] Animation problem using double buffering
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: [RESOLVED] Animation problem using double buffering

Hybrid View

  1. #1
    Join Date
    Jun 1999
    Posts
    490

    [RESOLVED] Animation problem using double buffering

    Hi,

    I solved the following problem:
    http://forums.codeguru.com/showthrea...ight=animation

    but now i have lost that code. I dont know how to do it. Somebody plz guide me how to do animation using double buffering code.

    Zulfi.

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,981

    Re: Animation problem using double buffering

    You create two bitmaps of the same size and bitdepth
    one holds the active/visible frame
    one holds the new frame you're working on

    your OnPaint only blits the invalidated rect from the visible bitmap to the screen.

    When your new frame is finished, you flip the pointers/handles to the 2 bitmaps so the "new frame" becomes the visible frame, and the previously visible frame now becomes the new frame bitmap you'll be working on.
    Invalidate the entire screen, OnPaint will do the rest
    Repeat above 3 steps until whatever end condition is reached.

  3. #3
    Join Date
    Jun 1999
    Posts
    490

    Re: Animation problem using double buffering

    Hi,
    Thanks for your guidance. Sorry I am following the previous approach given in the link which says:
    To get rid of the flickering, handle WM_ERASEBKGND and just return TRUE in it.
    To get rid of the black, you need to paint the entire memory DC in the color you want before starting to paint your road etc. You can use FillRect for this.
    I have added WM_ERASEBKGND. It has removed the flickering but the black color is there as mentioned in that post and the grey color window is also created which slowly engulfs the entire screen.
    I dont know how to paint the entire memory DC???
    Kindly guide me what should i do.

    Zulfi.

  4. #4
    Join Date
    Jun 1999
    Posts
    490

    Re: Animation problem using double buffering

    Hi,
    I have modified my OnDraw but still i cant solve the problem. Somebody plz guide me.
    Code:
    void CAnimationCarView::OnDraw(CDC* pDC)
    {
    	CAnimationCarDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	// TODO: add draw code for native data here
       
    	CBitmap XMap,*pOldMap;
    	CRect rc;
        GetClientRect(&rc);
    	//pDC->DPtoLP( rc );
        CBrush  brBackground( RGB( 255, 255, 100 ) );
        //pDC->FillRect( rc, &brBackground );*/
    	XMap.CreateCompatibleBitmap(pDC,rc.Width(), rc.Height());
    	CDC WorkDC;
    	CDC *MyDC=&WorkDC;
    	MyDC->CreateCompatibleDC(pDC);
    	MyDC->FillRect(rc, &brBackground);
    	pOldMap=MyDC->SelectObject(&XMap);
    	road(MyDC);
    	mountain(MyDC);
    	stationary_car(MyDC);
    	pDC->BitBlt(0, 0, rc.Width(), rc.Height(), MyDC, 0, 0, SRCCOPY);
    	MyDC->SelectObject(pOldMap);
    	
    	XMap.DeleteObject();
    	MyDC->DeleteDC();
    	
    
    }
    I have attached the output of program. Somebody plz guide me.

    Zulfi.
    Attached Images Attached Images  

  5. #5

    Re: Animation problem using double buffering

    You should use the codes like the following:
    CDC dc;
    dc.CreateCompatibleDC(pDC);
    CBitmap bmpMem;
    bmpMem.CreateCompatibleBitmap(pDC, rectClient.Width(), rectClient.Height());
    CBitmap* pBmpOld = dc.SelectObject(&bmpMem);

    int oldBkMode = dc.SetBkMode(TRANSPARENT);

    COLORREF oldTextColor = dc.SetTextColor(m_crInActiveTabText);

    // Do your filling here.
    // dc.FillRect(..);

    dc.SetBkMode(oldBkMode);

    pDC->BitBlt(rectClient.left, rectClient.top,rectClient.Width(), rectClient.Height(),&dc, 0, 0, SRCCOPY);
    dc.SelectObject(pBmpOld);
    bmpMem.DeleteObject();

    If the problem is still exist, I think you should check the InvalidateRect method that you used for your animating.

    Best Regards

    Jackie Zhang

    (E-XD++ MFC Visualization Library at: code-home.com)

  6. #6
    Join Date
    Jun 1999
    Posts
    490

    Re: Animation problem using double buffering

    Hi,
    Thanks for your response. I am getting syntax error at the following line:
    Code:
    COLORREF oldTextColor = dc.SetTextColor(m_crInActiveTabText);
    The error is:

    D:\Zulfi\VC6\CGAM LAB\AnimationCar\AnimationCarView.cpp(250) : error C2065: 'm_crInActiveTabText' : undeclared identifier
    What value should be assigned to it??
    Plz guide me.

    Zulfi.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,557

    Re: Animation problem using double buffering

    See http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

    The parameter for SetTextColor() is of type COLORREF and specifies the colour of the text as an RGB colour value. If the compiler is complaining that 'm_crInActiveTabText' is an undeclared identifer, then you need to determine what colour the text here is required to be set and use that value.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Animation problem using double buffering

    If you do not draw any text then you can comment this line out. Or just remove it.
    Victor Nijegorodov

  9. #9
    Join Date
    Jun 1999
    Posts
    490

    Re: Animation problem using double buffering

    Hi,
    Thanks for your guidance. I have made changes based upon my understanding of Andytim's hints:
    Code:
    CRect rectClient;
    GetClientRect(&rectClient);
    CDC WorkDC;
    WorkDC.CreateCompatibleDC(pDC);
    CBitmap bmpMem;
    bmpMem.CreateCompatibleBitmap(pDC, rectClient.Width(), rectClient.Height());
    CBitmap* pBmpOld = WorkDC.SelectObject(&bmpMem);
    int oldBkMode = WorkDC.SetBkMode(TRANSPARENT);
    road(&WorkDC);
    mountain(&WorkDC);
    stationary_car(&WorkDC);
    WorkDC.SetBkMode(oldBkMode);
    pDC->BitBlt(rectClient.left, rectClient.top,rectClient.Width(), rectClient.Height(),&WorkDC, 0, 0, SRCCOPY);
    WorkDC.SelectObject(pBmpOld);
    bmpMem.DeleteObject();
    However I dont have a one line code for filling. There are actually functions and not rectangle and intead they are polygons so i have called these functions

    Code:
    road(&WorkDC);
    mountain(&WorkDC);
    stationary_car(&WorkDC);



    instead of:
    Code:
           
    // Do your filling here.
    // dc.FillRect(..);
    Despite of these changes my program output is exactly same as it was before (output attached). I am still getting the grey window which covers my actual output and i dont want this.

    Plz solve this problem. Thanks for your time and guidance.

    Zulfi.

  10. #10
    Join Date
    Jun 1999
    Posts
    490

    Re: Animation problem using double buffering

    Hi,
    Thanks. I have got solution for my problem:
    http://social.msdn.microsoft.com/For...orum=vcgeneral
    It was same as suggested by Mr. andytim but i was not able to find out that i was doing the double buffereing inside the stationary_car method also.

    My altered code (i.e ::OnDraw(...) and stationary_car(...) method) is now:
    Code:
    void CAnimationCarView::OnDraw(CDC* pDC)
    {
    	CAnimationCarDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	if (!pDoc)
    		return;
    
    CRect rectClient;
    GetClientRect(&rectClient);
    CDC WorkDC;
    WorkDC.CreateCompatibleDC(pDC);
    CBitmap bmpMem;
    bmpMem.CreateCompatibleBitmap(pDC, rectClient.Width(), rectClient.Height());
    CBitmap* pBmpOld = WorkDC.SelectObject(&bmpMem);
    int oldBkMode = WorkDC.SetBkMode(TRANSPARENT);
    land(&WorkDC); 
    	
    	road(&WorkDC);
    	mountain(&WorkDC);
    	stationary_car(&WorkDC);
    WorkDC.SetBkMode(oldBkMode);
    pDC->BitBlt(rectClient.left, rectClient.top,rectClient.Width(), rectClient.Height(),&WorkDC, 0, 0, SRCCOPY);
    WorkDC.SelectObject(pBmpOld);
    bmpMem.DeleteObject();
    }
    
    }
    void CAnimationCarView::stationary_car(CDC* MyDC)
    {
    	
    	CPoint poly5[8]={CPoint(i,402),CPoint(j,402),CPoint(j,415),CPoint(k,415), CPoint(j, 402), CPoint(j, 415), CPoint(k, 415), CPoint(i, 402) };
        //CPoint poly6[8]={CPoint(n,415),CPoint(o,428),CPoint(l,415),CPoint(m,428), CPoint(l, 415), CPoint(n, 415), CPoint(o, 428), CPoint(m, 428) };
    	//not making a proper rectangle as with lines
    	pOldPen=NULL;
    	pOldBrush= NULL;
    
    	
    	pOldBrush=NULL;
    	pOldBrush=MyDC->SelectObject(&brGrey);
    	MyDC->Polygon(poly5, 4);
    	//line(i,402,j,402);
    	Line(n,415,o,428,MyDC);
    	//line(l,415,m,428);
    	Line(l,415,m,428,MyDC);
    	//line(l,415,n,415);
    	Line(l,415,n,415,MyDC);
    	//line(o,428,m,428);
    	Line(o,428,m,428,MyDC);
    	pOldBrush=NULL;
    	pOldBrush=MyDC->SelectObject(&brGrey);
    	CPen penBlack(PS_SOLID, 5, RGB(0,0,0));//Black color
    	pOldPen=NULL;
    	pOldPen=MyDC->SelectObject(&penBlack);
    	MyDC->MoveTo(r,434);
    	MyDC->AngleArc(q,434,5,0,360);
    	//pDC->FloodFill(q,434,RGB(0,0,0));
    	//circle(r,434,5);
    	pOldPen=NULL;
    	pOldPen=MyDC->SelectObject(&penBlack);
    	MyDC->AngleArc(r,434,5,0,360);
    	
    	
    	
    }
    Rest of the functions are same. I am also no more using floodfill in stationary_car(....)

    Zulfi.
    Last edited by Zulfi Khan2; March 5th, 2014 at 10:19 PM.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: Animation problem using double buffering

    Please edit your post replacing the Quote tags with the Code ones!
    Otherwise your code is unreadable!
    Victor Nijegorodov

  12. #12
    Join Date
    Jun 1999
    Posts
    490

    Re: [RESOLVED] Animation problem using double buffering

    Hi,
    Thanks again for everybody's cooperation.

    Zulfi.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center