📄 childview.cpp
字号:
// ChildView.cpp : implementation of the CChildView class
// 编码: Leezy
// 最后修改日期:5.9
//
#include "stdafx.h"
#include "SameBall.h"
#include "Ball.h"
#include "MapNode.h"
#include "SameBallMap.h"
#include "ChildView.h"
#include "mainfrm.h"
#include "NameInDlg.h"
#include "ScoreList.h"
#include "wave.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#ifdef _DEBUG
#define OutMg(x) MessageBox(x);
#else
#define OutMg(x) ;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChildView
CChildView::CChildView()
{
for(int i=0;i<SNDEFF_COUNT;i++)
m_grlpSoundEffect[i]=NULL;
m_lpDirectSound=NULL;
m_pIDirectDraw=NULL;
m_lpDDSPrimary=NULL;
m_lpDDSSource=NULL;
m_lpDDPalette=NULL;
m_dPointMouseAt.x=-1;
m_dPointMouseAt.y=-1;
m_bIsActive=true;
}
CChildView::~CChildView()
{
}
BEGIN_MESSAGE_MAP(CChildView,CWnd )
//{{AFX_MSG_MAP(CChildView)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_WM_TIMER()
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
ON_COMMAND(ID_VIEW_SCORELIST, OnViewScorelist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers
BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;
HBRUSH hBrush;
hBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), hBrush, NULL);
return TRUE;
}
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
m_bNeedPaint=true;
// Do not call CWnd::OnPaint() for painting messages
}
BOOL CChildView::InitDD()
{
// 初始化目标Rect
m_dMap.SetSize(m_iObjSize);
// 计算静态源图位置
m_dRed.left=0;
m_dRed.top=0;
m_dRed.right=m_iObjSize-1;
m_dRed.bottom=m_iObjSize-1;
m_dGreen.CopyRect(m_dRed);
m_dGreen.OffsetRect(0,m_iObjSize);
m_dBlue.CopyRect(m_dGreen);
m_dBlue.OffsetRect(0,m_iObjSize);
m_dBlack.CopyRect(m_dBlue);
m_dBlack.OffsetRect(0,m_iObjSize*4);
m_iActivePage=0;
HRESULT hr;
hr = DirectDrawCreate(NULL,&m_pIDirectDraw,NULL);
if FAILED( hr )
{
MessageBox("DirectDraw 初始化错!\nDirectDrawCreate","Line:65");
} // if
// Set our cooperative level to be windowed...
hr = m_pIDirectDraw->SetCooperativeLevel(m_hWnd,
DDSCL_NORMAL);
if FAILED( hr )
{
MessageBox("DirectDraw 初始化错!\nSetCooperativeLevel","Line:73");
} // if
// Create our primary surface
DDSURFACEDESC ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = m_pIDirectDraw->CreateSurface(&ddsd,&m_lpDDSPrimary,NULL);
if ( FAILED( hr ) )
{
MessageBox("DirectDraw 初始化错!\nCreateSurface","Line:86");
} // if
// Since we're windowed, we need to create a clipper
// and attach it to the primary surface.
IDirectDrawClipper* lpDDClipper;
hr = m_pIDirectDraw->CreateClipper(0,&lpDDClipper,NULL);
if ( FAILED( hr ) ) {
MessageBox("DirectDraw 初始化错!\nCreateClipper","Line:94");
} // if
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
ASSERT(pFrame);
hr = lpDDClipper->SetHWnd(0,pFrame->GetSafeHwnd());
if ( FAILED( hr ) )
{
MessageBox("DirectDraw 初始化错!\nSetHWnd","Line:101");
} // if
hr = m_lpDDSPrimary->SetClipper(lpDDClipper);
if ( FAILED( hr ) )
{
MessageBox("DirectDraw 初始化错!\nSetClipper","Line:107");
} // if
// Check the surface format...will we need a palette?
DDPIXELFORMAT ddpf;
ZeroMemory(&ddpf,sizeof(ddpf));
ddpf.dwSize = sizeof(ddpf);
hr = m_lpDDSPrimary->GetPixelFormat(&ddpf);
if ( FAILED( hr ) )
{
MessageBox("DirectDraw 初始化错!\nGetPixelFormat","Line:117");
} // if
if ( ddpf.dwFlags & DDPF_PALETTEINDEXED8 ) {
// We are palettized, so create a palette and attach it to
// the primary surface.
hr = LoadDDPalette(IDB_BALL64,&m_lpDDPalette);
if ( FAILED( hr ) )
{
MessageBox("DirectDraw 初始化错!\nLoadDDPalette","Line:126");
} // if
m_lpDDSPrimary->SetPalette(m_lpDDPalette);
if ( FAILED( hr ) )
{
MessageBox("DirectDraw 初始化错!\nSetPalette","Line:132");
} // if
} // if
// Load our table bitmap
hr = LoadDDBitmap(m_uBmpID,&m_lpDDSSource);
lpDDClipper->Release();
if(SUCCEEDED(hr))
{
// m_iDrawMode=DRAWMODE_DIRECTDRAW;
return true;
}
else
{
MessageBox("DirectDraw 调用失败!\n程序无法继续,对不起!","error");
// m_iDrawMode=DRAWMODE_WINGDI;
PostMessage(WM_QUIT); // will be removed
return false;
}
}
HRESULT CChildView::CreateSecondarySurface(int cx, int cy, IDirectDrawSurface **ppDDS)
{
// Check our pointer
ASSERT(ppDDS != NULL);
if ( ppDDS == NULL ) {
// NULL input pointer...
return E_POINTER;
} // if
// Complete the surface description structure
DDSURFACEDESC ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = cx;
ddsd.dwHeight = cy;
HRESULT hr = m_pIDirectDraw->CreateSurface(&ddsd,ppDDS,NULL);//&pIDDSTemp,NULL);
if ( FAILED(hr) ) {
// Some error
*ppDDS = NULL;
return hr;
} // if
return hr;
}
HRESULT CChildView::LoadDDBitmap(UINT nID, IDirectDrawSurface **ppDDS)
{
ASSERT(ppDDS != NULL);
if ( ppDDS == NULL ) {
// NULL input pointer...
return E_POINTER;
} // if
// Pull the bitmap from resources
CBitmap bmImage;
VERIFY(bmImage.LoadBitmap(nID));
// Determine its size
BITMAP bm;
bmImage.GetBitmap(&bm);
// Create a surface to contain the bitmap
HRESULT hr = CreateSecondarySurface(bm.bmWidth,bm.bmHeight,ppDDS);//&pIDDSTemp);
if ( FAILED(hr) ) {
// Some error
*ppDDS = NULL;
return hr;
} // if
// Copy the bitmap to the surface
hr = CopyDDBitmap(&bmImage,*ppDDS);//pIDDSTemp);
if ( FAILED(hr) ) {
// Some error
*ppDDS = NULL;
return hr;
} // if
return hr;
}
HRESULT CChildView::CopyDDBitmap(CBitmap *pbmImage, IDirectDrawSurface *pDDS)
{
// Restore the surface
pDDS->Restore();
// Create a memory DC to contain the bitmap
CDC dcImage;
dcImage.CreateCompatibleDC(NULL);
CBitmap* pBitmapMono = (CBitmap*)dcImage.SelectObject(pbmImage);
// Determine its size
BITMAP bm;
pbmImage->GetBitmap(&bm);
// Complete the surface description
DDSURFACEDESC ddsd;
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
pDDS->GetSurfaceDesc(&ddsd);
// Copy in the bitmap
HDC hdc = NULL;
HRESULT hr = pDDS->GetDC(&hdc);
if ( SUCCEEDED(hr) ) {
::StretchBlt(hdc,0,0,ddsd.dwWidth,ddsd.dwHeight,dcImage,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
pDDS->ReleaseDC(hdc);
} // if
// Clean up
dcImage.SelectObject(pBitmapMono);
return hr;
}
HRESULT CChildView::ReLoadDDBitmap(UINT nID, IDirectDrawSurface *pDDS)
{
// Pull the bitmap from resources
CBitmap bmImage;
VERIFY(bmImage.LoadBitmap(nID));
// Copy the bitmap back to the surface
return CopyDDBitmap(&bmImage,pDDS);
}
HRESULT CChildView::LoadDDPalette(UINT idBitmap, IDirectDrawPalette **ppDDP)
{
PALETTEENTRY ape[256];
for ( int i = 0; i < 256; i++ ) {
ape[i].peRed = (BYTE)(((i >> 5) & 0x07) * 255 / 7);
ape[i].peGreen = (BYTE)(((i >> 2) & 0x07) * 255 / 7);
ape[i].peBlue = (BYTE)(((i >> 0) & 0x03) * 255 / 3);
ape[i].peFlags = (BYTE)0;
} // for
HRSRC hBitmap = NULL;
if ( (hBitmap = ::FindResource(NULL,MAKEINTRESOURCE(idBitmap),RT_BITMAP))) {
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)::LockResource(::LoadResource(NULL,hBitmap));
if ( lpbi != NULL ) {
RGBQUAD* prgb = (RGBQUAD*)((BYTE*)lpbi + lpbi->biSize);
int iNumColors;
if (lpbi == NULL || lpbi->biSize < sizeof(BITMAPINFOHEADER))
iNumColors = 0;
else if (lpbi->biBitCount > 8)
iNumColors = 0;
else if (lpbi->biClrUsed == 0)
iNumColors = 1 << lpbi->biBitCount;
else
iNumColors = lpbi->biClrUsed;
// Pull the color information
for( i = 0; i < iNumColors; i++ ) {
ape[i].peRed = prgb[i].rgbRed;
ape[i].peGreen = prgb[i].rgbGreen;
ape[i].peBlue = prgb[i].rgbBlue;
ape[i].peFlags = 0;
} // for
} // if
} // if
HRESULT hr = m_pIDirectDraw->CreatePalette(DDPCAPS_8BIT,ape,ppDDP,NULL);//&pIDDPTemp,NULL);
if ( FAILED(hr) ) {
// Some error
*ppDDP = NULL;
return hr;
} // if
return hr;
}
BOOL CChildView::DestroyWindow()
{
FreeDD();
FreeDS();
KillTimer(1);
return CWnd ::DestroyWindow();
}
int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd ::OnCreate(lpCreateStruct) == -1)
return -1;
if(InitDD())
OutMg("InitDD OK");
if(InitDS())
OutMg("InitDS OK");
PlaySoundEffect(SNDEFF_MUSIC,DSBPLAY_LOOPING);
m_dMap.ReNew(); // modified by leezy May.20
m_bNeedPaint=true;
SetTimer(1,60,NULL);
return 0;
}
void CChildView::OnFileNew()
{
m_dMap.ReNew();
m_bNeedPaint=true;
ShowStatus(-1,0,150); // add by leezy May.20
ShowMessage("新游戏");
}
void CChildView::ShowStatus(int nBallCount, int nScore, int nRemain)
{
CString dS;
CSameBallApp* pApp;
pApp=(CSameBallApp*)AfxGetApp();
if(nBallCount>=0)
{
dS.Format("选定: %5d",nBallCount);
((CMainFrame*)pApp->m_pMainWnd)->ShowStatus(1,dS);
}
if(nBallCount>=2)
{
nBallCount-=2;
dS.Format("分值: %5d",nBallCount*nBallCount);
((CMainFrame*)pApp->m_pMainWnd)->ShowStatus(2,dS);
}
else
{
dS.Format("分值: %5d",0);
((CMainFrame*)pApp->m_pMainWnd)->ShowStatus(2,dS);
}
if(nScore>=0)
{
dS.Format("已得分: %5d",nScore);
((CMainFrame*)pApp->m_pMainWnd)->ShowStatus(3,dS);
}
if(nRemain>=0)
{
dS.Format("剩余: %5d",nRemain);
((CMainFrame*)pApp->m_pMainWnd)->ShowStatus(4,dS);
}
}
void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
int ti,iSeleted;
if(point.x==0 || point.y==0)
{
ShowMessage("移动鼠标以选择相同的球");
ShowStatus(0);
m_dMap.MouseOn(-1,-1,iSeleted);
return;
}
point.x=point.x/m_iObjSize;
point.y=point.y/m_iObjSize;
if(point==m_dPointMouseAt)
return;
ti=m_dMap.MouseOn(point.y,point.x,iSeleted);
ASSERT(ti!=MOUSEON_ERROR);
if(ti==MOUSEON_SUCCESS)
{
PlaySoundEffect(SNDEFF_ACTIVE);
m_bNeedPaint=true;
ShowStatus(iSeleted);
ShowMessage("点击左键以消除此块");
}
if(ti==MOUSEON_NOTSEL)
{
m_bNeedPaint=true;
ShowStatus(0);
ShowMessage("移动鼠标以选择相同的球");
}
m_dPointMouseAt.x=point.x;
m_dPointMouseAt.y=point.y;
// CWnd ::OnMouseMove(nFlags, point);
}
void CChildView::ShowMessage(CString nS)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -