3.學習HGE的Tutorial 03

    前面幾個都是學完後再記錄,這次邊學邊記錄吧。碰到的問題都可以馬上記下來:)

    首先在Visual Studio2010中配置好HGE的運行環境。把源碼放進去看編譯是否通過。VS提示找不到"LIBC.lib"。不過沒關係,我們實際不需要這個文件。在網上搜索了一下,原因沒看懂,不過找到個解決方案。只要在"項目屬性"中的"鏈接器"欄中的"忽略特定庫"中寫入打不開的文件的名稱,例如文中的LIBCD.lib再編譯就好。

    Tutorial3教我們如何使用HGE中HGE helper classes的部分內容。應該就是精靈(sprite)、微粒(particle)、文字(font)。

    首先申明我們要用的HGE變量:

        hgeSprite* spr;

        hgeSprite* spt;//這兩個hgeSprite到底是什麼現在還沒弄清楚。。。

        hgeFont* fnt;//控制文字用

        hgeParticleSystem* par;//控制微粒用

    在FrameFunc函數中添加以下語句,更新微粒系統:

        par->info.nEmission=(int)(dx*dx+dy*dy)*2;

        par->MoveTo(x,y);

        par->Update(dt);

    這裏的info是一個很複雜的結構,裏面存數了該微粒系統的信息。找了一下開發文檔,裏面列出了一大堆變量。在這裏做下翻譯工作吧,自己以後回憶的時候也快點(還是喜歡中文呀):

struct hgeParticleSystemInfo
{
  hgeSprite*  sprite;
  int         nEmission;
  float       fLifetime;

  float       fParticleLifeMin;
  float       fParticleLifeMax;

  float       fDirection;
  float       fSpread;
  bool        bRelative;

  float       fSpeedMin;
  float       fSpeedMax;

  float       fGravityMin;
  float       fGravityMax;

  float       fRadialAccelMin;
  float       fRadialAccelMax;

  float       fTangentialAccelMin;
  float       fTangentialAccelMax;

  float       fSizeStart;
  float       fSizeEnd;
  float       fSizeVar;

  float       fSpinStart;
  float       fSpinEnd;
  float       fSpinVar;

  hgeColor    colColorStart;
  hgeColor    colColorEnd;
  float       fColorVar;
  float       fAlphaVar;
};

hgeSprite*  sprite; (“精靈”?顯然我還是不知道這是什麼。。。。)

Pointer to a hgeSprite object to use for rendering particles. Probably, the most useful sprite's parameters here are texture, blending mode and anchor point. Besides the hgeSprite itself you may use here hgeAnimation or other classes derived from it.

指向一個用來呈現微粒的hgeSprite(精靈)對象。可能,這裏sprite的最有用的參數就是texture(紋理),blending mode(綁定模式)和anchor point(定位點)。除了hgeSprite類本身,你還可以用hgeAnimation或其他hgeSprite的導出類。
int         nEmission;
Number of particles to be emitted each second. Note that maximum number of particles simultaneously allowed in one particle system is 500. If this number is exceeded, new particles wouldn't be created until some of the current particles die. The number of simultaneously alive aprticles depends on nEmission, fParticleLifeMin and fParticleLifeMax members.

每秒發射的微粒數。注意。在一個微粒系統中最多允許500個微粒同時存在。如果達到這個數目則新的微粒不會產生,直到以前的微粒消亡。同時存活的微粒數取決於nEmission,fParticleLifeMin 和 fParticleLifeMax。
float       fLifetime;
The period of time during which the particle system should emit particles (in seconds). When this amount of time is passed since call to the Fire method, the particles are no more emitted. If = -1.0 the particle system keeps emitting particles until stopped manually.

微粒系統發射微粒的時間間隔(單位爲秒)。用Fire的方法發射微粒,則即使到了間隔時間,微粒也不再發射。如果取值=-1.0,則保持發射微粒,直到手動終止。
float       fParticleLifeMin;
The minimum period of lifetime for a specific particle in seconds.

一個微粒的最小生命週期,單位爲秒。
float       fParticleLifeMax;The maximum period of lifetime for a specific particle in seconds.

一個微粒的最大生命週期,單位爲秒。
float       fDirection; This is an angle in radians, defining the particles emitting direction.

 這是一個弧度角,定義了微粒的發射方向。
float       fSpread;

This is an angle in radians, defining the sector within which the particles are emitted. For example, if it is 0.5, the emitted particles direction lies within fDirection-0.5 and fDirection+0.5. If the fSpread is 2*pi, the fDirection parameter becomes meaningless as the whole circle is covered.

這是一個弧度角,定義了微粒的發射區域。例如,如果取值爲0.5,則微粒在fDirection-0.5到fDirection+0.5的區域內發射。如果取值2*pi,則FDirection參數失去意義,因爲整個圓周都被包圍了。

bool        bRelative;

If true, the fDirection member is treated relatively to the current particle system movement direction. If false, the fDirection member is treated absolutely (0 means straight up).

如果爲ture,則fDirection與當前微粒系統的運動方向相關。如果爲false,則fDirection的方向沒有相關性(0表示垂直向上)。
float       fSpeedMin;

The minimum starting speed for a specific particle in pixels per second.

某微粒的最小起始速度,單位爲“像素每秒”。

float       fSpeedMax;

The maximum starting speed for a specific particle in pixels per second.

某微粒的最大起始速度,單位爲“像素每秒”。
float       fGravityMin;

The minimum gravity value for a specific particle. The gravity is basically just an acceleration along the y-axis.

某微粒的最小重力。“重力”僅僅就是y方向的加速度。

float       fGravityMax;

The maximum gravity value for a specific particle. The gravity is basically just an acceleration along the y-axis.

某微粒的最大重力。“重力”僅僅就是y方向的加速度。
float       fRadialAccelMin;

The minimum value of the acceleration directed towards the particle system's center for a specific particle.

某微粒指向微粒系統中心的最小加速度。

float       fRadialAccelMax;

The maximum value of the acceleration directed towards the particle system's center for a specific particle.

某微粒指向微粒系統中心的最大加速度。
float       fTangentialAccelMin;

The minimum value of the acceleration directed perpendicularly to the radial acceleration for a specific particle.

以微粒系統中心爲圓心,某微粒垂直於半徑方向的最小加速度,即切向最小加速度。

float       fTangentialAccelMax;

The maximum value of the acceleration directed perpendicularly to the radial acceleration for a specific particle.

以微粒系統中心爲圓心,某微粒垂直於半徑方向的最大加速度,即切向最大加速度。
float       fSizeStart;

The starting scaling for a specific particle. 1.0 means the default sprite's size.

某微粒的初始大小,1.0代表默認大小。

float       fSizeEnd;

The ending scaling for a specific particle. 1.0 means the default sprite's size.

某微粒的結束大小,1.0爲默認大小。

float       fSizeVar;

The starting scaling variation from 0.0 (means no variation) to 1.0 (means random variation from fSizeStart to fSizeEnd).

初始大小偏差,從0.0(表示無偏差)到1.0(表示隨機偏差,取fSizeStart到fSizeEnd間的隨機值)。
float       fSpinStart;

The starting spin for a specific particle in radians.

某微粒的初始旋轉弧度。

float       fSpinEnd;

The ending spin for a specific particle in radians.

某微粒的結束旋轉弧度。

float       fSpinVar;

The starting spin variation from 0.0 (means no variation) to 1.0 (means random variation from fSpinStart to fSpinEnd).

初始旋轉弧度偏差,從0.0(表示無偏差)到1.0(表示隨機偏差,取fSpinStart到fSpinEnd間的任意值)。
hgeColor    colColorStart;

The starting color and opacity for a specific particle. See hgeColor class for details. If colColorStart.r == -1 a special coloring mode is turned on: the particle system stops altering RGB of it's sprite and uses the color already set in it (the same for all particles). Only the alpha component is changed for each particle.

某微粒的初始顏色和不透明度。詳情請看hgeColor class。如果colColorStart.r == -1,一個特殊的色彩模式將被開啓:微粒系統停止更新顏色,並且使用內置色彩(所有微粒系統的內置色彩都一樣)。僅僅只有alpha部分每個微粒系統都不一樣。

hgeColor    colColorEnd;

The ending color and opacity for a specific particle. See hgeColor class for details.

某微粒的終止顏色和不透明度。詳情請看hgeColor class。

float       fColorVar;

The starting color variation from 0.0 (means no variation) to 1.0 (means random variation between the starting and ending colors).

初始顏色的偏差,從0.0(無偏差)到1.0(隨機偏差,取從初始色到終止色間的任意值)。

float       fAlphaVar;

The starting opacity variation from 0.0 (means no variation) to 1.0 (means random variation between the starting and ending opacities).

初始不透明度偏差,從0.0(無偏差)到1.0(隨機偏差,取值爲初始不透明度到終止不透明度之間的隨機值)。

    呼。。。爲了解釋這個info,寫了這麼多。跟着的兩個函數就好理解些了。MoveTo(x,y)將微粒系統移動到x,y。MoveTo的函數原型爲void MoveTo(float x,float y,bool bMoveParticles = false);x,y爲新的位置。bMoveParticles爲true,則所有活動微粒都一向新位置。若bMoveParticles爲 false或忽略,則僅微粒系統的發射器被移動到新位置x,y。Update(dt);更新微粒系統,dt爲兩次調用Update函數的時間差。

    看完這一部分我們繼續(不懂的先放着吧,我也不明白)。下面在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();

    說到顯示文字,這個是C風格的,對於我這種沒學過C的人來說,有點怪的感覺。不過還算好懂。printf的幾個參數分別爲:座標x,座標y,對齊方式,輸出字符串,輸出值。其中輸出值與輸出字符串相對應,"%i"表示輸出整型,"%d"輸出十進制數,"%.2f"輸出兩位有效數字的float類型。除了這個函數外還有個printfb。這個函數中有兩個定義寬和高的參數不知道怎麼用?好像是可以定義將文本顯示在某個矩形區域內。除此之外,如果純粹輸出字符串,還可以用fnt->render(x,y,HGETEXT_LEFT,"Show")函數。函數的幾個參數分別爲顯示位置的x,y左邊,對齊方式,字符串。另外,在這幾個函數中,只有printfb的對齊方式是可以混合的,比如HGETEXT_LEFT | HGETEXT_MIDDLE。

    看完以上部分,終於來到主函數WinMain了。(好多啊~)

    在WinMain中首先加載了紋理tex=hge->Texture_Load("particles.png");。不知爲何非要加載一張圖片,明明那張png圖片上面就什麼都沒有呀?然後增加了一個精靈:spr=new hgeSprite(tex, 96, 64, 32, 32);spr->SetColor(0xFFFFA000);spr->SetHotSpot(16,16);。看了hgeSprite的構造函數說明後,前一個問題好像有點明白了。如果hgeSprite不加載紋理圖片,那麼紋理效果就是默認的白色不透明底圖,那麼就只能看到一個小方塊了。hgeSprite的構造函數各參數的意義分別如下:紋理圖、Sprite在紋理中的x座標、Sprite在紋理中的y座標、Sprite的寬、Sprite的高。接着設置精靈顏色。HGE裏的顏色以0x開頭,後面只十六進制數。前兩位是Alpha值(感覺就是不透明度吧?從00到FF圖像逐漸就顯示出來了),後兩位是紅色,接着兩個代表黃色、最後兩個代表藍色。如果你覺得用十六進制設置挺麻煩的,也可以用ARGB(255,255,160,0)的方法,效果和用0xFFFFA000是一樣的。從Alpha到RGB色彩,取值都是從0到255。最後設置熱點hgeHotSpot。“熱點”按我的理解就是Sprite(精靈)內部的原點,其各參數都以熱點爲參考。Toturial03中這裏的熱點設置的是Sprite的中點。

    接着在WinMain中設置字體fnt=new hgeFont("font1.fnt");。這裏要加載一個字體文件,文件可以用HGE自帶的字體編輯工具生成。確實它對中文的支持不好,如何讓他顯示中文,現在我還沒有去了解。搜索其他內容是看到網上有很多關於讓HGE顯示中文的文章,等我需要用中文字體時再去了解吧。

    下面終於開始加載微粒系統了!

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

     剛纔突然又有一點明白前面的問題了:)上一個精靈spr獲得的是紋理particles.png中的有透明圓的部分,將不透明度設爲255(最高)、色彩設爲橙色(255,160,0)後,那個圓就顯現出來了。現在讓精靈spt獲取particles.png中有透明星星的部分,將其作爲微粒系統的精靈。然後設置顏色綁定模式、熱點。再啓動hge的微粒系統。這裏的"trail.psi"是微粒系統的參數信息,可以用HGE自帶的軟件編輯生成他,這樣就好創建微粒系統了。後面的"spt",即微粒系統發射時用的精靈,本例中爲星星。最後,發射Fire()。用Fire()函數發射微粒,發射將不循環。也就是說每Fire()一次,就發射一次。這一點在翻譯上面的info時已經見過了。

    其他的什麼開始程序、終止釋放空間,這些就不說了,應該都知道吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章