電腦遊戲製作開發設計論壇 首頁 電腦遊戲製作開發設計論壇
任何可以在PC上跑的遊戲都可以討論,主要以遊戲之製作開發為主軸,希望讓台灣的遊戲人有個討論、交流、教學、經驗傳承的園地
 
 常見問題常見問題   搜尋搜尋   會員列表會員列表   會員群組會員群組   會員註冊會員註冊 
 個人資料個人資料   登入檢查您的私人訊息登入檢查您的私人訊息   登入登入 

Google
[HGE] HGE 1.81 所附範例研究報告(2008/08/26更新)

 
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 遊戲程式進階班:各種進階圖型引擎或遊戲引擎
上一篇主題 :: 下一篇主題  
發表人 內容
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:09 星期六    文章主題: [HGE] HGE 1.81 所附範例研究報告(2008/08/26更新) 引言回覆

前言(胡言亂語) :

期中考完沒事做,就來研究 (毆
現在只是簡單大概的分析一下,有空(?)應該會弄得詳細一點.

總共範例有 8 個,應該會弄完吧.. ? (應該...)

有點想放棄XNA了 (碎碎唸
寫出來的東西要丟給別人玩好麻煩 囧

網站 :

HGE 官網連結 : http://hge.relishgames.com/
HGE 線上文件庫 : http://hge.relishgames.com/doc/index.html

目錄和更新情報 :

根據標題的最後更新日期 :
紅色代表還沒動手
藍色代表沒有改變
綠色代表有更新

1. HGE 範例研究報告 第一小節 (2008/05/10上傳) 快速連結 [基礎 HGE 應用]
2. HGE 範例研究報告 第二小節 (2008/08/26修改) 快速連結 [使用 輸入, 聲音 和 繪出]
3. HGE 範例研究報告 第三小節 (2008/08/26上傳) 快速連結 [輔助類別應用]
4. HGE 範例研究報告 第四小節
5. HGE 範例研究報告 第五小節
6. HGE 範例研究報告 第六小節
7. HGE 範例研究報告 第七小節
8. HGE 範例研究報告 第八小節

_________________
已經畢業了!!


babu61509 在 2008-8-26, AM 10:32 星期二 作了第 7 次修改
回頂端
檢視會員個人資料 發送私人訊息
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:09 星期六    文章主題: 引言回覆

HGE 範例研究報告 第一小節

HGE 1.81 hge_tut01.cpp

(註解有大概翻譯一下,看不懂翻譯的就看原文吧XD)

代碼:

/*
** Haaf's Game Engine 1.8
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** hge_tut01 - Minimal HGE application
** hge_tut01 - 基礎 HGE 應用
*/


#include "..\..\include\hge.h"

HGE *hge = 0;

// This function will be called by HGE once per frame.
// Put your game loop code here. In this example we
// just check whether ESC key has been pressed.
// 每 frame 時 HGE 會呼叫此函數.
// 將你的遊戲迴圈碼放在這邊.
// 這裡的範例是檢查 esc 是否被按下.
bool FrameFunc()
{
   // By returning "true" we tell HGE
   // to stop running the application.
   // 當傳回 "true" 時代表停止迴圈
   if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;

   // Continue execution
   return false;
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
   // Here we use global pointer to HGE interface.
   // Instead you may use hgeCreate() every
   // time you need access to HGE. Just be sure to
   // have a corresponding hge->Release()
   // for each call to hgeCreate()
   // 這裡我們用全域指標指向 HGE 介面.
   // 當你使用 hgeCreate() 來存取 HGE 時,請確認後面不用時有 hge->Release() 釋放.
   hge = hgeCreate(HGE_VERSION);

   // Set our frame function
   // 設定 frame 函數
   hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);

   // Set the window title
   // 設定 視窗 標題
   hge->System_SetState(HGE_TITLE, "HGE Tutorial 01 - Minimal HGE application");
   
   // Run in windowed mode
   // Default window size is 800x600
   // 在視窗模式運作,預設是 800x600
   hge->System_SetState(HGE_WINDOWED, true);

   // Don't use BASS for sound
   // 不使用 BASS 聲音輸出
   hge->System_SetState(HGE_USESOUND, false);

   // Tries to initiate HGE with the states set.
   // If something goes wrong, "false" is returned
   // and more specific description of what have
   // happened can be read with System_GetErrorMessage().
   // 嘗試初始化 HGE 狀態設定.
   // 發生錯誤時會傳回 "false" ,可以透過 System_GetErrorMessage()
   // 來了解更多資訊.
   if(hge->System_Initiate())
   {
      // Starts running FrameFunc().
      // Note that the execution "stops" here
      // until "true" is returned from FrameFunc().
      // 開始執行 FrameFunc().
      // 註 : 直到 FrameFunc() 傳回 "true" 之前,程式會停在這行.
      hge->System_Start();
   }
   else
   {   
      // If HGE initialization failed show error message
      // 如果 HGE 初始化有問題就顯示錯誤資訊
      MessageBox(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
   }

   // Now ESC has been pressed or the user
   // has closed the window by other means.
   // 執行到這邊不是 ESC 被按下就是使用者透過其他方式關閉視窗

   // Restore video mode and free
   // all allocated resources
   // 還原顯示模式和釋放資源
   hge->System_Shutdown();

   // Release the HGE interface.
   // If there are no more references,
   // the HGE object will be deleted.
   // 釋放 HGE 介面.
   hge->Release();

   return 0;
}


從上方程式碼可得知 :
代碼:

0. 和一般 WIN32 程式一樣,從 int WINAPI WinMain() 開始執行.
1. 存取 HGE 函式是透過指標存取,使用 hgeCreate(HGE_VERSION) 建立,使用 hge->Release() 釋放.
2. HGE 系統變數要使用 hge->System_SetState() 設定,這邊有用到 :
   a. HGE_FRAMEFUNC : 每 frame 都會執行的函數.
   b. HGE_TITLE : 視窗標題.
   c. HGE_WINDOWED : 是否視窗化.
   d. HGE_USESOUND : 是否使用 bass 聲音輸出.
2. 系統變數設定完畢,使用 hge->System_Initiate() 初始化,初始化有問題會傳回 false ,詳細錯誤資訊在 hge->System_GetErrorMessage().
3. 初始化完成,呼叫 hge->System_Start() 開始遊戲迴圈,遊戲迴圈函式傳回true時即跳出.
4. 呼叫 hge->System_Shutdown(); 還原顯示模式和釋放資源.
5. 呼叫 hge->Release() 釋放 HGE 介面.

基本程式流程 :
代碼:

1. hgeCreate(HGE_VERSION) 建立 HGE 介面
2. hge->System_SetState() 設定 HGE 系統變數
3. hge->System_Initiate() 初始化,失敗往 5
   4. hge->System_Start() 開始遊戲迴圈,遊戲迴圈傳回true時即跳出
5. hge->System_Shutdown(); 原顯示模式和釋放資源
6. hge->Release() 釋放 HGE 介面

_________________
已經畢業了!!


babu61509 在 2008-5-10, AM 11:14 星期六 作了第 1 次修改
回頂端
檢視會員個人資料 發送私人訊息
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:10 星期六    文章主題: 引言回覆

HGE 範例研究報告 第二小節

HGE 1.81 hge_tut02.cpp

(註解有大概翻譯一下,看不懂翻譯的就看原文吧XD)

代碼:

/*
** Haaf's Game Engine 1.8
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** hge_tut02 - Using input, sound and rendering
** hge_tut02 - 使用 輸入, 聲音 和 繪出
*/


// Copy the files "particles.png" and "menu.wav"
// from the folder "precompiled" to the folder with
// executable file. Also copy hge.dll and bass.dll
// to the same folder.
// 從 "precompiled" 複製 "particles.png" 和 "menu.wav" 到執行資料夾.
// hge.dll 和 bass.dll 也要複製到同資料夾.

#include "..\..\include\hge.h"

HGE *hge=0;

// Quad is the basic primitive in HGE
// used for rendering graphics.
// Quad contains 4 vertices, numbered
// 0 to 3 clockwise.
// Quad 在 HGE 中是用來繪出圖像的基礎.
// Quad 包含了 4 個頂點,順時針的編號為 0 到 3.

hgeQuad quad;

// Handle for a sound effect
// 聲音效果的 Handle
HEFFECT snd;

// Some "gameplay" variables and constants
// 一些 "gameplay" 變數和常數
float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;

const float speed=90;
const float friction=0.98f;

// This function plays collision sound with
// parameters based on sprite position and speed
// 此函數根據 sprite 位置 和 速度 播放碰撞聲音.
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);
}

