66
XNA 4.0 基基基基 基基基基基基基 基基基 基基基 © 2011

XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Embed Size (px)

Citation preview

Page 1: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

XNA 4.0 基本類別靜宜大學資工系蔡奇偉 副教授© 2011

Page 2: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

大綱• Point Structure• Rectangle Structure• Vector2 Structure• Color Structure• Matrix• GraphicsDeviceManager• Game Class• Texture2D• 參考資料

Page 3: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Point Structure – 用於表示 2D 的位置

Namespace: Microsoft.Xna.Framework

Constructor

public Point (int x, int y)

範例

Point p = new Point(3, 4);

Point q;

q.X = 3;

q.Y = p.Y + 4;

Public Fields

int X;int Y;

註: struct 和 class 非常相似,最大的差異在於 struct 是 value 型態,而 class 是 reference 型態。

Page 4: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

Point.Zero // 原點位置 (0, 0)

Public Methods

範例

Point p = new Point(3, 4);

Point q = new Point(5, 6);

Point r = q;

if (p.Equals(q)) { … } // false

if (p != q) { … } // true

if (q == r) { … } // true

Namespace: Microsoft.Xna.Framework

Page 5: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Rectangle Structure – 用於表示 2D 的矩形

Namespace: Microsoft.Xna.Framework

Constructor

public Rectangle (int x, int y, int width, int height)

範例

Rectangle p = new Rectangle(10, 20, 56, 78);

Rectangle q = p;

q.X = p.Y * 3;

Public Fields

int Width, Height;int X, Y;

(X, Y)

Height

Width

x

y

Page 6: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Namespace: Microsoft.Xna.Framework

Public Properties

(X, Y)

Height

Width

Top

Right

Bottom

Left

Location

Center

Rectangle.Empty: 空矩形,即 X = 0, Y = 0, Height = 0, 與 Width = 0

isEmpty: true if the rectangle is empty; otherwise false.

get or set the location of rectangle ; eg,

rect.Location = Point.Zero; // move r to (0, 0)

Page 7: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public bool Contains (int x, int y)• public bool Contains (Point pt)• public void Contains (ref Point pt, out bool result)

true false範例

Rectangle rect = new Rectangle(10, 20, 56, 78);

Point pt1 = new Point(12, 25);

rect.Contains(pt1) // true

rect.Contains(1000, 2000) // false

矩形包含點

Page 8: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public bool Contains (Rectangle r)• public void Contains (ref Rectangle r, out bool result)

false

範例Rectangle rect = new Rectangle(10, 20, 56, 78);

Rectangle r1 = new Rectangle(15, 25, 10, 10);

rect.Contains(r1) // true

r1.Location = new Point(180, 200);

rect.Contains(r1) // false

矩形包含矩形

true false

Page 9: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public bool Intersects (Rectangle r)• public void Intersects (ref Rectangle r, out bool result)

true

範例Rectangle rect = new Rectangle(10, 20, 56, 78);

Rectangle r1 = new Rectangle(15, 25, 10, 10);

rect.Intersects(r1) // true

r1.Location = new Point(180, 200);

rect.Intersects(r1) // false

矩形是否相交

true false

Page 10: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public static Rectangle Intersect (Rectangle r1, Rectangle r2)

• public static void Intersect (ref Rectangle r1, ref Rectangle r2, out Rectangle result )

範例Rectangle r1 = new Rectangle(10, 20, 56, 78);

Rectangle r2 = new Rectangle(35, 40, 60, 50);

Rectangle r3 = Rectangle.Intersect(r1, r2);

// r3: (X = 35, Y = 40, Width = 31, Height = 50)

矩形的交集

Page 11: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public static Rectangle Union (Rectangle r1, Rectangle r2)

• public static void Union (ref Rectangle r1, ref Rectangle r2, out Rectangle result )

範例Rectangle r1 = new Rectangle(10, 20, 56, 78);

Rectangle r2 = new Rectangle(35, 40, 60, 50);

Rectangle r3 = Rectangle.Union(r1, r2);

// r3: (X = 10, Y = 20, Width = 85, Height = 78)

矩形的聯集

Page 12: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public void Inflate (int h, int v)

範例Rectangle r1 = new Rectangle(10, 20, 56, 78);

r1.Inflate(10, 20);

// r1: (0, 0, 76, 118)

延伸或壓縮

v

h

v > 0, h > 0 v < 0, h < 0

Page 13: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public void Offset (int dx, int dy)

• public void Offset (Point amount)

範例Rectangle r1 = new Rectangle(10, 20, 56, 78);

r1.Offset(-10, 10);

// r1: (0, 30, 56, 78)

位移

dy

dx

Page 14: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

範例Rectangle r1 = new Rectangle(10, 20, 56, 78);

Rectangle r2 = r1, r3 = r1;

if (r1.Equals(r2)) { … } // true

if (r1 == r2) { … } // true

if (r1 != r2) { … } // false

r2.Offset(1, 0);

if (r1 == r2) { … } // false

r3.Inflate(1, 2);

if (r1 == r3) { … } // false

矩形比較 兩矩形相等的條件為:位置與大小完全相同。

Page 15: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Vector2 Structure – 用於表示 2D 的向量

Namespace: Microsoft.Xna.Framework

Constructor

• public Vector2 (float x, float y)• public Vector2 (float v)

範例

Vector2 v = new Vector2(3, 4); // v = (3,4)

Vector2 u = new Vector2(5.0f); // u = (5,5)

u.X = v.Y + 3.0f; // u = (7,5)

Public Fields

float X;float Y;

Page 16: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

Vector2.Zero // (0,0) 零向量Vector2.UnitX // (1,0) x- 軸單位向量Vector2.UnitY // (0,1) y- 軸單位向量Vector2.One // (1,1) 向量

Namespace: Microsoft.Xna.Framework

Page 17: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

public static Vector2 Add (Vector2 value1, Vector2 value2)

public static void Add (ref Vector2 value1, ref Vector2 value2, out Vector2 result)

public static Vector2 operator + (Vector2 value1, Vector2 value2)

Addition

範例

Vector2 v = new Vector2(3, 4); // v = (3,4)

Vector2 u = new Vector2(5.0f); // u = (5,5)

Vector2 w;

w = Vector2.Add(u, v) // w = (8,9)

Vector2.Add(ref u, ref v, out w) // w = (8,9)

w = u + v; // w = (8,9)

Page 18: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

public static Vector2 Subtract (Vector2 value1, Vector2 value2)

public static void Subtract (ref Vector2 value1, ref Vector2 value2, out Vector2 result)

public static Vector2 operator - (Vector2 value1, Vector2 value2)

Subtract

範例

Vector2 v = new Vector2(3, 4); // v = (3,4)

Vector2 u = new Vector2(5.0f); // u = (5,5)

Vector2 w;

w = Vector2.Subtract(u, v) // w = (-2,-1)

Vector2.Subtract(ref u, ref v, out w) // w = (-2,-1)

w = u - v; // w = (-2,-1)

Page 19: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

public static Vector2 Multiply (Vector2 value, float s)

public static void Multiply (ref Vector2 value, float s, out Vector2 result)

public static Vector2 operator * (float s, Vector2 value)

public static Vector2 operator * (Vector2 value, float s)

Scalar Multiplication

範例

Vector2 v = new Vector2(3, 4); // v = (3,4)

Vector2 w;

w = Vector2.Multiply(u, 2.0f) // w = (6,8)

Vector2.Multiply(ref u, 2.0f, out w) // w = (6,8)

w = 2.0f * v; // w = (6,8)

w = v * 2.0f; // w = (6,8)

Page 20: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

Component-wise Multiplication

範例

public static Vector2 Multiply (Vector2 value1, Vector2 value2)

public static void Multiply (ref Vector2 value1, ref Vector2 value2, out Vector2 result)

public static Vector2 operator * (Vector2 value1, Vector2 value2)

Vector2 v = new Vector2(3, 4); // v = (3,4)

Vector2 u = new Vector2(5.0f); // u = (5,5)

Vector2 w;

w = Vector2.Multiply(u, v) // w = (15,20)

Vector2.Multiply(ref u, ref v, out w) // w = (15,20)

w = u * v; // w = (15,20)

Page 21: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

public static Vector2 Divide (Vector2 value, float s)

