2013/09/20

XNA 4.0 簡介

XNA微軟針對遊戲跨平台出的一個框架,在2006年發布出XNA BuildXNA能夠開發出XBOX 360以及WindowsWP手機的遊戲,且XNA能夠直接使用顯示卡硬體

開發的朋友請下載VS 2010,為什麼不下載最新的2012或是2013,原因是XNA最新的版本為4.0微軟已不更新XNA,最新版本是4.0



XBOX 360以及WindowsWP三種產物的硬體規格皆不相同,XNA為了解決該問題,XNA提供兩種格式,分別是Reach和HiDef,以下表格將詳細說明Reach和HiDef的差異

Reach
HiDef
Supported platforms
Windows Phone 7 Series, Xbox 360, and any Windows PC with a DirectX 9 GPU that supports at least shader model 2.0
Xbox 360, and any Windows PC with a DirectX 10 (or equivalent: see below) GPU
Shader model
2.0  (but Windows Phone does not support custom shaders)
3.0+  (Xbox 360 supports custom shader extensions such as vfetch, which are not available on Windows)
Max texture size
2048
4096
Max cubemap size
512
4096
Max volume texture size
Volume textures are not supported
256
Non power of two textures
Conditional: cannot use wrap addressing mode, mipmaps, or DXT compression when the size is not a power of two
Yes
Non power of two cubemaps
No
Yes
Non power of two volume textures
Volume textures are not supported
Yes
Max primitives per draw call
65535
1048575
Index buffer formats
16 bit
16 and 32 bit
Vertex element formats
Color, Byte4, Single, Vector2, Vector3, Vector4, Short2, Short4, NormalizedShort2, NormalizedShort4
All of the Reach formats, plus HalfVector2, HalfVector4
Texture formats
Color, Bgr565, Bgra5551, Bgra4444, NormalizedByte2, NormalizedByte4, Dxt1, Dxt3, Dxt5
All of the Reach formats, plus Alpha8, Rg32, Rgba64, Rgba1010102, Single, Vector2, Vector4, HalfSingle, HalfVector2, HalfVector4. Floating point texture formats do not support filtering.
Vertex texture formats
Vertex texturing is not supported
Single, Vector2, Vector4, HalfSingle, HalfVector2, HalfVector4
Render target formats
Variable (see below)
Variable (see below)
Multiple render targets
No
Up to 4. Must all have the same bit depth. Supports alpha blending and independent write masks per rendertarget.
Occlusion queries
No
Yes
Separate alpha blend
No
Yes
Blend.SourceAlphaSaturation
Only for SourceBlend, not DestinationBlend
Yes
Max vertex streams
16
16
Max stream stride
255
255

要如何變換成Reach或HiDef呢?首先對專案點右鍵/屬性





只要選取Reach或HiDef選項,儲存完即可改變





先看平常遊戲的流程圖:





在來看XNA的流程圖:




可經由流程圖得知,XNA已經大幅度縮短遊戲開發的時間,並制定有規範的流程


程式碼如下:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame5
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // 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
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        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);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}





  • base是Java的super


參考資料:
http://www.cnblogs.com/rainstorm/archive/2013/02/10/2909817.html
http://gamedev.stackexchange.com/questions/9748/xna-am-i-screwing-up-the-loadcontent-for-texture2d
http://www.microsoft.com/taiwan/student/learn/XNA_in_Windows_Mobile.aspx
http://zh.wikipedia.org/wiki/XNA_Game_Studio_Express
http://blog.csdn.net/beyondma/article/details/5920135
http://www.dotblogs.com.tw/sonic10690/archive/2008/12/03/6175.aspx
www.csim.scu.edu.tw/~chiang/course/ComputerGame/Chap03.ppt
XNA 4遊戲程式設計:適用Windows PC、Phone、Xbox 360
http://blogs.msdn.com/b/shawnhar/archive/2010/03/12/reach-vs-hidef.aspx

照片參考:
www.csim.scu.edu.tw/~chiang/course/ComputerGame/Chap03.ppt