bool FrameFunc()
{
   // Get the time elapsed since last call of FrameFunc().
   // This will help us to synchronize on different
   // machines and video modes.
   // 取得自上次呼叫FrameFunc()所經過的時間.
   // 這會幫助我們在不同機器和顯示模式上同步.
   float dt=hge->Timer_GetDelta();

   // Process keys
   // 按鍵處理
   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;

   // Do some movement calculations 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();}

   // Set up quad's screen coordinates
   // 設定 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;

   // Continue execution
   // 繼續迴圈
   return false;
}

// This function will be called by HGE when
// the application window should be redrawn.
// Put your rendering code here.
// 此函數在應用程式視窗需重繪的時候會被 HGE 呼叫.
// 將你的繪出程式碼放在這邊.

bool RenderFunc()
{
   // Begin rendering quads.
   // This function must be called
   // before any actual rendering.
   // 開始繪出 quads
   // 此函數必須在開始繪出之前先呼叫.
   hge->Gfx_BeginScene();

   // Clear screen with black color
   // 用黑色塗滿螢幕.
   hge->Gfx_Clear(0);

   // Render quads here. This time just
   // one of them will serve our needs.
   // 這裡開始繪出 quads .
   hge->Gfx_RenderQuad(&quad);

   // End rendering and update the screen
   // 結束繪出並更新螢幕.
   hge->Gfx_EndScene();

   // RenderFunc should always return false
   // RenderFunc 應該總是傳回 false
   return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
   // Get HGE interface
   // 取得 HGW 介面
   hge = hgeCreate(HGE_VERSION);

   // Set up log file, frame function, render function and window title
   // 設定 紀錄檔,frame 函數,繪出 函數 和 視窗標題
   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");

   // Set up video mode
   // 設定顯示模式
   hge->System_SetState(HGE_WINDOWED, true);
   hge->System_SetState(HGE_SCREENWIDTH, 800);
   hge->System_SetState(HGE_SCREENHEIGHT, 600);
   hge->System_SetState(HGE_SCREENBPP, 32);

   if(hge->System_Initiate())
   {
      // Load sound and texture
      // 讀入 聲音 和 材質
      snd=hge->Effect_Load("menu.wav");
      quad.tex=hge->Texture_Load("particles.png");
      if(!snd || !quad.tex)
      {
         // If one of the data files is not found, display
         // an error message and shutdown.
         // 如果沒找到任何一個資料檔,顯示錯誤並結束
         MessageBox(NULL, "Can't load MENU.WAV or PARTICLES.PNG", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
         hge->System_Shutdown();
         hge->Release();
         return 0;
      }

      // Set up quad which we will use for rendering sprite
      // 設定繪出 sprite 用的 quad 混合模式
      quad.blend=BLEND_ALPHAADD | BLEND_COLORMUL | BLEND_ZWRITE;

      for(int i=0;i<4;i++)
      {
         // Set up z-coordinate of vertices
         // 設定 z 座標值
         quad.v[i].z=0.5f;
         // Set up color. The format of DWORD col is 0xAARRGGBB
         // 設定顏色,格式是 0xAARRGGBB (AA透明度,RR紅色,GG綠色,BB藍色)
         quad.v[i].col=0xFFFFA000;
      }

      // Set up quad's texture coordinates.
      // 0,0 means top left corner and 1,1 -
      // bottom right corner of the texture.
      // 設定 quad 的材質座標.
      // 0,0 代表左上,1,1代表材質的右下.
      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;

      // Let's rock now!
      // 出發 !
      hge->System_Start();

      // Free loaded texture and sound
      // 釋放 載入 材質 和 聲音
      hge->Texture_Free(quad.tex);
      hge->Effect_Free(snd);
   }
   else MessageBox(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);

   // Clean up and shutdown
   // 清除 和 關閉
   hge->System_Shutdown();
   hge->Release();
   return 0;
}


從上方程式碼可得知 :
代碼:

1. hgeQuad 是基礎四方形型別,HEFFECT 是聲音型別.
2. hge->Effect_PlayEx(HEFFECT型別,音量,位置,音準) 可以播放聲音.
3. hge->Timer_GetDelta()取得上次執行迴圈的時間間隔.
4. bool RenderFunc() 繪出用函數,hge->Gfx_BeginScene() 開始繪出前需呼叫,hge->Gfx_Clear(0)清除螢幕為黑色,hge->Gfx_RenderQuad(&quad)繪出quad,hge->Gfx_EndScene()結束繪出並更新螢幕.
5. 新的系統變數 :
   a. HGE_RENDERFUNC : 設定繪出函數.
   b. HGE_SCREENWIDTH : 設定視窗寬度.
   c. HGE_SCREENHEIGHT : 設定視窗高度.
   d. HGE_SCREENBPP : 設定顏色深度.
6. hge->Effect_Load 載入聲音檔,hge->Texture_Load 載入材質檔.
7. hge->Texture_Free()釋放 材質 ,hge->Effect_Free()釋放 聲音.
8. quad :
   a. quad.v[0-3].x 和 quad[0-3].y : 設定 quad 在螢幕上輸出座標和大小.
   b. quad.tex : quad 的材質.
   c. quad.blend : 設定 quad 的混合模式()
   d. quad.z : quad 的 z 軸座標.
   e. quad.col : quad 的顏色遮罩.
   f. quad.v[0-3].tx 和 quad.v[0-3].ty : 設定顯示材質檔圖片範圍.

基本程式流程 :
代碼:

1. hgeCreate(HGE_VERSION) 建立 HGE 介面
2. hge->System_SetState() 設定 HGE 系統變數
3. hge->System_Initiate() 初始化,失敗往 8
4. hge->Effect_Load() 載入聲音,hge->Texture_Load() 載入材質,失敗往 8
   5. FrameFunc() ,判斷輸入,有碰撞就執行 boom(),按下 esc 就往 7
   6. RenderFunc() 繪出,然後回到 5
   7. Texture_Free() Effect_Free() 釋放 材質 和 聲音,往 9
8. MessageBox() 顯示錯誤訊息
9. hge->System_Shutdown(),hge->Release() ,釋放 HGE

看的懂這個程式,就可以做出 顯示地圖 , 人物移動動畫(就是用方向鍵移動然後人物有走路動畫) 等等.
其實用這些已經可以寫出用鍵盤玩的遊戲了.
不過通通用quad畫的話會瘋掉,第三小節會教你用sprite畫.

[額外補充研究]-------------------------------------------------------------

整 合 第一小節和第二小節的試作品 :
會開啟 pic\background\backgound.png 並用quad顯示出來.
代碼:

#include "..\..\include\hge.h"

HGE *hge = 0;
hgeQuad quad;

// This function will be called by HGE once per frame.
// Put your game loop code here. In this example we
// just check whether ESC key has been pressed.
// 每 frame 時 HGE 會呼叫此函數.
// 將你的遊戲迴圈碼放在這邊.
// 這裡的範例是檢查 esc 是否被按下.
bool FrameFunc()
{
   float dt=hge->Timer_GetDelta();
   // By returning "true" we tell HGE
   // to stop running the application.
   // 當傳回 "true" 時代表停止迴圈
   if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
   // 設定繪出整張圖片
   quad.v[0].x = 0; quad.v[0].y = 0;
   quad.v[1].x = 640; quad.v[1].y = 0;
   quad.v[2].x = 640; quad.v[2].y = 480;
   quad.v[3].x = 0; quad.v[3].y = 480;

   // Continue execution
   return false;
}

bool RenderFunc()
{
   // Begin rendering quads.
   // This function must be called
   // before any actual rendering.
   // 開始繪出 quads
   // 此函數必須在開始繪出之前先呼叫.
   hge->Gfx_BeginScene();

   // Clear screen with black color
   // 用黑色塗滿螢幕.
   hge->Gfx_Clear(0);

   // Render quads here. This time just
   // one of them will serve our needs.
   // 這裡開始繪出 quads .
   hge->Gfx_RenderQuad(&quad);

   // End rendering and update the screen
   // 結束繪出並更新螢幕.
   hge->Gfx_EndScene();

   // RenderFunc should always return false
   // RenderFunc 應該總是傳回 false
   return false;
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
   // Here we use global pointer to HGE interface.
   // Instead you may use hgeCreate() every
   // time you need access to HGE. Just be sure to
   // have a corresponding hge->Release()
   // for each call to hgeCreate()
   // 這裡我們用全域指標指向 HGE 介面.
   // 當你使用 hgeCreate() 來存取 HGE 時,請確認後面不用時有 hge->Release() 釋放.
   hge = hgeCreate(HGE_VERSION);

   // Set our frame function
   // 設定 frame 函數
   hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
   hge->System_SetState(HGE_RENDERFUNC, RenderFunc);

   // Set the window title
   // 設定 視窗 標題
   hge->System_SetState(HGE_TITLE, "Draw Picture !! (測試版)");
   
   // 在視窗模式運作,更改為是 640x480
   hge->System_SetState(HGE_WINDOWED, true);
   hge->System_SetState(HGE_SCREENWIDTH, 640);
   hge->System_SetState(HGE_SCREENHEIGHT, 480);
   hge->System_SetState(HGE_SCREENBPP, 32);
   // Don't use BASS for sound
   // 不使用 BASS 聲音輸出
   hge->System_SetState(HGE_USESOUND, false);

   // Tries to initiate HGE with the states set.
   // If something goes wrong, "false" is returned
   // and more specific description of what have
   // happened can be read with System_GetErrorMessage().
   // 嘗試初始化 HGE 狀態設定.
   // 發生錯誤時會傳回 "false" ,可以透過 System_GetErrorMessage()
   // 來瞭解更多資訊.
   if(hge->System_Initiate())
   {
      quad.tex = hge->Texture_Load("pic\\background\\background.png");

      if(!quad.tex)
      {
         // If one of the data files is not found, display
         // an error message and shutdown.
         // 如果沒找到任何一個資料檔,顯示錯誤並結束
         MessageBox(NULL, "Can't load background.png", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
         hge->System_Shutdown();
         hge->Release();
         return 0;
      }
    
     for(int i=0;i<4;i++)
      {
         // Set up z-coordinate of vertices
         // 設定 z 座標值
         quad.v[i].z=0.5f;
         // Set up color. The format of DWORD col is 0xAARRGGBB
         // 設定顏色,格式是 0xAARRGGBB (AA透明度,RR紅色,GG綠色,BB藍色)
         quad.v[i].col=0xFFFFFFFF;
      }
     // 讀入整張圖片
     quad.v[0].tx=0; quad.v[0].ty=0;
      quad.v[1].tx=1; quad.v[1].ty=0;
      quad.v[2].tx=1; quad.v[2].ty=1;
      quad.v[3].tx=0; quad.v[3].ty=1;

     // Starts running FrameFunc().
      // Note that the execution "stops" here
      // until "true" is returned from FrameFunc().
      // 開始執行 FrameFunc().
      // 註 : 直到 FrameFunc() 傳回 "true" 之前,程式會停在這行.
      hge->System_Start();

     hge->Texture_Free(quad.tex);
   }
   else
   {   
      // If HGE initialization failed show error message
      // 如果 HGE 初始化有問題就顯示錯誤資訊
      MessageBox(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
   }

   // Now ESC has been pressed or the user
   // has closed the window by other means.
   // 執行到這邊不是 ESC 被按下就是使用者透過其他方式關閉視窗

   // Restore video mode and free
   // all allocated resources
   // 還原顯示模式和釋放資源
   hge->System_Shutdown();

   // Release the HGE interface.
   // If there are no more references,
   // the HGE object will be deleted.
   // 釋放 HGE 介面.
   hge->Release();

   return 0;
}

_________________
已經畢業了!!


babu61509 在 2008-8-26, AM 9:40 星期二 作了第 5 次修改
回頂端
檢視會員個人資料 發送私人訊息
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:10 星期六    文章主題: 引言回覆

HGE 範例研究報告 第三小節

HGE 1.81 hge_tut03.cpp

(註解有大概翻譯一下,看不懂翻譯的就看原文吧XD)

代碼:

/*
** Haaf's Game Engine 1.8
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** hge_tut03 - Using helper classes
** hge_tut03 - 輔助類別應用
*/


// Copy the files "particles.png", "menu.wav",
// "font1.fnt", "font1.png" and "trail.psi" from
// the folder "precompiled" to the folder with
// executable file. Also copy hge.dll and bass.dll
// to the same folder.
// 從 "precompiled" 複製 "particles.png" , "menu.wav" ,
// "font1.fnt", "font1.png" 和 "trail.psi"到執行資料夾.
// hge.dll 和 bass.dll 也要複製到同資料夾.

#include "..\..\include\hge.h"
#include "..\..\include\hgesprite.h"
#include "..\..\include\hgefont.h"
#include "..\..\include\hgeparticle.h"


// Pointer to the HGE interface.
// Helper classes require this to work.
// HGE 介面指標
// 輔助類別需要這個才能用
HGE *hge=0;


// Pointers to the HGE objects we will use
// 一些指向我們會用的HGE物件指標
hgeSprite*         spr;
hgeSprite*         spt;
hgeFont*         fnt;
hgeParticleSystem*   par;

// Handles for HGE resourcces
// HGE 資源處理
HTEXTURE         tex;
HEFFECT            snd;

// Some "gameplay" variables
// 一些 "gameplay" 變數
float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;

const float speed=90;
const float friction=0.98f;

// Play sound effect
// 播放聲音特效
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);
}

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

   // Process keys
   // 按鍵處理
   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;

   // Do some movement calculations 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();}

   // Update particle system
   // 更新粒子系統
   par->info.nEmission=(int)(dx*dx+dy*dy)*2;
   par->MoveTo(x,y);
   par->Update(dt);

   return false;
}


