GameSetup.cpp
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   GameSetup.cpp
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the game setup dialog that enables the user
  11. *                       to specify the tank's color and game board complexity.
  12. *                       
  13. *                                                                             
  14. *   Authors: Eran Yariv - 28484475                                           
  15. *            Moshe Zur  - 24070856                                           
  16. *                                                                            
  17. *                                                                            
  18. *   Date: 23/09/98                                                           
  19. *                                                                            
  20. ******************************************************************************/
  21. #include "stdafx.h"
  22. #include "Tanks.h"
  23. #include "TanksDlg.h"
  24. #include "GameSetup.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CGameSetup dialog
  32. CGameSetup::CGameSetup(CWnd* pParent /*=NULL*/)
  33. : CDialog(CGameSetup::IDD, pParent)
  34. {
  35. //{{AFX_DATA_INIT(CGameSetup)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CGameSetup::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CGameSetup)
  42. DDX_Control(pDX, IDC_COMPLEXITY, m_Complexity);
  43. DDX_Control(pDX, IDOK, m_ctrOK);
  44. DDX_Control(pDX, IDCANCEL, m_ctrCancel);
  45. //}}AFX_DATA_MAP
  46. }
  47. BEGIN_MESSAGE_MAP(CGameSetup, CDialog)
  48. //{{AFX_MSG_MAP(CGameSetup)
  49. ON_WM_LBUTTONDOWN()
  50. ON_WM_PAINT()
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CGameSetup message handlers
  55. BOOL CGameSetup::OnInitDialog() 
  56. {
  57. CDialog::OnInitDialog();
  58.     RECT MyRect;
  59.     GetWindowRect (&MyRect);
  60.     SetWindowPos (&wndTop, MyRect.left, MyRect.top,
  61.                   MAX_TANKS * (10+TANK_ANIM_WIDTH) + 14, MyRect.bottom - MyRect.top, 0);
  62.     for (int i = 0; i < MAX_TANKS; i++) 
  63.         m_AnimTanks[i].Init (i+1, this);
  64.     m_Complexity.SetRange (1,20, TRUE);
  65. m_ctrCancel.LoadAVI(IDR_AVI_CANCEL);
  66. m_ctrOK.LoadAVI(IDR_AVI_OK);
  67.     LoadSettings();
  68.     m_AnimTanks[m_uSelectedTankID].Select ();   // Start with selected tank
  69.     CTanksDlg *pTanksDlg = (CTanksDlg *)GetParent();
  70.     if (!(TANKS_APP->GetStoredIsHostFlag()) ||  // Machine configured to play as a client or
  71.         pTanksDlg->IsGameOn())                  // Game is in session
  72.     {
  73.         // Don't allow the user to change map complexity level:
  74.         m_Complexity.EnableWindow (FALSE);
  75.         GetDlgItem (IDC_STATIC1)->EnableWindow (FALSE); // Static frame
  76.         GetDlgItem (IDC_STATIC2)->EnableWindow (FALSE); // "Complex" static text
  77.         GetDlgItem (IDC_STATIC3)->EnableWindow (FALSE); // "Simple" static text
  78.     }
  79. return TRUE;  // return TRUE unless you set the focus to a control
  80.               // EXCEPTION: OCX Property Pages should return FALSE
  81. }
  82. void CGameSetup::OnLButtonDown(UINT nFlags, CPoint point) 
  83. {
  84.     CWnd *pSon = ChildWindowFromPoint (point);
  85.     for (UINT i=0; i<MAX_TANKS; i++)
  86.         if (pSon == GetDlgItem (IDC_ANIMATE_TANK1+i))
  87.         {
  88.             if (i == m_uSelectedTankID)
  89.                 break;  // Already selected
  90.                 // Stop previously selected tank:
  91.             m_AnimTanks[m_uSelectedTankID].Unselect ();
  92.                 // Select this one:
  93.             ((CAnimateTank*)pSon)->Select();
  94.             m_uSelectedTankID = i;
  95.             break;
  96.         }
  97. CDialog::OnLButtonDown(nFlags, point);
  98. }
  99. void CGameSetup::OnOK() 
  100. {
  101.     if (!UpdateData())
  102.         // Error in data
  103.         return;
  104.     BOOL bRes =
  105.         TANKS_APP->WriteProfileInt (GAME_SETUP_SECTION, TANK_TYPE_ENTRY, m_uSelectedTankID);
  106.     if (bRes)
  107.         bRes = TANKS_APP->WriteProfileInt (GAME_SETUP_SECTION, MAP_DIFF_ENTRY, m_Complexity.GetPos());
  108.     if (!bRes)
  109.     {
  110.         AfxMessageBox (IDS_CANT_SAVE_GAME_SETUP);
  111.         return;
  112.     }
  113.     CDialog::OnOK();
  114. }
  115. void CGameSetup::LoadSettings()
  116. {
  117.     m_uSelectedTankID = 
  118.         TANKS_APP->GetStoredPreferedTankID();
  119.     m_Complexity.SetPos (TANKS_APP->GetStoredMapComplexity());
  120.     UpdateData (FALSE);
  121. }