📄 billiardsdoc.cpp
字号:
// BilliardsDoc.cpp : implementation of the CBilliardsDoc class
//
#include "stdafx.h"
#include "Billiards.h"
#include "BilliardsDoc.h"
#include "BilliardsFormView.h"
#include "BilliardsPlayView.h"
#include "ClientSocket.h"
#include "MainFrm.h"
#include <Mmsystem.h>
#include "PropertyList.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBilliardsDoc
IMPLEMENT_DYNCREATE(CBilliardsDoc, CDocument)
BEGIN_MESSAGE_MAP(CBilliardsDoc, CDocument)
//{{AFX_MSG_MAP(CBilliardsDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBilliardsDoc construction/destruction
CBilliardsDoc::CBilliardsDoc()
{
m_pListenSocket=new CListenSocket(this);
m_pClientSocket=new CClientSocket(this);
m_bPlayer=IDLE;
}
CBilliardsDoc::~CBilliardsDoc()
{
}
BOOL CBilliardsDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CBilliardsDoc serialization
void CBilliardsDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CBilliardsDoc diagnostics
#ifdef _DEBUG
void CBilliardsDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CBilliardsDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBilliardsDoc commands
BOOL CBilliardsDoc::AcceptConnection()
{
if(!m_pListenSocket->Accept(*m_pClientSocket)){
AfxMessageBox("接受连接失败!");
return 0;
}
CString strTemp;
strTemp="0"+m_strServer;
m_pClientSocket->Send(strTemp,strTemp.GetLength());
return 1;
}
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':
strTemp.Delete(0);
if(m_bPlayer==SERVER){
pFormView->GetDlgItem(IDC_STATIC_CLIENT)->SetWindowText(strTemp);
pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
//pFormView->m_ctlListMSG.AddItem("新用户加入游戏");
m_strClient=strTemp;
sndPlaySound("res\\notify.wav",SND_ASYNC);
}
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("res\\ringin.wav",SND_ASYNC);
}
else{
pItem=new CPropertyItem(m_strServer,strTemp,PIT_EDIT,"");
pFormView->m_ctlListMSG.AddPropItem(pItem);
if(pView->m_bSoundMsg)
sndPlaySound("res\\ringin.wav",SND_ASYNC);
}
pItem=NULL;
break;
case '3':
if(m_bPlayer==SERVER){
AfxMessageBox("客户端已离开游戏",MB_OK);
this->m_pClientSocket->ShutDown();
this->m_pClientSocket->Close();
pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
}
else{
AfxMessageBox("服务器端已离开游戏",MB_OK);
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;
}
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();
}
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));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -