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

📄 mapeditframe.cpp

📁 游戏地图编辑器原理
💻 CPP
字号:
// mapeditFrame.cpp : implementation file
//

#include "stdafx.h"
#include "mapedit.h"
#include "mapeditFrame.h"
#include "mapApp.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// mapeditFrame

IMPLEMENT_DYNCREATE(mapeditFrame, CFrameWnd)

CBitmap* drawmap[200];
CBitmap* tempbmp;
CBitmap* tempbmp1;
CBitmap* tempbmp2;
CPoint get_mouse_point;
int offsetx,offsety;
int i,smap,getmap,mapppx;
int x,y,mapx=128,mapy=128,movex=0,movey=0,oldmapx,oldmapy;
char strp[13];
char strs[3];
int savemapx=128,savemapy=128;
int loadx,loady;
int add=0;
BOOL save=FALSE,mouse_left=FALSE,ESC=FALSE;
CRect selectmap[20];
CRect realmap[128][128];

CDC *pandc;
CDC *bmpdc;
CDC *screendc;
CDC *scr;
CDC *disscreendc;
int map[128][128];

mapeditFrame::mapeditFrame()
{Create(NULL,"mapeditor");
CClientDC dc(this);
	CFile file;
CString msg;
CString sFile("1.map");
pandc=new CDC;
bmpdc=new CDC;
screendc=new CDC;
scr=new CDC;
disscreendc=new CDC;
tempbmp=new CBitmap;
tempbmp2=new CBitmap;
tempbmp->CreateCompatibleBitmap(&dc,1024,600);
tempbmp2->CreateCompatibleBitmap(&dc,1024,600);
tempbmp1=new CBitmap;
tempbmp1->CreateCompatibleBitmap(&dc,2048,1024);
pandc->CreateCompatibleDC(&dc);
bmpdc->CreateCompatibleDC(&dc);
scr->CreateCompatibleDC(&dc);
screendc->CreateCompatibleDC(&dc);
disscreendc->CreateCompatibleDC(&dc);
pandc->SelectObject(tempbmp);
screendc->SelectObject(tempbmp1);
disscreendc->SelectObject(tempbmp2);




for(i=0;i<50;i++)//拼地图
	{sprintf(strp,"mapcon/%d.bmp",i);
	drawmap[i]=new CBitmap;
	drawmap[i]->m_hObject=(HBITMAP)::LoadImage(NULL,strp,IMAGE_BITMAP,51,51,LR_LOADFROMFILE);
	}

for(i=0;i<20;i++)
{
	bmpdc->SelectObject(drawmap[i]);
pandc->BitBlt(51*i+2,0,51,51,bmpdc,0,0,SRCCOPY);
selectmap[i].left=51*i+3;
selectmap[i].top=650;
selectmap[i].right=selectmap[i].left+51;
selectmap[i].bottom=selectmap[i].top+51;	
}
	


if(!file.Open(sFile,CFile::modeRead))
{
	msg.Format("Faile to open %s",&sFile);
	AfxMessageBox(msg);
}



file.Read(strs,3);
loadx=atoi(strs);
file.Read(strs,3);
loady=atoi(strs);

		

for(x=0;x<128;x++)
{
	for(y=0;y<128;y++)
	{
		file.Read(strs,3);
		map[y][x]=atoi(strs);
	}
}
file.Close();
movex=0;
movey=0;
Invalidate(TRUE);
return;
	



}
mapeditFrame::~mapeditFrame()
{
}


BEGIN_MESSAGE_MAP(mapeditFrame, CFrameWnd)
	//{{AFX_MSG_MAP(mapeditFrame)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_KEYDOWN()
	ON_BN_CLICKED(1,Onsave)
	ON_BN_CLICKED(2,onload)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// mapeditFrame message handlers

void mapeditFrame::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	int dismap;
	// TODO: Add your message handler code here
	
	// Do not call CFrameWnd::OnPaint() for painting messages
	
	for(x=0;x<21;x++)
{
	for(y=0;y<13;y++)
	{
		getmap=map[movey+y][movex+x];
		bmpdc->SelectObject(drawmap[getmap]);
		

		screendc->BitBlt(x*50,y*50,51,51,bmpdc,0,0,SRCCOPY);
		
	}
}

	dc.BitBlt(1,654,1024,51,pandc,0,0,SRCCOPY);

	dc.BitBlt(0,0,1024,600,screendc,0,0,SRCCOPY);
	dc.BitBlt(200,601,51,51,scr,0,0,SRCCOPY);
	dismap=movey+offsety;
	sprintf(strp,"mapy:%d  ",dismap);
	dc.TextOut(360,601,strp);
	dismap=movex+offsetx;
	sprintf(strp,"mapx:%d  ",dismap);
	dc.TextOut(300,601,strp);

	sprintf(strp,"%d",map[3][3]);
	dc.TextOut(550,601,strp);
	oldmapx=movex+offsetx;
	oldmapy=movey+offsety;
}

int mapeditFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
CButton* Onsave;
CButton* Onload;
Onsave=new CButton;
Onload=new CButton;
Onsave->Create("保存",BS_PUSHBUTTON|WS_VISIBLE,CRect(10,620,80,650),this,1);
Onload->Create("载入",BS_PUSHBUTTON|WS_VISIBLE,CRect(100,620,170,650),this,2);
	return 0;
SetTimer(1,50,NULL);
}







void mapeditFrame::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CRect selectrect;
	selectrect.top=0;
	selectrect.left=0;
	selectrect.right=selectrect.left+1024;
	selectrect.bottom=selectrect.top+600;


	mouse_left=TRUE;
	

	if(selectrect.PtInRect(point))
	{
	offsetx=point.x/51;
	offsety=point.y/51;
	map[movey+offsety][movex+offsetx]=smap;
	}

		for(i=0;i<20;i++)
	{
		if(selectmap[i].PtInRect(point))
		{
			smap=i+add;
		}
	}
	
	
scr->SelectObject(drawmap[smap]);

	for(x=0;x<21;x++)
{
	for(y=0;y<13;y++)
	{
		getmap=map[movey+y][movex+x];
		bmpdc->SelectObject(drawmap[getmap]);
		

		screendc->BitBlt(x*51,y*51,51,51,bmpdc,0,0,SRCCOPY);
		
	}
}
	Invalidate(FALSE);
	CFrameWnd::OnLButtonDown(nFlags, point);
}

void mapeditFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if(movey>0&&nChar==VK_UP)
		movey=movey-1;
	if(movey<114&&nChar==VK_DOWN)
		movey=movey+1;
	if(movex>0&&nChar==VK_LEFT)
		movex=movex-1;
	if(movex<106&&nChar==VK_RIGHT)
		movex=movex+1;
	if(nChar==VK_F1)
	{
		add=add+20;
		for(i=0;i<20;i++)
{
	bmpdc->SelectObject(drawmap[i+add]);
pandc->BitBlt(51*i+2,0,51,51,bmpdc,0,0,SRCCOPY);	
}
	}
	if(add>0&&nChar==VK_F2)
	{
		add=add-20;
			for(i=0;i<20;i++)
{
	bmpdc->SelectObject(drawmap[i+add]);
pandc->BitBlt(51*i+2,0,51,51,bmpdc,0,0,SRCCOPY);	
}
	}
	if(nChar==VK_ESCAPE)
		ESC=TRUE;
	Invalidate(FALSE);
	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

void mapeditFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
		Invalidate(TRUE);
	if(ESC)
		KillTimer(nIDEvent);
	CFrameWnd::OnTimer(nIDEvent);
}

void mapeditFrame::onload()
{CFile file;
CString msg;
CString sFile("1.map");

if(!file.Open(sFile,CFile::modeRead))
{
	msg.Format("Faile to open %s",&sFile);
	AfxMessageBox(msg);
}



file.Read(strs,3);
loadx=atoi(strs);
file.Read(strs,3);
loady=atoi(strs);

		

for(x=0;x<128;x++)
{
	for(y=0;y<128;y++)
	{
		file.Read(strs,3);
		map[y][x]=atoi(strs);
	}
}
file.Close();
movex=0;
movey=0;
Invalidate(TRUE);
return;
}
void mapeditFrame::Onsave()//保存地图文件
{

CFile file;
CString msg;
CString sFile("1.map");

if(!file.Open(sFile,CFile::modeCreate|CFile::modeWrite))
{
	msg.Format("Faile to create %s",&sFile);
	AfxMessageBox(msg);
}


sprintf(strs,"%d",savemapx);
file.Write(strs,3);
sprintf(strs,"%d",savemapy);
file.Write(strs,3);


		

for(x=0;x<128;x++)
{
	for(y=0;y<128;y++)
	{
		sprintf(strs,"%d  ",map[y][x]);
		file.Write(strs,3);
		
	}
}
file.Close();
MessageBox("保存成功","文件保存",MB_OK);

}

⌨️ 快捷键说明

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