bool RenderFunc()
{
   // Render graphics
   // 繪出圖形
   hge->Gfx_BeginScene();
   hge->Gfx_Clear(0);
   par->Render();
   spr->Render(x, y);
   fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d (constant)", hge->Timer_GetDelta(), hge->Timer_GetFPS());
   hge->Gfx_EndScene();

   return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
   hge = hgeCreate(HGE_VERSION);

   hge->System_SetState(HGE_LOGFILE, "hge_tut03.log");
   hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
   hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
   hge->System_SetState(HGE_TITLE, "HGE Tutorial 03 - Using helper classes");
   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);

   if(hge->System_Initiate()) {

      // Load sound and texture
      // 載入聲音和材質
      snd=hge->Effect_Load("menu.wav");
      tex=hge->Texture_Load("particles.png");
      if(!snd || !tex)
      {
         // If one of the data files is not found, display
         // an error message and shutdown.
         // 如果有資料檔沒找到
         // 顯示錯誤並關閉
         MessageBox(NULL, "Can't load one of the following files:\nMENU.WAV, PARTICLES.PNG, FONT1.FNT, FONT1.PNG, TRAIL.PSI", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
         hge->System_Shutdown();
         hge->Release();
         return 0;
      }

      // Create and set up a sprite
      // 建立並設定 sprite
      spr=new hgeSprite(tex, 96, 64, 32, 32);
      spr->SetColor(0xFFFFA000);
      spr->SetHotSpot(16,16);

      // Load a font
      // 載入字型
      fnt=new hgeFont("font1.fnt");

      // Create and set up a particle system
      // 建立並設定粒子系統
      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();

      // Let's rock now!
      // 出發 !
      hge->System_Start();

      // Delete created objects and free loaded resources
      // 刪除已建立物件並釋放載入資源
      delete par;
      delete fnt;
      delete spt;
      delete spr;
      hge->Texture_Free(tex);
      hge->Effect_Free(snd);
   }

   // Clean up and shutdown
   // 清除並關閉
   hge->System_Shutdown();
   hge->Release();
   return 0;
}


