⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 map.cpp

📁 diablo图形引擎例子
💻 CPP
字号:
// Map.cpp: implementation of the Map class.
//
//////////////////////////////////////////////////////////////////////

#include <assert.h>
#include <stdlib.h>
#include "Map.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Map::Map()
{
	m_lpMapBlock=NULL;
	m_lpMapFileHeader=NULL;
	m_lpImageList=NULL;
}

Map::~Map()
{
	if(m_lpMapBlock) delete m_lpMapBlock;
	if (m_lpMapFileHeader) delete m_lpMapFileHeader;
	if (m_lpImageList) delete m_lpImageList;
}

void Map::ConvertPosition(int & x, int & y)
{
	int xo,yo,x1,y1;

	xo=x+m_lpCharactar->m_ixOffset-320+16-8;
	yo=y+m_lpCharactar->m_iyOffset-240+32;

	x1 = yo + (xo/2);
	y1 = yo - (xo/2);

	if(x1 < 0) x1 -= 31; 
	if(y1 < 0) y1 -= 31;

	x = x1 /32;
	y = y1 /32;
}

bool Map::LoadMapFile(char *lpszFilename)
{
	FILE *fp=fopen(lpszFilename,"rb");
	assert(fp);

	m_lpMapFileHeader=(MAPFILEHEADER *)malloc(sizeof(MAPFILEHEADER));

	fread(m_lpMapFileHeader,sizeof(MAPFILEHEADER),1,fp);

	int width=m_lpMapFileHeader->mWidth;
	int height=m_lpMapFileHeader->mHeight;

	m_lpMapBlock=(MAPBLOCK* )malloc(width*height*sizeof(MAPBLOCK));
	
	fread(m_lpMapBlock,sizeof(MAPBLOCK),width*height,fp);
	
	fclose(fp);
	return true;

}

bool Map::LoadMapCGL(char *lpszFilename,bool bColorMode)
{
	if (m_lpImageList!=NULL) delete  m_lpImageList;
	
	m_lpImageList=new ImageList;
	
    return m_lpImageList->Create(lpszFilename,bColorMode,4);
}




