![]() |
|
|
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 HGE interface that is required by most of the helper classes to work: 在这个指南中我们将学习怎样使用一些HGE的辅助类。首先,我们包含所有需要的头文件并且定义全局HGE接口指针,这需要到多数辅助类去工作。 #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. 现在我们声明我们将要用到的HGE对象的指针。 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. 在帧函数FrameFunc中我们更新粒子系统对象:我们基于当前精灵速度来调整粒子发散速度,并且移动他到一个正确的精灵区域。 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: 在描述函数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(); 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: 在WinMain函数中我们在HGE初始化之后创建HGE对象,我们将使用。首先,我们创建并设置一个精灵: 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.fnt and 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: 当游戏循环结束,我们释放所有创建的HGE对象: 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. 完整的带详细注释的该指南的源代码,你可以在文件夹tutorials\tutorial03中找到。必需的媒体文件你可以在文件夹tutorials\precompiled 中找到。 上一页12 下一页
|