從上方程式碼可得知 :
代碼:

1. 要使用 sprite, font ,ParticleSystem 要分別 include hgesprite.h, hgefont.h, hgeparticle.h .
2. hgeSprite 是 sprite 類別,hgeFont 是 font 類別,hgeParticleSystem 是 ParticleSystem 類別 .
3. HTEXTURE 是 材質型別 .
4. par->info.nEmission 設定每秒產生的粒子數(最多不能超過500),par->MoveTo 移動粒子位置,par->Update 設定多久更新粒子一次 .
5. par->Render() 繪出粒子系統,spr->Render 繪出 spr,fnt->printf 繪出 fnt .
6. 新的系統變數 :
   a. HGE_FPS : 設定fps最高更新率.
7. spr->SetColor 設定顏色,spr->SetHotSpot 設定基準點 .
8. par->Fire 啟用 par .

基本程式流程 就不寫了,跟上節的差不多,只是多了一些指令 .

其實也就是
開始 -> WinMain() -> FrameFunc() -> RenderFunc() -> FrameFunc() -> RenderFunc() -> ... -> WinMain() -> 結束
的循環.

這次應用了 sprite ,字型和粒子系統 離寫出一個遊戲也越來越近了喔= w=
p.s 原本的hge是不支援雙字節字型輸出的,不過有 hgettf 這種東西可以讓它支援
大陸那邊也有一些支援中文的方法,有興趣的人可以去找找看

