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

📄 gamemap.cpp

📁 一个类似坦克大战的小小游戏
💻 CPP
字号:
// Map.cpp: implementation of the CMap class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MapEdit.h"
#include "GameMap.h"
#include <commdlg.h>
#include "Fense.h"
#include "Wall.h"
#include "Seaweed.h"
#include "Barbette.h"
#include "Island.h"
#include "Reef.h"

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

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

CGameMap::CGameMap() : m_nWidth(MAP_WIDTH), m_nHeight(MAP_HEIGHT)
{
	std::locale::global(std::locale("")); // Add by Vigame, 2007-7-27, to solve the 
										  // problem can not read chinese path.
}

CGameMap::~CGameMap()
{

}

// Map Controls ///////////////////////////////////////////////////////

BOOL CGameMap::CreateMap()
{
	return TRUE;
}

BOOL CGameMap::LoadMap(list<CGameObject> &inObjectList, CGameResource &inResource)
{
	OPENFILENAME ofn;       // Common dialog box structure   
	char szFile[255];       // Buffer for file name   
//	HANDLE hFile;           // The handle of file 
	
	// Initialize OPENFILENAME   
	ZeroMemory(&ofn, sizeof(ofn));   
	ofn.lStructSize = sizeof(ofn);   
	ofn.hwndOwner = NULL;   
	ofn.lpstrFile = szFile;   
	  
	// Set lpstrFile[0] to '\0' so that GetOpenFileName does not     
	// use the contents of szFile to initialize itself.   
	ofn.lpstrFile[0] = '\0';   
	ofn.nMaxFile = sizeof(szFile);   
	ofn.lpstrFilter = "*.CMAP\0*.CMAP\0";   
	ofn.nFilterIndex = 1;   
	ofn.lpstrFileTitle = NULL;   
	ofn.nMaxFileTitle = 0;   
	ofn.lpstrInitialDir = NULL;   
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;   
	
	// Display the save dialog box.     
	if (GetOpenFileName(&ofn))   
	{   
		// Load map
		ifstream inData;
		inData.open(ofn.lpstrFile);

		// Load object count first
		inData >> m_nObjectCount;
	
		// Save the position of objects
		int i;
		for (i = 0; i != m_nObjectCount; i++)
		{
			int objType;
			inData >> objType;
			switch (objType)
			{
			case 2:		// OT_Fense
				{
					CFense fense;
					inData >> fense.m_nX >> fense.m_nY >> fense.m_nWidth >> fense.m_nHeight;
					inData >> fense.m_nTextureIndex >> fense.m_nHp;
					fense.m_pTexture = inResource.m_textureFense[fense.m_nTextureIndex].m_pTexture;
					fense.m_objType = (Object_Type)objType;
					inObjectList.push_back(fense);
					break;
				}
			case 3:		// OT_Wall
				{
					CWall wall;
					inData >> wall.m_nX >> wall.m_nY >> wall.m_nWidth >> wall.m_nHeight;
					inData >> wall.m_nTextureIndex >> wall.m_nHp;
					wall.m_pTexture = inResource.m_textureWall[wall.m_nTextureIndex].m_pTexture;
					wall.m_objType = (Object_Type)objType;
					inObjectList.push_back(wall);
					break;
				}
			case 4:		// OT_Barbette
				{
					CBarbette barbette;
					inData >> barbette.m_nX >> barbette.m_nY >> barbette.m_nWidth >> barbette.m_nHeight;
					inData >> barbette.m_nTextureIndex >> barbette.m_nHp;
					barbette.m_pTexture = inResource.m_textureBarbette[barbette.m_nTextureIndex].m_pTexture;
					barbette.m_objType = (Object_Type)objType;
					inObjectList.push_back(barbette);
					break;
				}
			case 5:		// OT_Island
				{
					CIsland island;
					inData >> island.m_nX >> island.m_nY >> island.m_nWidth >> island.m_nHeight;
					inData >> island.m_nTextureIndex >> island.m_nHp;
					island.m_pTexture = inResource.m_textureIsland[island.m_nTextureIndex].m_pTexture;
					island.m_objType = (Object_Type)objType;
					inObjectList.push_back(island);
					break;
				}
			case 6:		// OT_Seaweed
				{
					CSeaweed seaweed;
					inData >> seaweed.m_nX >> seaweed.m_nY >> seaweed.m_nWidth >> seaweed.m_nHeight;
					inData >> seaweed.m_nTextureIndex >> seaweed.m_nHp;
					seaweed.m_pTexture = inResource.m_textureSeaweed.m_pTexture;
					seaweed.m_objType = (Object_Type)objType;
					inObjectList.push_back(seaweed);
					break;
				}
			case 7:		// OT_Reef
				{
					CReef reef;
					inData >> reef.m_nX >> reef.m_nY >> reef.m_nWidth >> reef.m_nHeight;
					inData >> reef.m_nTextureIndex >> reef.m_nHp;
					reef.m_pTexture = inResource.m_textureReef[reef.m_nTextureIndex].m_pTexture;
					reef.m_objType = (Object_Type)objType;
					inObjectList.push_back(reef);
					break;
				}
			default:
				break;

			} // End switch
		}	// End for
		inData.close();
		return TRUE;
	}
	return FALSE;
}

BOOL CGameMap::SaveMap(list<CGameObject> &inObjectList)
{
	OPENFILENAME ofn;       // Common dialog box structure   
	char szFile[255];       // Buffer for file name   
//	HANDLE hFile;           // The handle of file 
	
	// Initialize OPENFILENAME   
	ZeroMemory(&ofn, sizeof(ofn));   
	ofn.lStructSize = sizeof(ofn);   
	ofn.hwndOwner = NULL;   
	ofn.lpstrFile = szFile;   
	  
	// Set lpstrFile[0] to '\0' so that GetOpenFileName does not     
	// use the contents of szFile to initialize itself.   
	ofn.lpstrFile[0] = '\0';   
	ofn.nMaxFile = sizeof(szFile);   
	ofn.lpstrFilter = "*.CMAP\0*.CMAP\0";   
	ofn.nFilterIndex = 1;   
	ofn.lpstrFileTitle = NULL;   
	ofn.nMaxFileTitle = 0;   
	ofn.lpstrInitialDir = NULL;   
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;   
	
	// Display the save dialog box.     
	if (GetSaveFileName(&ofn))   
	{   
		// Save map
		ofstream outData;
		outData.open(ofn.lpstrFile);

		// Save object count first
		outData << inObjectList.size() << endl;
	
		// Save the position of objects
		list<CGameObject>::iterator itr;
		for (itr = inObjectList.begin(); itr != inObjectList.end(); itr++)
		{
			outData << itr->m_objType << setw(8);
			outData << itr->m_nX << setw(8) << itr->m_nY << setw(8);
			outData << itr->m_nWidth << setw(8) << itr->m_nHeight << setw(8);
			outData << itr->m_nTextureIndex << setw(8) << itr->m_nHp << endl;
		}
		outData.close();
		return TRUE;
	}
	return FALSE;
}

⌨️ 快捷键说明

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