void Map::DrawMapLayer(int x, int y, WORD *lpBitmap,long lPitch)
{

	///delete this
	m_lpCharactar->Loop();


	//draw floor

	int mapx,mapy,xo,yo,xa,ya,mx,my,screenx,screeny;
	int i,j,tx,ty,length;

    
	//转换坐标
    mapx = x / 32;
    xo   = x % 32;
    mapy = y / 32;
    yo   = y % 32;
    xa   = xo - yo;
    ya   = xo/2 + yo/2;

    mx = mapx;
    my = mapy;
    screeny = -ya-32;
	
    
	// 垂直画屏幕循环
    for (i=0;i<33;i++)	// 画40 列图块
	{
      tx = mx;
      ty = my;
      screenx =  -xa-64;
	  length = 12;		// number of tiles to be drawn horizontally (even lines)
      if(i&1)			// if line is odd :
	  {
        length++;		// one more tile to be drawn horizontally (so 25)
        screenx-=32;	// pre-step 16 pixels back left
      }
	  
	  // This loop takes care of the horizontal screen draw which is done first
      for (j=0;j<length;j++) // it is repeated (length) time, one for each tile in the line
	  {
         // This loop takes care of all the tiles (num) in this tile coordinates
	
		   if(tx>=0 && tx<m_lpMapFileHeader->mWidth && ty>=0 && ty<m_lpMapFileHeader->mHeight)
			{
					int index= m_lpMapBlock[tx+ty*m_lpMapFileHeader->mWidth].ElementIndex[0];
					if (index>0 && index<m_lpImageList->m_iTotalFrame) 
					{			
							m_lpImageList->Draw(index,screenx,screeny,lpBitmap,lPitch);
					}
			}
 
		screenx+=64; // move 64 pixels right for next tile
	    tx+=1;ty-=1;
     }
     screeny+=16; // move 16 pixels down for next line
     if(i & 1) // if line is odd
	  {
        mx+=1;
      } 
	  else		// if line is even
	  {
        my+=1;
         
      }
  } 



		//////////////////

	//转换坐标
    mapx = x / 32;
    xo   = x % 32;
    mapy = y / 32;
    yo   = y % 32;
    xa   = xo - yo;
    ya   = xo/2 + yo/2;

    mx = mapx;
    my = mapy;
    screeny = -ya-32;
	
    
	// 垂直画屏幕循环
    for (i=0;i<33;i++)	// 画40 列图块
	{
      tx = mx;
      ty = my;
      screenx =  -xa-64;
	  length = 12;		// number of tiles to be drawn horizontally (even lines)
      if(i&1)			// if line is odd :
	  {
        length++;		// one more tile to be drawn horizontally (so 25)
        screenx-=32;	// pre-step 16 pixels back left
      }
	  
	  // This loop takes care of the horizontal screen draw which is done first
      for (j=0;j<length;j++) // it is repeated (length) time, one for each tile in the line
	  {
         // This loop takes care of all the tiles (num) in this tile coordinates
	
		   if(tx>=0 && tx<m_lpMapFileHeader->mWidth && ty>=0 && ty<m_lpMapFileHeader->mHeight)
			{
			   for (int k=1;k<3;k++)
				{
					int index= m_lpMapBlock[tx+ty*m_lpMapFileHeader->mWidth].ElementIndex[k];
					if (index>0 && index<m_lpImageList->m_iTotalFrame) 
					{			
				
												
				/*	 int xx,yy,mmx,mmy;
					 xx=m_lpCharactar->m_ix;
					 yy=m_lpCharactar->m_iy;
					 mmx=xx/32;
					 mmy=yy/32;

  					 if (tx>=mmx && ty >=mmy)
								m_lpImageList->DrawAlpha750(index,screenx,screeny-m_lpImageList->GetHeight(index)+32+m_lpMapBlock[tx+ty*m_lpMapFileHeader->mWidth].ElementHeight[k],lpBitmap,lPitch);
					 else 
					 	      	m_lpImageList->Draw(index,screenx,screeny-m_lpImageList->GetHeight(index)+32+m_lpMapBlock[tx+ty*m_lpMapFileHeader->mWidth].ElementHeight[k],lpBitmap,lPitch);
								*/
						m_lpImageList->DrawCenter(index,screenx,screeny+32+m_lpMapBlock[tx+ty*m_lpMapFileHeader->mWidth].ElementHeight[k],lpBitmap,lPitch);
					}
				}
			   m_lpCharactar->Draw(screenx,screeny,tx,ty,lpBitmap,lPitch);
			}
		screenx+=64; // move 64 pixels right for next tile
        tx+=1;ty-=1;
     }
     screeny+=16; // move 16 pixels down for next line
     if(i & 1) // if line is odd
	  {
        mx+=1;
      } 
	  else		// if line is even
	  {
        my+=1;
         
      }
  } 



 // SaveAllPos(x,y);

 // SortY();

 // DrawIt(lpBitmap,lPitch);

}


void Map::CreateCharactar(bool bColorMode)
{
	m_lpCharactar=new Charactar;
	m_lpCharactar->SetupMapInfo(m_lpMapBlock,m_lpMapFileHeader->mWidth,m_lpMapFileHeader->mHeight);
	m_lpCharactar->Load("charactar.ini",bColorMode);
	
}

bool Map::GetMoveStatus(int x, int y)
{
	if (x<0 || x>=m_lpMapFileHeader->mWidth || y<0 || y>=m_lpMapFileHeader->mHeight) return false;

	int ofs=x+y*m_lpMapFileHeader->mWidth;

	if (m_lpMapBlock[ofs].bMoveAble==false || m_lpMapBlock[ofs].bHaveAnother==true) return false;
	else return true;
}