public static void Divide (ref Vector2 value, float s, out Vector2 result)

public static Vector2 operator * (Vector2 value, float s)

Scalar Division

範例

Vector2 v = new Vector2(6, 8); // v = (6,8)

Vector2 w;

w = Vector2.Divide(u, 2.0f) // w = (3,4)

Vector2.Divide(ref u, 2.0f, out w) // w = (3,4)

w = v / 2.0f; // w = (3,4)

Page 22: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

Component-wise Division

範例

public static Vector2 Divide (Vector2 value1, Vector2 value2)

public static void Divide (ref Vector2 value1, ref Vector2 value2, out Vector2 result)

public static Vector2 operator / (Vector2 value1, Vector2 value2)

Vector2 v = new Vector2(10, 15); // v = (10,15)

Vector2 u = new Vector2(5); // u = (5,5)

Vector2 w;

w = Vector2.Divide(u, v) // w = (2,3)

Vector2.Divide(ref u, ref v, out w) // w = (2,3)

w = u / v; // w = (2,3)

Page 23: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public static Vector2 Negate (Vector2 value)

• public static void Negate (ref Vector2 value, out Vector2 result)

• public static Vector2 operator - (Vector2 value)

Negate

範例

Vector2 u = new Vector2(3, 4); // u = (3,4)

r = Vector2.Negate(u); // r = (-3,-4)

r = -u; // r = (-3,-4)

Page 24: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

Equal

範例

public bool Equals (Vector2 other)

public static bool operator == (Vector2 value1, Vector2 value2)

public static bool operator != (Vector2 value1, Vector2 value2)

Vector2 v = new Vector2(10, 15); // v = (10,15)

Vector2 u = new Vector2(5); // u = (5,5)

Vector2 w = v;

v.Equals(u) // false

v.Equals(w) // true

v == w // true

v != u // true

Page 25: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

Distance

public static float Distance (Vector2 value1, Vector2 value2)

public static void Distance (ref Vector2 value1, ref Vector2 value2, out float result)

public static float DistanceSquared (Vector2 value1, Vector2 value2)

public static void DistanceSquared (ref Vector2 value1, ref Vector2 value2,

out float result)

1 2

2 21 2

2 2 21 2

( , ), ( , )

distance( , ) ( ) ( )

distance( , ) ( ) ( )

v a b v c d

v v c a d b

v v c a d b

Page 26: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

Length

public float Length ()

public float LengthSquared ()

2 2

2 2 2

( , )

length( )

length( )

v a b

v a b

v a b

Page 27: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public static float Dot (Vector2 u, Vector2 v)

• public static void Dot (ref Vector2 u, ref Vector2 v, out float result)

內積

( , ), ( , )a b c d

ac bd

u v

u v

範例

Vector2 u = new Vector2(3, 4); // u = (3,4)

Vector2 v = new Vector2(5.0f); // v = (5,5)

float r = Vector2.Dot(u,v); // r = 35

Page 28: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public void Normalize ()

• public static Vector2 Normalize (Vector2 value)

• public static void Normalize (ref Vector2 value, out Vector2 result)

正規化(轉換成同方向的單位向量)

2 2

( , )

1ˆ ( , )

a b

a ba b

u

uu

u

範例

Vector2 u = new Vector2(3, 4); // u = (3,4)

u.Normalize(); // u = (3/5,4/5)

Vector2.Normalize(u); // u = (3/5,4/5)

Page 29: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public static Vector2 Max (Vector2 u, Vector2 v)

• public static void Max (ref Vector2 u, ref Vector2 v, out Vector2 r)

• public static Vector2 Min (Vector2 u, Vector2 v)

• public static void Min (ref Vector2 u, ref Vector2 v, out Vector2 r)

Max & Min

範例

Vector2 u = new Vector2(3, 4); // u = (3,4)

Vector2 v = new Vector2(5, 2); // v = (5,2)

Vector2 r;

r = Vector2.Max(u,v); // r = (5,4)

Vector2.Min(ref u,ref v,out r); // r = (3,2)

Page 30: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public static Vector2 Clamp (Vector2 value1, Vector2 min,

Vector2 max)

