/ balltimeView.h : interface of the CBalltimeView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_BALLTIMEVIEW_H__1517C00C_BB30_11D5_B5B9_444553540000__INCLUDED_)
#define AFX_BALLTIMEVIEW_H__1517C00C_BB30_11D5_B5B9_444553540000__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define     VELOCITY    5   /* pixels per move velocity */
#define     BALLRAD     20  /* radius of the ball */
#define     MINRAD      15  /* how close to the wall it can come */
class CBalltimeView : public CView
{
protected: // create from serialization only
        CBalltimeView();
        DECLARE_DYNCREATE(CBalltimeView)

// Attributes
public:
        CBalltimeDoc* GetDocument();

// Operations
public:

// Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CBalltimeView)
        public:
        virtual void OnDraw(CDC* pDC);  // overridden to draw this view
        virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
        protected:
        //}}AFX_VIRTUAL

// Implementation
public:
        void DrawBall();
        CDC* pDC;
        int m_nYSize;
        int m_nXSize;
        BOOL m_bDrawOn;
        virtual ~CBalltimeView();
#ifdef _DEBUG
        virtual void AssertValid() const;
        virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
        //{{AFX_MSG(CBalltimeView)
        afx_msg void OnShow();
        afx_msg void OnSize(UINT nType, int cx, int cy);
        afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
        afx_msg void OnTimer(UINT nIDEvent);
        afx_msg void OnDestroy();
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in balltimeView.cpp
inline CBalltimeDoc* CBalltimeView::GetDocument()
   { return (CBalltimeDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_BALLTIMEVIEW_H__1517C00C_BB30_11D5_B5B9_444553540000__INCLUDED_)



------------------------------------------------------------------------------------------

// balltimeView.cpp : implementation of the CBalltimeView class
//

#include "stdafx.h"
#include "balltime.h"

#include "balltimeDoc.h"
#include "balltimeView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView

IMPLEMENT_DYNCREATE(CBalltimeView, CView)

BEGIN_MESSAGE_MAP(CBalltimeView, CView)
        //{{AFX_MSG_MAP(CBalltimeView)
        ON_COMMAND(IDM_SHOW, OnShow)
        ON_WM_SIZE()
        ON_WM_CREATE()
        ON_WM_TIMER()
        ON_WM_DESTROY()
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView construction/destruction

CBalltimeView::CBalltimeView()
{
        // TODO: add construction code here
        m_bDrawOn=FALSE;

}

CBalltimeView::~CBalltimeView()
{
}

BOOL CBalltimeView::PreCreateWindow(CREATESTRUCT& cs)
{
        // TODO: Modify the Window class or styles here by modifying
        //  the CREATESTRUCT cs

        return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView drawing

void CBalltimeView::OnDraw(CDC* pDC)
{
        CBalltimeDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        // TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView diagnostics

#ifdef _DEBUG
void CBalltimeView::AssertValid() const
{
        CView::AssertValid();
}

void CBalltimeView::Dump(CDumpContext& dc) const
{
        CView::Dump(dc);
}

CBalltimeDoc* CBalltimeView::GetDocument() // non-debug version is inline
{
        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBalltimeDoc)));
        return (CBalltimeDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView message handlers

void CBalltimeView::OnShow() 
{
        // TODO: Add your command handler code here
        if (m_bDrawOn)
                m_bDrawOn = FALSE ;  // toggle drawing on/off
        else
                m_bDrawOn = TRUE ;
        
}

void CBalltimeView::OnSize(UINT nType, int cx, int cy) 
{
        CView::OnSize(nType, cx, cy);
        
        // TODO: Add your message handler code here
        m_nXSize = cx ;
        m_nYSize = cy ;
        
}

int CBalltimeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
        if (CView::OnCreate(lpCreateStruct) == -1)
                return -1;
        
        // TODO: Add your specialized creation code here
        SetTimer(1,10,NULL);
        
        return 0;
}

void CBalltimeView::OnTimer(UINT nIDEvent) 
{
        // TODO: Add your message handler code here and/or call default
        if (m_bDrawOn) DrawBall();

        
        CView::OnTimer(nIDEvent);
}

void CBalltimeView::DrawBall()
{


/* DrawBall() draws a red ball on the window hWnd every time the */
/* function is called.  The ball is BALLRAD in radius.  The walls */
/* (client area size) are from 0,0 to nXSize,nYsize.  The location */
/* is moved each time by VELOCITY units. The signs of the X and Y */
/* velocity terms nVelX,nVelY are reversed each time the ball gets */
/* within MINRAD units of a wall.  This causes the ball to appear */
/* to bounce off the wall.  We draw the ball at its old location */
/* using a white brush which effectively erases it. Then we redraw */
/* it at it's new location using a red brush */

        static  int bX = 2*BALLRAD, bY = 2*BALLRAD,     // ball start X,Y
                bVelX = VELOCITY, bVelY = VELOCITY; // X,Y velocity

        // Erase the old ball
        pDC = GetDC () ;
        CBrush newBrush(RGB (255, 255,255));
        CBrush* pOldBrush = pDC->SelectObject (&newBrush) ;
    CPen newPen(PS_SOLID, 1, RGB(255,255,255));
        CPen* pOldPen = pDC->SelectObject (&newPen) ;
    pDC->Ellipse (bX-BALLRAD, bY-BALLRAD, bX+BALLRAD, bY+BALLRAD) ;

        bX = bX + bVelX ;        // compute new center X,Y of ball
        bY = bY + bVelY ;

        if (bY > m_nYSize)              // check if user moved walls and hid ball
                bY = 2 * BALLRAD ;  // put the ball back at the starting...
        if (bX > m_nXSize)      // position if it is not going to be...
                bX = 2 * BALLRAD ;  // visible with the new window size

        if (bY < MINRAD || m_nYSize - bY < MINRAD)  // check if it hit a wall
                bVelY = -1 * bVelY ;                    // if so, change direction
        if (bX < MINRAD || m_nXSize - bX < MINRAD)
                bVelX = -1 * bVelX ;

        // Draw the ball in red at its new location
        CBrush newBrush1(RGB (255,0,0));
        CBrush* pOldBrush1 = pDC->SelectObject (&newBrush1) ;
    pDC->Ellipse (bX-BALLRAD, bY-BALLRAD, bX+BALLRAD, bY+BALLRAD) ;

        // get rid of pens and brushes
    pDC->SelectObject (pOldBrush) ;
    pDC->SelectObject (pOldBrush1) ;
        pDC->SelectObject (pOldPen) ;

}

void CBalltimeView::OnDestroy() 
{
        CView::OnDestroy();
        
        // TODO: Add your message handler code here
        KillTimer(1);
}