您的位置:首页 > 编程语言

Windows Phone 7 编程实践–XNA变身记

2011-03-19 22:23 225 查看
我们以MSDN上Using a Basic Effect with Texturing介绍的XNA Game 4.0 TexturedQuad_Sample为例,说明如何将Windows上运行的XNA程序快速部署到Windows Phone 7手机和Xbox 360上测试并运行。



[b]作品目标:Windows Phone 7 开发的实用手册
[/b]

Windows Phone 7 编程实践

参考资料:

Programming Windows Phone 7

MSDN Library -- Windows Phone Development
UI Design and Interaction Guide for Windows Phone 7 v2.0

Designing Web Sites for Phone Browsers

Develop for Windows Phone & XBOX 360 Code Sample

Windows Phone 7 Application Certification Requirements


在本书的整理过程中,力求从深度和不同的侧面诠释Windows Phone的开发技术。由于个人能力和水平有待提高,很多问题的分析还很肤浅。敬请各位不吝赐教,提出改进建议。

目录

前言
TexturedQuad程序说明

MSDN上的英文说明

Windows上运行结果

变身Windows Phone 7程序
生成新的Windows Phone 7工程

在Windows Phone 7的模拟器中运行结果
代码究竟发生变化了吗?
Game1.cs
Program.cs
揭秘

前言

XNA是Windows Phone 7在应用和游戏方面的主要开发方式,XNA起源于游戏界大名鼎鼎的DirectX,是微软对于C#版DirectX的修正和扩充版本。

如果您以前就做过XNA在Windows和Xbox 360,那么您很幸运,因为您以前做的XNA的程序将很容易部署到Windows Phone 7的手机上,几乎不用改代码。这是多么不可思议的事,曾有人说明,没有一个程序可以运行在所有的操作系统上。

我们以MSDN上Using a Basic Effect with Texturing介绍的XNA Game 4.0 TexturedQuad_Sample为例,说明如何将Windows上运行的XNA程序快速部署到Windows Phone 7手机和Xbox 360上测试并运行。

特别感谢微软技术专家Bruce.Wang在TechED云计算中国巡演青岛站的精彩讲解,让我知道了XNA的特性。可以写下这篇文章。

TexturedQuad程序说明

MSDN上的英文说明

Using a Basic Effect with Texturing

http://msdn.microsoft.com/en-us/library/bb464051.aspx

Demonstrates how to create and draw a simple quad—two triangles that form a rectangle or square—using DrawUserIndexedPrimitives.

This sample introduces the Quad class, which is used to construct a quad with a list of vertices and indices suitable for drawing with DrawUserIndexedPrimitives. The sample also demonstrates how to use BasicEffect to render the quad and to apply a texture to the primitive. For more information about BasicEffect, see Creating a Basic Effect.



The code in the topic shows you the technique for creating and drawing a quad. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download TexturedQuad_Sample.zip.

Windows上运行结果



这里姑且不谈程序本身实现的功能,下面我们就不手工修改一句代码的情况下,将这个程序移植到Windows Phone 7和Xbox 360上。前提条件是,您的系统中已经为VS2010安装了Windows Phone 7开发所有的环境和开发包,这里就不再累述安装过程。

变身Windows Phone 7程序

生成新的Windows Phone 7工程

在[Solution Explorer]中,选中" TexturedQuadWindows"工程,点击右键,如下图



选择[Create Copy of Project for Windows Phone],VS2010会自动为我们建立一个Windows Phone上运行的工程。如下图。



如图所示名称为[Windows Phone Copy of TexturedQuadWindows]就是系统为我们生成的,可运行在Windows Phone的工程。下面我们就修改工程的名称为TexturedQuadWindowsPhone,并将其设置为默认启动程序。



在Windows Phone 7的模拟器中运行结果

重新编译程序,在Windows Phone 7的模拟器中Debug运行,结果如下:



代码究竟发生变化了吗?

代码发生变化了吗?回答是否定的。

仔细瞧瞧Game1.cs和Program.cs文件有什么玄机。

Game1.cs

看看Game1.cs中发现在Update方法中有"#if WINDOWS"的字样,原来在windows下程序会处理Keyboard。

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

#if WINDOWS

if (Keyboard.GetState().IsKeyDown(Keys.Escape))

this.Exit();

#endif

// TODO: Add your update logic here

base.Update(gameTime);

}

Program.cs

在Program.cs文件中我们发现有"#if WINDOWS || XBOX"字样。

namespace TexturedQuadWindows

{

#if WINDOWS || XBOX

static
class
Program

{

///
<summary>

/// The main entry point for the application.

///
</summary>

static
void Main(string[] args)

{

using (Game1 game = new
Game1())

{

game.Run();

}

}

}

#endif

}

揭秘

其实在TexturedQuadWindows工程和TexturedQuadWindowsPhone中使用的Game1.cs和Program.cs,以及其他程序运行所需的文件都是一致的,但是为什么我们在移植过程中没有遇到任何阻碍呢。原程序在编写时已经考虑到移植的问题了,就是Game1.cs中的

#if WINDOWS

if (Keyboard.GetState().IsKeyDown(Keys.Escape))

this.Exit();

#endif

以及Program.cs中的

#if WINDOWS || XBOX

static
class
Program

{

///
<summary>

/// The main entry point for the application.

///
</summary>

static
void Main(string[] args)

{

using (Game1 game = new
Game1())

{

game.Run();

}

}

}

#endif

原程序已经根据Windows、Windows Phone和Xbox 360不同的硬件特点做了区分。因此我们在移植过程中借着东风,一蹴而就。

总结,我们写XNA程序时也应注意这方面的特点,编写好"#if WINDOWS"和"#if WINDOWS || XBOX",方便移植程序,从容变身。

将原程序变身为Xbox 360上运行的程序方法相同,请身临其境去尝试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: