HGE tutorial 03 学习笔记及摘录

Tutorial 03 - Using helper classes

In this tutorial we will learn how to use some of HGE helper classes. First, we include all the needed headers and declare the global pointer to the HGEinterface that is required by most of the helper classes to work:

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

HGE *hge=0;

Now we declare the pointers to the HGE objects we will use.

hgeSprite*          spr;
hgeSprite*          spt;
hgeFont*            fnt;
hgeParticleSystem*  par;

HTEXTURE            tex;

In the FrameFunc (frame function) we update the particle system object: we adjust it's emission rate based on the current sprite speed and move it to the current sprite location.

 
 par->info.nEmission=(int)(dx*dx+dy*dy)*2;
 par->MoveTo(x,y);
 par->Update(dt);

In the RenderFunc we render all our objects, calling their render methods:

 
  hge->Gfx_BeginScene();
  hge->Gfx_Clear(0);
  par->Render();
  spr->Render(x, y);
  fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d",
              hge->Timer_GetDelta(), hge->Timer_GetFPS());
  hge->Gfx_EndScene();

In the WinMain function after HGE is initiated we create the HGE objects, that we will use. First, we create and set up a sprite:

    
spr=new hgeSprite(tex, 96, 64, 32, 32);
spr->SetColor(0xFFFFA000);
spr->SetHotSpot(16, 16);

Then we load a font. The font is represented with two disk files: font1.fntand font1.png.

  
  fnt=new hgeFont("font1.fnt");

We create a particle system and a sprite for it:

    
spt=new hgeSprite(tex, 32, 32, 32, 32);
spt->SetBlendMode(
          BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
spt->SetHotSpot(16, 16);
par=new hgeParticleSystem("trail.psi", spt);
par->Fire();

Now all our objects have been created and we start the game loop:

 
   hge->System_Start();

When the game loop is finished, we delete all created HGE objects:

  
  delete par;
  delete fnt;
  delete spt;
  delete spr;

The rest of shutdown process is identical to the one demonstrated in previous tutorials. 

The complete source code with detailed comments for this tutorial you may find in the folder tutorials\tutorial03. The required media files you'll find in the folder tutorials\precompiled.


源码及简单注释

/*
** Haaf's Game Engine 1.8
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** hge_tut03 - Using helper classes
*/


// Copy the files "particles.png", "menu.wav",
// "font1.fnt", "font1.png" and "trail.psi" from
// the folder "precompiled" to the folder with
// executable file. Also copy hge.dll and bass.dll
// to the same folder.


#include "..\..\include\hge.h"
#include "..\..\include\hgesprite.h"
#include "..\..\include\hgefont.h"
#include "..\..\include\hgeparticle.h"


// Pointer to the HGE interface.
// Helper classes require this to work.
HGE *hge=0;


// Pointers to the HGE objects we will use
hgeSprite*			spr;
hgeSprite*			spt;
hgeFont*			fnt;
hgeParticleSystem*	par;

// Handles for HGE resourcces
HTEXTURE			tex;
HEFFECT				snd;

// Some "gameplay" variables
float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;

const float speed=90;
const float friction=0.98f;

// Play sound effect
void boom() {
	int pan=int((x-400)/4);
	float pitch=(dx*dx+dy*dy)*0.0005f+0.2f;
	hge->Effect_PlayEx(snd,100,pan,pitch);
}

bool FrameFunc()
{
	float dt=hge->Timer_GetDelta();

	// Process keys
	if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
	if (hge->Input_GetKeyState(HGEK_LEFT)) dx-=speed*dt;
	if (hge->Input_GetKeyState(HGEK_RIGHT)) dx+=speed*dt;
	if (hge->Input_GetKeyState(HGEK_UP)) dy-=speed*dt;
	if (hge->Input_GetKeyState(HGEK_DOWN)) dy+=speed*dt;

	// Do some movement calculations and collision detection	
	dx*=friction; dy*=friction; x+=dx; y+=dy;
	if(x>784) {x=784-(x-784);dx=-dx;boom();}
	if(x<16) {x=16+16-x;dx=-dx;boom();}
	if(y>584) {y=584-(y-584);dy=-dy;boom();}
	if(y<16) {y=16+16-y;dy=-dy;boom();}

	// Update particle system
	par->info.nEmission=(int)(dx*dx+dy*dy)*2;
	par->MoveTo(x,y);
	par->Update(dt);

	return false;
}


bool RenderFunc()
{
	// Render graphics
	hge->Gfx_BeginScene();
	hge->Gfx_Clear(0);
	par->Render();
	spr->Render(x, y);
	
	//打印间隔时间和FPS 注意:要将字体文件 .fnt文件和同名.png文件都要放到该工程下,不然不会显示字体
	fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d (constant)", hge->Timer_GetDelta(), hge->Timer_GetFPS());

	
	hge->Gfx_EndScene();

	return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "hge_tut03.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "HGE Tutorial 03 - Using helper classes");
	hge->System_SetState(HGE_FPS, 100);
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, 800);
	hge->System_SetState(HGE_SCREENHEIGHT, 600);
	hge->System_SetState(HGE_SCREENBPP, 32);

	if(hge->System_Initiate()) {

		// Load sound and texture
		snd=hge->Effect_Load("menu.wav");
		tex=hge->Texture_Load("particles.png");
		if(!snd || !tex)
		{
			// If one of the data files is not found, display
			// an error message and shutdown.
			MessageBox(NULL, "Can't load one of the following files:\nMENU.WAV, PARTICLES.PNG, FONT1.FNT, FONT1.PNG, TRAIL.PSI", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
			hge->System_Shutdown();
			hge->Release();
			return 0;
		}

		// Create and set up a sprite
		spr=new hgeSprite(tex, 96, 64, 32, 32);
		spr->SetColor(0xFFFFA000);
		spr->SetHotSpot(16,16);

		// Load a font
		fnt=new hgeFont("font1.fnt");

		// Create and set up a particle system
		spt=new hgeSprite(tex, 32, 32, 32, 32);
		spt->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
		spt->SetHotSpot(16,16);
		par=new hgeParticleSystem("trail.psi",spt); //加载粒子系统
		par->Fire();

		// Let's rock now!
		hge->System_Start();

		// Delete created objects and free loaded resources
		delete par;
		delete fnt;
		delete spt;
		delete spr;
		hge->Texture_Free(tex);
		hge->Effect_Free(snd);
	}

	// Clean up and shutdown
	hge->System_Shutdown();
	hge->Release();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值