HGE教程翻译(7)

Tutorial 07 – 成千上万的野兔

 

这篇教程示范HGE渲染多种混合模式。

 

创建精灵

我们跳过所有的技术细节直接来到动作。首先创建并初始化一对精灵,用于"game objects"和背景。注意背景精灵是如何从小的64x64图块创建并在底部上色。

 

HTEXTURE tex, bgtex;
     
hgeSprite *spr, *bgspr;
     

   
     
   
tex=hge->Texture_Load("zazaka.png");
     
spr=new hgeSprite(tex,0,0,64,64);
     
spr->SetHotSpot(32,32);
     

   
     
   
bgtex=hge->Texture_Load("bg2.png");
     
bgspr=new hgeSprite(bgtex,0,0,800,600);
     
bgspr->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND);
     
bgspr->SetColor(0xFF000000,0);
     
bgspr->SetColor(0xFF000000,1);
     
bgspr->SetColor(0xFF000040,2);
     
bgspr->SetColor(0xFF000040,3);
     

 

初始化游戏对象数组

现在我们创建数组,保存"game objects"的属性并用任意值填充。

 

#define MAX_OBJECTS 2000
     

   
     
   
struct sprObject
     
{
     
  float x,y;
     
  float dx,dy;
     
  float scale,rot;
     
  float dscale,drot;
     
  DWORD color;
     
};
     

   
     
   
sprObject*  pObjects;
     
int         nObjects;
     

   
     
   
pObjects=new sprObject[MAX_OBJECTS];
     
nObjects=1000;
     

   
     
   
for(i=0; i<MAX_OBJECTS; i++)
     
{
     
  pObjects[i].x=hge->Random_Float(0,SCREEN_WIDTH);
     
  pObjects[i].y=hge->Random_Float(0,SCREEN_HEIGHT);
     
  pObjects[i].dx=hge->Random_Float(-200,200);
     
  pObjects[i].dy=hge->Random_Float(-200,200);
     
  pObjects[i].scale=hge->Random_Float(0.5f,2.0f);
     
  pObjects[i].dscale=hge->Random_Float(-1.0f,1.0f);
     
  pObjects[i].rot=hge->Random_Float(0,M_PI*2);
     
  pObjects[i].drot=hge->Random_Float(-1.0f,1.0f);
     
}

 

改变混合模式

SetBlend函数使用一个04int变量来调整游戏对象数组的混合模式。

 

void SetBlend(int blend)
     
{
     
  static int sprBlend[5]=
     
  {
     
    BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE,
     
    BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE,
     
    BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE,
     
    BLEND_COLORMUL | BLEND_ALPHAADD   | BLEND_NOZWRITE,
     
    BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE
     
  };
     

   
     
   
  static DWORD sprColors[5][5]=
     
  {
     
    {0xFFFFFFFF, 0xFFFFE080, 0xFF80A0FF, 0xFFA0FF80, 0xFFFF80A0},
     
    {0xFF000000, 0xFF303000, 0xFF000060, 0xFF006000, 0xFF600000},
     
    {0x80FFFFFF, 0x80FFE080, 0x8080A0FF, 0x80A0FF80, 0x80FF80A0},
     
    {0x80FFFFFF, 0x80FFE080, 0x8080A0FF, 0x80A0FF80, 0x80FF80A0},
     
    {0x40202020, 0x40302010, 0x40102030, 0x40203010, 0x40102030}
     
  };
     

   
     
   
  if(blend>4) blend=0;
     
  nBlend=blend;
     

   
     
   
  spr->SetBlendMode(sprBlend[blend]);
     

   
     
   
  for(int i=0;i<MAX_OBJECTS;i++)
     
    { pObjects[i].color=sprColors[blend][hge->Random_Int(0,4)]; }
     
}
     

 

更新屏幕

 

FrameFunc中我们遍历游戏对象数组并计算它们的新位置。

 

  for(i=0; i<nObjects; i++)
     
  {
     
    pObjects[i].x+=pObjects[i].dx*dt;
     
    if(pObjects[i].x>SCREEN_WIDTH || pObjects[i].x<0)
     
      { pObjects[i].dx=-pObjects[i].dx; }
     

   
     
   
    pObjects[i].y+=pObjects[i].dy*dt;
     
    if(pObjects[i].y>SCREEN_HEIGHT || pObjects[i].y<0)
     
      { pObjects[i].dy=-pObjects[i].dy; }
     

   
     
   
    pObjects[i].scale+=pObjects[i].dscale*dt;
     
    if(pObjects[i].scale>2 || pObjects[i].scale<0.5)
     
      { pObjects[i].dscale=-pObjects[i].dscale; }
     

   
     
   
    pObjects[i].rot+=pObjects[i].drot*dt;
     
  }
     

渲染屏幕

RenderFunc中我们遍历游戏对象数组并渲染它们。

 

  hge->Gfx_BeginScene();
     
  bgspr->Render(0,0);
     
     
     
  for(i=0; i<nObjects; i++)
     
  {
     
    spr->SetColor(pObjects[i].color); 
     
    spr->RenderEx(pObjects[i].x, pObjects[i].y,
     
                  pObjects[i].rot, pObjects[i].scale);
     
  }
     

   
     
   
  hge->Gfx_EndScene();
     

 

使用UP DOWN键调整精灵的数量,空格键改变混合模式,ESC退出。

 

你可能感兴趣的:(游戏,struct,UP,float,Blend,2010)