📄 bubbleiidlg.cpp
字号:
// BubbleIIDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GLB.h"
#include "BubbleII.h"
#include "BubbleIIDlg.h"
#include <math.h>
#include "GhostThread.h"
#include "BubbleThread.h"
#include "windows.h"
#include "AboutDlg.h"
//#include "gdiplus.h"
//using namespace Gdiplus;
//ULONG_PTR gdiplusToken;
#define BMP_WIDTH 250 //位图宽度
#define BMP_HEIGHT 210 //位图高度
#define TIMER_START 1971 //定时器ID
#define GAME_TIMER 1972
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBubbleIIDlg dialog
CBubbleIIDlg::CBubbleIIDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBubbleIIDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBubbleIIDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
GLB::pBACKBMP->LoadBitmap(1);
m_bmpPlane=new CBitmap;
m_bmpPlane->LoadBitmap(IDB_BITMAP210);
// m_pAboutDlg=new CAboutDlg;
int i=0;
for(i=0; i<8; i++)
{
GLB::pSelf[i]=new CBitmap;
}
for(i=0; i<25; i++)
{
GLB::pBMPBackgrounds[i]=new CBitmap;
if(i<8)
GLB::pBMPBackgrounds[i]->LoadBitmap(10+i*10);
if(i>=10 && i<=16)
GLB::pBMPBackgrounds[i]->LoadBitmap(i);
//pBMPBackgrounds[20]~~pBMPBackgrounds[24]放置BUBBLE爆炸的5种火光
//20~23为4个方向的火光,24为中央的火光。
if(i>=20)
GLB::pBMPBackgrounds[i]->LoadBitmap(340+i);
}
for(i=0; i<30; i++)
{
GLB::pBMPGameover[i]=new CBitmap;
GLB::pBMPGameover[i]->LoadBitmap(470+i);
}
for(i=0; i<9; i++)
{
GLB::pBMPBounsA[i]=new CBitmap;
GLB::pBMPBounsA[i]->LoadBitmap(400+i);
}
for(i=0; i<30; i++)
{
GLB::pGhostThread[i]=new CGhostThread;
GLB::pBubbleThread[i]=new CBubbleThread;
GLB::bGhostsAlive[i]=FALSE;
GLB::bBubbleAlive[i]=FALSE;
}
for(i=0; i<2; i++)
{
m_U1key[i]=0;
m_U2key[i]=0;
}
m_iU1keyIndex=-1;
m_iU2keyIndex=-1;
}
CBubbleIIDlg::~CBubbleIIDlg()
{
// GdiplusShutdown(gdiplusToken);
GLB::bGameStarted=FALSE;
GLB::bHeightAlive=FALSE;
GLB::bRepaintThreadAlive=FALSE;
int i=0;
Sleep(20);
GLB::eGameType=GAMEOVER;
for(i=0; i<30; i++)
{
GLB::pGhostThread[i]->m_eState=GHOSTDEAD;
GLB::pBubbleThread[i]->m_state=DEAD;
}
Sleep(100);
for(i=0; i<30; i++)
{
delete GLB::pGhostThread[i];
delete GLB::pBubbleThread[i];
}
// delete GLB::pIBackMatrixBackup;
for(i=0; i<8; i++)
{
if(GLB::pSelf[i]!=NULL)
delete GLB::pSelf[i];
}
for(i=0; i<30; i++)
{
delete GLB::pBMPGameover[i];
}
for(i=0;i<9; i++)
{
delete GLB::pBMPBounsA[i];
}
for(i=0; i<20; i++)
{
delete GLB::pBMPBackgrounds[i];
}
delete pMenu;
delete m_bmpPlane;
// delete m_pPartMemDC;
delete GLB::pIBackMatrix;
delete GLB::pBACKBMPBackUp;
delete GLB::pBACKDC;
delete GLB::pBACKBMP;
delete GLB::pMemDC;
delete GLB::uBgBMP;
delete GLB::pMyWater;
delete GLB::pPartMemDC;
delete GLB::pMemBMP;
delete GLB::pAboutDlg;
}
void CBubbleIIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBubbleIIDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBubbleIIDlg, CDialog)
//{{AFX_MSG_MAP(CBubbleIIDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDM_NEWSINGLE, OnNewsingle)
ON_WM_TIMER()
ON_COMMAND(IDM_INTRODUCE, OnIntroduce)
ON_COMMAND(IDM_NEWMULTIFIGHT, OnNewMultifight)
ON_COMMAND(IDM_NEWCOOP, OnNewcoop)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBubbleIIDlg message handlers
BOOL CBubbleIIDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// CString savePath=dlgLoad.GetPathName();
MoveWindow(0,0,GLB::iWindowWidth+8,GLB::iWindowHeight+78);
// add the main menu
pMenu=new CMenu;
pMenu->LoadMenu(IDR_MAINMENU);
SetMenu(pMenu);
//装入位图
m_hrMyPicHolderLarge.CreateFromBitmap(1);
GLB::pMainDlg=(CBubbleIIDlg*)(AfxGetApp()->m_pMainWnd);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CBubbleIIDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (IsIconic())
{
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
if(!GLB::bInitiated)
{
MainInitiate();
GLB::bInitiated=TRUE;
}
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CBubbleIIDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBubbleIIDlg::RepaintIntroduce()
{
CDC *pDC=GetDC();
CPoint ptOrigin(0,0);
if(GLB::bHeightAlive)
m_bmpRenderTarget.Draw(pDC,ptOrigin);
else
{
//已经验证如果此时读入地图文件能够正确显示
pDC->BitBlt(0,0,600,422,GLB::pMemDC,0,0,SRCCOPY);
}
}
void CBubbleIIDlg::OnIntroduce()
{
WinExec("Notepad 游戏介绍.txt",SW_SHOW);
FinalWins();
// AfxBeginThread(AboutDlgHolder,NULL);
}
void CBubbleIIDlg::FinalWins()
{
GLB::bRepaintThreadAlive=FALSE;
Sleep(100);
CDC* pDC=GetDC();
KillTimer(GAME_TIMER);
/*
CBitmap * pTempMemBMP=new CBitmap;
pTempMemBMP->CreateCompatibleBitmap(pDC,GLB::iWindowWidth,GLB::iWindowHeight);
GLB::pMemDC->SelectObject(pTempMemBMP);
SetBkColor(GLB::pMemDC->m_hDC,RGB(175,175,175));
SetTextColor(GLB::pMemDC->m_hDC,RGB(0,255,0));
*/
if(!GLB::bWaterInited)
{
m_bmpRenderSource.Create32BitFromPicture(&m_hrMyPicHolderLarge,600,422);
m_bmpRenderTarget.Create32BitFromPicture(&m_hrMyPicHolderLarge,600,422);
//创建水纹对象
GLB::pMyWater->Create(600,422);
//初始化变量
m_bRunWater = TRUE;
GLB::bWaterInited=TRUE;
}
CRect rect(0,0,GLB::iWindowWidth,GLB::iWindowHeight);
CBrush graybrush(RGB(212,208,200));
pDC->FillRect(&rect,&graybrush);
m_iTimer = SetTimer(TIMER_START,10,NULL);
GLB::bHeightAlive=TRUE;
AfxBeginThread(HeightWater,NULL);
}
void CBubbleIIDlg::updownBMP(CBitmap *pBitmap)
{
GLB::pMemDC->SelectObject(pBitmap);
HBITMAP hBitmap=(HBITMAP)(*pBitmap);
::GetObject(hBitmap,sizeof(BITMAP),GLB::uBgBMP);
int width=GLB::uBgBMP->bmWidth, height=GLB::uBgBMP->bmHeight;
GLB::lBMPLength=width*height;
unsigned int* pInt=new unsigned int[GLB::lBMPLength];
GLB::pBACKBMP->GetBitmapBits(GLB::lBMPLength<<2,pInt);
int temp=0;
for(int i=0; i<width; i++)
{
for(int j=0; j<(height>>1); j++)
{
temp=*(pInt+j*width+i);
*(pInt+j*width+i)=*(pInt+(height-j-1)*width+i);
*(pInt+(height-j-1)*width+i)=temp;
}
}
GLB::pBACKBMP->SetBitmapBits(GLB::lBMPLength<<2,pInt);
delete pInt;
}
void CBubbleIIDlg::leftrightBMP(CBitmap *pBitmap)
{
GLB::pMemDC->SelectObject(pBitmap);
HBITMAP hBitmap=(HBITMAP)(*pBitmap);
::GetObject(hBitmap,sizeof(BITMAP),GLB::uBgBMP);
int width=GLB::uBgBMP->bmWidth, height=GLB::uBgBMP->bmHeight;
GLB::lBMPLength=width*height;
unsigned int* pInt=new unsigned int[GLB::lBMPLength];
GLB::pBACKBMP->GetBitmapBits(GLB::lBMPLength<<2,pInt);
int temp=0;
for(int j=0; j<height; j++)
{
for(int i=0; i<(width>>1); i++)
{
temp=*(pInt+j*width+i);
*(pInt+j*width+i)=*(pInt+(j+1)*width-i);
*(pInt+(j+1)*width-i)=temp;
}
}
GLB::pBACKBMP->SetBitmapBits(GLB::lBMPLength<<2,pInt);
}
BOOL CBubbleIIDlg::LoadMap()
{
CFile fin;
CString s;
s.Format("MAP\\a%.2d.map",GLB::iGameStage);
BOOL bSuccess=TRUE;
if(fin.Open(s,CFile::modeRead))
{
UINT i=fin.Read(GLB::pIBackMatrix,GLB::iXBlocks*GLB::iYBlocks*sizeof(int));
if(i==GLB::iXBlocks*GLB::iYBlocks*sizeof(int))
{
// memcpy(GLB::pIBackMatrixBackup,GLB::pIBackMatrix,GLB::iXBlocks*GLB::iYBlocks*sizeof(int));
CalcuBackInfo(GLB::pBMPBackgrounds[(GLB::iGameStage-1)%6+10]);
}
else
{
bSuccess=FALSE;
}
}
else
{
bSuccess=FALSE;
}
//如果无地图可读,则认为已经通关。
if(!bSuccess)
{
FinalWins();
}
else
{
if(GLB::eGameType!=GAMEOVER)
::PlaySound("SOUND\\CLEAR.WAV",NULL,SND_NODEFAULT|SND_ASYNC);
GLB::bStageWin=FALSE;
}
return bSuccess;
}
//DEL void CBubbleIIDlg::RepaintMemBackground()
//DEL {
//DEL //画第0图层:最底层的草地。
//DEL GLB::RepaintMemBG();
//DEL }
void CBubbleIIDlg::MainInitiate()
{
CDC* pDC=GetDC();
GLB::pMemDC->CreateCompatibleDC(pDC);
GLB::pMemBMP->CreateCompatibleBitmap(pDC,GLB::iWindowWidth,GLB::iWindowHeight+100);
GLB::pBACKDC->CreateCompatibleDC(pDC);
GLB::pPartMemDC->CreateCompatibleDC(pDC);
GLB::bGameStarted=TRUE;
updownBMP(GLB::pBACKBMP);
GLB::pMemDC->SelectObject(GLB::pMemBMP);
GLB::pBACKBMPBackUp=new unsigned int[GLB::lBMPLength];
GLB::pBACKBMP->GetBitmapBits(GLB::lBMPLength<<2,GLB::pBACKBMPBackUp);
GLB::pBACKDC->SelectObject(GLB::pBACKBMP);
if(LoadMap())
{
srand((unsigned)time(NULL));
for(int i=0; i<6; i++)
{
GLB::pGhostThread[i]->InitiateDCs(FALSE,i,pDC);
GLB::pGhostThread[i]->CreateThread();
}
//启动20个Bubble线程。
for(i=0; i<20; i++)
{
GLB::pBubbleThread[i]->InitiateDCs(pDC);
GLB::pBubbleThread[i]->CreateThread();
}
//[28][29]两个是人控制的,标志是Initiate中的第一个参数。
GLB::pGhostThread[28]->InitiateDCs(TRUE,28,pDC);
GLB::pGhostThread[28]->CreateThread();
GLB::pGhostThread[29]->InitiateDCs(TRUE,29,pDC);
GLB::pGhostThread[29]->CreateThread();
SetNPCGhosts();
m_iGameTimer=SetTimer(GAME_TIMER,2500,NULL);
}
ReleaseDC(pDC);
}
BOOL CBubbleIIDlg::PreTranslateMessage(MSG* pMsg)
{
if(GLB::eGameType==GAMEOVER)
return CDialog::PreTranslateMessage(pMsg);
UINT nKey = pMsg->wParam;
BOOL bEnterPressed=FALSE;
if(pMsg->message == WM_SYSKEYDOWN || pMsg->message == WM_KEYDOWN)
{ // some key was pressed down alright:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -