HGE指南2翻译

来源:互联网 发布:零编程基础学python 编辑:程序博客网 时间:2024/04/29 06:46

 Tutorial 02 - Using input, sound and rendering
使用输入,声音和表现

To render something we will need a hgeQuad structure. Quad is the basic primitive in HGE used for rendering graphics. Quad contains 4 vertices, numbered 0 to 3 clockwise. 为了表现某些事情我们需要一个hgeQuad结构。Quad在HGE中是最基础而又最原始的表现图像的方法。Quad包含4个顶点,顺时针的纪录0到3个数字。(潇潇注释:Quad就象我们windows中的rect类似,代表屏幕,其中按顺时针方向分布0到3,总共四个坐标点)

Also we'll need a handle for the sound effect.
我们也需要一个声音效果的句柄。
hgeQuad quad;

HEFFECT snd;
Here go some "gameplay" variables and constants that we will use for our show.
这里有一些游戏变量和常量,我们将用来显示。

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

Now we create the function that will play collision sound with parameters based on sprite position and speed. 现在我们创建函数,基于精灵的位置和速度的参数用来播放碰撞声音
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 to the frame function. First, we need to know the time elapsed since last call of frame function to adjust our animation speeds to the current frame rate. We obtain it by calling the Timer_GetDelta function. 现在到帧函数里面。首先,我们需要去知道从最后一次调用帧函数到现在过去的时间,来调整我们的动画速度到正确的帧速度。我们通过调用Timer_GetDelta函数获得它。
bool FrameFunc(){  float dt=hge->Timer_GetDelta();//Now we process the key presses. We use here Input_GetKeyState function to monitor the keys state. To catch the key clicks you may use Input_GetKey function.  //现在我们处理按键按下。我们在这里使用Input_GetKeyState函数去监听键盘状态。你可以用Input_GetKey函数去捕捉键单击。  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 we do some movement calculations and collision detection specific to our example: //现在我们将在我们的例子中进行一些移动计算和冲撞捕捉的细节   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 have updated the screen coordinates for our sprite and we should update the quad's vertices to reflect the changes.  //现在我们已经为我们的精灵更新屏幕,下面我们将在quad的顶点中更新这种变化  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;//We've done with updating and should return false to continue execution. //我们已经更新了,然后要返回false去继续执行  return false;}

Now to the rendering part. We are going to set up a separate function which will be called by HGE when the application window should be redrawn. 现在到表现部分。我们将设置一个独立的函数,当应用程序窗口将要重画的时候HGE将调用它
bool RenderFunc(){//To start rendering we call the Gfx_BeginScene function. Then we clear the screen with a call to Gfx_Clear and render the sprite with Gfx_RenderQuad. Finally we finish rendering and update the screen,  calling the Gfx_EndScene function. //为了开始描述我们将调用Gfx_BeginScene函数。然后我们清除屏幕通过调用Gfx_Clear并且通过Gfx_RenderQuad函数描述精灵。最后我们通过调用Gfx_EndScene函数完成描绘和更新屏幕。  hge->Gfx_BeginScene();  hge->Gfx_Clear(0);  hge->Gfx_RenderQuad(&quad);  hge->Gfx_EndScene();  return false;}

The rendering function should always return false. 描述函数将总是返回false.

Now let's see the changes in WinMain function. In this tutorial we set some more system states before HGE initiating. First, we should specify our rendering function. We also turn on the log file support and specify the desired video mode more explicitly: 现在让我们看看在WinMain函数的变化。在这个指南中我们将在HGE初始化之前设置一些更多的系统状态。首先,我们将指定我们的描述函数。我们也开启log文件支持,并且明确的指定更多的想要得视频模式。
 
  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);

When HGE is initiated we need to load a texture that holds graphics and a sound effect: 
    snd=hge->Effect_Load("menu.wav");    quad.tex=hge->Texture_Load("particles.png");

Now we should set up the hgeQuad structure that we will use for rendering. The quad's texture is already set up, now we set the blending mode and fill in the vertices array with the desired values. 现在我们设置为描绘所使用的hgeQuad结构。Quad的结构已经设置,现在我们在定点数组中使用想要得值来设置混合和填充模式

We don't use z-buffer in this example, so the z-order of vertices is ignored here, and we set it to an arbitrary value in range from 0.0 to 1.0, just to be sure. The format of a vertex color DWORD is 0xAARRGGBB. 在这个例子中我们不是用z缓冲,所以z轴顶点在这里是可以忽视的。并且我们设置他为从0.0到1.0范围中的任意值。一个顶点颜色的格式为DWORE是0XAARRGGBB.

The texture coordinates tx and ty for each vertex define the part of the texture to be rendered with this quad. The values are in range 0.0 to 1.0. 0,0 means top left corner and 1,1 - bottom right corner of the texture. Here we have a 128x128 texture and we want to use a 32x32 square part of it starting at 96,64. 这个结构相配的tx和ty为每一个顶点定义用quad去描述的结构的一部分。这个值是在范围0.0到1.0。0.0表示左顶点区域,1,1结构表示右下区域。这里我们有一个128*128结构并且我们想要用一个32*32平方部分他开始在96,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're ready to start the game loop with the System_Start function. When the frame function returns true and the game loop is finished we should free the loaded texture and the sound effect: 现在我们已经开始这个游戏循环用System_Start函数。当帧函数返回true并且游戏循环完成,我们将释放载入的结构和声音效果:
    hge->System_Start();    hge->Texture_Free(quad.tex);    hge->Effect_Free(snd);

The rest of shutdown process is identical to the one demonstrated in Tutorial 01. 停止函数的其余处理方法是和前一个指南里的实例一同样的

The complete source code with detailed comments for this tutorial you may find in the folder tutorials/tutorial02. The required media files you'll find in the folder tutorials/precompiled.
完整的带详细注释的该指南的源代码,你可以在文件夹tutorials/tutorial02中找到。必需的媒体文件你可以在文件夹tutorials/precompiled 中找到。

原创粉丝点击