当前位置:首页 > 网络台球游戏的网络编程(vc++)
数,用于处理控制消息。 ReceiveVelocity 函数定义如下。
void CBilliardsDoc::ReceiveVelocity(char *pBuf) {
Velocity *v=new Velocity; v=(Velocity *)pBuf;
CBilliardsPlayView* pPlayView;
pPlayView=(CBilliardsPlayView *)((CMainFrame *)AfxGetMainWnd())->m_wndSplitter. GetPane(0,0);
pPlayView->m_fVelocity[0][0]=v->fvalue0; pPlayView->m_fVelocity[0][1]=v->fvalue1; pPlayView->m_fVelocity[0][2]=v->fvalue2; while (pPlayView->m_bAnimate);
pPlayView->HitBall(); //调用视图类的碰撞算法 248 Visual C++游戏开发技术与实例 }
ReceiveMSG 函数定义如下。
BOOL CBilliardsDoc::ReceiveMSG(CString strTemp) {
CBilliardsFormView* pFormView;
CMainFrame *pView=(CMainFrame *)(AfxGetMainWnd()); CPropertyItem *pItem=NULL; POSITION pos;
for(pos=GetFirstViewPosition();pos!=NULL;) {
CView* pView = GetNextView(pos);
pFormView= DYNAMIC_DOWNCAST(CBilliardsFormView, pView);
if (pFormView != NULL) break; }
switch(strTemp.GetAt(0)){ case ’0’: //Login 消息 strTemp.Delete(0);
if(m_bPlayer==SERVER){
//如果是服务器,则表示有客户上站消息
pFormView->GetDlgItem(IDC_STATIC_CLIENT)->SetWindowText(strTemp);
pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
m_strClient=strTemp;
sndPlaySound(\}
else{//如果是客户机,则表示成功连接服务器
pFormView->GetDlgItem(IDC_STATIC_SERVER)->SetWindowText(strTemp);
pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
m_strServer=strTemp; }
break;
case ’1’: //聊天消息
strTemp.Delete(0);
if(m_bPlayer==SERVER){ pItem=new
CPropertyItem(m_strClient,strTemp,PIT_EDIT,\pFormView->m_ctlListMSG.AddPropItem(pItem); if(pView->m_bSoundMsg)
sndPlaySound(\}
else{
pItem=new
CPropertyItem(m_strServer,strTemp,PIT_EDIT,\pFormView->m_ctlListMSG.AddPropItem(pItem); if(pView->m_bSoundMsg)
sndPlaySound(\}
pItem=NULL; break;
第4 章网络游戏开发249 case ’3’: //Logout 消息 if(m_bPlayer==SERVER){
AfxMessageBox(\客户端已离开游戏\this->m_pClientSocket->ShutDown(); this->m_pClientSocket->Close();
pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE); }
else{
AfxMessageBox(\服务器端已离开游戏\
this->m_pClientSocket->ShutDown(); this->m_pClientSocket->Close(); ((CMainFrame
*)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl() .HideButton(ID_NEWGAME,FALSE); ((CMainFrame
*)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl() .HideButton(ID_JOINGAME,FALSE); ((CMainFrame
*)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl() .HideButton(ID_STOPGAME); ((CMainFrame
*)AfxGetMainWnd())->GetMenu()->GetSubMenu(0) ->EnableMenuItem(ID_NEWGAME,MF_ENABLED); ((CMainFrame
*)AfxGetMainWnd())->GetMenu()->GetSubMenu(0) ->EnableMenuItem(ID_JOINGAME,MF_ENABLED); ((CMainFrame
*)AfxGetMainWnd())->GetMenu()->GetSubMenu(0) ->EnableMenuItem(ID_STOPGAME,MF_GRAYED);
pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE); }
break; default: break; }
return 1; }
CListenSocket 也继承自CSocket,它是服务器端的连接监听Socket,所以需要重载OnAccept 以获
得网络连接消息。这也和前面介绍的CSocket 使用方法差别不大。 CListenSocket::CListenSocket(CBilliardsDoc* pDoc) {
m_pListenDoc=pDoc;
}
CListenSocket::~CListenSocket() { }
//接收连接的消息响应函数
void CListenSocket::OnAccept(int nErrorCode) {
//当有连接到来的时候,调用文档类的AcceptConnection 函数处理连接
if(nErrorCode==0)
m_pListenDoc->AcceptConnection(); }
250 Visual C++游戏开发技术与实例
OnAccept 函数使用了文档类函数AcceptConnection 处理连接。AcceptConnection 定义如下:
BOOL CBilliardsDoc::AcceptConnection() {
if(!m_pListenSocket->Accept(*m_pClientSocket)){ AfxMessageBox(\接受连接失败!\return 0; }
//向客户端发送成功登陆消息
CString strTemp;
strTemp=\
m_pClientSocket->Send(strTemp,strTemp.GetLength());
return 1; }
在CBilliardsDoc 中,函数SendVelocity 是用来发送击球位置的,击球消息的控制码是2。
void CBilliardsDoc::SendVelocity(float m_fVelocity[3]) {
Velocity v;
v.type=’2’;
v.fvalue0=m_fVelocity[0]; v.fvalue1=m_fVelocity[1]; v.fvalue2=m_fVelocity[2];
m_pClientSocket->Send(&v,sizeof(v)); }
共分享92篇相关文档