📄 puzzlebmp.cpp
字号:
for(int i = 0; i < nAmount; i++)
{
unsigned short usData;
fread(&usData, sizeof(unsigned short), 1, fp);
m_setAniInfo.push_back(usData);
}*/
fread(m_usPuzzleIndex, sizeof(unsigned short), m_nPuzzleAmount, fp);
if(m_dwVersion == 2)
{
// 移动速度
fread(&m_nRollSpeedX, sizeof(int), 1, fp);
fread(&m_nRollSpeedY, sizeof(int), 1, fp);
}
else
{
m_nRollSpeedX = m_nRollSpeedY = 0;
}
fclose(fp);
return true;
}
//------------------------------------------------
unsigned short CPuzzleBmp::GetGrid(int iPosX, int iPosY)
{
unsigned short usIndex;
if((iPosX < 0)||(iPosY < 0) || (iPosX >= m_infoSize.iWidth) || ((iPosY >= m_infoSize.iHeight)))
return 65535;
if((iPosY * m_infoSize.iWidth + iPosX) >= m_nPuzzleAmount)
return 65535;
unsigned short* pusIndex = m_usPuzzleIndex + iPosY * m_infoSize.iWidth + iPosX;
usIndex = *pusIndex;
return usIndex;
}
//------------------------------------------------
void CPuzzleBmp::GetSize(CMySize &infoSize)
{
infoSize.iHeight = m_infoSize.iHeight * m_nGrideSize;
infoSize.iWidth = m_infoSize.iWidth * m_nGrideSize;
}
//------------------------------------------------
void CPuzzleBmp::PreLoad(int iPosX, int iPosY)
{
// do preload ...
// 计算Cell坐标
CMyPos posCell;
posCell.x = -iPosX/m_nGrideSize;
posCell.y = -iPosY/m_nGrideSize;
CMyPos posView;
posView.x = iPosX + posCell.x*m_nGrideSize;
posView.y = iPosY + posCell.y*m_nGrideSize;
// 求显示范围
int nAmountX = PUZZLE_SCRX/m_nGrideSize+2;
int nAmountY = PUZZLE_SCRY/m_nGrideSize+2;
int nPreLoadSize = g_objGameDataSet.m_dwPuzzlePreloadSize;
// 开始显示
for(int i = 0-nPreLoadSize; i < nAmountY+nPreLoadSize; i++)
{
for(int j = 0-nPreLoadSize; j< nAmountX+nPreLoadSize; j++)
{
CMyPos posCellShow;
// 求要加载的Cell
posCellShow.x = posCell.x + j;
posCellShow.y = posCell.y + i;
// 剔除空Cell
if(posCellShow.x < 0)
continue;
if(posCellShow.y < 0)
continue;
CMySize infoSize;
this->GetSizeByGride(infoSize);
if(posCellShow.x >= infoSize.iWidth)
continue;
if(posCellShow.y >= infoSize.iHeight)
continue;
// 剔除非法Cell
int nDequeIndex = posCellShow.y*m_infoSize.iWidth +posCellShow.x;
if(nDequeIndex >= m_nPuzzleAmount)
continue;
if(nDequeIndex < 0)
continue;
unsigned short* pusIndex = m_usPuzzleIndex+nDequeIndex;
int nIndex = (int)*pusIndex;
if(nIndex == 65535)
{
// 透明, 不加载
}
else
{
// PreLoad
char szTitle[64];
sprintf(szTitle, "Puzzle%d", nIndex);
g_objGameDataSet.GetDataAni(m_szAniFile, szTitle, EXIGENCE_NORMAL, g_objGameDataSet.m_dwPuzzleLife);
}
}
}
}
//------------------------------------------------
void CPuzzleBmp::SetZoomInNum(int nZoom)
{
m_nZoomInNum = nZoom;
m_nGrideSize = PUZZLE_GRIDE_SIZE/nZoom;
}
//------------------------------------------------
void CPuzzleBmp::Bmp2Grid(CMyPos posBmp, CMyPos& posGrid)
{
posGrid.x = posBmp.x/m_nGrideSize;
posGrid.y = posBmp.y/m_nGrideSize;
}
//------------------------------------------------
void CPuzzleBmp::Grid2Bmp(CMyPos posGrid, CMyPos& posBmp)
{
posBmp.x = m_nGrideSize * posGrid.x + m_nGrideSize/2;
posBmp.y = m_nGrideSize * posGrid.y + m_nGrideSize/2;
}
//------------------------------------------------
void CPuzzleBmp::Unit(CPuzzleBmp* pPuzzle, CMyPos posGrid)
{
if(!pPuzzle)
return;
CMyPos posLayer = posGrid;
CMySize sizeLayer;
pPuzzle->GetSize(sizeLayer);
CMySize sizeBmp;
this->GetSizeByGride(sizeBmp);
for(int i = 0; i < sizeLayer.iHeight; i++)
{
for(int j = 0; j < sizeLayer.iWidth; j++)
{
CMyPos posLayerGrid;
posLayerGrid.x = posLayer.x + j;
posLayerGrid.y = posLayer.y + i;
if(posLayerGrid.x <0)
continue;
if(posLayerGrid.y <0)
continue;
if(posLayerGrid.x >= sizeBmp.iWidth)
continue;
if(posLayerGrid.y >= sizeBmp.iHeight)
continue;
if(pPuzzle->GetGrid(j,i) == 65535)
continue;
unsigned short* pusIndex = m_usPuzzleIndex + posLayerGrid.y*m_infoSize.iWidth +posLayerGrid.x;
*pusIndex = pPuzzle->GetGrid(j,i);
}
}
}
//------------------------------------------------
void CPuzzleBmp::Save(char* pszFile)
{
if(!pszFile)
return;
FILE* fp = fopen(pszFile, "wb");
if(!fp)
return;
// header..(8 bytes)
char szHeader[8]="PUZZLE2";
fwrite(szHeader, 1, 8, fp);
// AniFile.. (256 bytes)
char szAni[256];
strcpy(szAni, this->GetAniFile());
fwrite(szAni, 1, 256, fp);
// Size
CMySize infoSize;
this->GetSizeByGride(infoSize);
fwrite(&infoSize.iWidth, sizeof(UINT), 1, fp);
fwrite(&infoSize.iHeight, sizeof(UINT), 1, fp);
// Grids
/* int nAmount = m_setAniInfo.size();
for(int i = 0; i < nAmount; i++)
{
unsigned short usGrid = m_setAniInfo[i];
fwrite(&usGrid, sizeof(unsigned short), 1, fp);
}*/
fwrite(m_usPuzzleIndex, sizeof(unsigned short), m_nPuzzleAmount, fp);
fwrite(&m_nRollSpeedX, sizeof(int), 1, fp);
fwrite(&m_nRollSpeedY, sizeof(int), 1, fp);
fclose(fp);
return;
}
//-------------------------------------------------------------------------------
BOOL CPuzzleBmp::TestRect(CMyRect rectInfo, BOOL bTerrain, BOOL bTransparent)
{
// 判断是不是整个rectInfo矩形都被玷污过
// 如果是的返回false
/* if(!bTerrain)// 地表层是都要画地
{
if(CPuzzleBmp::IsDirty(rectInfo))
return false;
}
*/
// 将rectInfo加入脏矩形
// 条件:是地表层,并且不透明,Buffer没有溢出
if(bTerrain && (!bTransparent) && (s_nRectAmount < _MAX_DIRTY_RECT))
{
s_setRect[s_nRectAmount] = rectInfo;
s_nRectAmount++;
}
// 返回true
return true;
}
//-------------------------------------------------------------------------------
BOOL CPuzzleBmp::IsDirty(CMyRect rectInfo)
{
// 如果我只针对双重背景
// 我只要判断4个顶点 如果四个顶点都是被玷污过 return true
// 否则return false
for(int i = 0; i < s_nRectAmount; i ++)
{
BOOL bCheck[4];
CMyPos point;
point.x = rectInfo.l;
point.y = rectInfo.t;
bCheck[0] = CPhysics::IsPointInRect(point, s_setRect[i]);
point.x = rectInfo.l;
point.y = rectInfo.b;
bCheck[1] = CPhysics::IsPointInRect(point, s_setRect[i]);
point.x = rectInfo.r;
point.y = rectInfo.t;
bCheck[2] = CPhysics::IsPointInRect(point, s_setRect[i]);
point.x = rectInfo.r;
point.y = rectInfo.b;
bCheck[3] = CPhysics::IsPointInRect(point, s_setRect[i]);
if(bCheck[0] && bCheck[1] && bCheck[2] && bCheck[3])
return true;
}
return false;
}
//-------------------------------------------------------------------------------
int CPuzzleBmp::GetNormalGrideSize()
{
return PUZZLE_GRIDE_SIZE;
}
//-------------------------------------------------------------------------------
void CPuzzleBmp::ReapetRateCount(DWORD& dwPicAmount, DWORD& dwGrideAmount)
{
vector<unsigned short> m_setIndex;
dwGrideAmount = m_infoSize.iHeight * m_infoSize.iWidth;
for(int i = 0; i < m_infoSize.iWidth; i ++)
{
for(int j = 0; j < m_infoSize.iHeight; j ++)
{
unsigned short usIndex = this->GetGrid(i, j);
int nAmount = m_setIndex.size();
for(int k = 0; k < nAmount; k ++)
{
if(m_setIndex[k] == usIndex)
break;
}
if(k == nAmount)
{
m_setIndex.push_back(usIndex);
}
}
}
dwPicAmount = m_setIndex.size();
}
//-------------------------------------------------------------------------------
void CPuzzleBmp::GenerateAni(const char* pszAniFile)
{
if(!pszAniFile)
return;
FILE* fp = fopen(pszAniFile, "w");
if(!fp)
return;
vector<unsigned short> m_setIndex;
for(int i = 0; i < m_infoSize.iWidth; i ++)
{
for(int j = 0; j < m_infoSize.iHeight; j ++)
{
unsigned short usIndex = this->GetGrid(i, j);
int nAmount = m_setIndex.size();
for(int k = 0; k < nAmount; k ++)
{
if(m_setIndex[k] == usIndex)
break;
}
if(k == nAmount)
{
m_setIndex.push_back(usIndex);
}
}
}
for(int l = 0; l < m_setIndex.size(); l ++)
{
char szTitle[64];
sprintf(szTitle, "Puzzle%d", m_setIndex[l]);
}
fclose(fp);
}
//-------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -