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:

#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.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:

    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.