Tutorial 03 –
使用 helper classes
這次我們會學習使用HGE的一些協助類。首先,包含所有需要的標頭檔並聲明HGE全域指標,大多數協助類都需要它。
#include <hge.h>
#include <hgesprite.h>
#include <hgefont.h>
#include <hgeparticle.h>
HGE *hge=0;
現在聲明HGE對象。
hgeSprite* spr;
hgeSprite* spt;
hgeFont* fnt;
hgeParticleSystem* par;
HTEXTURE tex;
在FrameFunc中我們更新粒子系統對象:我們基於精靈速度調整噴射速率並移動它到當前精靈位置。
par->info.nEmission=(int)(dx*dx+dy*dy)*2;
par->MoveTo(x,y);
par->Update(dt);
在RenderFunc中我們渲染所有的對象,調用它們的渲染方法。
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();
在WinMain函數中HGE初始化後建立HGE對象。首先設定精靈:
spr=new hgeSprite(tex, 96, 64, 32, 32);
spr->SetColor(0xFFFFA000);
spr->SetHotSpot(16, 16);
接著載入字型。字型用兩個檔案描述font1.fnt
和 font1.png。
fnt=new hgeFont("font1.fnt");
建立粒子系統和精靈。
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();
現在所有對象建立完畢開始遊戲迴圈。
hge->System_Start();
遊戲迴圈結束我們刪除所有的HGE對象。
delete par;
delete fnt;
delete spt;
delete spr;
這個程式的結束值和先前的樣本是一樣的。