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

📄 ssprite.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 ".\ssprite.h"

SSprite::SSprite(void)
{
	this->m_type = 0;
	this->m_nCurFrame = 0;
	this->m_nFrames = 0;
	this->m_width = 0;
	this->m_height = 0;
	this->m_posX = 0;
	this->m_posY = 0;
	this->m_speedX = 0;
	this->m_speedY = 0;
	this->forward = true;
	this->m_animCount = 0;
	count = 0;
	temp = 0;
	for(int i=0; i<MAX_SEQUENCE_LENGTH; i++) {
		m_animSeq[i] = 0;
	}
	for(int j=0; j<MAX_FRAME_NUM; j++) {
		this->m_ppSurfaces[j] = NULL;
	}
}

SSprite::~SSprite(void)
{
	UnInit();
}

bool SSprite::Init(const std::string& name, BDirectDrawSurface* ppSurfaces[], int type, int width, int height, int speedX, int speedY, int posX, int posY, int nFrames, std::string strAnimSeq)
{
	assert(ppSurfaces != NULL);
	assert(nFrames <= MAX_FRAME_NUM);
	assert(width > 0);
	assert(height > 0);
	assert(strAnimSeq.length() <= MAX_SEQUENCE_LENGTH*2);

	this->m_name = name;
	this->m_type = type;
	this->m_nCurFrame = 0;
	this->m_nFrames = nFrames;
	this->m_width = width;
	this->m_height = height;
	this->m_posX = posX;
	this->m_posY = posY;
	this->m_speedX = speedX;
	this->m_speedY = speedY;
	this->forward = true;

	count = 0;
	for(unsigned int i=0; i<strAnimSeq.length(); i+=2) {
		assert(strAnimSeq[i] >= '0' && strAnimSeq[i]-'0' <= nFrames);
		this->m_animSeq[count++] = strAnimSeq[i] - '0';
	}
	this->m_animCount = count;

	for(int i=0; i<nFrames; i++) {
		assert(ppSurfaces[i] != NULL);
		this->m_ppSurfaces[i] = ppSurfaces[i];
	}

	count = 0;
	temp = 0;

	return true;
}

bool SSprite::Draw(BDirectDrawSurface* pDest, int worldX, int worldY)
{
	assert(pDest != NULL);

	int screenX = m_posX - worldX;
	int screenY = m_posY - worldY;

	bool bret = false;

	assert(m_nCurFrame >=0 && m_nCurFrame < MAX_FRAME_NUM);
	assert(this->m_ppSurfaces[m_nCurFrame] != NULL);
	if(forward) {
		bret = pDest->Draw(this->m_ppSurfaces[m_nCurFrame], screenX, screenY);
	}
	else {
		//this function needs hardware surpport and it is very slow
		bret = pDest->DrawMirror(this->m_ppSurfaces[m_nCurFrame], screenX, screenY);
	}
	if(!bret) {
		DXTRACE_MSG("Draw sprite error");
	}

	return bret;
}

void SSprite::Anim(void)
{
	if(this->m_animCount == 1)
		return;
	this->m_posX += this->m_speedX;
	count++;
	if(count == 8) {
		count = 0;
		temp++;
		if(temp >= this->m_animCount) {
			temp = 0;
			if(this->m_speedX != 0) {
				this->m_speedX = -this->m_speedX;
				forward = !forward;
			}
		}
		this->m_nCurFrame = this->m_animSeq[temp];
	}
}

void SSprite::UnInit(void)
{
}

int SSprite::GetX(void)
{
	return this->m_posX;
}

int SSprite::GetY(void)
{
	return this->m_posY;
}

bool SSprite::HitTest(SSprite* sprite)
{
	assert(sprite != NULL);

	int x11 = this->m_posX, x12 = this->m_posX + this->m_width;
	int y11 = this->m_posY, y12 = this->m_posY + this->m_height;
	int x21 = sprite->m_posX, x22 = sprite->m_posX + sprite->m_width;
	int y21 = sprite->m_posY, y22 = sprite->m_posY + sprite->m_height;

	if(x12 > x21 && x11 < x22)
		if(y12 > y21 && y11 < y22) {
			return true;
		}

	return false;
}

int SSprite::GetType(void)
{
	return this->m_type;
}

const std::string& SSprite::GetName(void)
{
	return this->m_name;
}

⌨️ 快捷键说明

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