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

📄 smap.cpp

📁 用VC++及DirectX实现的SuperMario小游戏
💻 CPP
字号:
/*
Author: Bear

This source is free to anybody.
If you have any problem with this or some advice to me, please:
    mailto: heyang22118952.student@sina.com
        or  yang45249.student@sina.com
or you can contact me through my QQ:     261570581

Welcome to discuss game programming techniques with me :)
And I like to play games!
*/
#include ".\smap.h"
#include <fstream>

SMap::SMap(void)
: loaded(false), nSurfaces(0), worldX(0), worldY(0), speedX(0), speedY(0), rowStart(0), rowEnd(1), numCols(10)
{
	for(int i=0; i<MAX_SLICE_NUM; i++) {
		this->ppSurfaces[i] = NULL;
	}
	for(int j=0; j<MAP_HEIGHT; j++)
		for(int k=0; k<MAP_WIDTH; k++)
			this->m_map[j][k] = 0;
}

SMap::~SMap(void)
{
	UnLoad();
}

bool SMap::Load(BDirectDraw* pdd, BBitmap* pbitmap, const char* fileName)
{
	assert(pdd != NULL);
	assert(pbitmap != NULL);
	assert(fileName != NULL && fileName != "");

	if(loaded) {
		UnLoad();
	}
	std::ifstream ifs(fileName);
	assert(ifs);
	char buf[255];
	ifs.getline(buf, 255);	//comment
	ifs>>worldX>>worldY>>speedX>>speedY>>rowStart>>rowEnd>>numCols;
	assert(rowStart>=0);
	assert(rowEnd >= 0);
	assert(rowEnd >= rowStart);
	assert(numCols > 0);
	for(int i=0; i<MAP_HEIGHT; i++) {
		ifs>>buf;
		for(int j=0; j<MAP_WIDTH; j++) {
			if(buf[j] >= '0' && buf[j] <= '9')
				this->m_map[i][j] = buf[j] - '0';
			else if(buf[j] >= 'a' && buf[j] <= 'z')
				this->m_map[i][j] = buf[j] - 'a' + 10;
			else if(buf[j] >= 'A' && buf[j] <= 'Z')
				this->m_map[i][j] = buf[j] - 'A' + 10;
			else  {
				return false;
			}
		}
	}
	for(int i=0; i<=rowEnd-rowStart; i++) {
		for(int j=0; j<numCols; j++) {
			assert((i*numCols+j) < MAX_SLICE_NUM);
			if((this->ppSurfaces[i*numCols+j] = new BDirectDrawSurface(pdd->CreateOffScreenSurface(SLICE_WIDTH, SLICE_HEIGHT, 0, -1))) == NULL) {
				DXTRACE_MSG("Failed to create new directdraw surface, may be there's not enough memory.");
				return false;
			}
			assert(SLICE_WIDTH*j < pbitmap->GetWidth());
			assert(SLICE_HEIGHT*(i+rowStart) < pbitmap->GetHeight());
			if(!this->ppSurfaces[i*numCols+j]->LoadBitmap(pbitmap, j, i+rowStart)) {
				DXTRACE_MSG("BDirectDrawSurface::LoadBitmap() failed");
				return false;
			}
		}
	}
	this->nSurfaces = (rowEnd-rowStart+1) * numCols;
	
	loaded = true;
	return loaded;
}

bool SMap::Draw(BDirectDrawSurface* pDest)
{
	assert(loaded);
	int i, j, k;

	for(i=0; i<MAP_HEIGHT; i++) {
		j = worldX / SLICE_WIDTH;
		k = j + SCREEN_WIDTH/SLICE_WIDTH;
		k += (((worldX % SLICE_WIDTH) == 0) ? 0 : 1);
		for(; j<k; j++) {
			assert(this->m_map[i][j] < this->nSurfaces);
			if(!pDest->Draw(this->ppSurfaces[this->m_map[i][j]], j*SLICE_WIDTH - worldX, i*SLICE_HEIGHT, 1, false)) {
				DXTRACE_MSG("SMap::Draw(): Failed to draw surface");
			}
		}
	}

	return true;
}

void SMap::UnLoad(void)
{
	for(int i = 0; i < this->nSurfaces; i++) {
		if(this->ppSurfaces[i] != NULL) {
			delete this->ppSurfaces[i];
			this->ppSurfaces[i] = NULL;
		}
	}
	loaded = false;
}

bool SMap::MoveTo(int posX, int posY)
{
	assert(posY == 0);	//this map do not surpport vertical move currently

	this->worldX = posX;
	this->worldY = posY;

	return CheckWorld();
}

bool SMap::CheckWorld(void)
{
	if(this->worldX < 0)
	{
		this->worldX = 0;
		return false;
	}
	if(this->worldX > (MAP_WIDTH*SLICE_WIDTH - SCREEN_WIDTH)) {
		this->worldX = MAP_WIDTH*SLICE_WIDTH - SCREEN_WIDTH;
		return false;
	}

	return true;
}

int SMap::GetX(void)
{
	return this->worldX;
}

int SMap::GetY(void)
{
	return this->worldY;
}

bool SMap::Save(char* fileName)
{
	assert(fileName != NULL && fileName != "");
	std::ofstream ofs(fileName);
	assert(ofs);
	ofs<<";worldX\tworldY\tspeedX\tspeedY\trowStart\trowEnd\tnumCols"<<std::endl;
	ofs<<"0\t0\t"<<speedX<<"\t"<<speedY<<"\t"<<rowStart<<"\t"<<rowEnd<<"\t"<<numCols<<"\t"<<std::endl;
	for(int i=0; i<MAP_HEIGHT; i++) {
		for(int j=0; j<MAP_WIDTH; j++) {
			int t = this->m_map[i][j];
			if(t>=0 && t<=9)
				ofs<<t;
			else if(t>9 && t<36)
				ofs<<(char)(t+'a'-10);
			else assert(false);
		}
		ofs<<std::endl;
	}

	return true;
}

bool SMap::DrawSlices(BDirectDrawSurface* pDest, int startX, int startY, float scale)
{
	assert(loaded);
	assert(pDest != NULL);
	assert(scale > 0);

	for(int i = 0; i < this->nSurfaces; i++) {
		if(!pDest->Draw(this->ppSurfaces[i], startX + (int)(i*SLICE_WIDTH*scale), startY, scale, false)) {
			DXTRACE_MSG("SMap::DrawSlices(): Failed to draw surface");
		}
	}

	return true;
}

BDirectDrawSurface* SMap::GetSurface(int index)
{
	assert(loaded);
	assert(index >= 0 && index < this->nSurfaces);

	return this->ppSurfaces[index];
}

void SMap::Set(int row, int col, int surfaceIndex)
{
	assert(row >= 0 && row < MAP_HEIGHT);
	assert(col >= 0 && col < MAP_WIDTH);
	assert(surfaceIndex >= 0 && surfaceIndex < this->nSurfaces);

	this->m_map[row][col] = surfaceIndex;
}

⌨️ 快捷键说明

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