• public static void Clamp (ref Vector2 value1, ref Vector2 min,

ref Vector2 max, out Vector2 result)

Clamp - Restricts a value to be within a specified range.

範例

Vector2 u = new Vector2(0.3f, 0.4f); // u = (0.3,0.4)

Vector2 v = new Vector2(-5, 2); // v = (-5,2)

Vector2 min = new Vector2(0, 0); // min = (0,0)

Vector2 max = new Vector2(1, 1); // max = (1,1)

Vector2 r;

r = Vector2.Clamp(u,min,max); // r = (0.3,0.4)

r = Vector2.Clamp(v,min,max); // r = (0,1)

Page 31: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public static Vector2 Transform (Vector2 position, Matrix matrix)

• public static void Transform (Vector2[] sourceArray, ref Matrix matrix,

Vector2[] destinationArray)

• public static void Transform (Vector2[] sourceArray, int sourceIndex,

ref Matrix matrix,

Vector2[] destinationArray, int destinationIndex, int length)

Transforms one or more Vector2s by a Matrix

Page 32: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

• public static Vector2 Reflect (Vector2 vector, Vector2 normal)

• public static void Reflect (ref Vector2 vector, ref Vector2 normal,

out Vector2 result)

Reflect

n

v

v’

範例

Vector2 u = new Vector2(3, 4);

Vector2 n = new Vector2(-1, 0);

Vector2 r = Vector2.Reflect(u, n);

// r = (-3,4)

必須為單位向量

' (2 )v v v n n

Page 33: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Color Structure – 用於表示顏色

Namespace: Microsoft.Xna.Framework

Constructor

• public Color (int r, int g, int b) • public Color (int r, int g, int b, int a)• public Color (float r, float g, float b) • public Color (float r, float g, float b, float a) • public Color (Vector3 vector)• public Color (Vector4 vector)

範例

Color c1 = new Color(255,0,0); // red

Color c2 = new Color(1.0f,0.0f,0.0f); // red

Color c3 = new Color(255,0,0,128); // 半透明 red

註: r: red, g: green, b: blue, a: alpha int 型態的參數必須介於 0 至 255 之間。 float 型態的參數必須介於 0.0 至 1.0 之間。

Page 34: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

A // 讀或寫顏色中的 alpha 值R // 讀或寫顏色中的 red 值G // 讀或寫顏色中的 green 值B // 讀或寫顏色中的 blue 值

Namespace: Microsoft.Xna.Framework

範例

Color c1 = new Color(255,0,0); // red

int red = c1.R; // red = 255

c1.B = 96; // c1 = (255,0,96)

Page 35: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

Color.White // 白色 (R:255 G:255 B:255 A:255)

Color.Red // 紅色 (R:255 G:0 B:0 A:255)

Color.Green // 綠色 (R:0 G:255 B:0 A:255)

Color.Blue // 藍色 (R:0 G:0 B:255 A:255)

Color.Black // 黑色 (R:0 G:0 B:0 A:255)

Color.Cyan // 青色 (R:0 G:255 B:255 A:255)

Color.Magenta // 洋紅色 (R:255 G:0 B:255 A:255)

Color.Yellow // 黃色 (R:255 G:255 B:0 A:255)

其他近百種顏色的名稱,請參考 XNA 參考手冊。

Namespace: Microsoft.Xna.Framework

Page 36: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

Equal

範例

public bool Equals (Color other)

public static bool operator == (Color value1, Color value2)

public static bool operator != (Color value1, Color value2)

Color c1 = new Color(255,0,0); // c1 = (255,0,0)

Color c2 = new Color(0,255,0); // c2 = (0,255,0)

Color c3 = c1; // c3 = (255,0,0)

c1.Equals(c2) // false

c1.Equals(c3) // true

c1 == c3 // true

c1 != c2 // true

Page 37: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

public static Color Multiply (Color value, float s)

public static Color operator * (Color value, float s)

Scalar Multiplication

範例

Color c1 = new Color(0.8f,0.4f,0.6f); // c1 = (0.8,0.4,0.6)

Color c2 = Color.Multiply(c1, 0.5f); // c2 = (0.4,0.2,0.3)

Color c3 = c1 * 0.5f; // c3 = (0.4,0.2,0.3)

