HGE学习(2) Target

 Target我理解为一个场景,在绘制函数中,可以指定为这个场景,即画出来的东西在这个场景的纹理上,然后即可多次在屏幕上画出拥有这个纹理的精灵。

 

 

 

#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();
	virtual bool			RealRenderFunc();
private:
	hgeSprite*				m_pSprBall;
	hgeSprite*				m_pSprPar;
	HTEXTURE				m_hSrc;
	hgeFont*				m_pFont;
	hgeParticleSystem*		m_pPar;
	HTARGET					m_hTar;
	hgeSprite*				m_pSpTar;
};


 

#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_pSpTar=NULL;
	m_hTar=0;

	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();

	m_hTar=m_pHGE->Target_Create(512,512,false);
	m_pSpTar=new hgeSprite(m_pHGE->Target_GetTexture(m_hTar),0,0,512,512);
	S_INITIAL_TEST(m_pSpTar);

	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_pSpTar->Render(0,0);
	m_pSpTar->Render(0,100);
	m_pSpTar->Render(0,200);
}

bool MyGame::RealRenderFunc()
{
	m_pHGE->Gfx_BeginScene(m_hTar);
	m_pHGE->Gfx_Clear(0);
	m_pPar->Render();
	m_pSprBall->Render(fX,fY);
	m_pHGE->Gfx_EndScene();

	return SGameBase::RealRenderFunc();
}


 

#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();
}

你可能感兴趣的:(HGE学习(2) Target)