HGE tutorial translation (2)

Source: Internet
Author: User

 

Tutorial 02-
Use sound, input, and image

To render something, we need the hgeQuad struct. Quad isHGE. It contains four vertices, ranging from 0 to 3 clockwise.

At the same time, we need the handle of the sound effect.

 

hgeQuad quad;
 
HEFFECT snd;

 

Here we put some variables and constants required by the game.

 

float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;
 
const float speed=90;
const float friction=0.98f;

 

Here we create a function for playing the collision sound. The parameter is the location and speed of the genie.

 

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

 

Now comes to frame function. We need to know the time from the last function call to the present to adjust the animation speed at the current frame rate. Use Timer_GetDelta to obtain the time.

 

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

 

Now let's process the buttons. We use the Input_GetKeyState function to check the button status. Click Input_GetKey for the capture key.

 

  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;

 

Now let's do some motion computing 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();}

 

Now we update the screen and update the vertices of quad to reflect the changes.

 

  quad.v[0].x=x-16; quad.v[0].y=y-16;
  quad.v[1].x=x+16; quad.v[1].y=y-16;
  quad.v[2].x=x+16; quad.v[2].y=y+16;
  quad.v[3].x=x-16; quad.v[3].y=y+16;

 

End of update should returnFalseRun the program.

 

  return false;
}

 

Now comes to the rendering section. We will set the function called when the window is refreshed.

 

bool RenderFunc()
{

 

Call the Gfx_BeginScene function to start rendering. Use Gfx_Clear to clear the screen and use the Gfx_RenderQuad rendering wizard. Finally, we end rendering and update the screen and call Gfx_EndScene.

 

  hge->Gfx_BeginScene();
  hge->Gfx_Clear(0);
  hge->Gfx_RenderQuad(&quad);
  hge->Gfx_EndScene();
 
  return false;
}

 

Rendering functions should always returnFalse.

Now let's take a lookWinMainFunction changes. In this exampleHGESet more system statuses before initialization. Specify the rendering function first. You must also enable recording file support and specify the video mode.

 

  hge->System_SetState(HGE_LOGFILE, "hge_tut02.log");
  hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
  hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
  hge->System_SetState(HGE_TITLE,
           "HGE Tutorial 02 - Using input, sound and rendering");
 
  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);

 

WhenHGEAfter initialization, We need to load textures and sound effects.

 

    snd=hge->Effect_Load("menu.wav");
    quad.tex=hge->Texture_Load("particles.png");

 

Now we set the hgeQuad struct to be rendered. The texture has been set. Now set the rendering mode and fill in the vertex data.

In this example, we do not use z-buffer, so the z-order of the vertex is ignored. We can set a value from 0.0 to 1.0. ColorDWORDThe value format is 0 xw.rggbb.

TxAndTxDefines the rendering position of each part of the texture on the sprite. The value range is 0.0 to 1.0. 0, 0 indicates the upper left corner, and 1, 1 indicates the lower right corner. There is a x texture. We want to use its 32
Part of the x32 size starting from or 64.

 

    quad.blend=BLEND_ALPHAADD | BLEND_COLORMUL | BLEND_ZWRITE;
 
    for(int i=0;i<4;i++)
    {
      quad.v[i].z=0.5f;
      quad.v[i].col=0xFFFFA000;
    }
 
    quad.v[0].tx=96.0/128.0; quad.v[0].ty=64.0/128.0;
    quad.v[1].tx=128.0/128.0; quad.v[1].ty=64.0/128.0;
    quad.v[2].tx=128.0/128.0; quad.v[2].ty=96.0/128.0;
    quad.v[3].tx=96.0/128.0; quad.v[3].ty=96.0/128.0;

 

Now we are going to start the game loop with System_Start. When frame
Function returnTrueAt the end of the game, we should release the loaded textures and sound effects.

 

    hge->System_Start();
 
    hge->Texture_Free(quad.tex);
    hge->Effect_Free(snd);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.