📄 maze.cpp
字号:
// Maze.cpp: implementation of the CMaze class.
//
//////////////////////////////////////////////////////////////////////
#include "ddutil.h"
#include <list>
#include "Maze.h"
#include <fstream>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace std;
CMaze::CMaze(char* pszTextureBMP): m_bDisplayGrid(false),m_pPtDeploy(0)//初始化符表
{
m_pszTextureBMP = pszTextureBMP; //迷宫地图纹理贴图图案
m_szName[0]=0;
}
CMaze::~CMaze()
{
delete[] m_pPtDeploy;
}
bool CMaze::LoadMaze(char *pFilename) //从文件加载迷宫数据
{
fstream MazeData(pFilename,std::ios::in ); //用流的方式关联打开文件用于读
MazeData.getline(m_szName,200); //读入一行,碰到换行符结束,读入的是迷宫名字
long nHV;
SMazeCoord MazeCoord;
m_lHLines.erase(m_lHLines.begin(), m_lHLines.end());
m_lVLines.erase(m_lVLines.begin(), m_lVLines.end());
delete[] m_pPtDeploy;
// Read number of horizontal lines
MazeData >> nHV; // Number of Horizontal rows 首先读入水平的行数
for (int nI = 0; nI < nHV; nI++) { //读取 数据 nY(第几行),nX1(从哪一列起),nX2(到哪一列结束)
MazeData >> MazeCoord.nY >> MazeCoord.nX1 >> MazeCoord.nX2;
m_lHLines.push_back(MazeCoord);
}
MazeData >> nHV; // Number of Horizontal rows
for (nI = 0; nI < nHV; nI++) {
MazeData >> MazeCoord.nX >> MazeCoord.nY1 >> MazeCoord.nY2;
m_lVLines.push_back(MazeCoord);
}
//读入物品放置点数据
MazeData >> m_nNoOfDeployPt; // No of sprite deploy location on the Maze.
m_pPtDeploy = new POINT[m_nNoOfDeployPt];
if (m_pPtDeploy == NULL)
return false;
for (nI = 0; nI < m_nNoOfDeployPt; nI++) {
MazeData >> m_pPtDeploy[nI].x >> m_pPtDeploy[nI].y ;
}
return true;
}
void CMaze::Draw(CSurface *pSurface)//画迷宫
{
HBITMAP hBMP = NULL;
BITMAP bmp;
if( pSurface == NULL )
return;
pSurface->Clear(0); //使用颜色0,清除表面
// Try to load the bitmap as a resource, if that fails, try it as a file
// 加载迷宫纹理图案
hBMP = (HBITMAP) LoadImage( GetModuleHandle(NULL), m_pszTextureBMP,
IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION );
if( hBMP == NULL )
{
hBMP = (HBITMAP) LoadImage( NULL, m_pszTextureBMP,
IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION );
if( hBMP == NULL )
return;
}
// Get size of the bitmap
GetObject( hBMP, sizeof(bmp), &bmp ); //取得图像的尺寸
m_bmpTexture = bmp;
list<SMazeCoord>::iterator iList; //
//Draw horizontal lines
//SMazeCoord 以行和行和列的方式记录了迷宫数据,比如行方式: 3 3 6, 表示 y=3 ;x=3、4、5、6 有4格填充了纹理
for (iList = m_lHLines.begin(); iList != m_lHLines.end(); iList++) //沿着水平方向,遍历所有的行
{
for (int nI= iList->nX1; nI <= iList->nX2; nI++) { //填充水平方向的每一段
pSurface->DrawBitmap(hBMP,nI * bmp.bmWidth,iList->nY * bmp.bmHeight, bmp.bmWidth, bmp.bmHeight);
}
}
// Draw vertical lines 列方式填充,
for (iList = m_lVLines.begin(); iList != m_lVLines.end(); iList++) //遍历列段
{
for (int nI= iList->nY1; nI <= iList->nY2; nI++) { // 填充所有的竖直小段
pSurface->DrawBitmap(hBMP,iList->nX * bmp.bmWidth, nI * bmp.bmHeight, bmp.bmWidth, bmp.bmHeight);
}
}
DeleteObject( hBMP );
// Draw grid based on the texture size
// 如果m_bDisplayGrid为真,划线打格,显示格子:格子大小为纹理大小
if (m_bDisplayGrid) {
POINT p1= {0,0};
POINT p2={0,0};
// Draw horizontal lines 画横线
p2.x=pSurface->GetSurfaceDesc().dwWidth; //右边沿
for (long i= 0 + bmp.bmHeight; i < pSurface->GetSurfaceDesc().dwHeight; i+= bmp.bmHeight) {
p1.y = p2.y=i;
pSurface->DrawLine(p1,p2,RGB(255,0,0));
}
// Draw vertical lines 画竖线
p1.y=0;
p2.y=pSurface->GetSurfaceDesc().dwHeight;
for ( i= 0 + bmp.bmWidth; i < pSurface->GetSurfaceDesc().dwWidth; i+= bmp.bmWidth) {
p1.x = p2.x=i;
pSurface->DrawLine(p1,p2,RGB(255,0,0));
}
}
if (m_bDisplayDeploy) { // 显示放置物品的位置点
RECT rc;
for (long i = 0; i < m_nNoOfDeployPt ; i++) {
rc.top = m_pPtDeploy[i].y * bmp.bmHeight;
rc.bottom = rc.top + m_szDeploy.cy;
rc.left = m_pPtDeploy[i].x * bmp.bmWidth;
rc.right = rc.left + m_szDeploy.cx;
pSurface->DrawRect(rc,RGB(0,255,0));
}
}
}
void CMaze::DisplayGrid(bool bDisplay /* = false */) //设置是否显示格子
{
m_bDisplayGrid = bDisplay;
}
bool CMaze::IsHit(RECT& rcRect) //判断 rcRect 是否与迷宫格子相交,即碰撞
{
list<SMazeCoord>::iterator iList;
RECT rcMaze;
RECT rcIntersect;
//Check horizontal lines
for (iList = m_lHLines.begin(); iList != m_lHLines.end(); iList++) //检测水平格子
{
rcMaze.top = iList->nY * m_bmpTexture.bmHeight;
rcMaze.left = iList->nX1 * m_bmpTexture.bmWidth;
rcMaze.bottom = (iList->nY + 1) * m_bmpTexture.bmHeight;
rcMaze.right = (iList->nX2 + 1) * m_bmpTexture.bmWidth;
if (::IntersectRect(&rcIntersect, &rcMaze, &rcRect))
return true;
}
// Check vertical lines
for (iList = m_lVLines.begin(); iList != m_lVLines.end(); iList++) //检测竖直格子
{
rcMaze.top = iList->nY1 * m_bmpTexture.bmHeight;
rcMaze.left = iList->nX * m_bmpTexture.bmWidth;
rcMaze.bottom = (iList->nY2 + 1) * m_bmpTexture.bmHeight;
rcMaze.right = (iList->nX + 1) * m_bmpTexture.bmWidth;
if (::IntersectRect(&rcIntersect, &rcMaze, &rcRect))
return true;
}
return false;
}
void CMaze::DisplayDeployRects(SIZE& sz, bool bDisplay) //设置是否显示放置物品的位置点
{
m_szDeploy = sz;
m_bDisplayDeploy = bDisplay;
}
POINT CMaze::GetRandDeployPoint() //从所有的放置物品的位置点中抽取一个,返回该点
{
if (m_pPtDeploy) {
return m_pPtDeploy[rand() % m_nNoOfDeployPt];
} else {
POINT pt = {0,0};
return pt;
}
}
const char* CMaze::GetName() //获取迷宫的名字
{
return m_szName;
}
void CMaze::GetPathFindingArray(short* pnPathArray, int nCols) //nCols 每行有多少格,即列数
{
list<SMazeCoord>::iterator iList;
// pnPathArray 记录了整个屏幕格子,填充了迷宫纹理的格子被置值“1”
// (行号)*nCols+X
for (iList = m_lHLines.begin(); iList != m_lHLines.end(); iList++) //逐行
{
for (int nI= iList->nX1; nI <= iList->nX2; nI++) {
pnPathArray[iList->nY * nCols + nI ] = 1;
}
}
// Draw vertical lines
for (iList = m_lVLines.begin(); iList != m_lVLines.end(); iList++) //逐列
{
for (int nI= iList->nY1; nI <= iList->nY2; nI++) {
pnPathArray[nI*nCols + iList->nX] = 1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -