Animation Prob

Discussion in 'Homework Help' started by zulfi100, Feb 19, 2014.

  1. zulfi100

    Thread Starter Member

    Jun 7, 2012
    320
    0
    Hi,
    I have developed an animation program in Visual C++ 6.0. When i excute it , it shows me the object moving but it has two problems:
    i) The portions which are not filled becomes black
    ii) A window is created from the right bottom of screen and slowly completely engulfs the entire screen.
    The code is given below:
    Code ( (Unknown Language)):
    1.  
    2.  
    3. CAnimationCarView::CAnimationCarView()
    4. {
    5.     // TODO: add construction code here
    6.     i=150,j=190,k=145,l=140,m=138,n=195,o=197,p=170,q=148,r=185;
    7.     penGrey.CreatePen(PS_SOLID, 5, RGB(128,128,128));//GREY color
    8.     penLGrey.CreatePen(PS_SOLID,5, RGB(220,220,220));//LGREY color
    9.     penBlack.CreatePen(PS_SOLID, 5, RGB(0,0,0));
    10.     pOldPen=NULL;
    11.     brGrey.CreateSolidBrush(RGB(128, 128, 128));
    12.     brLGrey.CreateSolidBrush(RGB(220,220,220));
    13.     brBlack.CreateSolidBrush(RGB(0, 0, 0));
    14.     pOldBrush=NULL;
    15. }
    16.  
    17. CAnimationCarView::~CAnimationCarView()
    18. {
    19. }
    20.  
    21. BOOL CAnimationCarView::PreCreateWindow(CREATESTRUCT& cs)
    22. {
    23.     // TODO: Modify the Window class or styles here by modifying
    24.     //  the CREATESTRUCT cs
    25.  
    26.     return CView::PreCreateWindow(cs);
    27. }
    28.  
    29. /////////////////////////////////////////////////////////////////////////////
    30. // CAnimationCarView drawing
    31.  
    32. void CAnimationCarView::road(CDC* pDC){
    33.     CPoint poly6[8]={CPoint(0,400), CPoint(210,400),CPoint(210,440),CPoint(0,440)};
    34.     CPen pen(PS_SOLID, 5, RGB(220,220,220));
    35.     CPen* pOldPen=NULL;
    36.     pOldPen=pDC->SelectObject(&pen);
    37.     CBrush br(RGB(220, 220, 220));
    38.     CBrush* pOldBrush=NULL;
    39.     pOldBrush=pDC->SelectObject(&br);
    40.     pDC->Polygon(poly6, 4);
    41.     pDC->SelectObject(pOldBrush);
    42.     pDC->SelectObject(pOldPen);
    43.     }
    44.  
    45. void CAnimationCarView::mountain(CDC* pDC)
    46. {
    47.     //setcolor(BROWN);
    48.     CPen penBrown(PS_SOLID, 5, RGB(150,75,0));
    49.     CPen* pOldPen=NULL;
    50.     pOldPen=pDC->SelectObject(&penBrown);
    51.     //line(0,250,700,250);
    52.     pDC->MoveTo(0,250);
    53.     pDC->LineTo(700,250);
    54.     //line(0,250,0,160);
    55.     pDC->MoveTo(0,250);
    56.     pDC->LineTo(0,160);
    57.     //line(0,160,30,200);
    58.     pDC->MoveTo(0,160);
    59.     pDC->LineTo(30,200);
    60.     //line(30,200,80,120);
    61.     pDC->MoveTo(30,200);
    62.     pDC->LineTo(80,120);
    63.     //arc(90,124,30,150,10);
    64.     pDC->AngleArc(90,124,10,30,150);
    65.     //line(99,119,150,180);
    66.     pDC->MoveTo(99,119);
    67.     pDC->LineTo(150,180);
    68.     //line(150,180,220,100);
    69.     pDC->MoveTo(150,180);
    70.     pDC->LineTo(220,100);
    71.     //arc(234,106,30,150,15);
    72.     pDC->AngleArc(234,106,15,30,150);
    73.     //line(248,99,320,200);
    74.     pDC->MoveTo(248,99);
    75.     pDC->LineTo(320,200);
    76.     //line(320,200,370,125);
    77.     pDC->MoveTo(320,200);
    78.     pDC->LineTo(370,125);
    79.     //arc(380,129,30,150,10);
    80.     pDC->AngleArc(380,129,10,30,150);
    81.     //line(389,124,430,180);
    82.     pDC->MoveTo(389,124);
    83.     pDC->LineTo(430,180);
    84.     //line(430,180,505,90);
    85.     pDC->MoveTo(430,180);
    86.     pDC->LineTo(505,90);
    87.     //arc(519,96,30,150,15);
    88.     pDC->AngleArc(519,96,15,30,150);
    89.     //line(533,89,605,200);
    90.     pDC->MoveTo(533,89);
    91.     pDC->LineTo(605,200);
    92.     //line(605,200,700,110);
    93.     pDC->MoveTo(605,200);
    94.     pDC->LineTo(700,170);
    95.     //line(700,170,700,250);
    96.     pDC->MoveTo(700,170);
    97.     pDC->LineTo(700,250);
    98.     CBrush brBrown(RGB(150, 75, 0));
    99.     CBrush* pOldBrush=NULL;
    100.     pOldBrush=pDC->SelectObject(&brBrown);
    101.     pDC->FloodFill(10,245,RGB(150,75,0));
    102.     pDC->SelectObject(pOldBrush);
    103.     pDC->SelectObject(pOldPen);
    104.    
    105. }
    106.  
    107. void CAnimationCarView::stationary_car(CDC* pDC)
    108. {
    109.     //int i=150,j=190,k=145,l=140,m=138,n=195,o=197,p=170,q=148,r=185;
    110.     //CPen penGrey(PS_SOLID, 5, RGB(128,128,128));//GREY color
    111.     CBitmap XMap,*pOldMap;
    112.     CPen* pOldPen=NULL;
    113.     pOldPen=pDC->SelectObject(&penGrey);
    114.     SIZE Size;
    115.     Size.cx = GetSystemMetrics(SM_CXSCREEN);
    116.     Size.cy = GetSystemMetrics(SM_CYSCREEN);
    117.     XMap.CreateCompatibleBitmap(pDC,Size.cx,Size.cy);
    118.     CDC WorkDC;
    119.     CDC *MyDC=&WorkDC;
    120.     MyDC->CreateCompatibleDC(pDC);
    121.     pOldMap=MyDC->SelectObject(&XMap);
    122.     //line(i,402,j,402);
    123.     MyDC->MoveTo(i,402);
    124.     MyDC->LineTo(j,402);
    125.     //line(j,415,k,415);
    126.     MyDC->MoveTo(j,415);
    127.     MyDC->LineTo(k,415);
    128.     //line(j,402,j,415);
    129.     MyDC->MoveTo(j,402);
    130.     MyDC->LineTo(j,415);
    131.     //line(k,415,i,402);
    132.     MyDC->MoveTo(k,415);
    133.     MyDC->LineTo(i,402);
    134.     //setfillstyle(SOLID_FILL,6);
    135.     //CBrush brGrey(RGB(128, 128, 128));
    136.     CBrush* pOldBrush=NULL;
    137.     //floodfill(p,408,6);
    138.     //NOTE IF FloodFill done after select object, color mixing would occur
    139.     pOldBrush=MyDC->SelectObject(&brGrey);
    140.     MyDC->FloodFill(p,408,RGB(128,128,128));
    141.     //line(n,415,o,428);
    142.     MyDC->MoveTo(n,415);
    143.     MyDC->LineTo(o,428);
    144.     //line(l,415,m,428);
    145.     MyDC->MoveTo(l,415);
    146.     MyDC->LineTo(m,428);
    147.     //line(l,415,n,415);
    148.     MyDC->MoveTo(l,415);
    149.     MyDC->LineTo(n,415);
    150.     //line(o,428,m,428);
    151.     MyDC->MoveTo(o,428);
    152.     MyDC->LineTo(m,428);
    153.     //setfillstyle(SOLID_FILL,6);
    154.     //floodfill(p,422,6);
    155.     MyDC->FloodFill(p,422,RGB(128,128,128));
    156.     MyDC->SelectObject(pOldBrush);
    157.     MyDC->SelectObject(pOldPen);
    158.     //Drawing wheels
    159.     //circle(q,434,5);
    160.     CBrush brBlack(RGB(0, 0, 0));
    161.     pOldBrush=NULL;
    162.     pOldBrush=MyDC->SelectObject(&brBlack);
    163.     //CPen penBlack(PS_SOLID, 5, RGB(0,0,0));//Black color
    164.     pOldPen=NULL;
    165.     pOldPen=MyDC->SelectObject(&penBlack);
    166.     MyDC->MoveTo(r,434);
    167.     MyDC->AngleArc(q,434,5,0,360);
    168.     MyDC->FloodFill(q,434,RGB(0,0,0));
    169.     //circle(r,434,5);
    170.     MyDC->AngleArc(r,434,5,0,360);
    171.     pDC->BitBlt(j,k,Size.cx,Size.cy,MyDC,0,0,SRCCOPY);
    172.     MyDC->SelectObject(pOldPen);
    173.     MyDC->SelectObject(pOldBrush);
    174.     MyDC->SelectObject(pOldMap);
    175.     XMap.DeleteObject();
    176.     MyDC->DeleteDC();
    177.  
    178. }
    179.  
    180.  
    181. void CAnimationCarView::OnDraw(CDC* pDC)
    182. {
    183.     CAnimationCarDoc* pDoc = GetDocument();
    184.     ASSERT_VALID(pDoc);
    185.     // TODO: add draw code for native data here
    186.    
    187.     CBitmap XMap,*pOldMap;
    188.     CRect rc;
    189.     GetClientRect(&rc);
    190.     /*pDC->DPtoLP( rc );
    191.     CBrush  brBackground( RGB( 255, 255, 100 ) );
    192.     pDC->FillRect( rc, &brBackground );*/
    193.     XMap.CreateCompatibleBitmap(pDC,rc.Width(), rc.Height());
    194.     CDC WorkDC;
    195.     CDC *MyDC=&WorkDC;
    196.     MyDC->CreateCompatibleDC(pDC);
    197.     pOldMap=MyDC->SelectObject(&XMap);
    198.     road(MyDC);
    199.     mountain(MyDC);
    200.     stationary_car(MyDC);
    201.     pDC->BitBlt(0, 0, rc.Width(), rc.Height(), MyDC, 0, 0, SRCCOPY);
    202.     MyDC->SelectObject(pOldMap);
    203.    
    204.     XMap.DeleteObject();
    205.     MyDC->DeleteDC();
    206.    
    207.  
    208. }
    209.  
    210.  
    211. int CAnimationCarView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    212. {
    213.     if (CView::OnCreate(lpCreateStruct) == -1)
    214.         return -1;
    215.     SetTimer(1,200,NULL);
    216.     // TODO: Add your specialized creation code here
    217.    
    218.     return 0;
    219. }
    220.  
    221. void CAnimationCarView::OnTimer(UINT nIDEvent)
    222. {
    223.     // TODO: Add your message handler code here and/or call default
    224.     if(i>0){
    225.     i=i-3;
    226.     Invalidate();
    227.     j=j-3;k=k-3;l=l-3;m=m-3;n=n-3;o=o-3;p=p-3;q=q-3;r=r-3;
    228.     }
    229.     CView::OnTimer(nIDEvent);
    230. }
    231.  
    232. BOOL CAnimationCarView::OnEraseBkgnd(CDC* pDC)
    233. {
    234.     // TODO: Add your message handler code here and/or call default
    235.     return TRUE;
    236.     return CView::OnEraseBkgnd(pDC);
    237. }
    238.  
    239.  
    240.  
    241.  
    The header file is:

    view.h
    Code ( (Unknown Language)):
    1.  
    2. // AnimationCarView.h : interface of the CAnimationCarView class
    3. //
    4. /////////////////////////////////////////////////////////////////////////////
    5.  
    6. #if !defined(AFX_ANIMATIONCARVIEW_H__868E797E_E1BA_48B0_BA06_C84A7591A32B__INCLUDED_)
    7. #define AFX_ANIMATIONCARVIEW_H__868E797E_E1BA_48B0_BA06_C84A7591A32B__INCLUDED_
    8.  
    9. #if _MSC_VER > 1000
    10. #pragma once
    11. #endif // _MSC_VER > 1000
    12.  
    13.  
    14. class CAnimationCarView : public CView
    15. {
    16. protected: // create from serialization only
    17.     CAnimationCarView();
    18.     DECLARE_DYNCREATE(CAnimationCarView)
    19.  
    20. // Attributes
    21. public:
    22.     CAnimationCarDoc* GetDocument();
    23.  
    24. // Operations
    25. public:
    26.  void mountain(CDC*);
    27.  void road(CDC* pDC);
    28.  void stationary_car(CDC* pDC);
    29.  int i,j,k,l,m,n,o,p,q,r;
    30.  CPen penGrey, penLGrey, penBlack;
    31.  CPen *pOldPen;
    32.  CBrush brGrey, brLGrey, brBlack;
    33.  CBrush* pOldBrush;
    34. // Overrides
    35.     // ClassWizard generated virtual function overrides
    36.     //{{AFX_VIRTUAL(CAnimationCarView)
    37.     public:
    38.     virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    39.     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    40.     virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
    41.     protected:
    42.     virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    43.     virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    44.     virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    45.     //}}AFX_VIRTUAL
    46.  
    47. // Implementation
    48. public:
    49.     virtual ~CAnimationCarView();
    50. #ifdef _DEBUG
    51.     virtual void AssertValid() const;
    52.     virtual void Dump(CDumpContext& dc) const;
    53. #endif
    54.  
    55. protected:
    56.  
    57. // Generated message map functions
    58. protected:
    59.     //{{AFX_MSG(CAnimationCarView)
    60.     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    61.     afx_msg void OnTimer(UINT nIDEvent);
    62.     afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    63.     //}}AFX_MSG
    64.     DECLARE_MESSAGE_MAP()
    65. };
    66.  
    67. #ifndef _DEBUG  // debug version in AnimationCarView.cpp
    68. inline CAnimationCarDoc* CAnimationCarView::GetDocument()
    69.    { return (CAnimationCarDoc*)m_pDocument; }
    70. #endif
    71.  
    72. /////////////////////////////////////////////////////////////////////////////
    73.  
    74. //{{AFX_INSERT_LOCATION}}
    75. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    76.  
    77. #endif // !defined(AFX_ANIMATIONCARVIEW_H__868E797E_E1BA_48B0_BA06_C84A7591A32B__INCLUDED_)
    78.  
    79.  
    80.  

    Somebody advised me:

    There is no flickering now but I dont know how to paint the memory DC. Somebody plz guide me how to solve this problem. I have attached the output of program.

    Zulfi.
     
Loading...