本章目的 2D / 3D 遊戲程式設計入門使用 XNA 3.0 與 C# 探討 XNA 遊戲程式內部的基本架構與遊戲開發流程 示範如何完成一個簡單的 XNA 遊戲方案 第三章 XNA 遊戲程式基本架構 1 2 新增 XNA 專案 新增 XNA 專案 3 4
XNA 相關的命名空間 Game1.cs 程式中的六個函數 using Microsoft.Xna.Framework; // 和 XNA 架構相關的型別 using Microsoft.Xna.Framework.Audio; // 和 XNA 聲音相關的型別 using Microsoft.Xna.Framework.Content; // 和 XNA 內容輸出入相關的型別 using Microsoft.Xna.Framework.GamerServices; // 和 XNA 玩家權限 相關的型別 using Microsoft.Xna.Framework.Graphics; // 和 XNA 繪出相關的型別 using Microsoft.Xna.Framework.Input; // 和 XNA 輸入相關的型別 using Microsoft.Xna.Framework.Media; // 和 XNA 多媒體相關的型別 using Microsoft.Xna.Framework.Net; // 和 XNA 網路相關的型別 using Microsoft.Xna.Framework.Storage; // 和 XNA 儲存相關的型別 Game1() Update() Draw() 5 6 XNA 的方案總管 一個圖示 (Game.ico) 一個.png 圖形檔案 (GameThumbnail.png) 兩個程式檔案 (Game1.cs 和 Program.cs) Program.cs 程式 using System; namespace WindowsGame1 static class Program static void Main(string[] args) using (Game1 game = new Game1()) game.run(); 7 8
一般遊戲程式的流程 Game1 類別內遊戲程式的流程 初始化繪圖卡 遊戲初始化 Game1() 一般初始化 遊戲初始化 載入資源 (2D 圖形 3D 模型 音效檔 字型等 ) 得到使用者的輸入 ( 鍵盤 滑鼠 ) 載入資源 (2D 圖形 3D 模型 音效檔 字型等 ) 遊戲迴圈 執行必要的邏輯更新 ( 移動 碰撞 AI 結束 ) 繪出 ( 圖形產出 聲音 ) 遊戲迴圈 得到使用者的輸入 ( 鍵盤 滑鼠 ) 執行必要的邏輯更新 ( 移動 碰撞 AI 結束 ) 關閉繪圖卡 繪出 ( 圖形產出 聲音 ) 釋放資源 釋放資源 9 10 game.run() 的執行流程 Game1 的全域變數與建構元 遊戲迴圈 public class Game1 : Microsoft.Xna.Framework.Game GraphicsDeviceManager graphics; SpriteBatch spritebatch; public Game1() graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; 11 12
使用 SpriteBatch 物件來繪出 2D 圖形 spritebatch.begin(); // 設定 2D 圖形的繪出方式 spritebatch.draw(...); // 繪出第一張 2D 圖形 spritebatch.draw(...); // 繪出第二張 2D 圖形 spritebatch.end();// 結束 2D 圖形的繪出 內容資源管理員 Content.RootDirectory = "Content"; 13 14 XNA 內建的資源檔案輸入器 XNA 內建的資源檔案輸入器 AudioImporters 用來讀入音效檔案 EffectImporter 用來讀入.fx 著色器特效檔案, 該檔案是以高階著色語言 (HLSL) 撰寫, 用來描述頂點著色器 像素著色器等產出方式與特效的檔案 FBXImporter 用來讀入以.fbx 格式儲存的 3D 模型檔案 XImporter 用來讀入以. x 格式儲存的 3D 模型檔案 TextureImporter 用來讀入 2D 圖形檔案, 可支援的格式包括.bmp.dds.dib.hdr.jpg.pfm.png.ppm 和.tga FontDescriptionImporter 用來讀入以.spritefont 格式儲存的字型檔案 XAP 輸入器用來讀入以 XACT 工具製作出來的聲音檔案 15 16
XNA 的內容資源管道流程 遊戲初始化 Importer Document Object Model (DOM).XNB file (binary file) Content Content Content Processor Compiler Loader 專案設計期間 專案執行期間 protected override void // TODO: Add your initialization logic here base.; 17 18 上載資源檔案 protected override void // Create a new SpriteBatch, which can be used to draw textures. spritebatch = new SpriteBatch(GraphicsDevice); // TODO: use this.content to load your game content here 釋放資源 protected override void // TODO: Unload any non ContentManager content here 19 20
Update() 得到輸入 進行邏輯更新 protected override void Update(GameTime gametime) // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons. ( Back == ButtonState.Pressed) this.exit(); // TODO: Add your update logic here base.update(gametime); 21 遊戲迴圈內定每秒執行 60 次 遊戲迴圈每秒執行 60 次 22 更換更新時間 public Game1() this.targetelapsedtime = new TimeSpan(0, 0, 0, 0, 33);// 三十分之一秒 運作流程改為連續更新 public Game1() this.isfixedtimestep = false; 23 24
Draw() 繪出 XNA 專案加入新的類別 protected override void Draw(GameTime gametime) graphics.graphicsdevice.clear(color.cornflowe p rblue); // TODO: Add your drawing code here base.draw(gametime); 25 26 XNA 專案加入 GameComponent 類別 Game1 物件的子元件 GameComponent 物件 Game1 GameComponent1 建構元 () 27 28
Game1 物件的子元件 DrawableGameComponent 物件 Game DrawableGameComponent 建構元 () The End 29 30