📄 childview.cpp
字号:
CString dS;
CSameBallApp* pApp;
pApp=(CSameBallApp*)AfxGetApp();
((CMainFrame*)pApp->m_pMainWnd)->ShowStatus(0,nS);
}
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
int iResult;
int iSeleted,iScoreCount,iRemainBalls;
m_bNeedPaint=true;
point.x=point.x/m_iObjSize;
point.y=point.y/m_iObjSize;
iResult=m_dMap.KillBall(point.y,point.x,iScoreCount,iRemainBalls);
if(iResult==KILLBALL_ERROR || iResult==KILLBALL_NOT)
return;
PlaySoundEffect(SNDEFF_KILL);
ShowStatus(-1,iScoreCount,iRemainBalls);
if(iResult==KILLBALL_OK)
{
iResult=m_dMap.MouseOn(m_dPointMouseAt.y,m_dPointMouseAt.x,iSeleted);
ASSERT(iResult!=MOUSEON_ERROR);
if(iResult==MOUSEON_SUCCESS)
{
ShowStatus(iSeleted);
ShowMessage("点击左键以消除此块");
}
if(iResult==MOUSEON_NOTSEL)
{
ShowStatus(0);
ShowMessage("移动鼠标以选择相同的球");
}
return;
}
ShowMessage("游戏结束。");
OnTimer(1);
PlaySoundEffect(SNDEFF_GAMEOVER);
GameOver(iScoreCount);
OnFileNew();
iResult=m_dMap.MouseOn(m_dPointMouseAt.y,m_dPointMouseAt.x,iSeleted);
ASSERT(iResult!=MOUSEON_ERROR);
if(iResult==MOUSEON_SUCCESS)
{
ShowStatus(iSeleted);
ShowMessage("点击左键以消除此块");
}
if(iResult==MOUSEON_NOTSEL)
{
ShowStatus(0);
ShowMessage("移动鼠标以选择相同的球");
}
// CWnd ::OnLButtonDown(nFlags, point);
}
void CChildView::OnTimer(UINT nIDEvent)
{
if(nIDEvent!=1)
return;
CPoint dPoint;
CRect ddtRect;
CRect drcRect;
CRect* prcRect;
MapNode* pNode;
HRESULT ddrval;
dPoint.x=dPoint.y=0;
ClientToScreen(&dPoint);
if(m_bNeedPaint) // 绘制静态区
{
m_iActivePage=0;
m_bNeedPaint=false;
m_dMap.BeginGetStatic();
pNode=m_dMap.GetNextStatic();
while(pNode)
{
// source area
if(pNode->iGroupNumber<0)
{
prcRect=&m_dBlack;
}
else
switch(pNode->pBall->iClr)
{
case 0:
prcRect=&m_dRed;
break;
case 1:
prcRect=&m_dGreen;
break;
case 2:
prcRect=&m_dBlue;
}
// destination area
ddtRect.CopyRect(&pNode->dRect);
ddtRect.OffsetRect(dPoint);
// paint
while( 1 )
{
ddrval = m_lpDDSPrimary->Blt(&ddtRect, m_lpDDSSource, prcRect, 0, NULL );
if( ddrval == DD_OK )
break;
if( ddrval == DDERR_SURFACELOST )
if(!m_lpDDSPrimary->Restore() == DD_OK &&
m_lpDDSSource->Restore() == DD_OK &&
ReLoadDDBitmap(m_uBmpID,m_lpDDSSource) == DD_OK)
break;
if( ddrval != DDERR_WASSTILLDRAWING )
break;
}
// get next node
pNode=m_dMap.GetNextStatic();
}
//
}
if(m_bIsActive) // 绘制动态区
{
m_dMap.BeginGetActive();
pNode=m_dMap.GetNextActive();
if(!pNode || !pNode->pBall)
return;
// source area
if(++m_iActivePage>10)
{
if(m_iActivePage>20)
m_iActivePage=11;
drcRect.left=(m_iActivePage-11)*m_iObjSize;
drcRect.right=drcRect.left+m_iObjSize;
drcRect.top=(pNode->pBall->iClr+3)*m_iObjSize;
drcRect.bottom=drcRect.top+m_iObjSize;
}
else
{
drcRect.left=(m_iActivePage)*m_iObjSize;
drcRect.right=drcRect.left+m_iObjSize;
drcRect.top=pNode->pBall->iClr*m_iObjSize;
drcRect.bottom=drcRect.top+m_iObjSize;
}
while(pNode)
{
// destination area
ddtRect.CopyRect(&pNode->dRect);
ddtRect.OffsetRect(dPoint);
// paint
while( 1 )
{
ddrval = m_lpDDSPrimary->Blt(&ddtRect, m_lpDDSSource, drcRect, 0, NULL );
if( ddrval == DD_OK )
break;
if( ddrval == DDERR_SURFACELOST )
if(!m_lpDDSPrimary->Restore() == DD_OK &&
m_lpDDSSource->Restore() == DD_OK &&
ReLoadDDBitmap(m_uBmpID,m_lpDDSSource) == DD_OK)
break;
if( ddrval != DDERR_WASSTILLDRAWING )
break;
}
// get next node
pNode=m_dMap.GetNextActive();
}
}
}
void CChildView::OnEditUndo()
{
int iResult,iSeleted,iScoreCount,iRemain;
if(m_dMap.OnUndo(iScoreCount,iRemain)==ONUNDO_ERROR)
return;
iResult=m_dMap.MouseOn(m_dPointMouseAt.y,m_dPointMouseAt.x,iSeleted);
ASSERT(iResult!=MOUSEON_ERROR);
if(iResult==MOUSEON_SUCCESS)
{
ShowStatus(iSeleted,iScoreCount,iRemain);
ShowMessage("点击左键以消除此块");
}
if(iResult==MOUSEON_NOTSEL)
{
ShowStatus(0,iScoreCount,iRemain);
ShowMessage("移动鼠标以选择相同的球");
}
m_bNeedPaint=true;
}
void CChildView::OnUpdateEditUndo(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_dMap.bCanUndo);
}
void CChildView::GameOver(int ndCurrentScore)
{
int i,j,k;
BOOL bOnList=false;
int dId=NULL;
CString sB;
CString asName[5];
CNameInDlg dlg;
int adScore[5];
m_bIsActive=false;
for(i=0,j=1;i<5;i++)
{
if(!((CSameBallApp*)AfxGetApp())->GetScoreListItem(j,sB,k))
if((sB==_T("Error")) && i==0)
{
dlg.SetWindowText("排行榜数据出错!请将你的名字输入");
dlg.DoModal();
asName[0]=dlg.m_sName;
adScore[0]=ndCurrentScore;
for(k=1;k<5;k++)
{
asName[k]=_T("Noname");
adScore[k]=0;
}
break ;
}
if(k>=ndCurrentScore)
{
asName[i]=sB;
adScore[i]=k;
j++;
}
else
{
// get input
dlg.DoModal();
asName[i]=dlg.m_sName;
adScore[i]=ndCurrentScore;
ndCurrentScore=0;
bOnList=true;
switch(i+1)
{
case 1: dId=IDC_NAME1; break;
case 2: dId=IDC_NAME2; break;
case 3: dId=IDC_NAME3; break;
case 4: dId=IDC_NAME4; break;
case 5: dId=IDC_NAME5; break;
}
}
if(j>5 && i<4)
{
asName[4]=_T("Noname");
adScore[4]=0;
break;
}
}
if(bOnList)
for(i=0;i<5;i++)
{
if(!((CSameBallApp*)AfxGetApp())->SetScoreListItem(i+1,asName[i],adScore[i]))
((CSameBallApp*)AfxGetApp())->SetScoreListItem(i+1,"NewPlayer",adScore[i]);
}
CScoreList sdlg;
if(bOnList)
{
sdlg.m_dCurrentId=dId;
if(dId==IDC_NAME1)
sdlg.m_sMessage=_T("恭喜你荣登榜首!");
else
sdlg.m_sMessage=_T("恭喜你上榜!");
}
else
{
sdlg.m_dCurrentId=0;
sdlg.m_sMessage=_T("很遗憾,你未能上榜!");
}
sdlg.m_sName1=asName[0];
sdlg.m_sScore1.Format("%4d",adScore[0]);
sdlg.m_sName2=asName[1];
sdlg.m_sScore2.Format("%4d",adScore[1]);
sdlg.m_sName3=asName[2];
sdlg.m_sScore3.Format("%4d",adScore[2]);
sdlg.m_sName4=asName[3];
sdlg.m_sScore4.Format("%4d",adScore[3]);
sdlg.m_sName5=asName[4];
sdlg.m_sScore5.Format("%4d",adScore[4]);
sdlg.DoModal();
ShowStatus(-1,0,150);
m_bIsActive=true;
}
void CChildView::OnViewScorelist()
{
int i,j,k;
// BOOL bOnList=false;
CString sB;
CString asName[5];
CScoreList sdlg;
int adScore[5];
m_bIsActive=false;
for(i=0,j=1;i<5;i++,j++)
{
if(!((CSameBallApp*)AfxGetApp())->GetScoreListItem(j,sB,k))
if((sB==_T("Error")) && i==0)
{
MessageBox("排行榜出错!");
return ;
}
asName[i]=sB;
adScore[i]=k;
}
sdlg.m_dCurrentId=0;
sdlg.m_sName1=asName[0];
sdlg.m_sScore1.Format("%4d",adScore[0]);
sdlg.m_sName2=asName[1];
sdlg.m_sScore2.Format("%4d",adScore[1]);
sdlg.m_sName3=asName[2];
sdlg.m_sScore3.Format("%4d",adScore[2]);
sdlg.m_sName4=asName[3];
sdlg.m_sScore4.Format("%4d",adScore[3]);
sdlg.m_sName5=asName[4];
sdlg.m_sScore5.Format("%4d",adScore[4]);
sdlg.DoModal();
m_bIsActive=true;
}
void CChildView::FreeDD()
{
if(m_lpDDPalette)
{
m_lpDDPalette->Release();
m_lpDDPalette=NULL;
}
if(m_lpDDSPrimary)
{
m_lpDDSPrimary->Release();
m_lpDDSPrimary=NULL;
}
if(m_lpDDSSource)
{
m_lpDDSSource->Release();
m_lpDDSSource=NULL;
}
if(m_pIDirectDraw)
{
m_pIDirectDraw->Release();
m_pIDirectDraw=NULL;
}
}
void CChildView::FreeDS()
{
DWORD dStatus;
for(int i=0;i<SNDEFF_COUNT;i++)
{
if(m_grlpSoundEffect[i])
{
dStatus=m_grlpSoundEffect[i]->GetStatus(&dStatus);
if((dStatus & DSBSTATUS_LOOPING) ||(dStatus & DSBSTATUS_PLAYING))
m_grlpSoundEffect[i]->Stop();
m_grlpSoundEffect[i]->Release();
m_grlpSoundEffect[i]=NULL;
}
}
}
BOOL CChildView::InitDS()
{
// Create the DS object
if (DirectSoundCreate(NULL, &m_lpDirectSound, NULL) != DS_OK) {
MessageBox("Could not create DirectSound object!",
"DirectSound Error");
return FALSE;
}
// Set the cooperation level for the DS object
if (m_lpDirectSound->SetCooperativeLevel(m_hWnd,//GetSafeHwnd(),
DSSCL_NORMAL) != DS_OK) {
MessageBox("Could not set cooperative level!",
"DirectSound Error");
return FALSE;
}
// Initialize the DS buffers
if (!InitBuffers()) {
MessageBox("Could not initialize DirectSound buffers!",
"DirectSound Error");
return FALSE;
}
return true;
}
BOOL CChildView::InitBuffers()
{
// Initialize waves
CWave waves[SNDEFF_COUNT];
if(!waves[SNDEFF_MUSIC].Create(IDW_MUSIC))
return false;
if(!waves[SNDEFF_KILL].Create(IDW_KILL))
return false;
if(!waves[SNDEFF_ACTIVE].Create(IDW_ACTIVE))
return false;
if(!waves[SNDEFF_GAMEOVER].Create(IDW_GAMEOVER))
return false;
// Initialize DS buffers
for (int i = 0; i < SNDEFF_COUNT; i++)
{
// Get the wave information
DWORD dwDataLen = waves[i].GetDataLen();
WAVEFORMATEX wfFormat;
waves[i].GetFormat(wfFormat);
// Setup the DS buffer description
DSBUFFERDESC dsbdDesc;
ZeroMemory(&dsbdDesc, sizeof(DSBUFFERDESC));
dsbdDesc.dwSize = sizeof(DSBUFFERDESC);
dsbdDesc.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_STATIC;
dsbdDesc.dwBufferBytes = dwDataLen;
dsbdDesc.lpwfxFormat = &wfFormat;
// Create the DS buffer
if (m_lpDirectSound->CreateSoundBuffer(&dsbdDesc,
&m_grlpSoundEffect[i], NULL) != DS_OK)
return FALSE;
// Lock the DS buffer
BYTE* pDSBuffData;
if (m_grlpSoundEffect[i]->Lock(0, dwDataLen, (LPLPVOID)&pDSBuffData,
&dwDataLen, NULL, 0, 0) != DS_OK)
return FALSE;
// Write wave data to the DS buffer
dwDataLen = waves[i].GetData(pDSBuffData, dwDataLen);
// Unlock the DS buffer
if (m_grlpSoundEffect[i]->Unlock(pDSBuffData, dwDataLen, NULL, 0) !=
DS_OK)
return FALSE;
}
return TRUE;
}
BOOL CChildView::RestoreDSBuffers()
{
// Restore the buffers
for(int i=0;i<SNDEFF_COUNT;i++)
{
if (m_grlpSoundEffect[i]->Restore() != DS_OK)
return FALSE;
}
// Re-initialize the buffers
return InitBuffers();
}
void CChildView::PlaySoundEffect(int nSound,DWORD nFlag)
{
DWORD dStatus;
if(!m_grlpSoundEffect[nSound])
return;
dStatus=m_grlpSoundEffect[nSound]->GetStatus(&dStatus);
if((dStatus & DSBSTATUS_LOOPING) ||(dStatus & DSBSTATUS_PLAYING))
{
if(m_grlpSoundEffect[nSound]->Stop()!=DS_OK)
OutMg("Can't stop playing!");
}
m_grlpSoundEffect[nSound]->Play(0,0,nFlag);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -