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

📄 mapeditdlg.cpp

📁 一个类似坦克大战的小小游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/**************************************************************************************
	Project  Name			: Map Edit of my game
	Module Name				: Main UI
	File Name				: MapEditDlg.cpp : implementation file
	Create					: 2007-7-5, by Vigame
	Completed				: 2007-7-12, by Vigame
	Update					: 
	Copyright				: o(∩_∩)o
	Reference				: 
	Abstrct					: You may do whatever you want with this code, as long as 
							 you include this copyright notice in your implementation
							 files.Comments and bug Reports: vigor1985@gmail.com,
							  QQ:15061260, I'll right here wait for you, ^_^
																by Vigame, 2007-7-12
 **************************************************************************************/

#include "stdafx.h"
#include "MapEdit.h"
#include "MapEditDlg.h"
#include "Fense.h"
#include "Reef.h"
#include "Island.h"
#include "Seaweed.h"
#include "Wall.h"
#include "Barbette.h"

#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")

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

const int LINE_LENGTH = 64;		// Used to draw square
const int MAPVIEW_X = 24;
const int MAPVIEW_Y = 120;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapEditDlg dialog

CMapEditDlg::CMapEditDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMapEditDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMapEditDlg)

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMapEditDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMapEditDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMapEditDlg, CDialog)
	//{{AFX_MSG_MAP(CMapEditDlg)
	ON_COMMAND(ID_OPEN, OnLoadmap)
	ON_COMMAND(ID_SAVE, OnSavemap)
	ON_COMMAND(ID_NEW, OnNewmap)
//	ON_HELP()
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_NEWMAP, OnNewmap)
	ON_BN_CLICKED(IDC_LOADMAP, OnLoadmap)
	ON_BN_CLICKED(IDC_SAVEMAP, OnSavemap)
	ON_WM_TIMER()
	ON_WM_CANCELMODE()
	ON_BN_CLICKED(IDC_FENSE, OnFense)
	ON_BN_CLICKED(IDC_BARBETTE, OnBarbette)
	ON_BN_CLICKED(IDC_WALL, OnWall)
	ON_BN_CLICKED(IDC_ISALAND, OnIsaland)
	ON_BN_CLICKED(IDC_REEF, OnReef)
	ON_BN_CLICKED(IDC_SEAWEED, OnSeaweed)
	ON_WM_MOUSEMOVE()
	ON_WM_CAPTURECHANGED()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_CHAR()
	ON_WM_LBUTTONDOWN()
	ON_WM_CHARTOITEM()
	ON_WM_LBUTTONUP()
	ON_WM_CLOSE()
	ON_WM_KEYDOWN()
	ON_WM_COMPAREITEM()
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_EXIT, &CMapEditDlg::OnExit)
	ON_COMMAND(ID_HELP, &CMapEditDlg::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapEditDlg message handlers

BOOL CMapEditDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	// Set timer
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	m_menu.LoadMenu(IDR_MENU1);
	SetMenu(&m_menu);
	
	// TODO: Add extra initialization here
	GetDlgItem(IDC_SELECTE)->MoveWindow(192, 26, 640, 64);
	GetDlgItem(IDC_MAPVIEW)->MoveWindow(24, 120, MAP_WIDTH, MAP_HEIGHT);

//	Reset();	// Reset data, I use it in CreateNewMap().

	// Init map view resources
	m_gameGraphics.InitD3D(::GetDlgItem(m_hWnd, IDC_MAPVIEW));
	m_gameResource.LoadAll(m_gameGraphics.Getd3dDevice(), D3DCOLOR_ARGB(255, 0 ,0, 0));

	// Init select box resources, er...this is a very foolish method to fill the select
	// box with pictures, but it's easy. You can use the resource load by m_gameResource,
	// so that you can save 4MB RAM I guess, but you have more calculation. I'm lazy, :)
	m_selectGraphics.InitD3D(::GetDlgItem(m_hWnd, IDC_SELECT));
	m_selectResource.LoadAll(m_selectGraphics.Getd3dDevice(), D3DCOLOR_ARGB(255, 0 ,0, 0));

	// Create new map
	CreateNewMap();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMapEditDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMapEditDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		Refresh();
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMapEditDlg::OnQueryDragIcon()
{

	return (HCURSOR) m_hIcon;
}

// New map ///////////////////////////////////////////////////////////////

void CMapEditDlg::OnNewmap() 
{
	// TODO: Add your control notification handler code here
	int nRtn = ::MessageBox(m_hWnd, "是否保存原来的地图?", "保存地图", MB_YESNOCANCEL);
	if (IDYES == nRtn)
	{
		m_gameMap.SaveMap(m_listMapObject);
		CreateNewMap();
	}
	else if (IDNO == nRtn)
	{
		CreateNewMap();
	}

}

// Load map //////////////////////////////////////////////////////////////

void CMapEditDlg::OnLoadmap()
{
	int nRtn = ::MessageBox(m_hWnd, "是否保存修改的地图?", "保存地图", MB_YESNOCANCEL);
	if (IDYES == nRtn)
	{
		m_gameMap.SaveMap(m_listMapObject);
		Reset();
		m_gameMap.LoadMap(m_listMapObject, m_gameResource);
	}
	else if (IDNO == nRtn)
	{
		Reset();
		m_gameMap.LoadMap(m_listMapObject, m_gameResource);
	}	
}

// Save map //////////////////////////////////////////////////////////////

void CMapEditDlg::OnSavemap()
{
	m_gameMap.SaveMap(m_listMapObject);
}

// Refresh //////////////////////////////////////////////////////////////

void CMapEditDlg::Refresh()
{
	if (m_gameGraphics.BeginScene())
	{
		DrawSea();
		DrawScenery();
		m_gameGraphics.EndScene();
	}

	if (m_selectGraphics.BeginScene())
	{
		FillSelectBox();
		m_selectGraphics.EndScene();
	}
	DrawSelectGridding();
	DrawSelectSquare();
	FlagObjectSelected();
}

// Draw sea /////////////////////////////////////////////////////////////

void CMapEditDlg::DrawSea()
{
	// Draw background -- sea
	list<CGameObject>::iterator itr;
	for (itr = m_listSea.begin(); itr != m_listSea.end(); itr++)
	{		
		// Draw sea
		int i, j;
		for (i = 0; i < 960; i += 64)
			for (j = 0; j < 540; j += 64)
			{
				itr->Draw(m_gameGraphics.Getd3dDevice(), i, j);
			}		
	}
}

// Draw Fense ////////////////////////////////////////////////////////////

void CMapEditDlg::DrawScenery()
{
	// Sort first
	m_listMapObject.sort();	

	// Draw the objects
	list<CGameObject>::iterator itr;
	for (itr = m_listMapObject.begin(); itr != m_listMapObject.end(); itr++)
	{		
		itr->Draw(m_gameGraphics.Getd3dDevice(), itr->m_nX, itr->m_nY);
	}
}

// Fill Select box ///////////////////////////////////////////////////////

void CMapEditDlg::FillSelectBox()
{
	switch (m_selectType)
	{
	case ST_Fense:		// Fill with fense
		{
			int i;
			for (i = 0; i < FENSE_COUNT; i++)
			{
				LPDIRECT3DTEXTURE9 pTexture = m_selectResource.m_textureFense[i].m_pTexture;
				m_selectGraphics.DrawObject(pTexture, i * 64, i, 64, 64, -1);
			}
			break;
		}

	case ST_Barbette:	// Fill with barbette
		{
			int i;
			for (i = 0; i < BARBETTE_COUNT; i++)
			{
				LPDIRECT3DTEXTURE9 pTexture = m_selectResource.m_textureBarbette[i].m_pTexture;
				m_selectGraphics.DrawObject(pTexture, i * 64, i, 64, 64, -1);
			}
			break;
		}

	case ST_Wall:		// Fill with walls
		{
			int i;
			for (i = 0; i < WALL_COUNT; i++)
			{
				LPDIRECT3DTEXTURE9 pTexture = m_selectResource.m_textureWall[i].m_pTexture;
				m_selectGraphics.DrawObject(pTexture, i * 64, i, 64, 64, -1);
			}
			break;
		}

	case ST_Island:		// Fill with islands
		{
			int i;
			for (i = 0; i < ISLAND_COUNT; i++)
			{
				LPDIRECT3DTEXTURE9 pTexture = m_selectResource.m_textureIsland[i].m_pTexture;
				m_selectGraphics.DrawObject(pTexture, i * 64, i, 64, 64, -1);
			}
			break;
		}

	case ST_Reef:		// Fill with the reef
		{
			int i;
			for (i = 0; i < REEF_COUNT; i++)
			{
				LPDIRECT3DTEXTURE9 pTexture = m_selectResource.m_textureReef[i].m_pTexture;
				m_selectGraphics.DrawObject(pTexture, i * 64, i, 64, 64, -1);
			}
			break;
		}

	default:
		{
			LPDIRECT3DTEXTURE9 pTexture = m_selectResource.m_textureSeaweed.m_pTexture;
			m_selectGraphics.DrawObject(pTexture, 0, 0, 64, 64, -1);
			break;
		}
	}
}

// Draw select gridding //////////////////////////////////////////////////////

void CMapEditDlg::DrawSelectGridding()
{
	int i;
	CClientDC dc(this);
	CPen pen(PS_SOLID, 1, RGB(0, 255, 0));
	CPen *pOldPen = dc.SelectObject(&pen);

	for (i = 192; i < (192 + 640); i += 64)
	{
		dc.MoveTo(i, 27);
		dc.LineTo(i + 64, 27);
		dc.LineTo(i + 64, 64 + 24);
		dc.LineTo(i, 64 + 24);
		dc.LineTo(i, 27);
	}

	dc.SelectObject(pOldPen);
}

// Draw squrare /////////////////////////////////////////////////////////

void CMapEditDlg::DrawSelectSquare()
{
	// Get mouse position first
	CPoint point;
	::GetCursorPos(&point);

⌨️ 快捷键说明

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