Page 38: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

public static Color Lerp (Color value1, Color value2, float t)

Linear Interpolation (線性內插)

範例

Color c1 =Color.Lerp(Color.Yellow, Color.Magenta, 0.5f);

1 2

1 2

Lerp( , , ), 0 1

(1 )

c c c t t

c t c t c

Page 39: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

public class Game1 : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;

int m = 0;

程式範例 讓視窗的背景循環地由黃色逐漸變成洋紅色。

(ColorLerp)

加入資料成員 m 。

Page 40: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

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

m++;

base.Update(gameTime);

}

每一循環把 m 增加 1 。

Page 41: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

protected override void Draw(GameTime gameTime)

{

Color BGColor = Color.Lerp(Color.Yellow, Color.Magenta,

(float)(m % 600 / 599.0));

GraphicsDevice.Clear(BGColor);

// TODO: Add your drawing code here

base.Draw(gameTime);

}

利用 m % 600 / 599 計算公式使得背景每 330 圈由黃色變成洋紅色。

Page 42: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Matrix Structure

Namespace: Microsoft.Xna.Framework

Constructor

public Matrix ( float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44 )

11 12 13 14

21 22 23 24

31 32 33 34

41 42 43 44

M M M M

M M M M

M M M M

M M M M

Page 43: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Fields

Namespace: Microsoft.Xna.Framework

M11 M12 M13 M14

M21 M22 M23 M24

M31 M32 M33 M34

M41 M42 M43 M44

範例

Matrix m = Matrix.Identity;

m.M22 = 2.0f;

m.M33 = 3.0f;1 0 0 0

0 2 0 0m =

0 0 3 0

0 0 0 1

1 0 0 0

0 1 0 0m =

0 0 1 0

0 0 0 1

Page 44: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

Namespace: Microsoft.Xna.Framework

範例

Matrix m = Matrix.Identity;

1 0 0 0

0 1 0 0m =

0 0 1 0

0 0 0 1

public static Matrix Identity { get; }

Page 45: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

Namespace: Microsoft.Xna.Framework

11 12 13 14

21 22 23 24

31 32 33 34

41 42 43 44

M M M M

M M M M

M M M M

M M M M

Right

Up

Backward

Translation

Left = Right

Down = Up

Forward = Backward

參考網頁連結

Page 46: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

public static Matrix CreateTranslation (float x, float y, float z)

public static void CreateTranslation (float x, float y, float z, out Matrix r)

public static Matrix CreateTranslation (Vector3 position)

public static void CreateTranslation (ref Vector3 position, out Matrix r)

建立平移矩陣

Page 47: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

Namespace: Microsoft.Xna.Framework

矩陣運算

1 2: , , :s M M M純 , 量 矩陣

1 2

1 2

1 2

1 2

1 2

1 2

/ (component-wise)

/

!

M

M M

M M

M M

s M M s

M M

M s

M M

M M

負號:加法:減法:矩陣乘法:純量乘法: 或

矩陣除法:純量除 法:矩陣相等:矩陣不等:

Page 48: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

GraphicsDeviceManager Class

Namespace: Microsoft.Xna.Framework

Constructor

public GraphicsDeviceManager (Game game)

範例

public class Game1 : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;

public Game1()

