📄 game.cpp
字号:
// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "tank.h"
#include "Game.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGame::CGame()
{
m_pMenuBuffer=NULL;
m_bFreeze=FALSE;
m_nBorderTime=0;
m_nFreezeTime=0;
m_nMenuPos=3; //默认为返回游戏
m_pDD=NULL;
m_pddsFrontBuffer=NULL;
m_pddsBackBuffer=NULL;
pcClipper=NULL;
m_pBackGroundBuffer=NULL;
m_pBulletBuffer=NULL;
m_pExplodeBuffer=NULL;
m_pGeneralBuffer=NULL;
m_pEggBuffer=NULL;
m_pWaitBuffer=NULL;
m_pGameOverBuffer=NULL;
m_pBeginBuffer=NULL;
m_pPlayerBuffer=NULL;
m_pTankAppBuffer=NULL;
m_pTankStateBuffer=NULL;
m_pEnemyBuffer=NULL;
m_pLevelBuffer=NULL;
m_bSinglePlayer=FALSE;
m_nEnemyCount=0;
m_nLeftEnemy=0;
m_nGameLevel=0;
m_nLeftPlayer=3;
m_nRightPlayer=3;
m_nLeftKills=0;
m_nRightKills=0;
m_nLeftScore=0;
m_nRightScore=0;
m_tankleft=m_tankright=NULL;
m_bLeftBring=false; //左边正在产生坦克
m_bRightBring=false;
m_hWnd=NULL;
m_rcWnd.left=0;
m_rcWnd.right=SCREEN_WIDTH;
m_rcWnd.top=0;
m_rcWnd.bottom=SCREEN_HEIGHT;
m_nGameState=GAMEBEGIN;
m_rcGeneral.SetRect(18 *TANKMAPCELL,37*TANKMAPCELL,22*TANKMAPCELL,40*TANKMAPCELL);
}
CGame::~CGame()
{
m_TankMap.DestroyBmp();
DestroyObject();
DDDestroy();
}
BOOL CGame::DDinit(HWND hWnd)
{
HRESULT hr;
m_hWnd=hWnd;
//create DirectDraw
hr = DirectDrawCreate( NULL, &m_pDD, NULL );
if( FAILED(hr) )
{
// AfxMessageBox( "Error Create DirectDraw\n" );
return FALSE;
}
m_pDD->SetCooperativeLevel( m_hWnd, DDSCL_NORMAL );
//create FrontBuffer
DDSURFACEDESC ddsd;
ZeroMemory( &ddsd, sizeof(ddsd) );
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = m_pDD->CreateSurface( &ddsd, &m_pddsFrontBuffer, NULL );
if( FAILED(hr) )
{
// AfxMessageBox( "Error Create Front Buffer\n" );
return FALSE;
}
//create BackBuffer
m_pddsBackBuffer = DDCreateSurface( SCREEN_WIDTH, SCREEN_HEIGHT );
if(!m_pddsBackBuffer)
{
// AfxMessageBox( "Error Create Back Buffer\n" );
return FALSE;
}
//create Clipper
if(FAILED(hr = m_pDD->CreateClipper(0, &pcClipper, NULL)))
{
// AfxMessageBox("Failed to create clipper.");
return FALSE;
}
if(FAILED(hr = pcClipper->SetHWnd(0, hWnd)))
{
pcClipper->Release();
// AfxMessageBox("Failed to create primary surface.");
return FALSE;
}
if(FAILED(hr = m_pddsFrontBuffer->SetClipper(pcClipper)))
{
pcClipper->Release();
// AfxMessageBox("Failed to create primary surface.");
return FALSE;
}
if( !LoadBitmaps() )
return FALSE;
return TRUE;
}
LPDIRECTDRAWSURFACE CGame::DDCreateSurface(int width, int height)
{
DDSURFACEDESC ddsd;
ZeroMemory( &ddsd, sizeof(ddsd) );
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = width;
ddsd.dwHeight = height;
LPDIRECTDRAWSURFACE pdds;
if( FAILED(m_pDD->CreateSurface( &ddsd, &pdds, NULL )) )
return NULL;
return pdds;
}
BOOL CGame::LoadBitmaps()
{
m_pBeginBuffer = DDLoadBitmap("pic\\bmp\\Begin.bmp");
if(!m_pBeginBuffer)
{
// AfxMessageBox("载入序幕图源失败");
return FALSE;}
//Egg
m_pEggBuffer = DDLoadBitmap("pic\\bmp\\Egg.bmp");
if(!m_pEggBuffer)
{
// AfxMessageBox("载入彩蛋图源失败");
return FALSE;}
DDSetColorKey( m_pEggBuffer, 0);
m_pExplodeBuffer = DDLoadBitmap("pic\\bmp\\explode.bmp");
if(!m_pExplodeBuffer)
{
// AfxMessageBox("载入爆炸效果图失败");
return FALSE;}
DDSetColorKey( m_pExplodeBuffer, RGB(0,0,255) );
m_pGeneralBuffer = DDLoadBitmap("pic\\bmp\\General.bmp");
if(!m_pGeneralBuffer)
{
// AfxMessageBox("载入大本营图失败");
return FALSE;}
DDSetColorKey( m_pGeneralBuffer, 0 );
m_pWaitBuffer = DDLoadBitmap("pic\\bmp\\Wait.bmp");
if(!m_pWaitBuffer)
{
// AfxMessageBox("载入等待图失败");
return FALSE;}
DDSetColorKey( m_pWaitBuffer, 0 );
m_pGameOverBuffer = DDLoadBitmap("pic\\bmp\\over.bmp");
if(!m_pGameOverBuffer)
{
// AfxMessageBox("载入结束图失败");
return FALSE;}
DDSetColorKey( m_pGameOverBuffer, 0 );
m_pBulletBuffer = DDLoadBitmap("pic\\bmp\\Bullet.bmp");
if(!m_pBulletBuffer)
{
// AfxMessageBox("载入子弹图失败");
return FALSE;}
DDSetColorKey( m_pBulletBuffer, 0 );
//Map Bitmap
LPDIRECTDRAWSURFACE pDDsTemp=NULL;
pDDsTemp = DDLoadBitmap(IDB_SOIL);
if(!pDDsTemp)
{
// AfxMessageBox("载入泥图源失败");
return FALSE;}
m_TankMap.SetSoilBmp(pDDsTemp);
pDDsTemp = DDLoadBitmap(IDB_STONE);
if(!pDDsTemp)
{
// AfxMessageBox("载入石图源失败");
return FALSE;}
m_TankMap.SetStoneBmp(pDDsTemp);
pDDsTemp = DDLoadBitmap(IDB_STEEL);
if(!pDDsTemp)
{
// AfxMessageBox("载入钢图源失败");
return FALSE;}
m_TankMap.SetSteelBmp(pDDsTemp);
pDDsTemp = DDLoadBitmap(IDB_WATER);
if(!pDDsTemp)
{
// AfxMessageBox("载入水图源失败");
return FALSE;}
m_TankMap.SetWaterBmp(pDDsTemp);
pDDsTemp = DDLoadBitmap(IDB_ICE);
if(!pDDsTemp)
{
// AfxMessageBox("载入冰图源失败");
return FALSE;}
DDSetColorKey( pDDsTemp, 0 );
m_TankMap.SetIceBmp(pDDsTemp);
pDDsTemp = DDLoadBitmap(IDB_TREE);
if(!pDDsTemp)
{
// AfxMessageBox("载入树图源失败");
return FALSE;}
DDSetColorKey( pDDsTemp, 0 );
m_TankMap.SetTreeBmp(pDDsTemp);
//Tank Bitmap
m_pPlayerBuffer = DDLoadBitmap("pic\\bmp\\TankPlayer.bmp");
if(!m_pPlayerBuffer)
{
// AfxMessageBox("载入坦克图源失败");
return FALSE;}
DDSetColorKey( m_pPlayerBuffer, RGB(20,20,140) );
m_pTankAppBuffer = DDLoadBitmap("pic\\bmp\\TankApp.bmp");
if(!m_pTankAppBuffer)
{
// AfxMessageBox("载入坦克出现图源失败");
return FALSE;}
DDSetColorKey( m_pTankAppBuffer, 0 );
m_pTankStateBuffer = DDLoadBitmap("pic\\bmp\\Tankstate.bmp");
if(!m_pTankStateBuffer)
{
// AfxMessageBox("载入坦克状态图源失败");
return FALSE;}
m_pEnemyBuffer = DDLoadBitmap("pic\\bmp\\TankEnemy.bmp");
if(!m_pEnemyBuffer)
{
// AfxMessageBox("载入敌方坦克图源失败");
return FALSE;}
DDSetColorKey( m_pEnemyBuffer, RGB(20,20,140) );
return TRUE;
}
LPDIRECTDRAWSURFACE CGame::DDLoadBitmap(LPCSTR szBitmap, int dx, int dy)
{
HBITMAP hbm;
BITMAP bm;
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE pdds;
//
// Try to load the bitmap as a resource, if that fails, try it as a file
//
hbm = (HBITMAP) LoadImage(GetModuleHandle(NULL), szBitmap, IMAGE_BITMAP, dx,
dy, LR_CREATEDIBSECTION);
if (hbm == NULL)
hbm = (HBITMAP) LoadImage(NULL, szBitmap, IMAGE_BITMAP, dx, dy,
LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if (hbm == NULL)
return NULL;
//
// Get size of the bitmap
//
GetObject(hbm, sizeof(bm), &bm);
//
// Create a DirectDrawSurface for this bitmap
//
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN ;
ddsd.dwWidth = bm.bmWidth;
ddsd.dwHeight = bm.bmHeight;
if (m_pDD->CreateSurface(&ddsd, &pdds, NULL) != DD_OK)
{
DeleteObject(hbm);
return NULL;
}
DDCopyBitmap(pdds, hbm, 0, 0, 0, 0);
DeleteObject(hbm);
return pdds;
}
LPDIRECTDRAWSURFACE CGame::DDLoadBitmap(int id, int dx, int dy)
{
HBITMAP hbm;
BITMAP bm;
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE pdds;
//
// Try to load the bitmap as a resource, if that fails, try it as a file
//
CBitmap bmp;
if(bmp.LoadBitmap(id)==FALSE)
return NULL;
hbm=HBITMAP(bmp.GetSafeHandle());
if (hbm == NULL)
return NULL;
//
// Get size of the bitmap
//
GetObject(hbm, sizeof(bm), &bm);
//
// Create a DirectDrawSurface for this bitmap
//
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = bm.bmWidth;
ddsd.dwHeight = bm.bmHeight;
if (m_pDD->CreateSurface(&ddsd, &pdds, NULL) != DD_OK)
return NULL;
DDCopyBitmap(pdds, hbm, 0, 0, 0, 0);
DeleteObject(hbm);///???
return pdds;
}
HRESULT CGame::DDCopyBitmap(LPDIRECTDRAWSURFACE pdds, HBITMAP hbm, int x, int y, int dx, int dy)
{
HDC hdcImage;
HDC hdc;
BITMAP bm;
DDSURFACEDESC ddsd;
HRESULT hr;
if (hbm == NULL || pdds == NULL)
return E_FAIL;
//
// Make sure this surface is restored.
//
pdds->Restore();
//
// Select bitmap into a memoryDC so we can use it.
//
hdcImage = CreateCompatibleDC(NULL);
if (!hdcImage)
TRACE("createcompatible dc failed\n");
SelectObject(hdcImage, hbm);
//
// Get size of the bitmap
//
GetObject(hbm, sizeof(bm), &bm);
dx = dx == 0 ? bm.bmWidth : dx; // Use the passed size, unless zero
dy = dy == 0 ? bm.bmHeight : dy;
//
// Get size of surface.
//
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
pdds->GetSurfaceDesc(&ddsd);
if ((hr = pdds->GetDC(&hdc)) == DD_OK)
{
StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, x, y,
dx, dy, SRCCOPY);
pdds->ReleaseDC(hdc);
}
DeleteDC(hdcImage);
return hr;
}
void CGame::DDDestroy()
{
if(m_pDD)
{
if(m_pddsFrontBuffer)
{
m_pddsFrontBuffer->Release();
m_pddsFrontBuffer=NULL;
if(m_pddsBackBuffer)
{
m_pddsBackBuffer->Release();
m_pddsBackBuffer=NULL;
}
if(pcClipper)
{
pcClipper->Release();
pcClipper=NULL;
}
if(m_pBackGroundBuffer)
{
m_pBackGroundBuffer->Release();
m_pBackGroundBuffer=NULL;
}
if(m_pMenuBuffer)
{
m_pMenuBuffer->Release();
m_pMenuBuffer=NULL;
}
if(m_pBulletBuffer)
{
m_pBulletBuffer->Release();
m_pBulletBuffer=NULL;
}
if(m_pExplodeBuffer)
{
m_pExplodeBuffer->Release();
m_pExplodeBuffer=NULL;
}
if(m_pGeneralBuffer)
{
m_pGeneralBuffer->Release();
m_pGeneralBuffer=NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -