home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual C++ 4 (2nd Edition) / VisualC4.ISO / mandelmt / mandmtvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  4.1 KB  |  184 lines

  1. // MandMTVw.cpp : implementation of the CMandelMTView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MandelMT.h"
  6.  
  7. #include "MandMDoc.h"
  8. #include "MandMTVw.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. // Mandelbrot set constants:
  17. #define CIMAX 1.2
  18. #define CIMIN -1.2
  19. #define CRMAX 1.0
  20. #define CRMIN -2.0
  21. #define NMAX 128
  22.  
  23. // Global variables for communicating among threads:
  24. int ColMax; 
  25. int RowMax;
  26. BOOL StopDraw;
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CMandelMTView
  30.  
  31. IMPLEMENT_DYNCREATE(CMandelMTView, CView)
  32.  
  33. BEGIN_MESSAGE_MAP(CMandelMTView, CView)
  34.    //{{AFX_MSG_MAP(CMandelMTView)
  35.    ON_WM_SIZE()
  36.    //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CMandelMTView construction/destruction
  41.  
  42. CMandelMTView::CMandelMTView()
  43. {
  44.    // TODO: add construction code here
  45.    
  46.    m_PDrawThread = 0;
  47. }
  48.  
  49. CMandelMTView::~CMandelMTView()
  50. {
  51.    delete m_PDrawThread;
  52. }
  53.  
  54. BOOL CMandelMTView::PreCreateWindow(CREATESTRUCT& cs)
  55. {
  56.    // TODO: Modify the Window class or styles here by modifying
  57.    //  the CREATESTRUCT cs
  58.  
  59.    return CView::PreCreateWindow(cs);
  60. }
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CMandelMTView drawing
  64.  
  65. UINT DrawFractal (LPVOID PHWndView)
  66. {
  67.    float CI;
  68.    CClientDC ClientDC (CWnd::FromHandle (*(HWND *)PHWndView));
  69.    int Col;
  70.    static DWORD ColorTable [6] =
  71.       {0x0000ff,  // red
  72.        0x00ff00,  // green
  73.        0xff0000,  // blue
  74.        0x00ffff,  // yellow
  75.        0xffff00,  // cyan
  76.        0xff00ff}; // magenta
  77.    int ColorVal;
  78.    float CR = (float)CRMIN;
  79.    float DCI = (float)((CIMAX - CIMIN) / (RowMax-1));
  80.    float DCR = (float)((CRMAX - CRMIN) / (ColMax-1));
  81.    float I;
  82.    float ISqr;
  83.    float R;
  84.    int Row;
  85.    float RSqr;
  86.  
  87.    for (Col = 0; Col < ColMax; ++Col)
  88.       { 
  89.       if (StopDraw)
  90.          break;
  91.  
  92.       CI = (float)CIMAX;
  93.  
  94.       for (Row = 0; Row < RowMax; ++Row)
  95.          {
  96.          R = (float)0.0;
  97.          I = (float)0.0;
  98.          RSqr = (float)0.0;
  99.          ISqr = (float)0.0;
  100.          ColorVal = 0;
  101.          while (ColorVal < NMAX && RSqr + ISqr < 4)
  102.             {
  103.             ++ColorVal;
  104.             RSqr = R * R;
  105.             ISqr = I * I;
  106.             I *= R;
  107.             I += I + CI;
  108.             R = RSqr - ISqr + CR;
  109.             }
  110.          ClientDC.SetPixelV (Col, Row, ColorTable [ColorVal % 6]);
  111.          CI -= DCI;
  112.          }
  113.  
  114.       CR += DCR;
  115.       }
  116.    
  117.    return (0);
  118. }
  119.  
  120. void CMandelMTView::OnDraw(CDC* pDC)
  121. {
  122.    CMandelMTDoc* pDoc = GetDocument();
  123.    ASSERT_VALID(pDoc);
  124.  
  125.    // TODO: add draw code for native data here
  126.  
  127.    if (m_PDrawThread)
  128.       {
  129.       StopDraw = TRUE;
  130.       m_PDrawThread->ResumeThread ();
  131.       ::WaitForSingleObject
  132.          (m_PDrawThread->m_hThread,  // drawing thread handle
  133.          INFINITE);                  // wait as long as necessary
  134.       delete m_PDrawThread;  
  135.       }
  136.      
  137.    m_PDrawThread = AfxBeginThread
  138.       (DrawFractal,
  139.       &m_hWnd,
  140.       THREAD_PRIORITY_BELOW_NORMAL,
  141.       0,
  142.       CREATE_SUSPENDED);
  143.    m_PDrawThread->m_bAutoDelete = FALSE;
  144.    StopDraw = FALSE;
  145.    m_PDrawThread->ResumeThread ();
  146. }
  147.  
  148. /////////////////////////////////////////////////////////////////////////////
  149. // CMandelMTView diagnostics
  150.  
  151. #ifdef _DEBUG
  152. void CMandelMTView::AssertValid() const
  153. {
  154.    CView::AssertValid();
  155. }
  156.  
  157. void CMandelMTView::Dump(CDumpContext& dc) const
  158. {
  159.    CView::Dump(dc);
  160. }
  161.  
  162. CMandelMTDoc* CMandelMTView::GetDocument() // non-debug version is inline
  163. {
  164.    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMandelMTDoc)));
  165.    return (CMandelMTDoc*)m_pDocument;
  166. }
  167. #endif //_DEBUG
  168.  
  169. /////////////////////////////////////////////////////////////////////////////
  170. // CMandelMTView message handlers
  171.  
  172. void CMandelMTView::OnSize(UINT nType, int cx, int cy) 
  173. {
  174.    CView::OnSize(nType, cx, cy);
  175.    
  176.    // TODO: Add your message handler code here
  177.  
  178.    if (cx == 0 || cy == 0)
  179.       return;
  180.    
  181.    ColMax = cx;
  182.    RowMax = cy;
  183. }
  184.