HGE学习(1) 粒子系统

为了不重复写代码,自己封装了一下HGE,用着还行。

粒子系统需要指定一个修饰的hgeSprite,及一个例子编辑器做出来的东西,更新了粒子数量后执行Update函数来进行更新,在RenderFunc里画出来。我是用的HGE的例子里面的。

在FrameFunc中更新粒子数量及位置。

 

#include "SGameBase.h"
#include <hge.h>
#include <hgesprite.h>
#include <hgefont.h>
#include <hgeparticle.h>

class MyGame:public SGameBase
{
public:
							MyGame(const char* szTitle):SGameBase(szTitle){}
	virtual					~MyGame(){}
protected:
	virtual bool			UserInitial();
	virtual void			UserUninitial();
	virtual void			UserFrameFunc();
	virtual void			UserRenderFunc();
private:
	hgeSprite*				m_pSprBall;
	hgeSprite*				m_pSprPar;
	HTEXTURE				m_hSrc;
	hgeFont*				m_pFont;
	hgeParticleSystem*		m_pPar;
};


 

#include "MyGame.h"

const int		g_nSpeed=800;
const float		g_fFriction=0.95;
static float fX=16;
static float fY=16;

bool MyGame::UserInitial()
{
	m_hSrc=0;
	m_pSprBall=NULL;
	m_pPar=NULL;
	m_pSprPar=NULL;

	m_hSrc=m_pHGE->Texture_Load("particles.png");
	S_INITIAL_TEST(m_hSrc);

	m_pSprBall=new hgeSprite(m_hSrc,96,64,32,32);
	S_INITIAL_TEST(m_pSprBall);
	m_pSprBall->SetHotSpot(16,16);
	m_pSprBall->SetColor(0xFFFFA000);

	m_pFont=new hgeFont("font2.fnt");
	S_INITIAL_TEST(m_pFont);

	m_pSprPar=new hgeSprite(m_hSrc,32,32,32,32);
	S_INITIAL_TEST(m_pSprPar);

	m_pPar=new hgeParticleSystem("trail.psi",m_pSprPar);
	S_INITIAL_TEST(m_pPar);
	m_pPar->Fire();

	return true;
}

void MyGame::UserUninitial()
{
	m_pHGE->Texture_Free(m_hSrc);
}

void MyGame::UserFrameFunc()
{
	static float fDisX=0.0f;
	static float fDisY=0.0f;

	float dt=m_pHGE->Timer_GetDelta();
	if(m_pHGE->Input_GetKeyState(HGEK_LEFT))
	{
		fDisX-=dt*g_nSpeed;
	}
	if(m_pHGE->Input_GetKeyState(HGEK_RIGHT))
	{
		fDisX+=dt*g_nSpeed;
	}
	if(m_pHGE->Input_GetKeyState(HGEK_UP))
	{
		fDisY-=dt*g_nSpeed;
	}
	if(m_pHGE->Input_GetKeyState(HGEK_DOWN))
	{
		fDisY+=dt*g_nSpeed;
	}

	fDisX*=g_fFriction;
	fDisY*=g_fFriction;


	fX+=fDisX;
	fY+=fDisY;

	if(fX<=16)
	{
		fX=16;
		fDisX=-fDisX;
	}
	if(fX>=784)
	{
		fX=784;
		fDisX=-fDisX;
	}
	if(fY<=16)
	{
		fY=16;
		fDisY=-fDisY;
	}
	if(fY>=584)
	{
		fY=584;
		fDisY=-fDisY;
	}

	m_pPar->info.nEmission=(fDisX*fDisX+fDisY*fDisY)/15;
	m_pPar->MoveTo(fX,fY);
	m_pPar->Update(m_pHGE->Timer_GetDelta());
}

void MyGame::UserRenderFunc()
{
	m_pFont->printf(5,5,HGETEXT_LEFT,"FPS: %d",m_pHGE->Timer_GetFPS());
	m_pPar->Render();
	m_pSprBall->Render(fX,fY);
}


 

#include "MyGame.h"

int CALLBACK WinMain( IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, IN LPSTR lpCmdLine, IN int nShowCmd )
{
	MyGame game("TANK");
	game.SetGameCursor("cursor.png");
	game.Exec();
}


 

 

你可能感兴趣的:(UP,callback,float)