Dlg_Start.cpp
上传用户:may_xy
上传日期:2007-08-09
资源大小:1519k
文件大小:9k
源码类别:

游戏

开发平台:

C/C++

  1. // Dlg_Start.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "斗地主.h"
  5. #include "Game.h"
  6. #include "Link.h"
  7. #include "Dlg_About.h"
  8. #include "Dlg_Start.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CDlg_Start dialog
  16. CDlg_Start::CDlg_Start(CWnd* pParent /*=NULL*/)
  17. : CDialog(CDlg_Start::IDD, pParent)
  18. {
  19. //{{AFX_DATA_INIT(CDlg_Start)
  20. m_ServerIP = _T("");
  21. m_Client = _T("请在此输入服务端IP地址");
  22. m_Name = _T("");
  23. m_Face_ID = 0;
  24. m_Net_States = _T("");
  25. m_Net_Info = _T("当前还没有建立网络连接");
  26. //}}AFX_DATA_INIT
  27. }
  28. void CDlg_Start::DoDataExchange(CDataExchange* pDX)
  29. {
  30. CDialog::DoDataExchange(pDX);
  31. //{{AFX_DATA_MAP(CDlg_Start)
  32. DDX_Text(pDX, EDIT_SERVER, m_ServerIP);
  33. DDX_Text(pDX, EDIT_CLIENT, m_Client);
  34. DDX_Text(pDX, EDIT_NAME, m_Name);
  35. DDV_MaxChars(pDX, m_Name, 6);
  36. DDX_Scroll(pDX, SCROLLBAR_FACE, m_Face_ID);
  37. DDX_Text(pDX, STATIC_NET_STATES, m_Net_Info);
  38. //}}AFX_DATA_MAP
  39. }
  40. BEGIN_MESSAGE_MAP(CDlg_Start, CDialog)
  41. //{{AFX_MSG_MAP(CDlg_Start)
  42. ON_BN_CLICKED(IDB_EXIT, OnExit)
  43. ON_BN_CLICKED(RADIO_CLIENT, OnClient)
  44. ON_BN_CLICKED(RADIO_LINK, OnLink)
  45. ON_BN_CLICKED(RADIO_SINGLE, OnSingle)
  46. ON_BN_CLICKED(RADIO_SERVER, OnServer)
  47. ON_WM_HSCROLL()
  48. ON_BN_CLICKED(IDB_START, OnStart)
  49. ON_WM_TIMER()
  50. ON_BN_CLICKED(IDB_GAME_MAKER, OnGameMaker)
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CDlg_Start message handlers
  55. //服务器端接收函数
  56. DWORD WINAPI CDlg_Start::Server_Login(LPVOID pParam)
  57. {
  58. CString str;
  59. char net_info[100];
  60. CLink listen;
  61. listen.SocketInit();
  62. listen.Create();
  63. listen.Bind();
  64. listen.Listen();
  65. s_m_szNetInfo.Format("%s已经建立服务器,等待另两位玩家加入...",pCenterPlayer->m_szName);
  66. //接收第一个客户的请求;
  67. listen.Accept(pServerLink1);
  68. pServerLink1->Recv(pRightPlayer,sizeof(CPlayerInfo));
  69. // str.Format("客户1名字:%sn头像ID:%d",pRight_Player->m_szName,pRight_Player->m_nFaceID);
  70. // AfxMessageBox(str);
  71. s_m_szNetInfo.Format("%s已经建立服务器n%s加入游戏,等待第三位玩家加入!",
  72. pCenterPlayer->m_szName,pRightPlayer->m_szName);
  73. //发送连接信息;
  74. sprintf(net_info,s_m_szNetInfo);
  75. pServerLink1->Send(net_info,sizeof(char)*100);
  76. //接收第二个客户的请求;
  77. listen.Accept(pServerLink2);
  78. listen.Close();
  79. pServerLink2->Recv(pLeftPlayer,sizeof(CPlayerInfo));
  80. // str.Format("客户2名字:%sn头像ID:%d",pLeft_Player->m_szName,pLeft_Player->m_nFaceID);
  81. // AfxMessageBox(str);
  82. s_m_szNetInfo.Format("%s已经建立服务器n%s加入游戏n%s加入游戏n开始游戏!!",
  83. pCenterPlayer->m_szName,pRightPlayer->m_szName,pLeftPlayer->m_szName);
  84. sprintf(net_info,s_m_szNetInfo);
  85. pServerLink2->Send(net_info,sizeof(char)*100);
  86.     //再发送一次连接信息;
  87. pServerLink1->Send(net_info,sizeof(char)*100);
  88. pServerLink2->Send(net_info,sizeof(char)*100);
  89. //给客户1,2发出客户名和头像ID号;
  90. pServerLink1->Send(pLeftPlayer,sizeof(CPlayerInfo));
  91. pServerLink1->Send(pCenterPlayer,sizeof(CPlayerInfo));
  92. pServerLink2->Send(pCenterPlayer,sizeof(CPlayerInfo));
  93. pServerLink2->Send(pRightPlayer,sizeof(CPlayerInfo));
  94. s_m_bOK = TRUE;
  95. return 1;
  96. }
  97. //客户端函数;
  98. DWORD WINAPI CDlg_Start::Client_Login(LPVOID pParam)
  99. {
  100. char net_info[100];
  101. //发送登陆信息到S
  102. pClientLink->Send(pCenterPlayer,sizeof(CPlayerInfo));
  103. //接收S连接信息;
  104. pClientLink->Recv(net_info,sizeof(char)*100);
  105. s_m_szNetInfo = net_info;
  106. ::Sleep(300);
  107. //再接收一次连接信息;
  108. pClientLink->Recv(net_info,sizeof(char)*100);
  109. s_m_szNetInfo = net_info;
  110. //接收另两位玩家信息;
  111. pClientLink->Recv(pRightPlayer,sizeof(CPlayerInfo));
  112. pClientLink->Recv(pLeftPlayer,sizeof(CPlayerInfo));
  113. s_m_bOK = TRUE;
  114. // CString str;
  115. // str.Format("左边玩家名字:%sn头像ID:%dnn右边玩家名字:%sn头像ID:%dn",
  116. // pLeft_Player->m_szName,pLeft_Player->m_nFaceID,pRight_Player->m_szName,pRight_Player->m_nFaceID);
  117. // AfxMessageBox(str);
  118. return 1;
  119. }
  120. CString CDlg_Start::s_m_szNetInfo = _T("");
  121. int CDlg_Start::s_m_bOK = FALSE;
  122. void CDlg_Start::OnStart() 
  123. {
  124. UpdateData(TRUE);
  125. if( m_Name == _T("") )
  126. {
  127. AfxMessageBox("请填入你的名字(昵称)!");
  128. GetDlgItem(EDIT_NAME)->SetFocus();
  129. return ;
  130. }
  131. GetDlgItem(IDB_START)->EnableWindow(FALSE);
  132. //玩家名字;
  133. sprintf(pCenterPlayer->m_szName,m_Name);
  134. //玩家选择的头像ID;
  135. pCenterPlayer->m_nFaceID = m_Face_ID;
  136. if( ((CButton* )GetDlgItem(RADIO_SINGLE))->GetCheck() )
  137. {
  138. pCenterPlayer->m_nPlayerType = 0; //类型0表示单人游戏;
  139. //随机初始化:
  140. pRightPlayer->m_nFaceID = rand()%4;
  141. sprintf(pRightPlayer->m_szName,"林江");
  142. pLeftPlayer->m_nFaceID = rand()%4;
  143. sprintf(pLeftPlayer->m_szName,"杨娱");
  144. CDialog::OnOK();
  145. }
  146. else
  147. {
  148. //服务器;
  149. if( ((CButton* )GetDlgItem(RADIO_SERVER))->GetCheck() )
  150. {
  151. pCenterPlayer->m_nPlayerType = 1; //类型1表示客户端;   
  152. pServerLink1 = new CLink;  //连接客户1;
  153. pServerLink2 = new CLink;  //连接客户2;
  154. m_hThread = ::CreateThread(NULL,0,Server_Login,0,0,&m_dwThreadId);
  155. SetTimer(1,200,NULL);  //用于更新的计时器;
  156. }
  157. //客户端;
  158. else
  159. {
  160. pCenterPlayer->m_nPlayerType = 2;   //类型2表示客户端;
  161. pClientLink = new CLink;
  162. pClientLink->SocketInit();
  163. pClientLink->Create();
  164. if( !pClientLink->Connect(m_Client) )
  165. {
  166. pClientLink->Close();
  167. delete pClientLink;
  168. GetDlgItem(IDB_START)->EnableWindow(TRUE);
  169. return ;
  170. }
  171.  
  172. m_hThread = ::CreateThread(NULL,0,Client_Login,0,0,&m_dwThreadId);
  173. SetTimer(1,200,NULL);  //用于更新的计时器; 
  174. }
  175. }//end if~
  176. }
  177. void CDlg_Start::OnExit() 
  178. {
  179. CDialog::OnCancel();
  180. }
  181. BOOL CDlg_Start::OnInitDialog() 
  182. {
  183. CDialog::OnInitDialog();
  184. ( (CButton* )GetDlgItem(RADIO_SINGLE) )->SetCheck(TRUE);
  185. ( (CButton* )GetDlgItem(RADIO_LINK) )->SetCheck(FALSE);
  186. //设置滚动条范围;
  187. CScrollBar* pSB = (CScrollBar*) GetDlgItem(SCROLLBAR_FACE);
  188. pSB->SetScrollRange(0, 3);
  189. m_cBitmap0.LoadBitmap("IDB_FACE0");
  190. m_cBitmap1.LoadBitmap("IDB_FACE1");
  191. m_cBitmap2.LoadBitmap("IDB_FACE2");
  192. m_cBitmap3.LoadBitmap("IDB_FACE3");
  193. SendDlgItemMessage(PIC_FACE,STM_SETIMAGE,IMAGE_BITMAP,LPARAM(m_cBitmap0.m_hObject) );
  194. CLink::GetHostIP();   //得到本机IP地址;
  195. pCenterPlayer = new CPlayerInfo;
  196. pLeftPlayer = new CPlayerInfo;
  197. pRightPlayer = new CPlayerInfo;
  198. GetDlgItem(EDIT_NAME)->SetFocus();
  199. return FALSE;             
  200. }
  201. //单人游戏;
  202. void CDlg_Start::OnSingle() 
  203. {
  204. ( (CButton* )GetDlgItem(RADIO_SINGLE) )->SetCheck(TRUE);
  205. ( (CButton* )GetDlgItem(RADIO_LINK) )->SetCheck(FALSE);
  206. GetDlgItem(RADIO_SERVER)->EnableWindow(FALSE);
  207. GetDlgItem(RADIO_CLIENT)->EnableWindow(FALSE);
  208. GetDlgItem(EDIT_SERVER)->EnableWindow(FALSE);
  209. GetDlgItem(EDIT_CLIENT)->EnableWindow(FALSE);
  210. }
  211. //多人对战;
  212. void CDlg_Start::OnLink() 
  213. {
  214. ( (CButton* )GetDlgItem(RADIO_SINGLE) )->SetCheck(FALSE);
  215. ( (CButton* )GetDlgItem(RADIO_LINK) )->SetCheck(TRUE);
  216. GetDlgItem(RADIO_SERVER)->EnableWindow();
  217. GetDlgItem(RADIO_CLIENT)->EnableWindow();
  218. GetDlgItem(EDIT_SERVER)->EnableWindow();
  219. GetDlgItem(EDIT_CLIENT)->EnableWindow();
  220. ( (CButton* )GetDlgItem(RADIO_SERVER) )->SetCheck(TRUE);
  221. ( (CButton* )GetDlgItem(RADIO_CLIENT) )->SetCheck(FALSE);
  222. m_ServerIP = "本机IP地址:";
  223. m_ServerIP += CLink::m_szIpaddress;
  224. UpdateData(FALSE);
  225. }
  226. //作子机(客户端);
  227. void CDlg_Start::OnClient() 
  228. {
  229. ( (CButton* )GetDlgItem(RADIO_SERVER) )->SetCheck(FALSE);
  230. ( (CButton* )GetDlgItem(RADIO_CLIENT) )->SetCheck(TRUE);
  231. GetDlgItem(EDIT_CLIENT)->SetFocus();
  232. }
  233. //本机作服务器;
  234. void CDlg_Start::OnServer() 
  235. {
  236. ( (CButton* )GetDlgItem(RADIO_SERVER) )->SetCheck(TRUE);
  237. ( (CButton* )GetDlgItem(RADIO_CLIENT) )->SetCheck(FALSE);
  238. }
  239. void CDlg_Start::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  240. {
  241. int nTemp1;
  242. nTemp1 = pScrollBar->GetScrollPos();
  243. switch(nSBCode)
  244. {
  245. case SB_LINELEFT: // left arrow button
  246. if(nTemp1 != 0)
  247. {
  248. pScrollBar->SetScrollPos(nTemp1-1);
  249. }
  250. break;
  251. case SB_LINERIGHT: // right arrow button
  252. if(nTemp1 < 3)
  253. {
  254. pScrollBar->SetScrollPos(nTemp1+1);
  255. }
  256. break;
  257. }
  258. switch(nTemp1)
  259. {
  260. case 0:
  261. SendDlgItemMessage(PIC_FACE,STM_SETIMAGE,IMAGE_BITMAP,LPARAM(m_cBitmap0.m_hObject) );
  262. break;
  263. case 1:
  264. SendDlgItemMessage(PIC_FACE,STM_SETIMAGE,IMAGE_BITMAP,LPARAM(m_cBitmap1.m_hObject) );
  265. break;
  266. case 2:
  267. SendDlgItemMessage(PIC_FACE,STM_SETIMAGE,IMAGE_BITMAP,LPARAM(m_cBitmap2.m_hObject) );
  268. break;
  269. case 3:
  270. SendDlgItemMessage(PIC_FACE,STM_SETIMAGE,IMAGE_BITMAP,LPARAM(m_cBitmap3.m_hObject) );
  271. break;
  272. }
  273. }
  274. void CDlg_Start::OnTimer(UINT nIDEvent) 
  275. {
  276. m_Net_Info = s_m_szNetInfo;
  277. UpdateData(FALSE);
  278. if( s_m_bOK )
  279. {
  280. KillTimer(1);
  281. CDialog::OnOK();
  282. }
  283. CDialog::OnTimer(nIDEvent);
  284. }
  285. void CDlg_Start::OnGameMaker() 
  286. {
  287. CDlg_About dlg;
  288. dlg.DoModal();
  289. }