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

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   Tanks.cpp
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the main application object. Holds the 
  11. *                       singleton objects that are shared by the apps threads.
  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. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CTanksApp
  31. BEGIN_MESSAGE_MAP(CTanksApp, CWinApp)
  32. //{{AFX_MSG_MAP(CTanksApp)
  33. //}}AFX_MSG_MAP
  34. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  35. END_MESSAGE_MAP()
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CTanksApp construction
  38. CTanksApp::CTanksApp() :
  39.     m_gIncomingMsgQueue(FALSE), // Incoming queue is non blocking
  40.     m_hSingleInstanceSemaphore (0),
  41.     m_uMaxRTT(MAX_VALID_RTT)
  42. {
  43. // TODO: add construction code here,
  44. // Place all significant initialization in InitInstance
  45.     m_pMainWnd = NULL;
  46.     for (int i = 0; i < MAX_TANKS; i++)
  47.         m_guRemoteTanksDirs[i] = INVALID_DIRECTION;
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. // The one and only CTanksApp object
  51. CTanksApp theApp;
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CTanksApp initialization
  54. BOOL CTanksApp::InitInstance()
  55. {
  56. // Standard initialization
  57. // If you are not using these features and wish to reduce the size
  58. //  of your final executable, you should remove from the following
  59. //  the specific initialization routines you do not need.
  60.     if (!IsOnlyInstance())
  61.         return FALSE;   // Cannot run more than one instance on same machine 
  62. #ifdef _AFXDLL
  63. Enable3dControls(); // Call this when using MFC in a shared DLL
  64. #else
  65. Enable3dControlsStatic(); // Call this when linking to MFC statically
  66. #endif
  67.     SetRegistryKey(IDS_REGISTRY_KEY);
  68.     m_gKbdManager.InitKeysTable();  // Set keys bindings before dialog is displayed
  69.     CTanksDlg dlg;
  70. m_pMainWnd = &dlg;
  71.     SetDialogBkColor (RGB(0,0,0), RGB(192, 192, 192));
  72. dlg.DoModal();
  73.     if (dlg.IsGameOn())
  74.         // Game dialog was closed abruptly
  75.         EndGameThreads ();
  76. // Since the dialog has been closed, return FALSE so that we exit the
  77. //  application, rather than start the application's message pump.
  78. return FALSE;
  79. }
  80. BOOL CTanksApp::StartGameThreads ()
  81. {
  82.     CWaitCursor cws;
  83.     BOOL bCommManagerRes =
  84.         TANKS_APP->m_gCommManager.OnNewGame();
  85.     if (FALSE == bCommManagerRes)
  86.     {   // Comm. manager failed to start:
  87.         TANKS_APP->m_gCommManager.OnStopGame();
  88.         return FALSE;
  89.     }
  90.         // Start the game rendering engine:
  91.     m_gGameManager.BeginGame ();
  92.     return TRUE;
  93. }
  94. void CTanksApp::EndGameThreads ()
  95. {
  96.     CWaitCursor cws;
  97.     m_gGameManager.EndGame ();
  98.     m_gCommManager.OnStopGame();
  99. }
  100. BOOL
  101. CTanksApp::GetStoredGUID (GUID *pGUID)
  102. {
  103.     wchar_t wszGUID[39];
  104.     // Get stored IID as string:
  105.     CString strGUID = GetProfileString (COMM_SECTION, CONNECTION_GUID_ENTRY, "");
  106.     
  107.     if (strGUID.IsEmpty())
  108.         return FALSE;
  109.     // Convert string to wide chars:
  110.     int nRes = mbstowcs (wszGUID, strGUID.GetBuffer(39), 39);
  111.     if (-1 == nRes)
  112.         return FALSE;
  113.     return (S_OK == IIDFromString (wszGUID, pGUID));
  114. }
  115. BOOL 
  116. CTanksApp::SetStoredGUID (GUID& GUID)
  117. {
  118.     wchar_t *wszGUID = NULL;
  119.     LPSTR szGUID = NULL;
  120.     BOOL bRes = FALSE;
  121.     if (S_OK != StringFromIID (GUID, &wszGUID))
  122.         goto CLEAN_UP;
  123.     szGUID = new char[wcslen(wszGUID) + 1];
  124.     if (-1 == wcstombs (szGUID, wszGUID, wcslen (wszGUID) + 1))
  125.         goto CLEAN_UP;
  126.     bRes = WriteProfileString(COMM_SECTION, CONNECTION_GUID_ENTRY, CString (szGUID));
  127. CLEAN_UP:
  128.     if (wszGUID)
  129.         ::CoTaskMemFree(wszGUID);
  130.     if (szGUID)
  131.         delete [] szGUID;
  132.     return bRes;
  133. }
  134. BOOL
  135. CTanksApp::IsOnlyInstance ()
  136. {
  137. #define SINGLE_INSTANCE_SEM_NAME        "TanksAppSingleInstanceSemaphore"
  138.     m_hSingleInstanceSemaphore = OpenSemaphore (SEMAPHORE_ALL_ACCESS, TRUE, SINGLE_INSTANCE_SEM_NAME);
  139.     if (NULL != m_hSingleInstanceSemaphore)
  140.     {   // Other instance exists
  141.         AfxMessageBox (IDS_SINGLE_INSTANCE, MB_OK | MB_ICONEXCLAMATION);
  142.         return FALSE;
  143.     }
  144.     m_hSingleInstanceSemaphore = CreateSemaphore (NULL, 0, 1, SINGLE_INSTANCE_SEM_NAME);
  145.     if (NULL != m_hSingleInstanceSemaphore)
  146.         return TRUE;
  147.     DWORD dwErr = GetLastError ();
  148.     CString cstr;
  149.     cstr.Format ("Cannot create semaphore : %d", dwErr);
  150.     AfxMessageBox (cstr,MB_OK | MB_ICONHAND);
  151.     return FALSE;
  152. }
  153. CTanksApp::~CTanksApp ()
  154. {
  155.     if (m_hSingleInstanceSemaphore)
  156.         CloseHandle (m_hSingleInstanceSemaphore);
  157. }