void Map::SaveAllPos(int x,int y)
{
	
	WORD i,j;
	int mapx,mapy,mx,my,tx,ty,xo,yo,xa,ya,screenx,screeny,length;

	int countz=0;

	mapx = x / 32;
    xo   = x % 32;
    mapy = y / 32;
    yo   = y % 32;
    xa   = xo - yo;
    ya   = xo/2 + yo/2;

    mx = mapx;
    my = mapy;
    screeny = -ya-32;
	
    
	// 垂直画屏幕循环
    for (i=0;i<33;i++)	// 画40 列图块
	{
      tx = mx;
      ty = my;
      screenx =  -xa-64;
	  length = 12;		// number of tiles to be drawn horizontally (even lines)
      if(i&1)			// if line is odd :
	  {
        length++;		// one more tile to be drawn horizontally (so 25)
        screenx-=32;	// pre-step 16 pixels back left
      }
	  
	  // This loop takes care of the horizontal screen draw which is done first
      for (j=0;j<length;j++) // it is repeated (length) time, one for each tile in the line
	  {
         // This loop takes care of all the tiles (num) in this tile coordinates
	
		   if(tx>=0 && tx<m_lpMapFileHeader->mWidth && ty>=0 && ty<m_lpMapFileHeader->mHeight)
			{
			   for (int k=0;k<2;k++)
				{
					int index= m_lpMapBlock[tx+ty*m_lpMapFileHeader->mWidth].ElementIndex[k];
					if (index>0 && index<m_lpImageList->m_iTotalFrame) 
					{			
				
						ZBuffer[countz].x=screenx;
						ZBuffer[countz].y=screeny;
						ZBuffer[countz].tx=tx;
						ZBuffer[countz].ty=ty;
						ZBuffer[countz].index=index;
						ZBuffer[countz].k=k;
						ZBuffer[countz].type=0;
						countz++;
						m_ztt=countz;
					//	m_lpImageList->Draw(index,screenx,screeny-m_lpImageList->GetHeight(index)+32+m_lpMapBlock[tx+ty*m_lpMapFileHeader->mWidth].ElementHeight[k],lpBitmap,lPitch);
					}
				}
		//	   m_lpCharactar->Draw(screenx,screeny,tx,ty,lpBitmap,lPitch);

			   int cx,cy,cmapx,cmapy,cxo,cyo,cxa,cya;

			   cx=m_lpCharactar->m_ix;cy=m_lpCharactar->m_iy;
			   cmapx = cx / 32;
			   cxo   = cx % 32;
			   cmapy = cy / 32;
			   cyo   = cy % 32;
			   cxa   = cxo - cyo;
			   cya   = cxo/2 + cyo/2;

			   if (tx==cmapx && ty==cmapy)
			   {
					ZBuffer[countz].x=screenx;
					ZBuffer[countz].y=screeny;
					ZBuffer[countz].tx=tx;
					ZBuffer[countz].ty=ty;
					//ZBuffer[countz].index=index;
					ZBuffer[countz].xa=cxa;
					ZBuffer[countz].ya=cya;
					//ZBuffer[countz].k=k;
					ZBuffer[countz].type=1;
					countz++;
					m_ztt=countz;
			   }			   

			}
		screenx+=64; // move 64 pixels right for next tile
        tx+=1;ty-=1;
     }
     screeny+=16; // move 16 pixels down for next line
     if(i & 1) // if line is odd
	  {
        mx+=1;
      } 
	  else		// if line is even
	  {
        my+=1;
         
      }
  } 

}

void Map::DrawIt(WORD *lpBitmap, long lPitch)
{	
	int screenx,screeny,index,k,tx,ty;

	for(int i=0;i<m_ztt;i++)
	{
		if (ZBuffer[i].type==0)
		{
			screenx=ZBuffer[i].x;
			screeny=ZBuffer[i].y;
			index=ZBuffer[i].index;
			k=ZBuffer[i].k;
			tx=ZBuffer[i].tx;
			ty=ZBuffer[i].ty;

			m_lpImageList->Draw(index,screenx,screeny-m_lpImageList->GetHeight(index)+32+m_lpMapBlock[tx+ty*m_lpMapFileHeader->mWidth].ElementHeight[k],lpBitmap,lPitch);
		}
		else 
		{
			int xa,ya;
			screenx=ZBuffer[i].x;
			screeny=ZBuffer[i].y;
			index=ZBuffer[i].index;
			k=ZBuffer[i].k;
			tx=ZBuffer[i].tx;
			ty=ZBuffer[i].ty;
			xa=ZBuffer[i].xa;
			ya=ZBuffer[i].ya;
			
			m_lpCharactar->DrawiT(screenx+xa,screeny+ya,lpBitmap,lPitch);

		}

	}

}

void Map::SortY()
{
	for (int i=0;i<m_ztt-1;i++)
	 for(int j=i+1;j<m_ztt;j++)
	 {
		if (ZBuffer[i].y>=ZBuffer[j].y)
		{
			int temp;
			temp=ZBuffer[i].y;
			ZBuffer[i].y=ZBuffer[j].y;
			ZBuffer[j].y=temp;
		}
	 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -