home *** CD-ROM | disk | FTP | other *** search
- // MandMTVw.cpp : implementation of the CMandelMTView class
- //
-
- #include "stdafx.h"
- #include "MandelMT.h"
-
- #include "MandMDoc.h"
- #include "MandMTVw.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- // Mandelbrot set constants:
- #define CIMAX 1.2
- #define CIMIN -1.2
- #define CRMAX 1.0
- #define CRMIN -2.0
- #define NMAX 128
-
- // Global variables for communicating among threads:
- int ColMax;
- int RowMax;
- BOOL StopDraw;
-
- /////////////////////////////////////////////////////////////////////////////
- // CMandelMTView
-
- IMPLEMENT_DYNCREATE(CMandelMTView, CView)
-
- BEGIN_MESSAGE_MAP(CMandelMTView, CView)
- //{{AFX_MSG_MAP(CMandelMTView)
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMandelMTView construction/destruction
-
- CMandelMTView::CMandelMTView()
- {
- // TODO: add construction code here
-
- m_PDrawThread = 0;
- }
-
- CMandelMTView::~CMandelMTView()
- {
- delete m_PDrawThread;
- }
-
- BOOL CMandelMTView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CView::PreCreateWindow(cs);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMandelMTView drawing
-
- UINT DrawFractal (LPVOID PHWndView)
- {
- float CI;
- CClientDC ClientDC (CWnd::FromHandle (*(HWND *)PHWndView));
- int Col;
- static DWORD ColorTable [6] =
- {0x0000ff, // red
- 0x00ff00, // green
- 0xff0000, // blue
- 0x00ffff, // yellow
- 0xffff00, // cyan
- 0xff00ff}; // magenta
- int ColorVal;
- float CR = (float)CRMIN;
- float DCI = (float)((CIMAX - CIMIN) / (RowMax-1));
- float DCR = (float)((CRMAX - CRMIN) / (ColMax-1));
- float I;
- float ISqr;
- float R;
- int Row;
- float RSqr;
-
- for (Col = 0; Col < ColMax; ++Col)
- {
- if (StopDraw)
- break;
-
- CI = (float)CIMAX;
-
- for (Row = 0; Row < RowMax; ++Row)
- {
- R = (float)0.0;
- I = (float)0.0;
- RSqr = (float)0.0;
- ISqr = (float)0.0;
- ColorVal = 0;
- while (ColorVal < NMAX && RSqr + ISqr < 4)
- {
- ++ColorVal;
- RSqr = R * R;
- ISqr = I * I;
- I *= R;
- I += I + CI;
- R = RSqr - ISqr + CR;
- }
- ClientDC.SetPixelV (Col, Row, ColorTable [ColorVal % 6]);
- CI -= DCI;
- }
-
- CR += DCR;
- }
-
- return (0);
- }
-
- void CMandelMTView::OnDraw(CDC* pDC)
- {
- CMandelMTDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
-
- // TODO: add draw code for native data here
-
- if (m_PDrawThread)
- {
- StopDraw = TRUE;
- m_PDrawThread->ResumeThread ();
- ::WaitForSingleObject
- (m_PDrawThread->m_hThread, // drawing thread handle
- INFINITE); // wait as long as necessary
- delete m_PDrawThread;
- }
-
- m_PDrawThread = AfxBeginThread
- (DrawFractal,
- &m_hWnd,
- THREAD_PRIORITY_BELOW_NORMAL,
- 0,
- CREATE_SUSPENDED);
- m_PDrawThread->m_bAutoDelete = FALSE;
- StopDraw = FALSE;
- m_PDrawThread->ResumeThread ();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMandelMTView diagnostics
-
- #ifdef _DEBUG
- void CMandelMTView::AssertValid() const
- {
- CView::AssertValid();
- }
-
- void CMandelMTView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
-
- CMandelMTDoc* CMandelMTView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMandelMTDoc)));
- return (CMandelMTDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CMandelMTView message handlers
-
- void CMandelMTView::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
-
- // TODO: Add your message handler code here
-
- if (cx == 0 || cy == 0)
- return;
-
- ColMax = cx;
- RowMax = cy;
- }
-