{

graphics = new GraphicsDeviceManager(this);

Handles the configuration and management of the graphics device.

Page 49: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

PreferredBackBufferHeight // BackBuffer 的高度PreferredBackBufferWidth // BackBuffer 的寬度PreferredBackBufferFormat // BackBuffer 的像素格式

Namespace: Microsoft.Xna.Framework

PreferredBackBufferHeight 和 PreferredBackBufferWidth 可用來設定遊戲視窗或螢幕的高度與寬度。它們的預設值分別為

• GraphicsDeviceManager.DefaultBackBufferHeight (480)

• GraphicsDeviceManager.DefaultBackBufferWidth (800)

Page 50: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

範例

public class Game1 : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;

public Game1()

{

graphics = new GraphicsDeviceManager(this);

graphics.PreferredBackBufferWidth = 1024;

graphics.PreferredBackBufferHeight = 768

設定視窗大小為 1024x768

Page 51: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

public Game1()

{

graphics = new GraphicsDeviceManager(this);

graphics.PreferredBackBufferWidth = 1024;

graphics.PreferredBackBufferHeight = 768

graphics.IsFullScreen = true;

Public Properties

IsFullScreen // 判斷或設定全螢幕模式

Namespace: Microsoft.Xna.Framework

範例 設定解析度為 1024x768 的全螢幕

註:不適用於 Xbox 360 系統。

Page 52: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Method

public void ToggleFullScreen () // 切換視窗與全螢幕模式

Namespace: Microsoft.Xna.Framework

註:不適用於 Xbox 360 系統。

Page 53: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Screen Resolution

Name Resolution Aspect Ratio

VGA 640×480 4:3

SVGA 800×600 4:3

XGA 1024×768 4:3

SXGA 1280×1024 4:3

UXGA 1600×1200 4:3

WSVGA 1024×600 16:9

WXGA 1280×720 16:9

WXGA 1366×768 16:9

HDTV 1920×1080 16:9

WXGA 1280×800 16:10

WSXGA+ 1680×1050 16:10

WUXGA 1920×1200 16:10

Wiki: Graphic display resolutions

PC Screens

Page 54: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Screen Resolution

Name Resolution Aspect Ratio

AV (composite) 640×480 4:3

480p (normal) 640×480 4:3

480p (widescreen) 640×480 4:3

720p (widescreen) 1280×720 16:9

1080i/1080p (widescreen) 1920×1080 16:9

Xbox 360

Page 55: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Game Class

Namespace: Microsoft.Xna.Framework

Constructor

public Game ()

Provides basic graphics device initialization, game logic, and rendering code.

Page 56: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

Namespace: Microsoft.Xna.Framework

Page 57: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

GraphicsDevice

Public Properties

名稱 說明DisplayMode Retrieves the display mode's spatial resolution,

color resolution, and refresh frequency.

GraphicsProfile Gets the graphics profile. The default value is GraphicsProfile.Reach.

PresentationParameters Gets the presentation parameters associated with this graphics device.

Viewport Gets or sets a viewport identifying the portion of the render target to receive draw calls.

Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders.

Page 58: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

ViewportDefines the window dimensions of a render-target surface onto which a 3D volume projects.

Viewport

(X, Y)

Height

Width

Page 59: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

Namespace: Microsoft.Xna.Framework

Page 60: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Clears Resource Buffers

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(backgroundColor);

// TODO: Add your drawing code here

base.Draw(gameTime);

}

範例 清除視窗或螢幕

Page 61: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Window

Public Properties

ClientBounds

Page 62: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

public Game1()

{

graphics = new GraphicsDeviceManager(this);

Content.RootDirectory = "Content";

Window.AllowUserResizing = true;

Window.Title = “My First XNA Game”;

範例 設定視窗的屬性

Page 63: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Texture2D Class – 用於儲存 texture images

Namespace: Microsoft.Xna.Framework.Graphics

Constructor

public Texture2D (GraphicsDevice graphicsDevice, int width, int height)

public Texture2D (GraphicsDevice graphicsDevice, int width, int height,

bool mipMap, SurfaceFormat format)

Page 64: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Properties

名稱 說明

Bounds Image 的尺吋( Rectangle 型態)

Format 像素格式

GraphicsDevice 使用的繪圖裝置

Height 圖高

LevelCount Mipmap 的層數

Width 圖寬

Page 65: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

Public Methods

名稱 說明

FromStream 從資料流讀進圖片資料

GetData 取出 texture 資料存入指定的陣列中

SaveAsJpeg 存入 jpeg 格式的檔案中

SaveAsPng 存入 png 格式的檔案中

SetData 指定資料陣列為圖片資料

Page 66: XNA 4.0 基本類別 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 Point Structure Rectangle Structure Vector2 Structure Color Structure Matrix GraphicsDeviceManager Game

從 Content 中讀入圖片

1. 在 Game1 類別中加入資料成員如下:Texture2D texture;

2. 在 LoadContent() 方法中載入圖片如下:

protected override void LoadContent()

{

// TODO: use this.Content to load your game content here

texture = Content.Load<Texture2D>(@"Images/ok");

}