顯示具有 XNA 標籤的文章。 顯示所有文章
顯示具有 XNA 標籤的文章。 顯示所有文章

2014/01/03

XNA 使用Kinect並顯示影像

寫過Kinect會知道顯示影像是透過byte的array去取得,在將值讓UI去呈現
XNA則是透過座標以及一些方框去接收,透過Draw方法將影像描繪出來


程式碼:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Kinect;

namespace WindowsGame3
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {

        /// <summary>
        /// 圖形管理
        /// </summary>
        GraphicsDeviceManager graphics;


        /// <summary>
        /// 繪圖
        /// </summary>
        SpriteBatch spriteBatch;


        /// <summary>
        /// Kinect Sensor
        /// </summary>
        KinectSensor kinect;


        /// <summary>
        /// Kinect框架
        /// </summary>
        Texture2D texture2D;

        /// <summary>
        /// 矩形
        /// </summary>
        readonly Rectangle rect = new Rectangle(0, 0, 640, 480);

        /// <summary>
        /// 影像資料
        /// </summary>
        byte[] dataPixels;

        /// <summary>
        /// Kinect初始化
        /// </summary>
        bool isKinectInit = false;


        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>
        /// Kinect初始化
        /// </summary>
        private void Init()
        {
            var sensor = ((from s in KinectSensor.KinectSensors where (s.IsRunning != true && s.Status == KinectStatus.Connected) select s)).FirstOrDefault();

            if (sensor == null)
                isKinectInit = false;
            else
            {
                this.kinect = sensor;
                this.kinect.Start();
                this.kinect.ColorStream.Enable();
                this.kinect.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinect_ColorFrameReady);
            }
        }

        void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame == null)
                    return;

                dataPixels = new byte[frame.PixelDataLength];
                frame.CopyPixelDataTo(dataPixels);

                texture2D = new Texture2D(GraphicsDevice, 640, 480);
                texture2D.SetData(dataPixels);

            }
        }

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

            if (texture2D != null)
            {

                spriteBatch.Begin();
                spriteBatch.Draw(texture2D, rect, Color.White);
                spriteBatch.End();
            }
            base.Draw(gameTime);
        }
    }
}

2014/01/02

XNA 載入文字

XNA要顯示文字必須在Content專案加入『Sprite Font』

Sprite Font裡面又包含字體、大小、間距、字聚、風格以及字元的選擇
在尚未修改前的格式如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    <!--
    Modify this string to change the font that will be imported.
    -->
    <FontName>Segoe UI Mono</FontName>

    <!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    -->
    <Size>14</Size>

    <!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    -->
    <Spacing>0</Spacing>

    <!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    -->
    <UseKerning>true</UseKerning>

    <!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    -->
    <Style>Regular</Style>

    <!--
    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    -->
    <!-- <DefaultCharacter>*</DefaultCharacter> -->

    <!--
    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.
    -->
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
   
  </Asset>
</XnaContent>

初始字只支援Ascii code的32到126字元,想新增其他字元可以在『CharacterRegion』結尾處在新增一個『CharacterRegion』區塊

2013/12/25

XNA 載入圖片

先在WindowsGame1Content專案點右鍵,加入/現有項目



選擇任何一張圖片



2013/10/16

XBOX360 偵測按鈕

XNA有提供GamePadState類別可以偵測觸發了哪個按鈕,XBOX360可以在同時間偵測4個手把的動作
要取得第一個把手觸發的按鈕,可以宣告一個GamePadState類別,並且透過GamePadGetState去取得狀態

GamePadGetState的方法可以帶入指定的手把編號,手把編號可以透過PlayerIndex列舉去指定

GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

當第一個手把按下手把上的按鈕,就會將程式標題改成按鈕名稱




2013/09/29

將XNA佈署到Xbox360上

XNA佈署到Xbox360上應該是很簡單地,第一次用了3個小時才佈署上,幹 微軟 有你的!失敗乃寫編程常有的事情

首先將你的Xbox360開機,開機完後,先將Xbox360更新


更新大概二十分鐘就結束了,這時間你可以先去辦一個開發者帳號

先點到App Hub,會看到WP以及Xbox,選擇Xbox,像我有在開發WP者再去申請WP就好了

點左邊的Join to submit your Xbox 360 games

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呢?首先對專案點右鍵/屬性