home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / fire / firedlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  11.0 KB  |  462 lines

  1. // firedlg.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "fire.h"
  15. #include "firedlg.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CAboutDlg dialog used for App About
  24.  
  25. class CAboutDlg : public CDialog
  26. {
  27. public:
  28.     CAboutDlg();
  29.  
  30. // Dialog Data
  31.     //{{AFX_DATA(CAboutDlg)
  32.     enum { IDD = IDD_ABOUTBOX };
  33.     //}}AFX_DATA
  34.  
  35. // Implementation
  36. protected:
  37.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  38.     //{{AFX_MSG(CAboutDlg)
  39.     virtual BOOL OnInitDialog();
  40.     //}}AFX_MSG
  41.     DECLARE_MESSAGE_MAP()
  42. };
  43.  
  44. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  45. {
  46.     //{{AFX_DATA_INIT(CAboutDlg)
  47.     //}}AFX_DATA_INIT
  48. }
  49.  
  50. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  51. {
  52.     CDialog::DoDataExchange(pDX);
  53.     //{{AFX_DATA_MAP(CAboutDlg)
  54.     //}}AFX_DATA_MAP
  55. }
  56.  
  57. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  58.     //{{AFX_MSG_MAP(CAboutDlg)
  59.         // No message handlers
  60.     //}}AFX_MSG_MAP
  61. END_MESSAGE_MAP()
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CAboutDlg message handlers
  65.  
  66. BOOL CAboutDlg::OnInitDialog()
  67. {
  68.     CDialog::OnInitDialog();
  69.     CenterWindow();
  70.  
  71.     return TRUE;  // return TRUE  unless you set the focus to a control
  72. }
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CFireDlg dialog
  76.  
  77. CFireDlg::CFireDlg(CWnd* pParent /*=NULL*/)
  78.     : CDialog(CFireDlg::IDD, pParent)
  79. {
  80.     //{{AFX_DATA_INIT(CFireDlg)
  81.     m_nSetting = 0;
  82.     //}}AFX_DATA_INIT
  83.     m_nSettingMin = 0;
  84.     m_nSettingMax = 0;
  85.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  86.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  87. }
  88.  
  89. void CFireDlg::DoDataExchange(CDataExchange* pDX)
  90. {
  91.     CDialog::DoDataExchange(pDX);
  92.     //{{AFX_DATA_MAP(CFireDlg)
  93.     DDX_Control(pDX, IDC_SETTINGCTRL, m_SettingCtrl);
  94.     DDX_Control(pDX, IDC_SETTING, m_Setting);
  95.     DDX_Control(pDX, IDC_APPLY, m_Apply);
  96.     DDX_Control(pDX, IDC_PROPERTY, m_Property);
  97.     DDX_Control(pDX, IDC_HEIGHT, m_Height);
  98.     DDX_Control(pDX, IDC_COLOR, m_Color);
  99.     DDX_Control(pDX, IDC_BURN, m_Burn);
  100.     DDX_Control(pDX, IDC_FIRE, m_FireWnd);
  101.     DDX_Text(pDX, IDC_SETTING, m_nSetting);
  102.     DDV_MinMaxInt(pDX, m_nSetting, m_nSettingMin, m_nSettingMax);
  103.     //}}AFX_DATA_MAP
  104. }
  105.  
  106. BEGIN_MESSAGE_MAP(CFireDlg, CDialog)
  107.     //{{AFX_MSG_MAP(CFireDlg)
  108.     ON_WM_SYSCOMMAND()
  109.     ON_WM_PAINT()
  110.     ON_WM_QUERYDRAGICON()
  111.     ON_BN_CLICKED(IDC_APPLY, OnApply)
  112.     ON_NOTIFY(TVN_SELCHANGED, IDC_PROPERTY, OnPropertySelchanged)
  113.     ON_NOTIFY(TCN_SELCHANGE, IDC_COLOR, OnColorSelchange)
  114.     ON_WM_DESTROY()
  115.     ON_WM_TIMER()
  116.     ON_EN_CHANGE(IDC_SETTING, OnSettingChange)
  117.     ON_WM_ACTIVATE()
  118.     ON_WM_VSCROLL()
  119.     //}}AFX_MSG_MAP
  120. END_MESSAGE_MAP()
  121.  
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CFireDlg message handlers
  124.  
  125. BOOL CFireDlg::OnInitDialog()
  126. {
  127.     CDialog::OnInitDialog();
  128.     CenterWindow();
  129.  
  130.     // Add "About..." menu item to system menu.
  131.  
  132.     // IDM_ABOUTBOX must be in the system command range.
  133.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  134.     ASSERT(IDM_ABOUTBOX < 0xF000);
  135.  
  136.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  137.     CString strAboutMenu;
  138.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  139.     if (!strAboutMenu.IsEmpty())
  140.     {
  141.         pSysMenu->AppendMenu(MF_SEPARATOR);
  142.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  143.     }
  144.  
  145.     m_FireWnd.InitFire(CFireWnd::red);
  146.  
  147.     TC_ITEM TabCtrlItem;
  148.     TabCtrlItem.mask = TCIF_TEXT;
  149.     TabCtrlItem.pszText = "Wood";
  150.     m_Color.InsertItem( 0, &TabCtrlItem );
  151.     TabCtrlItem.pszText = "Natural Gas";
  152.     m_Color.InsertItem( 1, &TabCtrlItem );
  153.     TabCtrlItem.pszText = "Kryptonite";
  154.     m_Color.InsertItem( 2, &TabCtrlItem );
  155.  
  156.     m_Height.SetRange(1, 100);
  157.     m_Height.SetTicFreq(10);
  158.     m_Height.SetPos(25);
  159.  
  160.     m_Burn.SetRange( 0, 100 );
  161.     m_Burn.SetPos( 25 );
  162.  
  163.     m_SettingCtrl.SetRange( 0, 100 );
  164.     m_SettingCtrl.SetBase( 10 );
  165.     m_SettingCtrl.SetPos( 0 );
  166.  
  167.     TV_INSERTSTRUCT TreeCtrlItem;
  168.  
  169.     TreeCtrlItem.hParent = TVI_ROOT;
  170.     TreeCtrlItem.hInsertAfter = TVI_LAST;
  171.     TreeCtrlItem.item.mask = TVIF_TEXT | TVIF_PARAM;
  172.     TreeCtrlItem.item.pszText = "Fire";
  173.     TreeCtrlItem.item.lParam = 0;
  174.     HTREEITEM hTreeItem1 = m_Property.InsertItem(&TreeCtrlItem);
  175.  
  176.     TreeCtrlItem.hParent = hTreeItem1;
  177.     TreeCtrlItem.item.pszText = "Decay";
  178.     TreeCtrlItem.item.lParam = 1;
  179.     m_Property.InsertItem(&TreeCtrlItem);
  180.  
  181.     TreeCtrlItem.item.pszText = "Flammability";
  182.     TreeCtrlItem.item.lParam = 2;
  183.     m_Property.InsertItem(&TreeCtrlItem);
  184.  
  185.     TreeCtrlItem.item.pszText = "Maximum Heat";
  186.     TreeCtrlItem.item.lParam = 3;
  187.     m_Property.InsertItem(&TreeCtrlItem);
  188.  
  189.     TreeCtrlItem.item.pszText = "Spread Rate";
  190.     TreeCtrlItem.item.lParam = 4;
  191.     m_Property.InsertItem(&TreeCtrlItem);
  192.  
  193.     TreeCtrlItem.item.pszText = "Size";
  194.     TreeCtrlItem.item.lParam = 5;
  195.     m_Property.InsertItem(&TreeCtrlItem);
  196.  
  197.     TreeCtrlItem.hParent = TVI_ROOT;
  198.     TreeCtrlItem.item.pszText = "Render";
  199.     TreeCtrlItem.item.lParam = 0;
  200.     HTREEITEM hTreeItem2 = m_Property.InsertItem(&TreeCtrlItem);
  201.  
  202.     TreeCtrlItem.hParent = hTreeItem2;
  203.     TreeCtrlItem.item.pszText = "Smoothness";
  204.     TreeCtrlItem.item.lParam = 6;
  205.     m_Property.InsertItem(&TreeCtrlItem);
  206.  
  207.     TreeCtrlItem.item.pszText = "Distribution";
  208.     TreeCtrlItem.item.lParam = 7;
  209.     m_Property.InsertItem(&TreeCtrlItem);
  210.  
  211.     TreeCtrlItem.item.pszText = "Chaos";
  212.     TreeCtrlItem.item.lParam = 8;
  213.     m_Property.InsertItem(&TreeCtrlItem);
  214.  
  215.     m_Property.Expand(hTreeItem1,TVE_EXPAND);
  216.     m_Property.Expand(hTreeItem2,TVE_EXPAND);
  217.  
  218.     m_uTimerID = SetTimer(0x451,33,NULL);
  219.  
  220.     m_Apply.EnableWindow( FALSE );
  221.  
  222.     return TRUE;  // return TRUE  unless you set the focus to a control
  223. }
  224.  
  225. void CFireDlg::OnSysCommand(UINT nID, LPARAM lParam)
  226. {
  227.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  228.     {
  229.         CAboutDlg dlgAbout;
  230.         dlgAbout.DoModal();
  231.     }
  232.     else
  233.     {
  234.         CDialog::OnSysCommand(nID, lParam);
  235.     }
  236. }
  237.  
  238. // If you add a minimize button to your dialog, you will need the code below
  239. //  to draw the icon.  For MFC applications using the document/view model,
  240. //  this is automatically done for you by the framework.
  241.  
  242. void CFireDlg::OnPaint()
  243. {
  244.     if (IsIconic())
  245.     {
  246.         CPaintDC dc(this); // device context for painting
  247.  
  248.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  249.  
  250.         // Center icon in client rectangle
  251.         int cxIcon = GetSystemMetrics(SM_CXICON);
  252.         int cyIcon = GetSystemMetrics(SM_CYICON);
  253.         CRect rect;
  254.         GetClientRect(&rect);
  255.         int x = (rect.Width() - cxIcon + 1) / 2;
  256.         int y = (rect.Height() - cyIcon + 1) / 2;
  257.  
  258.         // Draw the icon
  259.         dc.DrawIcon(x, y, m_hIcon);
  260.     }
  261.     else
  262.     {
  263.         CDialog::OnPaint();
  264.     }
  265. }
  266.  
  267. // The system calls this to obtain the cursor to display while the user drags
  268. //  the minimized window.
  269. HCURSOR CFireDlg::OnQueryDragIcon()
  270. {
  271.     return (HCURSOR) m_hIcon;
  272. }
  273.  
  274. void CFireDlg::OnColorSelchange(NMHDR* pNMHDR, LRESULT* pResult)
  275. {
  276.     int nColor = m_Color.GetCurSel();
  277.  
  278.     switch(nColor)
  279.     {
  280.     case 0:
  281.         m_FireWnd.InitFire(CFireWnd::red);
  282.         break;
  283.     case 1:
  284.         m_FireWnd.InitFire(CFireWnd::blue);
  285.         break;
  286.     case 2:
  287.         m_FireWnd.InitFire(CFireWnd::green);
  288.         break;
  289.     }
  290.  
  291.     *pResult = 0;
  292. }
  293.  
  294. void CFireDlg::OnSettingChange()
  295. {
  296.     if (m_Apply.m_hWnd != NULL)
  297.         m_Apply.EnableWindow();
  298. }
  299.  
  300. void CFireDlg::OnPropertySelchanged(NMHDR* pNMHDR, LRESULT* pResult)
  301. {
  302.     NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  303.  
  304.     switch(pNMTreeView->itemNew.lParam)
  305.     {
  306.     case 0:
  307.         m_nSetting = 0;
  308.         m_nSettingMin = 0;
  309.         m_nSettingMax = 0;
  310.         break;
  311.     case 1:
  312.         m_nSetting = m_FireWnd.m_nDecay;
  313.         m_nSettingMin = 1;
  314.         m_nSettingMax = 100;
  315.         m_SettingCtrl.SetRange(m_nSettingMin, m_nSettingMax);
  316.         break;
  317.     case 2:
  318.         m_nSetting = m_FireWnd.m_nFlammability;
  319.         m_nSettingMin = 1;
  320.         m_nSettingMax = 399;
  321.         m_SettingCtrl.SetRange(m_nSettingMin, m_nSettingMax);
  322.         break;
  323.     case 3:
  324.         m_nSetting = m_FireWnd.m_nMaxHeat;
  325.         m_nSettingMin = 0;
  326.         m_nSettingMax = 223;
  327.         m_SettingCtrl.SetRange(m_nSettingMin, m_nSettingMax);
  328.         break;
  329.     case 4:
  330.         m_nSetting = m_FireWnd.m_nSpreadRate;
  331.         m_nSettingMin = 1;
  332.         m_nSettingMax = 100;
  333.         m_SettingCtrl.SetRange(m_nSettingMin, m_nSettingMax);
  334.         break;
  335.     case 5:
  336.         {
  337.             CSize size = m_FireWnd.GetBitmapSize();
  338.             m_nSetting = m_FireWnd.m_nSize;
  339.             m_nSettingMin = 40;
  340.             m_nSettingMax = size.cx;
  341.             m_SettingCtrl.SetRange(m_nSettingMin, m_nSettingMax);
  342.         }
  343.         break;
  344.     case 6:
  345.         m_nSetting = m_FireWnd.m_nSmoothness;
  346.         m_nSettingMin = 0;
  347.         m_nSettingMax = 5;
  348.         m_SettingCtrl.SetRange(m_nSettingMin, m_nSettingMax);
  349.         break;
  350.     case 7:
  351.         m_nSetting = m_FireWnd.m_nDistribution;
  352.         m_nSettingMin = 0;
  353.         m_nSettingMax = 10;
  354.         m_SettingCtrl.SetRange(m_nSettingMin, m_nSettingMax);
  355.         break;
  356.     case 8:
  357.         m_nSetting = m_FireWnd.m_nChaos;
  358.         m_nSettingMin = 1;
  359.         m_nSettingMax = 100;
  360.         m_SettingCtrl.SetRange(m_nSettingMin, m_nSettingMax);
  361.         break;
  362.     }
  363.  
  364.     if(pNMTreeView->itemNew.lParam == 0)
  365.     {
  366.         UpdateData(FALSE);
  367.         m_Apply.EnableWindow(FALSE);
  368.         m_Setting.EnableWindow(FALSE);
  369.         m_SettingCtrl.EnableWindow(FALSE);
  370.     }
  371.     else
  372.     {
  373.         UpdateData(FALSE);
  374.         m_Setting.EnableWindow();
  375.         m_SettingCtrl.EnableWindow();
  376.     }
  377.  
  378.     *pResult = 0;
  379. }
  380.  
  381. void CFireDlg::OnApply()
  382. {
  383.     if (!UpdateData())
  384.         return;
  385.  
  386.     HTREEITEM hTreeItem = m_Property.GetSelectedItem();
  387.  
  388.     TV_ITEM TreeCtrlItem;
  389.     TreeCtrlItem.mask = TVIF_HANDLE | TVIF_PARAM;
  390.     TreeCtrlItem.hItem = hTreeItem;
  391.  
  392.     m_Property.GetItem(&TreeCtrlItem);
  393.  
  394.     switch(TreeCtrlItem.lParam)
  395.     {
  396.     case 0:
  397.         m_nSetting = 0;
  398.         break;
  399.     case 1:
  400.         m_FireWnd.m_nDecay = m_nSetting;
  401.         break;
  402.     case 2:
  403.         m_FireWnd.m_nFlammability = m_nSetting;
  404.         break;
  405.     case 3:
  406.         m_FireWnd.m_nMaxHeat = m_nSetting;
  407.         break;
  408.     case 4:
  409.         m_FireWnd.m_nSpreadRate = m_nSetting;
  410.         break;
  411.     case 5:
  412.         m_FireWnd.m_nSize = m_nSetting;
  413.         break;
  414.     case 6:
  415.         m_FireWnd.m_nSmoothness = m_nSetting;
  416.         break;
  417.     case 7:
  418.         m_FireWnd.m_nDistribution = m_nSetting;
  419.         break;
  420.     case 8:
  421.         m_FireWnd.m_nChaos = m_nSetting;
  422.         break;
  423.     }
  424.  
  425.     m_Apply.EnableWindow( FALSE );
  426. }
  427.  
  428. void CFireDlg::OnDestroy()
  429. {
  430.     CDialog::OnDestroy();
  431.  
  432.     KillTimer(m_uTimerID);
  433. }
  434.  
  435. void CFireDlg::OnTimer(UINT nIDEvent)
  436. {
  437.     CDialog::OnTimer(nIDEvent);
  438.  
  439.     m_FireWnd.RenderFlame();
  440.     m_FireWnd.PaintFlame();
  441.     if ((rand() % 10) == 0)
  442.         m_Burn.SetPos( rand() % 100 );
  443.  
  444.     // Eat spurious WM_TIMER messages
  445.     MSG msg;
  446.     while(::PeekMessage(&msg, m_hWnd, WM_TIMER, WM_TIMER, PM_REMOVE));
  447. }
  448.  
  449. void CFireDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  450. {
  451.     CDialog::OnActivate(nState, pWndOther, bMinimized);
  452.  
  453.     m_FireWnd.SendMessage(WM_QUERYNEWPALETTE);  // redo the palette if necessary
  454. }
  455.  
  456. void CFireDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  457. {
  458.     m_FireWnd.SetMaxBurn(100 - m_Height.GetPos());
  459.  
  460.     CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
  461. }
  462.