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

📄 sspritefactory.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 ".\sspritefactory.h"
#include "SMario.h"
#include <fstream>

SSpriteFactory::SSpriteFactory(void)
: loaded(false), nKinds(0)
{
	for(int i=0; i<SPRITE_KINDS; i++) {
		for(int j=0; j<MAX_FRAME_NUM; j++) {
			this->ppSurfaces[i][j] = NULL;
		}
	}
}

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

bool SSpriteFactory::Load(BDirectDraw* pdd, BBitmap* pBitmap, const char* spriteFileName)
{
	assert(pdd != NULL);
	assert(pBitmap != NULL);
	assert(spriteFileName != NULL && spriteFileName != "");

	if(loaded) {
		UnLoad();
	}
	std::ifstream ifs(spriteFileName);
	assert(ifs);
	char buf[256];
	ifs.getline(buf, 255);	//comment
	int i=0;
	while(!ifs.eof()) {
		if(i == SPRITE_KINDS)
			return false;
		ifs>>name[i]>>type[i]>>width[i]>>height[i]>>speedX[i]>>speedY[i]>>row[i]>>colStart[i]>>colEnd[i]>>animSeq[i];
		if(width[i]<=0 || height[i]<=0 || row[i]<0 || colStart[i]<0 || colEnd[i]<0 || colStart[i]>colEnd[i] || colEnd[i]-colStart[i]>=MAX_FRAME_NUM) {
			return false;
		}
		for(int j=0; j <= colEnd[i]-colStart[i]; j++) {
			if((this->ppSurfaces[i][j] = new BDirectDrawSurface(pdd->CreateOffScreenSurface(width[i], height[i], 0, 0x0045ff))) == NULL) {
				DXTRACE_MSG("Failed to create new directdraw surface, may be there's not enough memory.");
				return false;
			}
			if(!this->ppSurfaces[i][j]->LoadBitmap(pBitmap, colStart[i]+j, row[i])) {
				DXTRACE_MSG("BDirectDrawSurface::LoadBitmap() failed");
				return false;
			}
		}
		i++;
	}
	nKinds = i;
	assert(nKinds == SPRITE_KINDS);

	loaded = true;
	return loaded;
}

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

SSprite* SSpriteFactory::Create(const char* spriteName, int posX, int posY)
{
	assert(loaded);
	assert(spriteName != NULL && spriteName != "");

	std::string temp(spriteName);
	for(int i=0; i<SPRITE_KINDS; i++) {
		if(temp.compare(name[i]) == 0) {
			SSprite* sprite = NULL;
			if(name[i].compare("mario") == 0) {
				sprite = new SMario();
			}
			else {
				sprite = new SSprite();
			}
			if(sprite == NULL) {
				DXTRACE_MSG("Create sprite failed, may be not enough memory");
				return NULL;
			}
			if(!sprite->Init(this->name[i], this->ppSurfaces[i], type[i], width[i], height[i],
				speedX[i], speedY[i], posX, posY, colEnd[i]-colStart[i]+1, animSeq[i])) {
				DXTRACE_MSG("Init sprite error");
				return NULL;
			}
			return sprite;
		}
	}
	return NULL;
}

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

	for(int i=0; i<this->nKinds; i++) {
		if(!pDest->Draw(ppSurfaces[i][0], startX + (int)(i*SLICE_WIDTH*scale), startY, scale, false)) {
			DXTRACE_MSG("SSpriteFactory::DrawSprites(): Failed to draw sprite");
			return false;
		}
	}
	return true;
}

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

	return this->ppSurfaces[index][0];
}

SSprite* SSpriteFactory::Create(int index, int posX, int posY)
{
	assert(loaded);
	assert(index >=0 && index < this->nKinds);
			
	SSprite* sprite = NULL;
	if(name[index].compare("mario") == 0) {
		sprite = new SMario();
	}
	else {
		sprite = new SSprite();
	}
	if(sprite == NULL) {
		DXTRACE_MSG("Create sprite failed, may be not enough memory");
		return NULL;
	}
	if(!sprite->Init(this->name[index], this->ppSurfaces[index], type[index], width[index], height[index],
		speedX[index], speedY[index], posX, posY, colEnd[index]-colStart[index]+1, animSeq[index])) {
			DXTRACE_MSG("Init sprite error");
			return NULL;
	}

	return sprite;
}

⌨️ 快捷键说明

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