_________________
已經畢業了!!


babu61509 在 2008-8-26, AM 10:27 星期二 作了第 1 次修改
回頂端
檢視會員個人資料 發送私人訊息
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:11 星期六    文章主題: 引言回覆

保留給第四小節
_________________
已經畢業了!!
回頂端
檢視會員個人資料 發送私人訊息
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:11 星期六    文章主題: 引言回覆

保留給第五小節
_________________
已經畢業了!!
回頂端
檢視會員個人資料 發送私人訊息
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:12 星期六    文章主題: 引言回覆

保留給第六小節
_________________
已經畢業了!!
回頂端
檢視會員個人資料 發送私人訊息
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:12 星期六    文章主題: 引言回覆

保留給第七小節
_________________
已經畢業了!!
回頂端
檢視會員個人資料 發送私人訊息
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2008-5-10, AM 11:12 星期六    文章主題: 引言回覆

保留給第八小節
_________________
已經畢業了!!
回頂端
檢視會員個人資料 發送私人訊息
從之前的文章開始顯示:   
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 遊戲程式進階班:各種進階圖型引擎或遊戲引擎 所有的時間均為 台灣時間 (GMT + 8 小時)
1頁(共1頁)

 
前往:  
無法 在這個版面發表文章
無法 在這個版面回覆文章
無法 在這個版面編輯文章
無法 在這個版面刪除文章
無法 在這個版面進行投票
可以 在這個版面附加檔案
可以 在這個版面下載檔案


Powered by phpBB © 2001, 2005 phpBB Group
正體中文語系由 phpbb-tw 維護製作